This commit is contained in:
Jorijn van der Graaf 2026-11-01 18:35:23 +01:00
commit 1ea50c50bc
62 changed files with 9982 additions and 608 deletions

View file

@ -0,0 +1,123 @@
// SPDX-License-Identifier: LGPL-3.0-only
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
// The file layer (Nostro:File): where the config and cache live, private files and what CreatePrivateFile refuses,
// and ReadSecret from a pipe and from a terminal, where it must not echo.
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <termios.h>
#include <unistd.h>
import std;
import Nostro;
#include "../Support/Check.inc"
using namespace Nostro;
using Test::Check;
using Test::Equal;
using Test::Throws;
namespace fs = std::filesystem;
namespace {
void Paths() {
const std::string home = std::getenv("HOME") ? std::string(std::getenv("HOME")) : std::string();
setenv("XDG_CONFIG_HOME", "/xdg/config", 1);
setenv("XDG_CACHE_HOME", "/xdg/cache", 1);
Equal(GetConfigPath().string(), std::string("/xdg/config/.config/nostro"), "the config path under XDG_CONFIG_HOME");
Equal(GetCachePath().string(), std::string("/xdg/cache/nostro"), "the cache path under XDG_CACHE_HOME");
unsetenv("XDG_CONFIG_HOME");
unsetenv("XDG_CACHE_HOME");
setenv("HOME", "/home/tester", 1);
Equal(GetConfigPath().string(), std::string("/home/tester/.config/nostro"), "the config path under HOME");
Equal(GetCachePath().string(), std::string("/home/tester/.cache/nostro"), "the cache path under HOME");
unsetenv("HOME");
Throws([] { GetConfigPath(); }, "XDG_CONFIG_HOME and HOME not set", "no config path without either variable");
Throws([] { GetCachePath(); }, "XDG_CACHE_HOME and HOME not set", "no cache path without either variable");
setenv("HOME", home.c_str(), 1);
}
std::uint32_t Mode(const fs::path& path) {
struct stat info{};
stat(path.c_str(), &info);
return info.st_mode & 0777;
}
void PrivateFiles(const fs::path& root) {
const fs::path file = root / "a" / "b" / "secret.json";
CreatePrivateFile(file);
Check(fs::is_regular_file(file), "the file is made, with the directories above it");
Equal(Mode(file), std::uint32_t{0600}, "the file is private");
Equal(Mode(file.parent_path()), std::uint32_t{0700}, "and so is its directory");
CreatePrivateFile(file);
Check(fs::is_regular_file(file), "making it again changes nothing");
// Neither the directory nor the file can be made inside a directory that is not writable.
const fs::path locked = root / "locked";
fs::create_directory(locked);
chmod(locked.c_str(), 0500);
Throws<std::system_error>([&locked] { CreatePrivateFile(locked / "inner" / "file"); }, "cannot create", "a directory that cannot be made");
Throws<std::system_error>([&locked] { CreatePrivateFile(locked / "file"); }, "cannot create", "a file that cannot be made");
chmod(locked.c_str(), 0700);
}
// Runs ReadSecret with `input` behind std::cin and something that is no terminal as stdin.
std::string SecretFrom(std::string_view input) {
std::istringstream in{std::string(input)};
std::streambuf* const original = std::cin.rdbuf(in.rdbuf());
std::string secret;
try {
secret = ReadSecret("Key: ");
} catch(...) {
std::cin.rdbuf(original);
throw;
}
std::cin.rdbuf(original);
return secret;
}
void Secrets() {
const std::int32_t saved = dup(STDIN_FILENO);
const std::int32_t null = open("/dev/null", O_RDONLY);
dup2(null, STDIN_FILENO);
Equal(SecretFrom(" my key \r\n"), std::string("my key"), "a secret is trimmed");
Equal(SecretFrom(" \t \n"), std::string(""), "a line of spaces is no secret");
Throws([] { SecretFrom(""); }, "nothing given for 'Key: '", "stdin ending before a line");
// On a terminal the typing is not echoed while it is read, and echoing is back afterwards.
const std::int32_t master = posix_openpt(O_RDWR | O_NOCTTY);
grantpt(master);
unlockpt(master);
std::array<char, 128> name{};
ptsname_r(master, name.data(), name.size());
const std::int32_t terminal = open(name.data(), O_RDWR | O_NOCTTY);
dup2(terminal, STDIN_FILENO);
clearerr(stdin);
std::cin.clear();
const std::string typed = "typed secret\n";
write(master, typed.data(), typed.size());
Equal(ReadSecret("Key: "), std::string("typed secret"), "a secret typed on a terminal");
termios after{};
tcgetattr(STDIN_FILENO, &after);
Check((after.c_lflag & ECHO) != 0, "echoing is back on after reading");
dup2(saved, STDIN_FILENO);
close(terminal);
close(master);
close(null);
close(saved);
clearerr(stdin);
std::cin.clear();
}
}
int main() {
const Test::Directory directory("files");
Paths();
PrivateFiles(directory.path);
Secrets();
return Test::Result();
}