diff --git a/CMakeLists.txt b/CMakeLists.txt index b01b368..8e6f112 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,7 @@ # For toolchains without make (MSVC on Windows). The client itself needs no # build step: include trace_client.hpp. This only builds and runs the tests. cmake_minimum_required(VERSION 3.10) -project(trace_client_cpp VERSION 0.3.0 LANGUAGES CXX) +project(trace_client_cpp VERSION 0.4.0 LANGUAGES CXX) set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD_REQUIRED ON) diff --git a/README.md b/README.md index e13d24d..33fc8d0 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ all three speak the same wire format and make the same promises. trace_client::TraceClient trace("https://trace.danielstephenson.dev", "MyGame", MYGAME_VERSION, // the program's own version, e.g. "1.4.0" settings.usageReportingKey, - settings.usageReportingEnabled); + trace_client::allowsReporting(settings.usageReportingEnabled)); trace.report("startup"); trace.report("level-complete", 3.0, {{"level", "forest"}}); @@ -64,7 +64,7 @@ written anywhere and no `install` tag is sent. trace_client::TraceClient trace("https://trace.danielstephenson.dev", "MyGame", MYGAME_VERSION, settings.usageReportingKey, trace_client::InstallId::fromFile(saveDirectory + "/trace-install-id"), - settings.usageReportingEnabled); + trace_client::allowsReporting(settings.usageReportingEnabled)); ``` `TraceClient::installIdFromFile(path)` does the work: the first line of the @@ -108,19 +108,28 @@ client never generates one, never reads or writes the file, and sends nothing. ## Turning it off -Reporting is **opt-out**. Any one of these yields a client that does nothing, -starts no thread and costs nothing; the first that applies is the reason: - -- **Environment, for every trace-reporting program at once:** - `TRACE_USAGE_REPORTING=off` (also `false`, `0`, `no`; case-insensitive) or - `DO_NOT_TRACK=1` (also `true`, `yes`; the - [consoledonottrack.com](https://consoledonottrack.com) convention). The - constructor checks these before anything else, so they win over the - program's own setting. Any other value, or an unset variable, leaves that - setting in charge. `trace_client::environmentOptsOut()` answers the same - question on its own. +Reporting is **opt-out**, and **every switch fails closed:** a value that +does not clearly mean "on" — a typo, a quoted `"false"`, an empty value — +turns reporting off, and nothing in this client ever turns a switch back on. +Any one of these yields a client that does nothing, starts no thread and costs +nothing; the first that applies is the reason: + +- **Environment, for every trace-reporting program at once** (case-insensitive, + trimmed): `TRACE_USAGE_REPORTING` set to anything but blank, `true`, `on`, + `yes` or `1` (so `off`, `false`, `0`, `no`, `disabled`, a typo, …); or + `DO_NOT_TRACK` set to anything but blank, `0`, `false`, `off` or `no` (so + `1`, `true`, `yes`, …; the + [consoledonottrack.com](https://consoledonottrack.com) convention). An + environment that cannot be read counts as opting out. The constructor checks + these before anything else, so they win over the program's own setting; an + unset or blank variable leaves that setting in charge. + `trace_client::environmentOptsOut()` answers the same question on its own. - **The program's own setting:** `enabled = false` (the constructor argument - after the key, or after the `InstallId`). + after the key, or after the `InstallId`). Read it with + `trace_client::allowsReporting(value)`, which fails closed the same way: only + `true`, or a string (`std::string` or `const char *`) reading `true`, `on`, + `yes` or `1` (any case, trimmed), allows reporting; `false`, a quoted + `"false"`, a typo, a null pointer, a number or any other type turns it off. - **No key** (or a blank one). `client.disabledReason()` says which one applied — `"environment"`, `"config"` @@ -211,7 +220,7 @@ The executable can be overridden before the include, e.g. `POST {baseUrl}/api/metrics` with `Authorization: Bearer `, `Content-Type: application/json; charset=utf-8`, -`User-Agent: trace-client-cpp/0.3.0 ()` and a body of +`User-Agent: trace-client-cpp/0.4.0 ()` and a body of ```json {"application":"MyGame","name":"command","tags":{"install":"0f8b6c1e-3a52-4c8e-9a0d-6e2f1b7c4d90","name":"home","version":"1.4.0"}} diff --git a/test/test_trace_client.cpp b/test/test_trace_client.cpp index 46c012a..12c3048 100644 --- a/test/test_trace_client.cpp +++ b/test/test_trace_client.cpp @@ -687,7 +687,8 @@ static void disabledReasonIsEmptyWhenTheClientReports() { static void environmentOptOutsDisableForEveryAcceptedValue() { StubServer server; - const char *off[] = {"off", "OFF", "false", "0", "no", " No "}; + // Fails closed: anything but blank or a clear "on" is an opt-out. + const char *off[] = {"off", "OFF", "false", "0", "no", " No ", "maybe", "disabled", "of", "\"on\"", "2"}; for (std::size_t i = 0; i < sizeof off / sizeof off[0]; ++i) { cleanEnvironment(); setEnv("TRACE_USAGE_REPORTING", off[i]); @@ -696,7 +697,8 @@ static void environmentOptOutsDisableForEveryAcceptedValue() { CHECK_EQ(std::string("environment"), client.disabledReason()); client.report("startup"); } - const char *dnt[] = {"1", "true", "TRUE", "yes"}; + // Fails closed: anything but blank or a clear "no" is an opt-out. + const char *dnt[] = {"1", "true", "TRUE", "yes", " Yes ", "on", "y", "2", "nope"}; for (std::size_t i = 0; i < sizeof dnt / sizeof dnt[0]; ++i) { cleanEnvironment(); setEnv("DO_NOT_TRACK", dnt[i]); @@ -709,19 +711,62 @@ static void environmentOptOutsDisableForEveryAcceptedValue() { CHECK_EQ(0u, server.received().size()); } -static void otherEnvironmentValuesLeaveTheProgramSettingInCharge() { +static void blankOrClearlyOnEnvironmentValuesLeaveTheProgramSettingInCharge() { StubServer server; - const char *values[] = {"on", "true", "1", "", "maybe"}; + const char *values[] = {"on", "true", "1", "", " ", " TRUE ", "Yes"}; + const char *dnt[] = {"0", "false", "", " OFF ", "No"}; for (std::size_t i = 0; i < sizeof values / sizeof values[0]; ++i) { - cleanEnvironment(); - setEnv("TRACE_USAGE_REPORTING", values[i]); - setEnv("DO_NOT_TRACK", i == 0 ? "0" : "false"); - trace_client::TraceClient client(server.baseUrl(), "MyGame", "1.2.3", "k"); - CHECK(client.isEnabled()); + for (std::size_t j = 0; j < sizeof dnt / sizeof dnt[0]; ++j) { + cleanEnvironment(); + setEnv("TRACE_USAGE_REPORTING", values[i]); + setEnv("DO_NOT_TRACK", dnt[j]); + CHECK(!trace_client::environmentOptsOut()); + trace_client::TraceClient client(server.baseUrl(), "MyGame", "1.2.3", "k"); + CHECK(client.isEnabled()); + } } cleanEnvironment(); } +static void allowsReportingIsTrueOnlyForAClearOn() { + CHECK(trace_client::allowsReporting(true)); + CHECK(trace_client::allowsReporting("true")); + CHECK(trace_client::allowsReporting("TRUE")); + CHECK(trace_client::allowsReporting(" on ")); + CHECK(trace_client::allowsReporting("Yes")); + CHECK(trace_client::allowsReporting("1")); + CHECK(trace_client::allowsReporting(std::string("on"))); + std::string owned("yes"); + CHECK(trace_client::allowsReporting(owned)); + char mutableText[] = "true"; + char *mutablePointer = mutableText; + CHECK(trace_client::allowsReporting(mutablePointer)); +} + +static void allowsReportingFailsClosedForEverythingElse() { + CHECK(!trace_client::allowsReporting(false)); + CHECK(!trace_client::allowsReporting("false")); + CHECK(!trace_client::allowsReporting("\"false\"")); + CHECK(!trace_client::allowsReporting("\"true\"")); + CHECK(!trace_client::allowsReporting("off")); + CHECK(!trace_client::allowsReporting("no")); + CHECK(!trace_client::allowsReporting("0")); + CHECK(!trace_client::allowsReporting("")); + CHECK(!trace_client::allowsReporting(" ")); + CHECK(!trace_client::allowsReporting("ture")); + CHECK(!trace_client::allowsReporting("enabled")); + CHECK(!trace_client::allowsReporting(std::string("off"))); + CHECK(!trace_client::allowsReporting(static_cast(NULL))); + CHECK(!trace_client::allowsReporting(nullptr)); + CHECK(!trace_client::allowsReporting(1)); + CHECK(!trace_client::allowsReporting(0)); + CHECK(!trace_client::allowsReporting(1.0)); + trace_client::TraceClient client("http://127.0.0.1:9", "MyGame", "1.2.3", "k", + trace_client::allowsReporting("\"false\"")); + CHECK(!client.isEnabled()); + CHECK_EQ(std::string("config"), client.disabledReason()); +} + static void environmentWinsOverTheConfigFlagAndTheKey() { setEnv("DO_NOT_TRACK", "1"); trace_client::TraceClient off("http://127.0.0.1:9", "MyGame", "1.2.3", "k", false); @@ -1914,7 +1959,9 @@ int main() { {"disabledClientSendsNothing", disabledClientSendsNothing}, {"disabledReasonIsEmptyWhenTheClientReports", disabledReasonIsEmptyWhenTheClientReports}, {"environmentOptOutsDisableForEveryAcceptedValue", environmentOptOutsDisableForEveryAcceptedValue}, - {"otherEnvironmentValuesLeaveTheProgramSettingInCharge", otherEnvironmentValuesLeaveTheProgramSettingInCharge}, + {"blankOrClearlyOnEnvironmentValuesLeaveTheProgramSettingInCharge", blankOrClearlyOnEnvironmentValuesLeaveTheProgramSettingInCharge}, + {"allowsReportingIsTrueOnlyForAClearOn", allowsReportingIsTrueOnlyForAClearOn}, + {"allowsReportingFailsClosedForEverythingElse", allowsReportingFailsClosedForEverythingElse}, {"environmentWinsOverTheConfigFlagAndTheKey", environmentWinsOverTheConfigFlagAndTheKey}, {"doNotTrackOptsOutWhateverTraceUsageReportingSays", doNotTrackOptsOutWhateverTraceUsageReportingSays}, {"disabledReasonIsTheFirstOptOutThatApplies", disabledReasonIsTheFirstOptOutThatApplies}, diff --git a/trace_client.hpp b/trace_client.hpp index 112cce7..8887903 100644 --- a/trace_client.hpp +++ b/trace_client.hpp @@ -1,5 +1,5 @@ /* - * trace-client 0.3.0 (C++) -- https://github.com/Stephenson-Software/trace-client-cpp + * trace-client 0.4.0 (C++) -- https://github.com/Stephenson-Software/trace-client-cpp * * One call to report that a program was used. Copy this header into a project * as is; there is nothing else to add. C++11 or later, no library to link @@ -10,7 +10,7 @@ #ifndef TRACE_CLIENT_HPP #define TRACE_CLIENT_HPP -#define TRACE_CLIENT_VERSION "0.3.0" +#define TRACE_CLIENT_VERSION "0.4.0" // The executable that carries a report over HTTPS: the system's own curl // (shipped with macOS, with Windows 10 1803 and later, and with nearly every @@ -906,19 +906,74 @@ inline void drain(std::shared_ptr shared) { } // namespace detail +namespace detail { + +/** true, on, yes or 1, ignoring case and surrounding space. */ +inline bool isOn(const std::string &value) { + std::string v = trimLower(value); + return v == "true" || v == "on" || v == "yes" || v == "1"; +} + +/** false, off, no or 0, ignoring case and surrounding space. */ +inline bool isNegative(const std::string &value) { + std::string v = trimLower(value); + return v == "false" || v == "off" || v == "no" || v == "0"; +} + +} // namespace detail + /** - * Whether the environment asks for usage reporting to be off, via - * TRACE_USAGE_REPORTING=off (also false, 0, no) or DO_NOT_TRACK=1 (also true, - * yes), case-insensitive. Any other value, or none, leaves the program's own - * setting in charge. + * Whether the environment asks for usage reporting to be off. Fails closed: + * TRACE_USAGE_REPORTING set to anything but blank, true, on, yes or 1 (so off, + * false, 0, no, a typo, ...), or DO_NOT_TRACK set to anything but blank, 0, + * false, off or no (so 1, true, yes, ...), case-insensitive and trimmed, turns + * reporting off. An environment that cannot be read counts as opting out too. + * Unset or blank variables leave the program's own setting in charge. Never + * throws. */ inline bool environmentOptsOut() { - std::string reporting = detail::trimLower(detail::getEnvironment(ENV_TRACE_USAGE_REPORTING)); - if (reporting == "off" || reporting == "false" || reporting == "0" || reporting == "no") return true; - std::string dnt = detail::trimLower(detail::getEnvironment(ENV_DO_NOT_TRACK)); - return dnt == "1" || dnt == "true" || dnt == "yes"; + try { + std::string reporting = detail::getEnvironment(ENV_TRACE_USAGE_REPORTING); + if (!detail::isBlank(reporting) && !detail::isOn(reporting)) return true; + std::string dnt = detail::getEnvironment(ENV_DO_NOT_TRACK); + return !detail::isBlank(dnt) && !detail::isNegative(dnt); + } catch (...) { + return true; // an environment that cannot be read may say DO_NOT_TRACK=1 + } } +/** + * Whether a program's own reporting setting, as read from its configuration, + * allows reporting -- for the constructor's enabled argument. Fails closed: + * only true, or a string reading true, on, yes or 1 (any case, surrounding + * space ignored), allows it. Anything else turns reporting off: false, a + * quoted "false", off, no, a typo, a null char pointer, a number, or any + * other type. Never throws. + * + * bool enabled = trace_client::allowsReporting(settings.get("usage-reporting.enabled")); + */ +inline bool allowsReporting(bool setting) { return setting; } + +inline bool allowsReporting(const std::string &setting) { + try { + return detail::isOn(setting); + } catch (...) { + return false; + } +} + +inline bool allowsReporting(const char *setting) { + return setting != NULL && allowsReporting(std::string(setting)); +} + +inline bool allowsReporting(char *setting) { + return allowsReporting(static_cast(setting)); +} + +/** Any other type -- a number, a null pointer, a struct -- is not a clear "on". */ +template +inline bool allowsReporting(const T &) { return false; } + /** * Where an installation's ID comes from, for the constructor that takes one. * An explicit id wins over a file; with neither, no "install" tag is sent. @@ -956,10 +1011,14 @@ struct InstallId { * TIMEOUT_SECONDS in total, then stops -- a CLI that reports and exits at * once still sends, and an unreachable server never hangs the exit. * - * Reporting is opt-out. The constructor decides once, first match wins, and - * disabledReason() says which: the environment (REASON_ENVIRONMENT), the - * program's own setting (REASON_CONFIG), a blank key (REASON_NO_KEY), or no - * way to send (REASON_UNAVAILABLE). + * Reporting is opt-out. Every switch fails closed: a value that does not + * clearly mean "on" -- a typo, a quoted "false", an empty value -- turns + * reporting off, and nothing in this client ever turns a switch back on. The + * constructor decides once, first match wins, and disabledReason() says + * which: the environment (REASON_ENVIRONMENT; see environmentOptsOut), the + * program's own setting (REASON_CONFIG; read it with allowsReporting so it + * fails closed too), a blank key (REASON_NO_KEY), or no way to send + * (REASON_UNAVAILABLE). * * Every event carries the program's own version as the tag "version" -- the * third argument to the constructor, required, so a "command" event can be @@ -980,7 +1039,7 @@ struct InstallId { * trace_client::TraceClient trace("https://trace.example.org", "MyGame", MYGAME_VERSION, * settings.key, * trace_client::InstallId::fromFile(dataDir + "/trace-install-id"), - * settings.usageReporting); + * trace_client::allowsReporting(settings.usageReporting)); * trace.report("startup"); * ... * trace.close(); // or let the destructor do it