From 9b6f87a0df9d10a1d5a0cc6fae322ab696a21d77 Mon Sep 17 00:00:00 2001 From: Daniel McCoy Stephenson Date: Fri, 20 Nov 2026 22:08:25 -0700 Subject: [PATCH] Freeze the install-ID file loader's behaviour in fixtures test/install-id-fixtures holds the shared corpus of hand-written ID files, and expected.txt records what installIdFromFile does with each at 0.4.0 (loaded , replaced or kept). installIdFixturesLoadAsReleased checks every line and that expected.txt names exactly the fixtures present, so an ID a release stored keeps loading as the same ID. theVersionIsTheSameEverywhere checks the header banner, TRACE_CLIENT_VERSION and the README's User-Agent against CMakeLists.txt. .gitattributes stores the fixtures as binary so CRLF, BOM and non-UTF-8 bytes survive checkout; the Makefile and CMakeLists.txt pass the source directory so MSVC finds them from build\Release. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LYNeumA4DYW41mYg7nCwNn --- .gitattributes | 1 + CMakeLists.txt | 3 + Makefile | 7 +- README.md | 4 + test/install-id-fixtures/bad-chars.id | 1 + test/install-id-fixtures/bom.id | 1 + test/install-id-fixtures/comment-first.id | 2 + test/install-id-fixtures/created.id | 1 + test/install-id-fixtures/crlf.id | 1 + test/install-id-fixtures/empty.id | 0 test/install-id-fixtures/expected.txt | 26 +++ test/install-id-fixtures/first-of-two.id | 2 + test/install-id-fixtures/id-then-not-utf8.id | 2 + .../invalid-lines-first.id | 4 + .../leading-blank-lines.id | 5 + test/install-id-fixtures/max-length.id | 1 + test/install-id-fixtures/non-ascii.id | 1 + test/install-id-fixtures/not-utf8.id | Bin 0 -> 13 bytes test/install-id-fixtures/padded.id | 1 + test/install-id-fixtures/plain.id | 1 + test/install-id-fixtures/tabs.id | 1 + test/install-id-fixtures/too-long.id | 1 + test/install-id-fixtures/trailing-newline.id | 1 + test/install-id-fixtures/uppercase-uuid.id | 1 + test/install-id-fixtures/whitespace-only.id | 2 + test/test_trace_client.cpp | 175 ++++++++++++++++++ 26 files changed, 243 insertions(+), 2 deletions(-) create mode 100644 .gitattributes create mode 100644 test/install-id-fixtures/bad-chars.id create mode 100644 test/install-id-fixtures/bom.id create mode 100644 test/install-id-fixtures/comment-first.id create mode 100644 test/install-id-fixtures/created.id create mode 100644 test/install-id-fixtures/crlf.id create mode 100644 test/install-id-fixtures/empty.id create mode 100644 test/install-id-fixtures/expected.txt create mode 100644 test/install-id-fixtures/first-of-two.id create mode 100644 test/install-id-fixtures/id-then-not-utf8.id create mode 100644 test/install-id-fixtures/invalid-lines-first.id create mode 100644 test/install-id-fixtures/leading-blank-lines.id create mode 100644 test/install-id-fixtures/max-length.id create mode 100644 test/install-id-fixtures/non-ascii.id create mode 100644 test/install-id-fixtures/not-utf8.id create mode 100644 test/install-id-fixtures/padded.id create mode 100644 test/install-id-fixtures/plain.id create mode 100644 test/install-id-fixtures/tabs.id create mode 100644 test/install-id-fixtures/too-long.id create mode 100644 test/install-id-fixtures/trailing-newline.id create mode 100644 test/install-id-fixtures/uppercase-uuid.id create mode 100644 test/install-id-fixtures/whitespace-only.id diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..54afe5b --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +test/install-id-fixtures/** binary diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e6f112..0651c33 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,6 +9,9 @@ find_package(Threads REQUIRED) add_executable(test_trace_client test/test_trace_client.cpp) target_link_libraries(test_trace_client Threads::Threads) +# The tests read test/install-id-fixtures and the version spots from here, +# wherever they run from (MSVC runs them from build/Release). +target_compile_definitions(test_trace_client PRIVATE TRACE_CLIENT_SOURCE_DIR="${CMAKE_CURRENT_SOURCE_DIR}") if(WIN32) target_link_libraries(test_trace_client ws2_32) endif() diff --git a/Makefile b/Makefile index ed7afc8..e74ff1d 100644 --- a/Makefile +++ b/Makefile @@ -4,16 +4,19 @@ CXX ?= g++ STD ?= c++11 CXXFLAGS ?= -std=$(STD) -Wall -Wextra -pedantic -Werror -pthread +# Where the tests find test/install-id-fixtures and the files whose version +# they compare; kept out of CXXFLAGS so overriding those keeps it. +SOURCE_DIR = -DTRACE_CLIENT_SOURCE_DIR=\"$(CURDIR)\" .PHONY: test test-libcurl clean build/test_trace_client: test/test_trace_client.cpp trace_client.hpp mkdir -p build - $(CXX) $(CXXFLAGS) test/test_trace_client.cpp -o $@ + $(CXX) $(CXXFLAGS) $(SOURCE_DIR) test/test_trace_client.cpp -o $@ build/test_trace_client_libcurl: test/test_trace_client.cpp trace_client.hpp mkdir -p build - $(CXX) $(CXXFLAGS) -DTRACE_CLIENT_USE_LIBCURL test/test_trace_client.cpp -o $@ -lcurl + $(CXX) $(CXXFLAGS) $(SOURCE_DIR) -DTRACE_CLIENT_USE_LIBCURL test/test_trace_client.cpp -o $@ -lcurl test: build/test_trace_client ./build/test_trace_client diff --git a/README.md b/README.md index 33fc8d0..e0b662f 100644 --- a/README.md +++ b/README.md @@ -256,6 +256,10 @@ and 20; libcurl; ThreadSanitizer, and AddressSanitizer with UndefinedBehaviorSanitizer), macOS and Windows (MSVC with the system `curl.exe`). +The ID files in `test/install-id-fixtures` are frozen once released: each must +keep loading as its line in `expected.txt` says. Never edit or delete one; a +new case is a new fixture plus its own `expected.txt` line. + ## License MIT. diff --git a/test/install-id-fixtures/bad-chars.id b/test/install-id-fixtures/bad-chars.id new file mode 100644 index 0000000..71c1170 --- /dev/null +++ b/test/install-id-fixtures/bad-chars.id @@ -0,0 +1 @@ +not an id! diff --git a/test/install-id-fixtures/bom.id b/test/install-id-fixtures/bom.id new file mode 100644 index 0000000..cfa81a3 --- /dev/null +++ b/test/install-id-fixtures/bom.id @@ -0,0 +1 @@ +bom-id diff --git a/test/install-id-fixtures/comment-first.id b/test/install-id-fixtures/comment-first.id new file mode 100644 index 0000000..a87026e --- /dev/null +++ b/test/install-id-fixtures/comment-first.id @@ -0,0 +1,2 @@ +# install id +real-id diff --git a/test/install-id-fixtures/created.id b/test/install-id-fixtures/created.id new file mode 100644 index 0000000..dadc3db --- /dev/null +++ b/test/install-id-fixtures/created.id @@ -0,0 +1 @@ +3f2b8c1e-7a4d-4e6f-9b2a-5c8d1e0f7a6b diff --git a/test/install-id-fixtures/crlf.id b/test/install-id-fixtures/crlf.id new file mode 100644 index 0000000..264c957 --- /dev/null +++ b/test/install-id-fixtures/crlf.id @@ -0,0 +1 @@ +crlf-id diff --git a/test/install-id-fixtures/empty.id b/test/install-id-fixtures/empty.id new file mode 100644 index 0000000..e69de29 diff --git a/test/install-id-fixtures/expected.txt b/test/install-id-fixtures/expected.txt new file mode 100644 index 0000000..2c94947 --- /dev/null +++ b/test/install-id-fixtures/expected.txt @@ -0,0 +1,26 @@ +# The install-ID file loader's released behaviour, one line per fixture file in this directory. +# loaded installIdFromFile returns exactly and leaves the file untouched. +# replaced the file holds no ID, so a fresh lowercase uuid4 plus "\n" is written and returned. +# kept the file cannot be read, so it is left byte-identical and a fresh uuid4 is +# returned for this process only. +# Lines marked loaded or kept, and the fixture files, must never change once released. +bad-chars.id replaced +bom.id replaced +comment-first.id loaded real-id +created.id loaded 3f2b8c1e-7a4d-4e6f-9b2a-5c8d1e0f7a6b +crlf.id loaded crlf-id +empty.id replaced +first-of-two.id loaded first-id +id-then-not-utf8.id loaded good-id +invalid-lines-first.id loaded third.is-valid +leading-blank-lines.id loaded after-blanks +max-length.id loaded aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa +non-ascii.id replaced +not-utf8.id replaced +padded.id loaded padded-id +plain.id loaded plain-id +tabs.id loaded tabbed_id +too-long.id replaced +trailing-newline.id loaded with.newline_1 +uppercase-uuid.id loaded 0F8FAD5B-D9CB-469F-A165-70867728950E +whitespace-only.id replaced diff --git a/test/install-id-fixtures/first-of-two.id b/test/install-id-fixtures/first-of-two.id new file mode 100644 index 0000000..12b50c2 --- /dev/null +++ b/test/install-id-fixtures/first-of-two.id @@ -0,0 +1,2 @@ +first-id +second-id diff --git a/test/install-id-fixtures/id-then-not-utf8.id b/test/install-id-fixtures/id-then-not-utf8.id new file mode 100644 index 0000000..5e6253f --- /dev/null +++ b/test/install-id-fixtures/id-then-not-utf8.id @@ -0,0 +1,2 @@ +good-id +ÿþ diff --git a/test/install-id-fixtures/invalid-lines-first.id b/test/install-id-fixtures/invalid-lines-first.id new file mode 100644 index 0000000..9b883c9 --- /dev/null +++ b/test/install-id-fixtures/invalid-lines-first.id @@ -0,0 +1,4 @@ +has space +not/valid +bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb +third.is-valid diff --git a/test/install-id-fixtures/leading-blank-lines.id b/test/install-id-fixtures/leading-blank-lines.id new file mode 100644 index 0000000..f96b0dc --- /dev/null +++ b/test/install-id-fixtures/leading-blank-lines.id @@ -0,0 +1,5 @@ + + + + +after-blanks diff --git a/test/install-id-fixtures/max-length.id b/test/install-id-fixtures/max-length.id new file mode 100644 index 0000000..f040e66 --- /dev/null +++ b/test/install-id-fixtures/max-length.id @@ -0,0 +1 @@ +aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa diff --git a/test/install-id-fixtures/non-ascii.id b/test/install-id-fixtures/non-ascii.id new file mode 100644 index 0000000..c9cf8fd --- /dev/null +++ b/test/install-id-fixtures/non-ascii.id @@ -0,0 +1 @@ +café-id diff --git a/test/install-id-fixtures/not-utf8.id b/test/install-id-fixtures/not-utf8.id new file mode 100644 index 0000000000000000000000000000000000000000..8c73662126113ce7f250bec73de75c337d1a284a GIT binary patch literal 13 UcmezWk0CF=M4_}KP1k}804+8JEdT%j literal 0 HcmV?d00001 diff --git a/test/install-id-fixtures/padded.id b/test/install-id-fixtures/padded.id new file mode 100644 index 0000000..7180291 --- /dev/null +++ b/test/install-id-fixtures/padded.id @@ -0,0 +1 @@ + padded-id diff --git a/test/install-id-fixtures/plain.id b/test/install-id-fixtures/plain.id new file mode 100644 index 0000000..08c035b --- /dev/null +++ b/test/install-id-fixtures/plain.id @@ -0,0 +1 @@ +plain-id \ No newline at end of file diff --git a/test/install-id-fixtures/tabs.id b/test/install-id-fixtures/tabs.id new file mode 100644 index 0000000..b8885ba --- /dev/null +++ b/test/install-id-fixtures/tabs.id @@ -0,0 +1 @@ + tabbed_id diff --git a/test/install-id-fixtures/too-long.id b/test/install-id-fixtures/too-long.id new file mode 100644 index 0000000..442b421 --- /dev/null +++ b/test/install-id-fixtures/too-long.id @@ -0,0 +1 @@ +cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc diff --git a/test/install-id-fixtures/trailing-newline.id b/test/install-id-fixtures/trailing-newline.id new file mode 100644 index 0000000..60a4d0b --- /dev/null +++ b/test/install-id-fixtures/trailing-newline.id @@ -0,0 +1 @@ +with.newline_1 diff --git a/test/install-id-fixtures/uppercase-uuid.id b/test/install-id-fixtures/uppercase-uuid.id new file mode 100644 index 0000000..37f9243 --- /dev/null +++ b/test/install-id-fixtures/uppercase-uuid.id @@ -0,0 +1 @@ +0F8FAD5B-D9CB-469F-A165-70867728950E diff --git a/test/install-id-fixtures/whitespace-only.id b/test/install-id-fixtures/whitespace-only.id new file mode 100644 index 0000000..58a592e --- /dev/null +++ b/test/install-id-fixtures/whitespace-only.id @@ -0,0 +1,2 @@ + + diff --git a/test/test_trace_client.cpp b/test/test_trace_client.cpp index 12c3048..ae21c75 100644 --- a/test/test_trace_client.cpp +++ b/test/test_trace_client.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,7 @@ typedef SOCKET socket_t; static const socket_t NO_SOCKET = INVALID_SOCKET; #else #include +#include #include #include #include @@ -1572,6 +1574,177 @@ static void installIdFromFileFallsBackToMemoryWhenThePathIsUnusable() { removeDirectory(base); } +// ---------------------------------------------------------------- install-ID fixtures + +// The repository's own directory, so the fixtures are found from wherever the +// tests run (MSVC runs them from build\Release). The Makefile and +// CMakeLists.txt both define it; "." is the repository root make runs from. +#if !defined(TRACE_CLIENT_SOURCE_DIR) +#define TRACE_CLIENT_SOURCE_DIR "." +#endif + +static std::string sourcePath(const std::string &relative) { + return std::string(TRACE_CLIENT_SOURCE_DIR) + "/" + relative; +} + +/** The names of the regular files in directory that end in suffix, sorted. */ +static std::vector filesEndingIn(const std::string &directory, const std::string &suffix) { + std::vector names; +#if defined(_WIN32) + WIN32_FIND_DATAA entry; + HANDLE search = FindFirstFileA((directory + "/*").c_str(), &entry); + if (search != INVALID_HANDLE_VALUE) { + do { + if (!(entry.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) names.push_back(entry.cFileName); + } while (FindNextFileA(search, &entry)); + FindClose(search); + } +#else + DIR *dir = opendir(directory.c_str()); + if (dir) { + while (struct dirent *entry = readdir(dir)) { + std::string name = entry->d_name; + if (trace_client::detail::pathKind(directory + "/" + name) == trace_client::detail::PATH_FILE) { + names.push_back(name); + } + } + closedir(dir); + } +#endif + std::vector matching; + for (std::size_t i = 0; i < names.size(); ++i) { + const std::string &name = names[i]; + if (name.size() > suffix.size() && name.compare(name.size() - suffix.size(), suffix.size(), suffix) == 0) { + matching.push_back(name); + } + } + std::sort(matching.begin(), matching.end()); + return matching; +} + +/** + * Every ID file a release shipped in test/install-id-fixtures still loads the + * way test/install-id-fixtures/expected.txt says it did: "loaded " returns + * exactly that ID and leaves the file alone, "replaced" writes a fresh + * lower-case UUID plus "\n" and returns it, "kept" leaves the file + * byte-identical and returns a fresh UUID for this process only. Released + * fixtures and their loaded/kept lines are frozen; a new case is a new + * fixture plus its own line. + */ +static void installIdFixturesLoadAsReleased() { + const std::string fixtures = sourcePath("test/install-id-fixtures"); + std::ifstream expected((fixtures + "/expected.txt").c_str(), std::ios::in | std::ios::binary); + CHECK(static_cast(expected)); + std::vector named; + std::string line; + while (std::getline(expected, line)) { + if (!line.empty() && line[line.size() - 1] == '\r') line.erase(line.size() - 1); + if (trace_client::detail::isBlank(line) || line[0] == '#') continue; + std::istringstream fields(line); + std::string file, outcome, id, extra; + fields >> file >> outcome; + if (outcome == "loaded") fields >> id; + bool wellFormed = !file.empty() && (outcome == "loaded" ? !id.empty() : outcome == "replaced" || outcome == "kept") + && !(fields >> extra); + if (!wellFormed) { + CHECK_EQ(std::string(" loaded | replaced | kept"), line); + continue; + } + named.push_back(file); + + const std::string original = readFile(fixtures + "/" + file); + const std::string base = tempBase(); + const std::string path = base + "/" + file; + writeFile(path, original); + CHECK_EQ(original, readFile(path)); + const std::string result = trace_client::TraceClient::installIdFromFile(path); + const std::string after = readFile(path); + if (outcome == "loaded") { + if (result != id) std::cerr << " " << file << ":" << std::endl; + CHECK_EQ(id, result); + CHECK(after == original); + } else if (outcome == "replaced") { + if (!isUuid(result) || after != result + "\n") std::cerr << " " << file << ":" << std::endl; + CHECK(isUuid(result)); + CHECK_EQ(result + "\n", after); + } else { + if (!isUuid(result) || after != original) std::cerr << " " << file << ":" << std::endl; + CHECK(isUuid(result)); + CHECK(after == original); + CHECK(original.find(result) == std::string::npos); + } + std::remove(path.c_str()); + removeDirectory(base); + } + std::sort(named.begin(), named.end()); + // expected.txt names every fixture, and only fixtures that are there. + std::vector present = filesEndingIn(fixtures, ".id"); + CHECK(!present.empty()); + CHECK(named == present); + if (named != present) { + for (std::size_t i = 0; i < present.size(); ++i) { + if (std::find(named.begin(), named.end(), present[i]) == named.end()) { + std::cerr << " " << present[i] << " has no line in expected.txt" << std::endl; + } + } + for (std::size_t i = 0; i < named.size(); ++i) { + if (std::find(present.begin(), present.end(), named[i]) == present.end()) { + std::cerr << " " << named[i] << " in expected.txt is not a fixture (or is named twice)" << std::endl; + } + } + } +} + +// ---------------------------------------------------------------- version consistency + +/** + * Every version between prefix and suffix in text, where the version is + * characters of [0-9A-Za-z.+-]; other text after prefix is not a match. + */ +static std::vector versionsBetween(const std::string &text, const std::string &prefix, + const std::string &suffix) { + std::vector found; + for (std::size_t at = text.find(prefix); at != std::string::npos; at = text.find(prefix, at + 1)) { + std::size_t begin = at + prefix.size(), end = begin; + while (end < text.size() && (std::isalnum(static_cast(text[end])) || text[end] == '.' + || text[end] == '+' || text[end] == '-')) { + ++end; + } + if (end > begin && text.compare(end, suffix.size(), suffix) == 0) found.push_back(text.substr(begin, end - begin)); + } + return found; +} + +/** + * The version CMakeLists.txt gives the project is the one written everywhere + * else -- the header's banner and TRACE_CLIENT_VERSION, and the README's + * User-Agent -- each exactly once, so a release bumps them together. + */ +static void theVersionIsTheSameEverywhere() { + std::vector build = + versionsBetween(readFile(sourcePath("CMakeLists.txt")), "project(trace_client_cpp VERSION ", " "); + CHECK_EQ(1u, build.size()); + if (build.size() != 1) return; + const std::string version = build[0]; + const std::string header = readFile(sourcePath("trace_client.hpp")); + const std::string readme = readFile(sourcePath("README.md")); + struct Spot { const char *file; const std::string *text; const char *prefix; const char *suffix; }; + const Spot spots[] = { + {"trace_client.hpp", &header, "trace-client ", " (C++) --"}, + {"trace_client.hpp", &header, "#define TRACE_CLIENT_VERSION \"", "\""}, + {"README.md", &readme, "trace-client-cpp/", " ("}, + }; + for (std::size_t i = 0; i < sizeof spots / sizeof spots[0]; ++i) { + std::vector found = versionsBetween(*spots[i].text, spots[i].prefix, spots[i].suffix); + if (found.size() != 1 || found[0] != version) { + std::cerr << " " << spots[i].file << ": " << spots[i].prefix << "X" << spots[i].suffix << std::endl; + } + CHECK_EQ(1u, found.size()); + for (std::size_t j = 0; j < found.size(); ++j) CHECK_EQ(version, found[j]); + } + CHECK_EQ(version, std::string(TRACE_CLIENT_VERSION)); +} + static void everyEventCarriesTheInstallIdFromTheFile() { StubServer server; std::string base = tempBase(); @@ -2005,6 +2178,8 @@ int main() { {"installIdFromFilePersistsOnceAndReusesIt", installIdFromFilePersistsOnceAndReusesIt}, {"installIdFromFileReadsTheFirstValidLine", installIdFromFileReadsTheFirstValidLine}, {"installIdFromFileFallsBackToMemoryWhenThePathIsUnusable", installIdFromFileFallsBackToMemoryWhenThePathIsUnusable}, + {"installIdFixturesLoadAsReleased", installIdFixturesLoadAsReleased}, + {"theVersionIsTheSameEverywhere", theVersionIsTheSameEverywhere}, {"everyEventCarriesTheInstallIdFromTheFile", everyEventCarriesTheInstallIdFromTheFile}, {"aDisabledClientNeverMakesUpOrWritesAnInstallId", aDisabledClientNeverMakesUpOrWritesAnInstallId}, {"anExplicitInstallIdIsTrimmedAndWinsOverTheFile", anExplicitInstallIdIsTrimmedAndWinsOverTheFile},