Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 CESNET, https://photonics.cesnet.cz/ |
| 3 | * Copyright (C) 2018 FIT CVUT, https://fit.cvut.cz/ |
| 4 | * |
| 5 | * Written by Václav Kubernát <kubervac@fit.cvut.cz> |
| 6 | * |
| 7 | */ |
| 8 | |
Václav Kubernát | 26b5608 | 2020-02-03 18:28:56 +0100 | [diff] [blame] | 9 | #include "trompeloeil_doctest.hpp" |
Jan Kundrát | 76b3ddc | 2019-07-03 10:31:27 +0200 | [diff] [blame] | 10 | #include <boost/core/demangle.hpp> |
Václav Kubernát | 24df80e | 2018-06-06 15:18:03 +0200 | [diff] [blame] | 11 | #include "ast_commands.hpp" |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 12 | #include "leaf_data_helpers.hpp" |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 13 | #include "parser.hpp" |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 14 | #include "static_schema.hpp" |
Václav Kubernát | 509ce65 | 2019-05-29 19:46:44 +0200 | [diff] [blame] | 15 | #include "utils.hpp" |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 16 | |
Jan Kundrát | 76b3ddc | 2019-07-03 10:31:27 +0200 | [diff] [blame] | 17 | std::ostream& operator<<(std::ostream& s, const set_ cmd) |
| 18 | { |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 19 | return s << "Command SET {path: " << pathToSchemaString(cmd.m_path, Prefixes::Always) << ", type " << boost::core::demangle(cmd.m_data.type().name()) << ", data: " << leafDataToString(cmd.m_data) << "}"; |
Jan Kundrát | 76b3ddc | 2019-07-03 10:31:27 +0200 | [diff] [blame] | 20 | } |
| 21 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 22 | TEST_CASE("leaf editing") |
| 23 | { |
Václav Kubernát | bddbb17 | 2018-06-13 16:27:39 +0200 | [diff] [blame] | 24 | auto schema = std::make_shared<StaticSchema>(); |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 25 | schema->addModule("mod"); |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 26 | schema->addModule("pizza-module"); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 27 | schema->addContainer("/", "mod:contA"); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 28 | schema->addLeaf("/", "mod:leafString", yang::String{}); |
| 29 | schema->addLeaf("/", "mod:leafDecimal", yang::Decimal{}); |
| 30 | schema->addLeaf("/", "mod:leafBool", yang::Bool{}); |
| 31 | schema->addLeaf("/", "mod:leafInt8", yang::Int8{}); |
| 32 | schema->addLeaf("/", "mod:leafInt16", yang::Int16{}); |
| 33 | schema->addLeaf("/", "mod:leafInt32", yang::Int32{}); |
| 34 | schema->addLeaf("/", "mod:leafInt64", yang::Int64{}); |
| 35 | schema->addLeaf("/", "mod:leafUint8", yang::Uint8{}); |
| 36 | schema->addLeaf("/", "mod:leafUint16", yang::Uint16{}); |
| 37 | schema->addLeaf("/", "mod:leafUint32", yang::Uint32{}); |
| 38 | schema->addLeaf("/", "mod:leafUint64", yang::Uint64{}); |
| 39 | schema->addLeaf("/", "mod:leafBinary", yang::Binary{}); |
Václav Kubernát | 222ecff | 2020-05-14 23:14:35 +0200 | [diff] [blame] | 40 | schema->addIdentity(std::nullopt, identityRef_{"mod", "food"}); |
| 41 | schema->addIdentity(std::nullopt, identityRef_{"mod", "vehicle"}); |
| 42 | schema->addIdentity(identityRef_{"mod", "food"}, identityRef_{"mod", "pizza"}); |
| 43 | schema->addIdentity(identityRef_{"mod", "food"}, identityRef_{"mod", "spaghetti"}); |
| 44 | schema->addIdentity(identityRef_{"mod", "pizza"}, identityRef_{"pizza-module", "hawaii"}); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 45 | schema->addLeaf("/", "mod:foodIdentRef", yang::IdentityRef{schema->validIdentities("mod", "food")}); |
| 46 | schema->addLeaf("/", "mod:pizzaIdentRef", yang::IdentityRef{schema->validIdentities("mod", "pizza")}); |
| 47 | schema->addLeaf("/mod:contA", "mod:identInCont", yang::IdentityRef{schema->validIdentities("mod", "pizza")}); |
| 48 | schema->addLeaf("/", "mod:leafEnum", createEnum({"lol", "data", "coze"})); |
| 49 | schema->addLeaf("/mod:contA", "mod:leafInCont", yang::String{}); |
Václav Kubernát | efcac93 | 2020-01-10 15:26:32 +0100 | [diff] [blame] | 50 | schema->addList("/", "mod:list", {"number"}); |
Václav Kubernát | 3a99f00 | 2020-03-31 02:27:41 +0200 | [diff] [blame] | 51 | schema->addLeaf("/mod:list", "mod:number", yang::Int32{}); |
| 52 | schema->addLeaf("/mod:list", "mod:leafInList", yang::String{}); |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 53 | schema->addLeaf("/", "mod:refToString", yang::LeafRef{"/mod:leafString", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:leafString"))}); |
| 54 | schema->addLeaf("/", "mod:refToInt8", yang::LeafRef{"/mod:leafInt8", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:leafInt8"))}); |
| 55 | schema->addLeaf("/", "mod:refToLeafInCont", yang::LeafRef{"/mod:contA/identInCont", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:contA/mod:identInCont"))}); |
| 56 | schema->addLeaf("/", "mod:intOrString", yang::Union{{yang::TypeInfo{yang::Int32{}}, yang::TypeInfo{yang::String{}}}}); |
| 57 | schema->addLeaf("/", "mod:twoInts", yang::Union{{yang::TypeInfo{yang::Uint8{}}, yang::TypeInfo{yang::Int16{}}}}); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 58 | schema->addLeaf("/", "mod:unionStringEnumLeafref", yang::Union{{ |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 59 | yang::LeafDataType{yang::String{}}, |
| 60 | yang::LeafDataType{createEnum({"foo", "bar"})}, |
| 61 | yang::LeafDataType{yang::LeafRef{"/mod:leafEnum", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:leafEnum"))}} |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 62 | }}); |
| 63 | |
| 64 | schema->addList("/", "mod:portSettings", {"port"}); |
| 65 | schema->addLeaf("/mod:portSettings", "mod:port", createEnum({"eth0", "eth1", "eth2"})); |
| 66 | schema->addList("/", "mod:portMapping", {"port"}); |
| 67 | schema->addLeaf("/mod:portMapping", "mod:port", createEnum({"utf1", "utf2", "utf3"})); |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 68 | schema->addLeaf("/", "mod:activeMappedPort", yang::LeafRef{"/mod:portMapping/mod:port", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:portMapping/mod:port"))}); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 69 | schema->addLeaf("/", "mod:activePort", yang::Union{{ |
Václav Kubernát | 13b23d7 | 2020-04-16 21:49:51 +0200 | [diff] [blame] | 70 | yang::TypeInfo{createEnum({"wlan0", "wlan1"})}, |
| 71 | yang::TypeInfo{yang::LeafRef{"/mod:portSettings/mod:port", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:portSettings/mod:port"))}}, |
| 72 | yang::TypeInfo{yang::LeafRef{"/mod:activeMappedPort", std::make_unique<yang::TypeInfo>(schema->leafType("/mod:activeMappedPort"))}}, |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 73 | yang::TypeInfo{yang::Empty{}}, |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 74 | }}); |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 75 | schema->addLeaf("/", "mod:dummy", yang::Empty{}); |
Václav Kubernát | abf5280 | 2020-05-19 01:31:17 +0200 | [diff] [blame] | 76 | schema->addLeaf("/", "mod:readonly", yang::Int32{}, yang::AccessType::ReadOnly); |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 77 | |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 78 | Parser parser(schema); |
| 79 | std::string input; |
| 80 | std::ostringstream errorStream; |
| 81 | |
| 82 | SECTION("valid input") |
| 83 | { |
| 84 | set_ expected; |
| 85 | |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 86 | SECTION("set mod:leafString \"some_data\"") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 87 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 88 | input = "set mod:leafString \'some_data\'"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 89 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 90 | expected.m_data = std::string("some_data"); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 93 | SECTION("set mod:contA/leafInCont 'more_data'") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 94 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 95 | input = "set mod:contA/leafInCont 'more_data'"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 96 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")}); |
| 97 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 98 | expected.m_data = std::string("more_data"); |
| 99 | } |
| 100 | |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 101 | SECTION("set mod:contA/leafInCont \"data with' a quote\"") |
Václav Kubernát | c18ffac | 2020-01-10 16:06:06 +0100 | [diff] [blame] | 102 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 103 | input = "set mod:contA/leafInCont \"data with' a quote\""; |
| 104 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")}); |
| 105 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")}); |
| 106 | expected.m_data = std::string("data with' a quote"); |
| 107 | } |
| 108 | |
| 109 | SECTION("set mod:contA/leafInCont 'data with\" a quote'") |
| 110 | { |
| 111 | input = "set mod:contA/leafInCont 'data with\" a quote'"; |
| 112 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")}); |
| 113 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")}); |
| 114 | expected.m_data = std::string("data with\" a quote"); |
| 115 | } |
| 116 | |
| 117 | SECTION("set mod:contA/leafInCont 'more d\tata'") // spaces in string |
| 118 | { |
| 119 | input = "set mod:contA/leafInCont 'more d\tata'"; |
Václav Kubernát | c18ffac | 2020-01-10 16:06:06 +0100 | [diff] [blame] | 120 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")}); |
| 121 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInCont")}); |
| 122 | expected.m_data = std::string("more d\tata"); |
| 123 | } |
| 124 | |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 125 | SECTION("set mod:list[number=1]/leafInList \"another_data\"") |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 126 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 127 | input = "set mod:list[number=1]/leafInList \"another_data\""; |
Václav Kubernát | c15fe82 | 2020-06-04 11:28:39 +0200 | [diff] [blame] | 128 | auto keys = ListInstance { |
Václav Kubernát | 7707cae | 2020-01-16 12:04:53 +0100 | [diff] [blame] | 129 | {"number", int32_t{1}}}; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 130 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, listElement_("list", keys)}); |
| 131 | expected.m_path.m_nodes.push_back(dataNode_{leaf_("leafInList")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 132 | expected.m_data = std::string("another_data"); |
| 133 | } |
| 134 | |
| 135 | SECTION("data types") |
| 136 | { |
| 137 | SECTION("string") |
| 138 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 139 | input = "set mod:leafString \"somedata\""; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 140 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafString")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 141 | expected.m_data = std::string("somedata"); |
| 142 | } |
| 143 | |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 144 | SECTION("int8") |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 145 | { |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 146 | input = "set mod:leafInt8 2"; |
| 147 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt8")}); |
| 148 | expected.m_data = int8_t{2}; |
| 149 | } |
| 150 | |
| 151 | SECTION("negative int8") |
| 152 | { |
| 153 | input = "set mod:leafInt8 -10"; |
| 154 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt8")}); |
| 155 | expected.m_data = int8_t{-10}; |
| 156 | } |
| 157 | |
| 158 | SECTION("uint8") |
| 159 | { |
| 160 | input = "set mod:leafUint8 2"; |
| 161 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint8")}); |
| 162 | expected.m_data = uint8_t{2}; |
| 163 | } |
| 164 | |
| 165 | SECTION("int16") |
| 166 | { |
| 167 | input = "set mod:leafInt16 30000"; |
| 168 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt16")}); |
| 169 | expected.m_data = int16_t{30'000}; |
| 170 | } |
| 171 | |
| 172 | SECTION("uint16") |
| 173 | { |
| 174 | input = "set mod:leafUint16 30000"; |
| 175 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint16")}); |
| 176 | expected.m_data = uint16_t{30'000}; |
| 177 | } |
| 178 | |
| 179 | SECTION("int32") |
| 180 | { |
| 181 | input = "set mod:leafInt32 30000"; |
| 182 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt32")}); |
| 183 | expected.m_data = int32_t{30'000}; |
| 184 | } |
| 185 | |
| 186 | SECTION("uint32") |
| 187 | { |
| 188 | input = "set mod:leafUint32 30000"; |
| 189 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint32")}); |
| 190 | expected.m_data = uint32_t{30'000}; |
| 191 | } |
| 192 | |
| 193 | SECTION("int32") |
| 194 | { |
| 195 | input = "set mod:leafInt32 30000"; |
| 196 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafInt32")}); |
| 197 | expected.m_data = int32_t{30'000}; |
| 198 | } |
| 199 | |
| 200 | SECTION("uint64") |
| 201 | { |
| 202 | input = "set mod:leafUint64 30000"; |
| 203 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafUint64")}); |
| 204 | expected.m_data = uint64_t{30'000}; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | SECTION("decimal") |
| 208 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 209 | input = "set mod:leafDecimal 3.14159"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 210 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafDecimal")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 211 | expected.m_data = 3.14159; |
| 212 | } |
| 213 | |
| 214 | SECTION("enum") |
| 215 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 216 | input = "set mod:leafEnum coze"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 217 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafEnum")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 218 | expected.m_data = enum_("coze"); |
| 219 | } |
| 220 | |
| 221 | SECTION("bool") |
| 222 | { |
Václav Kubernát | 744f57f | 2018-06-29 22:46:26 +0200 | [diff] [blame] | 223 | input = "set mod:leafBool true"; |
Václav Kubernát | 2eaceb8 | 2018-10-08 19:56:30 +0200 | [diff] [blame] | 224 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBool")}); |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 225 | expected.m_data = true; |
| 226 | } |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 227 | |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 228 | SECTION("union") |
| 229 | { |
| 230 | SECTION("int") |
| 231 | { |
| 232 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("intOrString")}); |
| 233 | input = "set mod:intOrString 90"; |
| 234 | expected.m_data = int32_t{90}; |
| 235 | } |
| 236 | SECTION("string") |
| 237 | { |
| 238 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("intOrString")}); |
| 239 | input = "set mod:intOrString \"test\""; |
| 240 | expected.m_data = std::string{"test"}; |
| 241 | } |
| 242 | |
| 243 | SECTION("union with two integral types") |
| 244 | { |
| 245 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("twoInts")}); |
| 246 | SECTION("uint8") |
| 247 | { |
| 248 | input = "set mod:twoInts 100"; |
| 249 | expected.m_data = uint8_t{100}; |
| 250 | } |
| 251 | SECTION("int16") |
| 252 | { |
| 253 | input = "set mod:twoInts 6666"; |
| 254 | expected.m_data = int16_t{6666}; |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | SECTION("union with enum and leafref to enum") |
| 259 | { |
| 260 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("unionStringEnumLeafref")}); |
| 261 | SECTION("string") |
| 262 | { |
| 263 | input = "set mod:unionStringEnumLeafref \"AHOJ\""; |
| 264 | expected.m_data = std::string{"AHOJ"}; |
| 265 | } |
| 266 | SECTION("enum") |
| 267 | { |
| 268 | input = "set mod:unionStringEnumLeafref bar"; |
| 269 | expected.m_data = enum_("bar"); |
| 270 | } |
| 271 | SECTION("enum leafref") |
| 272 | { |
| 273 | input = "set mod:unionStringEnumLeafref coze"; |
| 274 | expected.m_data = enum_("coze"); |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | SECTION("activePort") |
| 279 | { |
| 280 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("activePort")}); |
| 281 | input = "set mod:activePort "; |
| 282 | SECTION("1. anonymous enum") |
| 283 | { |
| 284 | SECTION("wlan0") |
| 285 | { |
| 286 | input += "wlan0"; |
| 287 | expected.m_data = enum_("wlan0"); |
| 288 | } |
| 289 | SECTION("wlan1") |
| 290 | { |
| 291 | input += "wlan1"; |
| 292 | expected.m_data = enum_("wlan1"); |
| 293 | } |
| 294 | } |
| 295 | SECTION("2. leafref to enum") |
| 296 | { |
| 297 | SECTION("eth0") |
| 298 | { |
| 299 | input += "eth0"; |
| 300 | expected.m_data = enum_("eth0"); |
| 301 | } |
| 302 | SECTION("eth1") |
| 303 | { |
| 304 | input += "eth1"; |
| 305 | expected.m_data = enum_("eth1"); |
| 306 | } |
| 307 | SECTION("eth2") |
| 308 | { |
| 309 | input += "eth2"; |
| 310 | expected.m_data = enum_("eth2"); |
| 311 | } |
| 312 | } |
| 313 | SECTION("3. leafref to leafref") |
| 314 | { |
| 315 | SECTION("utf1") |
| 316 | { |
| 317 | input += "utf1"; |
| 318 | expected.m_data = enum_("utf1"); |
| 319 | } |
| 320 | SECTION("utf2") |
| 321 | { |
| 322 | input += "utf2"; |
| 323 | expected.m_data = enum_("utf2"); |
| 324 | } |
| 325 | SECTION("utf3") |
| 326 | { |
| 327 | input += "utf3"; |
| 328 | expected.m_data = enum_("utf3"); |
| 329 | } |
| 330 | } |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 331 | SECTION("4. empty") |
| 332 | { |
| 333 | expected.m_data = empty_{}; |
| 334 | } |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 335 | } |
| 336 | } |
| 337 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 338 | SECTION("binary") |
| 339 | { |
| 340 | SECTION("zero ending '='") |
| 341 | { |
| 342 | input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++"; |
| 343 | expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++"}; |
| 344 | } |
| 345 | |
| 346 | SECTION("one ending '='") |
| 347 | { |
| 348 | input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++="; |
| 349 | expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++="}; |
| 350 | } |
| 351 | |
| 352 | SECTION("two ending '='") |
| 353 | { |
| 354 | input = "set mod:leafBinary This/IsABase64EncodedSomething++/342431++=="; |
| 355 | expected.m_data = binary_{"This/IsABase64EncodedSomething++/342431++=="}; |
| 356 | } |
| 357 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("leafBinary")}); |
| 358 | } |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 359 | |
| 360 | SECTION("identityRef") |
| 361 | { |
| 362 | SECTION("foodIdentRef") |
| 363 | { |
| 364 | input = "set mod:foodIdentRef "; |
| 365 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("foodIdentRef")}); |
| 366 | |
| 367 | SECTION("food") |
| 368 | { |
| 369 | input += "food"; |
| 370 | expected.m_data = identityRef_("food"); |
| 371 | } |
| 372 | SECTION("mod:food") |
| 373 | { |
| 374 | input += "mod:food"; |
| 375 | expected.m_data = identityRef_("mod", "food"); |
| 376 | } |
| 377 | SECTION("pizza") |
| 378 | { |
| 379 | input += "pizza"; |
| 380 | expected.m_data = identityRef_("pizza"); |
| 381 | } |
| 382 | SECTION("mod:pizza") |
| 383 | { |
| 384 | input += "mod:pizza"; |
| 385 | expected.m_data = identityRef_("mod", "pizza"); |
| 386 | } |
| 387 | SECTION("pizza-module:hawaii") |
| 388 | { |
| 389 | input += "pizza-module:hawaii"; |
| 390 | expected.m_data = identityRef_("pizza-module", "hawaii"); |
| 391 | } |
| 392 | } |
| 393 | SECTION("pizzaIdentRef") |
| 394 | { |
| 395 | input = "set mod:pizzaIdentRef "; |
| 396 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("pizzaIdentRef")}); |
| 397 | SECTION("pizza") |
| 398 | { |
| 399 | input += "pizza"; |
| 400 | expected.m_data = identityRef_("pizza"); |
| 401 | } |
| 402 | SECTION("mod:pizza") |
| 403 | { |
| 404 | input += "mod:pizza"; |
| 405 | expected.m_data = identityRef_("mod", "pizza"); |
| 406 | } |
| 407 | SECTION("pizza-module:hawaii") |
| 408 | { |
| 409 | input += "pizza-module:hawaii"; |
| 410 | expected.m_data = identityRef_("pizza-module", "hawaii"); |
| 411 | } |
| 412 | } |
| 413 | SECTION("mod:contA/identInCont") |
| 414 | { |
| 415 | input = "set mod:contA/identInCont "; |
| 416 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, container_("contA")}); |
| 417 | expected.m_path.m_nodes.push_back(dataNode_(leaf_("identInCont"))); |
| 418 | SECTION("pizza") |
| 419 | { |
| 420 | input += "pizza"; |
| 421 | expected.m_data = identityRef_("pizza"); |
| 422 | } |
| 423 | SECTION("mod:pizza") |
| 424 | { |
| 425 | input += "mod:pizza"; |
| 426 | expected.m_data = identityRef_("mod", "pizza"); |
| 427 | } |
| 428 | SECTION("pizza-module:hawaii") |
| 429 | { |
| 430 | input += "pizza-module:hawaii"; |
| 431 | expected.m_data = identityRef_("pizza-module", "hawaii"); |
| 432 | } |
| 433 | } |
| 434 | } |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 435 | SECTION("leafRef") |
| 436 | { |
| 437 | SECTION("refToString") |
| 438 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 439 | input = "set mod:refToString \"blabal\""; |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 440 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("refToString")}); |
| 441 | expected.m_data = std::string("blabal"); |
| 442 | } |
| 443 | |
| 444 | SECTION("refToInt8") |
| 445 | { |
| 446 | input = "set mod:refToInt8 42"; |
| 447 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("refToInt8")}); |
| 448 | expected.m_data = int8_t{42}; |
| 449 | } |
Václav Kubernát | 2d7b05c | 2020-04-05 14:02:37 +0200 | [diff] [blame] | 450 | |
| 451 | SECTION("refToLeafInCont") |
| 452 | { |
| 453 | input = "set mod:refToLeafInCont pizza"; |
| 454 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("refToLeafInCont")}); |
| 455 | expected.m_data = identityRef_{"pizza"}; |
| 456 | } |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 457 | } |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 458 | SECTION("empty") |
| 459 | { |
| 460 | input = "set mod:dummy "; |
| 461 | expected.m_path.m_nodes.push_back(dataNode_{module_{"mod"}, leaf_("dummy")}); |
| 462 | expected.m_data = empty_{}; |
| 463 | } |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | command_ command = parser.parseCommand(input, errorStream); |
| 467 | REQUIRE(command.type() == typeid(set_)); |
| 468 | REQUIRE(boost::get<set_>(command) == expected); |
| 469 | } |
| 470 | |
| 471 | SECTION("invalid input") |
| 472 | { |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 473 | std::string expectedError; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 474 | SECTION("missing space between a command and its arguments") |
| 475 | { |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 476 | SECTION("setmod:leafString some_data") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 477 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 478 | input = "setmod:leafString 'some_data'"; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 479 | } |
| 480 | } |
| 481 | |
| 482 | SECTION("missing space between arguments") |
| 483 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 484 | SECTION("set mod:leafString'lol'") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 485 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 486 | input = "set mod:leafString'lol'"; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 487 | } |
| 488 | } |
| 489 | |
| 490 | SECTION("non-leaf identifiers") |
| 491 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 492 | SECTION("set mod:nonexistent 'blabla'") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 493 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 494 | input = "set mod:nonexistent 'blabla'"; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 495 | } |
| 496 | |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 497 | SECTION("set mod:contA 'abde'") |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 498 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 499 | input = "set mod:contA 'abde'"; |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 503 | SECTION("wrong types") |
| 504 | { |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 505 | expectedError = "leaf data type mismatch"; |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 506 | SECTION("set mod:leafBool 'blabla'") |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 507 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 508 | input = "set mod:leafBool 'blabla'"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 509 | } |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 510 | SECTION("set mod:leafUint8 'blabla'") |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 511 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 512 | input = "set mod:leafUint8 'blabla'"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 513 | } |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 514 | SECTION("set mod:leafUint8 -5") |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 515 | { |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 516 | input = "set mod:leafUint8 -5"; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 517 | } |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 518 | SECTION("set mod:leafInt8 'blabla'") |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 519 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 520 | input = "set mod:leafInt8 'blabla'"; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 521 | } |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 522 | SECTION("set mod:leafInt8 130") |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 523 | { |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 524 | input = "set mod:leafInt8 130"; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 525 | } |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 526 | SECTION("set mod:leafUint16 'blabla'") |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 527 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 528 | input = "set mod:leafUint16 'blabla'"; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 529 | } |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 530 | SECTION("set mod:leafInt16 'blabla'") |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 531 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 532 | input = "set mod:leafInt16 'blabla'"; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 533 | } |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 534 | SECTION("set mod:leafUint32 'blabla'") |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 535 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 536 | input = "set mod:leafUint32 'blabla'"; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 537 | } |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 538 | SECTION("set mod:leafInt32 'blabla'") |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 539 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 540 | input = "set mod:leafInt32 'blabla'"; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 541 | } |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 542 | SECTION("set mod:leafUint64 'blabla'") |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 543 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 544 | input = "set mod:leafUint64 'blabla'"; |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 545 | } |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 546 | SECTION("set mod:leafInt64 'blabla'") |
Ivona Oboňová | 88c78ca | 2019-07-02 18:40:07 +0200 | [diff] [blame] | 547 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 548 | input = "set mod:leafInt64 'blabla'"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 549 | } |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 550 | SECTION("set mod:leafEnum 'blabla'") |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 551 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 552 | input = "set mod:leafEnum 'blabla'"; |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 553 | } |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 554 | SECTION("set mod:refToInt8 'blabla'") |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 555 | { |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 556 | input = "set mod:refToInt8 'blabla'"; |
Václav Kubernát | 6a8d1d9 | 2019-04-24 20:30:36 +0200 | [diff] [blame] | 557 | } |
Václav Kubernát | ebca255 | 2018-06-08 19:06:02 +0200 | [diff] [blame] | 558 | } |
| 559 | |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 560 | SECTION("wrong base64 strings") |
| 561 | { |
| 562 | SECTION("invalid character") |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 563 | input = "set mod:leafBinary dbahj-"; |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 564 | SECTION("equal sign in the middle") |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 565 | input = "set mod:leafBinary db=ahj"; |
Václav Kubernát | 1bbac7c | 2020-01-15 17:45:41 +0100 | [diff] [blame] | 566 | SECTION("enclosing in quotes") |
| 567 | input = "set mod:leafBinary 'dbahj'"; |
Václav Kubernát | ab53899 | 2019-03-06 15:30:50 +0100 | [diff] [blame] | 568 | } |
| 569 | |
Václav Kubernát | eeb3884 | 2019-03-20 19:46:05 +0100 | [diff] [blame] | 570 | SECTION("non-existing identity") |
| 571 | { |
| 572 | input = "set mod:foodIdentRef identityBLABLA"; |
| 573 | } |
| 574 | |
| 575 | SECTION("setting identities with wrong bases") |
| 576 | { |
| 577 | SECTION("set mod:foodIdentRef mod:vehicle") |
| 578 | { |
| 579 | input = "set mod:foodIdentRef mod:vehicle"; |
| 580 | } |
| 581 | SECTION("set mod:pizzaIdentRef mod:food") |
| 582 | { |
| 583 | input = "set mod:pizzaIdentRef mod:food"; |
| 584 | } |
| 585 | } |
| 586 | SECTION("setting different module identities without prefix") |
| 587 | { |
| 588 | input = "set mod:pizzaIdentRef hawaii"; |
| 589 | } |
| 590 | SECTION("identity prefix without name") |
| 591 | { |
| 592 | input = "set mod:contA/identInCont pizza-module:"; |
| 593 | } |
| 594 | |
Václav Kubernát | 2984f44 | 2020-02-20 17:43:35 +0100 | [diff] [blame] | 595 | SECTION("set a union path to a wrong type") |
| 596 | { |
| 597 | input = "set mod:intOrString true"; |
| 598 | } |
| 599 | |
Jan Kundrát | 379bb57 | 2020-05-07 03:23:13 +0200 | [diff] [blame] | 600 | SECTION("no space for empty data") |
| 601 | { |
| 602 | input = "set mod:dummy"; |
| 603 | } |
| 604 | |
Václav Kubernát | 0d42c51 | 2020-05-20 21:18:39 +0200 | [diff] [blame] | 605 | SECTION("empty path") |
| 606 | { |
| 607 | input = "set "; |
| 608 | } |
| 609 | |
Václav Kubernát | abf5280 | 2020-05-19 01:31:17 +0200 | [diff] [blame] | 610 | SECTION("setting readonly data") |
| 611 | { |
| 612 | input = "set mod:readonly 123"; |
| 613 | } |
| 614 | |
Jan Kundrát | c381e63 | 2019-03-14 13:39:11 +0100 | [diff] [blame] | 615 | REQUIRE_THROWS_AS(parser.parseCommand(input, errorStream), InvalidCommandException); |
Václav Kubernát | 777704d | 2020-01-15 18:48:06 +0100 | [diff] [blame] | 616 | REQUIRE(errorStream.str().find(expectedError) != std::string::npos); |
Václav Kubernát | 0720424 | 2018-06-04 18:12:09 +0200 | [diff] [blame] | 617 | } |
| 618 | } |