Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test/install-id-fixtures/** binary
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
1 change: 1 addition & 0 deletions test/install-id-fixtures/bad-chars.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
not an id!
1 change: 1 addition & 0 deletions test/install-id-fixtures/bom.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bom-id
2 changes: 2 additions & 0 deletions test/install-id-fixtures/comment-first.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# install id
real-id
1 change: 1 addition & 0 deletions test/install-id-fixtures/created.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3f2b8c1e-7a4d-4e6f-9b2a-5c8d1e0f7a6b
1 change: 1 addition & 0 deletions test/install-id-fixtures/crlf.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
crlf-id
Empty file.
26 changes: 26 additions & 0 deletions test/install-id-fixtures/expected.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# The install-ID file loader's released behaviour, one line per fixture file in this directory.
# <file> loaded <id> installIdFromFile returns exactly <id> and leaves the file untouched.
# <file> replaced the file holds no ID, so a fresh lowercase uuid4 plus "\n" is written and returned.
# <file> 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
2 changes: 2 additions & 0 deletions test/install-id-fixtures/first-of-two.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
first-id
second-id
2 changes: 2 additions & 0 deletions test/install-id-fixtures/id-then-not-utf8.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
good-id
ÿþ
4 changes: 4 additions & 0 deletions test/install-id-fixtures/invalid-lines-first.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
has space
not/valid
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb
third.is-valid
5 changes: 5 additions & 0 deletions test/install-id-fixtures/leading-blank-lines.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@




after-blanks
1 change: 1 addition & 0 deletions test/install-id-fixtures/max-length.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
1 change: 1 addition & 0 deletions test/install-id-fixtures/non-ascii.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
café-id
Binary file added test/install-id-fixtures/not-utf8.id
Binary file not shown.
1 change: 1 addition & 0 deletions test/install-id-fixtures/padded.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
padded-id
1 change: 1 addition & 0 deletions test/install-id-fixtures/plain.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
plain-id
1 change: 1 addition & 0 deletions test/install-id-fixtures/tabs.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tabbed_id
1 change: 1 addition & 0 deletions test/install-id-fixtures/too-long.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
1 change: 1 addition & 0 deletions test/install-id-fixtures/trailing-newline.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
with.newline_1
1 change: 1 addition & 0 deletions test/install-id-fixtures/uppercase-uuid.id
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0F8FAD5B-D9CB-469F-A165-70867728950E
2 changes: 2 additions & 0 deletions test/install-id-fixtures/whitespace-only.id
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


175 changes: 175 additions & 0 deletions test/test_trace_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <mutex>
#include <new>
#include <condition_variable>
#include <sstream>
#include <stdexcept>
#include <string>
#include <thread>
Expand All @@ -31,6 +32,7 @@ typedef SOCKET socket_t;
static const socket_t NO_SOCKET = INVALID_SOCKET;
#else
#include <arpa/inet.h>
#include <dirent.h>
#include <netinet/in.h>
#include <sys/select.h>
#include <sys/socket.h>
Expand Down Expand Up @@ -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<std::string> filesEndingIn(const std::string &directory, const std::string &suffix) {
std::vector<std::string> 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<std::string> 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 <id>" 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<bool>(expected));
std::vector<std::string> 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("<file> loaded <id> | <file> replaced | <file> 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<std::string> 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<std::string> versionsBetween(const std::string &text, const std::string &prefix,
const std::string &suffix) {
std::vector<std::string> 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<unsigned char>(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<std::string> 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<std::string> 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();
Expand Down Expand Up @@ -2005,6 +2178,8 @@ int main() {
{"installIdFromFilePersistsOnceAndReusesIt", installIdFromFilePersistsOnceAndReusesIt},
{"installIdFromFileReadsTheFirstValidLine", installIdFromFileReadsTheFirstValidLine},
{"installIdFromFileFallsBackToMemoryWhenThePathIsUnusable", installIdFromFileFallsBackToMemoryWhenThePathIsUnusable},
{"installIdFixturesLoadAsReleased", installIdFixturesLoadAsReleased},
{"theVersionIsTheSameEverywhere", theVersionIsTheSameEverywhere},
{"everyEventCarriesTheInstallIdFromTheFile", everyEventCarriesTheInstallIdFromTheFile},
{"aDisabledClientNeverMakesUpOrWritesAnInstallId", aDisabledClientNeverMakesUpOrWritesAnInstallId},
{"anExplicitInstallIdIsTrimmedAndWinsOverTheFile", anExplicitInstallIdIsTrimmedAndWinsOverTheFile},
Expand Down
Loading