|
| 1 | +// The first "test" is of course whether the header compiles standalone |
| 2 | +#include "nmos/log_gate.h" |
| 3 | + |
| 4 | +#include <sstream> |
| 5 | +#include "bst/test/test.h" |
| 6 | + |
| 7 | +namespace |
| 8 | +{ |
| 9 | + struct test_gate : nmos::experimental::log_gate |
| 10 | + { |
| 11 | + test_gate(std::ostream& error_log, std::ostream& access_log, nmos::experimental::log_model& model) |
| 12 | + : nmos::experimental::log_gate(error_log, access_log, model) {} |
| 13 | + using nmos::experimental::log_gate::pertinent; |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +//////////////////////////////////////////////////////////////////////////////////////////// |
| 18 | +BST_TEST_CASE(testLogGatePertinentCategories) |
| 19 | +{ |
| 20 | + using web::json::value_of; |
| 21 | + |
| 22 | + std::ostringstream error_log; |
| 23 | + std::ostringstream access_log; |
| 24 | + nmos::experimental::log_model model; |
| 25 | + test_gate gate(error_log, access_log, model); |
| 26 | + |
| 27 | + const std::list<nmos::category> no_categories; |
| 28 | + const std::list<nmos::category> access{ "access" }; |
| 29 | + const std::list<nmos::category> send_query_ws_events{ "send_query_ws_events" }; |
| 30 | + const std::list<nmos::category> both{ "send_query_ws_events", "access" }; |
| 31 | + |
| 32 | + // when logging_categories is omitted, all messages are pertinent |
| 33 | + BST_REQUIRE(gate.pertinent(no_categories)); |
| 34 | + BST_REQUIRE(gate.pertinent(access)); |
| 35 | + BST_REQUIRE(gate.pertinent(both)); |
| 36 | + |
| 37 | + // when logging_categories is empty, no messages are pertinent |
| 38 | + model.settings[nmos::fields::logging_categories] = web::json::value::array(); |
| 39 | + BST_REQUIRE(!gate.pertinent(no_categories)); |
| 40 | + BST_REQUIRE(!gate.pertinent(access)); |
| 41 | + BST_REQUIRE(!gate.pertinent(both)); |
| 42 | + |
| 43 | + // positive categories select the messages to be logged |
| 44 | + model.settings[nmos::fields::logging_categories] = value_of({ U("send_query_ws_events") }); |
| 45 | + BST_REQUIRE(!gate.pertinent(no_categories)); |
| 46 | + BST_REQUIRE(gate.pertinent(send_query_ws_events)); |
| 47 | + BST_REQUIRE(!gate.pertinent(access)); |
| 48 | + BST_REQUIRE(gate.pertinent(both)); |
| 49 | + |
| 50 | + // the empty string selects messages with no category |
| 51 | + model.settings[nmos::fields::logging_categories] = value_of({ U("") }); |
| 52 | + BST_REQUIRE(gate.pertinent(no_categories)); |
| 53 | + BST_REQUIRE(!gate.pertinent(access)); |
| 54 | + |
| 55 | + // a category prefixed with '!' excludes matching messages, even if another |
| 56 | + // category matches positively |
| 57 | + model.settings[nmos::fields::logging_categories] = value_of({ U("send_query_ws_events"), U("!access") }); |
| 58 | + BST_REQUIRE(gate.pertinent(send_query_ws_events)); |
| 59 | + BST_REQUIRE(!gate.pertinent(access)); |
| 60 | + BST_REQUIRE(!gate.pertinent(both)); |
| 61 | + BST_REQUIRE(!gate.pertinent(no_categories)); |
| 62 | + |
| 63 | + // when only excluded categories are specified, all other messages are pertinent |
| 64 | + model.settings[nmos::fields::logging_categories] = value_of({ U("!access") }); |
| 65 | + BST_REQUIRE(gate.pertinent(no_categories)); |
| 66 | + BST_REQUIRE(gate.pertinent(send_query_ws_events)); |
| 67 | + BST_REQUIRE(!gate.pertinent(access)); |
| 68 | + BST_REQUIRE(!gate.pertinent(both)); |
| 69 | + |
| 70 | + // a negative match takes precedence over the same category listed positively |
| 71 | + model.settings[nmos::fields::logging_categories] = value_of({ U("access"), U("!access") }); |
| 72 | + BST_REQUIRE(!gate.pertinent(access)); |
| 73 | + BST_REQUIRE(!gate.pertinent(both)); |
| 74 | + BST_REQUIRE(!gate.pertinent(no_categories)); |
| 75 | + BST_REQUIRE(!gate.pertinent(send_query_ws_events)); |
| 76 | + |
| 77 | + // "!" excludes messages with no category (negation of "") |
| 78 | + model.settings[nmos::fields::logging_categories] = value_of({ U("!") }); |
| 79 | + BST_REQUIRE(!gate.pertinent(no_categories)); |
| 80 | + BST_REQUIRE(gate.pertinent(access)); |
| 81 | + BST_REQUIRE(gate.pertinent(send_query_ws_events)); |
| 82 | + |
| 83 | + model.settings[nmos::fields::logging_categories] = value_of({ U("!"), U("!access") }); |
| 84 | + BST_REQUIRE(!gate.pertinent(no_categories)); |
| 85 | + BST_REQUIRE(!gate.pertinent(access)); |
| 86 | + BST_REQUIRE(gate.pertinent(send_query_ws_events)); |
| 87 | +} |
0 commit comments