token_unittest.cc 112 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724
  1. // Copyright (C) 2015-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <fstream>
  8. #include <eval/token.h>
  9. #include <dhcp/pkt4.h>
  10. #include <dhcp/pkt6.h>
  11. #include <dhcp/dhcp4.h>
  12. #include <dhcp/dhcp6.h>
  13. #include <dhcp/option_string.h>
  14. #include <dhcp/option_vendor.h>
  15. #include <dhcp/option_vendor_class.h>
  16. #include <log/logger_manager.h>
  17. #include <log/logger_name.h>
  18. #include <log/logger_support.h>
  19. #include <testutils/log_utils.h>
  20. #include <boost/shared_ptr.hpp>
  21. #include <boost/scoped_ptr.hpp>
  22. #include <gtest/gtest.h>
  23. #include <arpa/inet.h>
  24. using namespace std;
  25. using namespace isc::dhcp;
  26. using namespace isc::asiolink;
  27. using namespace isc::log;
  28. using namespace isc::dhcp::test;
  29. namespace {
  30. /// @brief Test fixture for testing Tokens.
  31. ///
  32. /// This class provides several convenience objects to be used during testing
  33. /// of the Token family of classes.
  34. class TokenTest : public LogContentTest {
  35. public:
  36. /// @brief Initializes Pkt4, Pkt6 and options that can be useful for
  37. /// evaluation tests.
  38. TokenTest() {
  39. pkt4_.reset(new Pkt4(DHCPDISCOVER, 12345));
  40. pkt6_.reset(new Pkt6(DHCPV6_SOLICIT, 12345));
  41. // Add options with easily identifiable strings in them
  42. option_str4_.reset(new OptionString(Option::V4, 100, "hundred4"));
  43. option_str6_.reset(new OptionString(Option::V6, 100, "hundred6"));
  44. pkt4_->addOption(option_str4_);
  45. pkt6_->addOption(option_str6_);
  46. // Change this to true if you need extra information about logging
  47. // checks to be printed.
  48. logCheckVerbose(false);
  49. }
  50. /// @brief Inserts RAI option with several suboptions
  51. ///
  52. /// The structure inserted is:
  53. /// - RAI (option 82)
  54. /// - option 1 (containing string "one")
  55. /// - option 13 (containing string "thirteen")
  56. void insertRelay4Option() {
  57. // RAI (Relay Agent Information) option
  58. OptionPtr rai(new Option(Option::V4, DHO_DHCP_AGENT_OPTIONS));
  59. OptionPtr sub1(new OptionString(Option::V4, 1, "one"));
  60. OptionPtr sub13(new OptionString(Option::V4, 13, "thirteen"));
  61. rai->addOption(sub1);
  62. rai->addOption(sub13);
  63. pkt4_->addOption(rai);
  64. }
  65. /// @brief Adds relay encapsulations with some suboptions
  66. ///
  67. /// This will add 2 relay encapsulations all will have
  68. /// msg_type of RELAY_FORW
  69. /// Relay 0 (closest to server) will have
  70. /// linkaddr = peeraddr = 0, hop-count = 1
  71. /// option 100 "hundred.zero", option 101 "hundredone.zero"
  72. /// Relay 1 (closest to client) will have
  73. /// linkaddr 1::1= peeraddr = 1::2, hop-count = 0
  74. /// option 100 "hundred.one", option 102 "hundredtwo.one"
  75. void addRelay6Encapsulations() {
  76. // First relay
  77. Pkt6::RelayInfo relay0;
  78. relay0.msg_type_ = DHCPV6_RELAY_FORW;
  79. relay0.hop_count_ = 1;
  80. relay0.linkaddr_ = isc::asiolink::IOAddress("::");
  81. relay0.peeraddr_ = isc::asiolink::IOAddress("::");
  82. OptionPtr optRelay01(new OptionString(Option::V6, 100,
  83. "hundred.zero"));
  84. OptionPtr optRelay02(new OptionString(Option::V6, 101,
  85. "hundredone.zero"));
  86. relay0.options_.insert(make_pair(optRelay01->getType(), optRelay01));
  87. relay0.options_.insert(make_pair(optRelay02->getType(), optRelay02));
  88. pkt6_->addRelayInfo(relay0);
  89. // Second relay
  90. Pkt6::RelayInfo relay1;
  91. relay1.msg_type_ = DHCPV6_RELAY_FORW;
  92. relay1.hop_count_ = 0;
  93. relay1.linkaddr_ = isc::asiolink::IOAddress("1::1");
  94. relay1.peeraddr_ = isc::asiolink::IOAddress("1::2");
  95. OptionPtr optRelay11(new OptionString(Option::V6, 100,
  96. "hundred.one"));
  97. OptionPtr optRelay12(new OptionString(Option::V6, 102,
  98. "hundredtwo.one"));
  99. relay1.options_.insert(make_pair(optRelay11->getType(), optRelay11));
  100. relay1.options_.insert(make_pair(optRelay12->getType(), optRelay12));
  101. pkt6_->addRelayInfo(relay1);
  102. }
  103. /// @brief Verify that the relay6 option evaluatiosn work properly
  104. ///
  105. /// Given the nesting level and option code extract the option
  106. /// and compare it to the expected string.
  107. ///
  108. /// @param test_level The nesting level
  109. /// @param test_code The code of the option to extract
  110. /// @param result_addr The expected result of the address as a string
  111. void verifyRelay6Option(const uint8_t test_level,
  112. const uint16_t test_code,
  113. const TokenOption::RepresentationType& test_rep,
  114. const std::string& result_string) {
  115. // Create the token
  116. ASSERT_NO_THROW(t_.reset(new TokenRelay6Option(test_level,
  117. test_code,
  118. test_rep)));
  119. // We should be able to evaluate it
  120. EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_));
  121. // We should have one value on the stack
  122. ASSERT_EQ(1, values_.size());
  123. // And it should match the expected result
  124. // Invalid nesting levels result in a 0 length string
  125. EXPECT_EQ(result_string, values_.top());
  126. // Then we clear the stack
  127. clearStack();
  128. }
  129. /// @brief Verify that the relay6 field evaluations work properly
  130. ///
  131. /// Given the nesting level, the field to extract and the expected
  132. /// address create a token and evaluate it then compare the addresses
  133. ///
  134. /// @param test_level The nesting level
  135. /// @param test_field The type of the field to extract
  136. /// @param result_addr The expected result of the address as a string
  137. void verifyRelay6Eval(const uint8_t test_level,
  138. const TokenRelay6Field::FieldType test_field,
  139. const int result_len,
  140. const uint8_t result_addr[]) {
  141. // Create the token
  142. ASSERT_NO_THROW(t_.reset(new TokenRelay6Field(test_level, test_field)));
  143. // We should be able to evaluate it
  144. EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_));
  145. // We should have one value on the stack
  146. ASSERT_EQ(1, values_.size());
  147. // And it should match the expected result
  148. // Invalid nesting levels result in a 0 length string
  149. EXPECT_EQ(result_len, values_.top().size());
  150. if (result_len != 0) {
  151. EXPECT_EQ(0, memcmp(result_addr, &values_.top()[0], result_len));
  152. }
  153. // Then we clear the stack
  154. clearStack();
  155. }
  156. /// @brief Convenience function. Removes token and values stacks.
  157. void clearStack() {
  158. while (!values_.empty()) {
  159. values_.pop();
  160. }
  161. t_.reset();
  162. }
  163. /// @brief Aux. function that stores integer values as 4 bytes string.
  164. ///
  165. /// @param value integer value to be stored
  166. /// @return 4 bytes long string with encoded value.
  167. string encode(uint32_t value) {
  168. string tmp(4,0);
  169. tmp[0] = value >> 24;
  170. tmp[1] = value >> 16;
  171. tmp[2] = value >> 8;
  172. tmp[3] = value;
  173. return (tmp);
  174. }
  175. TokenPtr t_; ///< Just a convenience pointer
  176. ValueStack values_; ///< evaluated values will be stored here
  177. Pkt4Ptr pkt4_; ///< A stub DHCPv4 packet
  178. Pkt6Ptr pkt6_; ///< A stub DHCPv6 packet
  179. OptionPtr option_str4_; ///< A string option for DHCPv4
  180. OptionPtr option_str6_; ///< A string option for DHCPv6
  181. OptionVendorPtr vendor_; ///< Vendor option used during tests
  182. OptionVendorClassPtr vendor_class_; ///< Vendor class option used during tests
  183. /// @brief Verify that the substring eval works properly
  184. ///
  185. /// This function takes the parameters and sets up the value
  186. /// stack then executes the eval and checks the results.
  187. ///
  188. /// @param test_string The string to operate on
  189. /// @param test_start The postion to start when getting a substring
  190. /// @param test_length The length of the substring to get
  191. /// @param result_string The expected result of the eval
  192. /// @param should_throw The eval will throw
  193. void verifySubstringEval(const std::string& test_string,
  194. const std::string& test_start,
  195. const std::string& test_length,
  196. const std::string& result_string,
  197. bool should_throw = false) {
  198. // create the token
  199. ASSERT_NO_THROW(t_.reset(new TokenSubstring()));
  200. // push values on stack
  201. values_.push(test_string);
  202. values_.push(test_start);
  203. values_.push(test_length);
  204. // evaluate the token
  205. if (should_throw) {
  206. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  207. ASSERT_EQ(0, values_.size());
  208. } else {
  209. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  210. // verify results
  211. ASSERT_EQ(1, values_.size());
  212. EXPECT_EQ(result_string, values_.top());
  213. // remove result
  214. values_.pop();
  215. }
  216. }
  217. /// @brief Creates vendor-option with specified value and adds it to packet
  218. ///
  219. /// This method creates specified vendor option, removes any existing
  220. /// vendor options and adds the new one to v4 or v6 packet.
  221. ///
  222. /// @param u universe (V4 or V6)
  223. /// @param vendor_id specifies enterprise-id value.
  224. void setVendorOption(Option::Universe u, uint32_t vendor_id) {
  225. vendor_.reset(new OptionVendor(u, vendor_id));
  226. switch (u) {
  227. case Option::V4:
  228. pkt4_->delOption(DHO_VIVSO_SUBOPTIONS);
  229. pkt4_->addOption(vendor_);
  230. break;
  231. case Option::V6:
  232. pkt6_->delOption(D6O_VENDOR_OPTS);
  233. pkt6_->addOption(vendor_);
  234. break;
  235. }
  236. }
  237. /// @brief Creates vendor-class option with specified values and adds it to packet
  238. ///
  239. /// This method creates specified vendor-class option, removes any existing
  240. /// vendor class options and adds the new one to v4 or v6 packet.
  241. /// It also creates data tuples with greek alphabet names.
  242. ///
  243. /// @param u universe (V4 or V6)
  244. /// @param vendor_id specifies enterprise-id value.
  245. /// @param tuples_size number of data tuples to create.
  246. void setVendorClassOption(Option::Universe u, uint32_t vendor_id,
  247. size_t tuples_size = 0) {
  248. // Create the option first.
  249. vendor_class_.reset(new OptionVendorClass(u, vendor_id));
  250. // Now let's add specified number of data tuples
  251. OpaqueDataTuple::LengthFieldType len = (u == Option::V4?OpaqueDataTuple::LENGTH_1_BYTE:
  252. OpaqueDataTuple::LENGTH_2_BYTES);
  253. const char * content[] = { "alpha", "beta", "delta", "gamma", "epsilon",
  254. "zeta", "eta", "theta", "iota", "kappa" };
  255. ASSERT_TRUE(tuples_size < sizeof(content));
  256. for (int i = 0; i < tuples_size; i++) {
  257. OpaqueDataTuple tuple(len);
  258. tuple.assign(string(content[i]));
  259. if (u == Option::V4 && i == 0) {
  260. // vendor-clas for v4 has a pecurilar quirk. The first tuple is being
  261. // added, even if there's no data at all.
  262. vendor_class_->setTuple(0, tuple);
  263. } else {
  264. vendor_class_->addTuple(tuple);
  265. }
  266. }
  267. switch (u) {
  268. case Option::V4:
  269. pkt4_->delOption(DHO_VIVCO_SUBOPTIONS);
  270. pkt4_->addOption(vendor_class_);
  271. break;
  272. case Option::V6:
  273. pkt6_->delOption(D6O_VENDOR_CLASS);
  274. pkt6_->addOption(vendor_class_);
  275. break;
  276. }
  277. }
  278. /// @brief Auxiliary function that evaluates tokens and checks result
  279. ///
  280. /// Depending on the universe, either pkt4_ or pkt6_ are supposed to have
  281. /// all the necessary values and options set. The result is checked
  282. /// on the values_ stack.
  283. ///
  284. /// @param u universe (V4 or V6)
  285. /// @param expected_result text representation of the expected outcome
  286. void evaluate(Option::Universe u, std::string expected_result) {
  287. switch (u) {
  288. case Option::V4:
  289. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  290. break;
  291. case Option::V6:
  292. EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_));
  293. break;
  294. default:
  295. ADD_FAILURE() << "Invalid universe specified.";
  296. }
  297. ASSERT_EQ(1, values_.size());
  298. EXPECT_EQ(expected_result, values_.top());
  299. }
  300. /// @brief Tests if vendor token behaves properly.
  301. ///
  302. /// @param u universe (V4 or V6)
  303. /// @param token_vendor_id enterprise-id used in the token
  304. /// @param option_vendor_id enterprise-id used in option (0 means don't
  305. /// create the option)
  306. /// @param expected_result text representation of the expected outcome
  307. void testVendorExists(Option::Universe u, uint32_t token_vendor_id,
  308. uint32_t option_vendor_id, std::string expected_result) {
  309. // Let's clear any old values, so we can run multiple cases in each test
  310. clearStack();
  311. // Create the token
  312. ASSERT_NO_THROW(t_.reset(new TokenVendor(u, token_vendor_id,
  313. TokenOption::EXISTS)));
  314. // If specified option is non-zero, create it.
  315. if (option_vendor_id) {
  316. setVendorOption(u, option_vendor_id);
  317. }
  318. evaluate(u, expected_result);
  319. }
  320. /// @brief Tests if vendor token properly returns enterprise-id.
  321. ///
  322. /// @param u universe (V4 or V6)
  323. /// @param option_vendor_id enterprise-id used in option (0 means don't
  324. /// create the option)
  325. /// @param expected_result text representation of the expected outcome
  326. void testVendorEnterprise(Option::Universe u, uint32_t option_vendor_id,
  327. std::string expected_result) {
  328. // Let's clear any old values, so we can run multiple cases in each test
  329. clearStack();
  330. ASSERT_NO_THROW(t_.reset(new TokenVendor(u, 0, TokenVendor::ENTERPRISE_ID)));
  331. if (option_vendor_id) {
  332. setVendorOption(u, option_vendor_id);
  333. }
  334. evaluate(u, expected_result);
  335. }
  336. /// @brief Tests if vendor class token properly returns enterprise-id.
  337. ///
  338. /// @param u universe (V4 or V6)
  339. /// @param option_vendor_id enterprise-id used in option (0 means don't
  340. /// create the option)
  341. /// @param expected_result text representation of the expected outcome
  342. void testVendorClassEnterprise(Option::Universe u, uint32_t option_vendor_id,
  343. std::string expected_result) {
  344. // Let's clear any old values, so we can run multiple cases in each test
  345. clearStack();
  346. ASSERT_NO_THROW(t_.reset(new TokenVendorClass(u, 0, TokenVendor::ENTERPRISE_ID)));
  347. if (option_vendor_id) {
  348. setVendorClassOption(u, option_vendor_id);
  349. }
  350. evaluate(u, expected_result);
  351. }
  352. /// @brief Tests if vendor class token can report existence properly.
  353. ///
  354. /// @param u universe (V4 or V6)
  355. /// @param token_vendor_id enterprise-id used in the token
  356. /// @param option_vendor_id enterprise-id used in option (0 means don't
  357. /// create the option)
  358. /// @param expected_result text representation of the expected outcome
  359. void testVendorClassExists(Option::Universe u, uint32_t token_vendor_id,
  360. uint32_t option_vendor_id, std::string expected_result) {
  361. // Let's clear any old values, so we can run multiple cases in each test
  362. clearStack();
  363. ASSERT_NO_THROW(t_.reset(new TokenVendorClass(u, token_vendor_id,
  364. TokenOption::EXISTS)));
  365. if (option_vendor_id) {
  366. setVendorClassOption(u, option_vendor_id);
  367. }
  368. evaluate(u, expected_result);
  369. }
  370. /// @brief Tests if vendor token can handle sub-options properly.
  371. ///
  372. /// @param u universe (V4 or V6)
  373. /// @param token_vendor_id enterprise-id used in the token
  374. /// @param token_option_code option code in the token
  375. /// @param option_vendor_id enterprise-id used in option (0 means don't
  376. /// create the option)
  377. /// @param option_code sub-option code (0 means don't create suboption)
  378. /// @param repr representation (TokenOption::EXISTS or HEXADECIMAL)
  379. /// @param expected_result text representation of the expected outcome
  380. void testVendorSuboption(Option::Universe u,
  381. uint32_t token_vendor_id, uint16_t token_option_code,
  382. uint32_t option_vendor_id, uint16_t option_code,
  383. TokenOption::RepresentationType repr, std::string expected) {
  384. // Let's clear any old values, so we can run multiple cases in each test
  385. clearStack();
  386. ASSERT_NO_THROW(t_.reset(new TokenVendor(u, token_vendor_id, repr,
  387. token_option_code)));
  388. if (option_vendor_id) {
  389. setVendorOption(u, option_vendor_id);
  390. if (option_code) {
  391. ASSERT_TRUE(vendor_);
  392. OptionPtr subopt(new OptionString(u, option_code, "alpha"));
  393. vendor_->addOption(subopt);
  394. }
  395. }
  396. evaluate(u, expected);
  397. }
  398. /// @brief Tests if vendor class token can handle data chunks properly.
  399. ///
  400. /// @param u universe (V4 or V6)
  401. /// @param token_vendor_id enterprise-id used in the token
  402. /// @param token_index data index used in the token
  403. /// @param option_vendor_id enterprise-id used in option (0 means don't
  404. /// create the option)
  405. /// @param data_tuples number of data tuples in the option
  406. /// @param expected_result text representation of the expected outcome
  407. void testVendorClassData(Option::Universe u,
  408. uint32_t token_vendor_id, uint16_t token_index,
  409. uint32_t option_vendor_id, uint16_t data_tuples,
  410. std::string expected) {
  411. // Let's clear any old values, so we can run multiple cases in each test
  412. clearStack();
  413. ASSERT_NO_THROW(t_.reset(new TokenVendorClass(u, token_vendor_id,
  414. TokenVendor::DATA, token_index)));
  415. if (option_vendor_id) {
  416. setVendorClassOption(u, option_vendor_id, data_tuples);
  417. }
  418. evaluate(u, expected);
  419. }
  420. };
  421. // This tests the toBool() conversions
  422. TEST_F(TokenTest, toBool) {
  423. ASSERT_NO_THROW(Token::toBool("true"));
  424. EXPECT_TRUE(Token::toBool("true"));
  425. ASSERT_NO_THROW(Token::toBool("false"));
  426. EXPECT_FALSE(Token::toBool("false"));
  427. // Token::toBool() is case-sensitive
  428. EXPECT_THROW(Token::toBool("True"), EvalTypeError);
  429. EXPECT_THROW(Token::toBool("TRUE"), EvalTypeError);
  430. // Proposed aliases
  431. EXPECT_THROW(Token::toBool("1"), EvalTypeError);
  432. EXPECT_THROW(Token::toBool("0"), EvalTypeError);
  433. EXPECT_THROW(Token::toBool(""), EvalTypeError);
  434. }
  435. // This simple test checks that a TokenString, representing a constant string,
  436. // can be used in Pkt4 evaluation. (The actual packet is not used)
  437. TEST_F(TokenTest, string4) {
  438. // Store constant string "foo" in the TokenString object.
  439. ASSERT_NO_THROW(t_.reset(new TokenString("foo")));
  440. // Make sure that the token can be evaluated without exceptions.
  441. ASSERT_NO_THROW(t_->evaluate(*pkt4_, values_));
  442. // Check that the evaluation put its value on the values stack.
  443. ASSERT_EQ(1, values_.size());
  444. EXPECT_EQ("foo", values_.top());
  445. // Check that the debug output was correct. Add the strings
  446. // to the test vector in the class and then call checkFile
  447. // for comparison
  448. addString("EVAL_DEBUG_STRING Pushing text string 'foo'");
  449. EXPECT_TRUE(checkFile());
  450. }
  451. // This simple test checks that a TokenString, representing a constant string,
  452. // can be used in Pkt6 evaluation. (The actual packet is not used)
  453. TEST_F(TokenTest, string6) {
  454. // Store constant string "foo" in the TokenString object.
  455. ASSERT_NO_THROW(t_.reset(new TokenString("foo")));
  456. // Make sure that the token can be evaluated without exceptions.
  457. ASSERT_NO_THROW(t_->evaluate(*pkt6_, values_));
  458. // Check that the evaluation put its value on the values stack.
  459. ASSERT_EQ(1, values_.size());
  460. EXPECT_EQ("foo", values_.top());
  461. // Check that the debug output was correct. Add the strings
  462. // to the test vector in the class and then call checkFile
  463. // for comparison
  464. addString("EVAL_DEBUG_STRING Pushing text string 'foo'");
  465. EXPECT_TRUE(checkFile());
  466. }
  467. // This simple test checks that a TokenHexString, representing a constant
  468. // string coded in hexadecimal, can be used in Pkt4 evaluation.
  469. // (The actual packet is not used)
  470. TEST_F(TokenTest, hexstring4) {
  471. TokenPtr empty;
  472. TokenPtr bad;
  473. TokenPtr nodigit;
  474. TokenPtr baddigit;
  475. TokenPtr bell;
  476. TokenPtr foo;
  477. TokenPtr cookie;
  478. // Store constant empty hexstring "" ("") in the TokenHexString object.
  479. ASSERT_NO_THROW(empty.reset(new TokenHexString("")));
  480. // Store bad encoded hexstring "0abc" ("").
  481. ASSERT_NO_THROW(bad.reset(new TokenHexString("0abc")));
  482. // Store hexstring with no digits "0x" ("").
  483. ASSERT_NO_THROW(nodigit.reset(new TokenHexString("0x")));
  484. // Store hexstring with a bad hexdigit "0xxabc" ("").
  485. ASSERT_NO_THROW(baddigit.reset(new TokenHexString("0xxabc")));
  486. // Store hexstring with an odd number of hexdigits "0x7" ("\a").
  487. ASSERT_NO_THROW(bell.reset(new TokenHexString("0x7")));
  488. // Store constant hexstring "0x666f6f" ("foo").
  489. ASSERT_NO_THROW(foo.reset(new TokenHexString("0x666f6f")));
  490. // Store constant hexstring "0x63825363" (DHCP_OPTIONS_COOKIE).
  491. ASSERT_NO_THROW(cookie.reset(new TokenHexString("0x63825363")));
  492. // Make sure that tokens can be evaluated without exceptions,
  493. // and verify the debug output
  494. ASSERT_NO_THROW(empty->evaluate(*pkt4_, values_));
  495. ASSERT_NO_THROW(bad->evaluate(*pkt4_, values_));
  496. ASSERT_NO_THROW(nodigit->evaluate(*pkt4_, values_));
  497. ASSERT_NO_THROW(baddigit->evaluate(*pkt4_, values_));
  498. ASSERT_NO_THROW(bell->evaluate(*pkt4_, values_));
  499. ASSERT_NO_THROW(foo->evaluate(*pkt4_, values_));
  500. ASSERT_NO_THROW(cookie->evaluate(*pkt4_, values_));
  501. // Check that the evaluation put its value on the values stack.
  502. ASSERT_EQ(7, values_.size());
  503. uint32_t expected = htonl(DHCP_OPTIONS_COOKIE);
  504. EXPECT_EQ(4, values_.top().size());
  505. EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4));
  506. values_.pop();
  507. EXPECT_EQ("foo", values_.top());
  508. values_.pop();
  509. EXPECT_EQ("\a", values_.top());
  510. values_.pop();
  511. EXPECT_EQ("", values_.top());
  512. values_.pop();
  513. EXPECT_EQ("", values_.top());
  514. values_.pop();
  515. EXPECT_EQ("", values_.top());
  516. values_.pop();
  517. EXPECT_EQ("", values_.top());
  518. // Check that the debug output was correct. Add the strings
  519. // to the test vector in the class and then call checkFile
  520. // for comparison
  521. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x");
  522. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x");
  523. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x");
  524. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x");
  525. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x07");
  526. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x666F6F");
  527. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x63825363");
  528. EXPECT_TRUE(checkFile());
  529. }
  530. // This simple test checks that a TokenHexString, representing a constant
  531. // string coded in hexadecimal, can be used in Pkt6 evaluation.
  532. // (The actual packet is not used)
  533. TEST_F(TokenTest, hexstring6) {
  534. TokenPtr empty;
  535. TokenPtr bad;
  536. TokenPtr nodigit;
  537. TokenPtr baddigit;
  538. TokenPtr bell;
  539. TokenPtr foo;
  540. TokenPtr cookie;
  541. // Store constant empty hexstring "" ("") in the TokenHexString object.
  542. ASSERT_NO_THROW(empty.reset(new TokenHexString("")));
  543. // Store bad encoded hexstring "0abc" ("").
  544. ASSERT_NO_THROW(bad.reset(new TokenHexString("0abc")));
  545. // Store hexstring with no digits "0x" ("").
  546. ASSERT_NO_THROW(nodigit.reset(new TokenHexString("0x")));
  547. // Store hexstring with a bad hexdigit "0xxabc" ("").
  548. ASSERT_NO_THROW(baddigit.reset(new TokenHexString("0xxabc")));
  549. // Store hexstring with an odd number of hexdigits "0x7" ("\a").
  550. ASSERT_NO_THROW(bell.reset(new TokenHexString("0x7")));
  551. // Store constant hexstring "0x666f6f" ("foo").
  552. ASSERT_NO_THROW(foo.reset(new TokenHexString("0x666f6f")));
  553. // Store constant hexstring "0x63825363" (DHCP_OPTIONS_COOKIE).
  554. ASSERT_NO_THROW(cookie.reset(new TokenHexString("0x63825363")));
  555. // Make sure that tokens can be evaluated without exceptions.
  556. ASSERT_NO_THROW(empty->evaluate(*pkt6_, values_));
  557. ASSERT_NO_THROW(bad->evaluate(*pkt6_, values_));
  558. ASSERT_NO_THROW(nodigit->evaluate(*pkt6_, values_));
  559. ASSERT_NO_THROW(baddigit->evaluate(*pkt6_, values_));
  560. ASSERT_NO_THROW(bell->evaluate(*pkt6_, values_));
  561. ASSERT_NO_THROW(foo->evaluate(*pkt6_, values_));
  562. ASSERT_NO_THROW(cookie->evaluate(*pkt6_, values_));
  563. // Check that the evaluation put its value on the values stack.
  564. ASSERT_EQ(7, values_.size());
  565. uint32_t expected = htonl(DHCP_OPTIONS_COOKIE);
  566. EXPECT_EQ(4, values_.top().size());
  567. EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4));
  568. values_.pop();
  569. EXPECT_EQ("foo", values_.top());
  570. values_.pop();
  571. EXPECT_EQ("\a", values_.top());
  572. values_.pop();
  573. EXPECT_EQ("", values_.top());
  574. values_.pop();
  575. EXPECT_EQ("", values_.top());
  576. values_.pop();
  577. EXPECT_EQ("", values_.top());
  578. values_.pop();
  579. EXPECT_EQ("", values_.top());
  580. // Check that the debug output was correct. Add the strings
  581. // to the test vector in the class and then call checkFile
  582. // for comparison
  583. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x");
  584. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x");
  585. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x");
  586. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x");
  587. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x07");
  588. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x666F6F");
  589. addString("EVAL_DEBUG_HEXSTRING Pushing hex string 0x63825363");
  590. EXPECT_TRUE(checkFile());
  591. }
  592. // This test checks that a TokenIpAddress, representing an IP address as
  593. // a constant string, can be used in Pkt4/Pkt6 evaluation.
  594. // (The actual packet is not used)
  595. TEST_F(TokenTest, ipaddress) {
  596. TokenPtr bad4;
  597. TokenPtr bad6;
  598. TokenPtr ip4;
  599. TokenPtr ip6;
  600. // Bad IP addresses
  601. ASSERT_NO_THROW(bad4.reset(new TokenIpAddress("10.0.0.0.1")));
  602. ASSERT_NO_THROW(bad6.reset(new TokenIpAddress(":::")));
  603. // IP addresses
  604. ASSERT_NO_THROW(ip4.reset(new TokenIpAddress("10.0.0.1")));
  605. ASSERT_NO_THROW(ip6.reset(new TokenIpAddress("2001:db8::1")));
  606. // Make sure that tokens can be evaluated without exceptions.
  607. ASSERT_NO_THROW(ip4->evaluate(*pkt4_, values_));
  608. ASSERT_NO_THROW(ip6->evaluate(*pkt6_, values_));
  609. ASSERT_NO_THROW(bad4->evaluate(*pkt4_, values_));
  610. ASSERT_NO_THROW(bad6->evaluate(*pkt6_, values_));
  611. // Check that the evaluation put its value on the values stack.
  612. ASSERT_EQ(4, values_.size());
  613. // Check bad addresses (they pushed '' on the value stack)
  614. EXPECT_EQ(0, values_.top().size());
  615. values_.pop();
  616. EXPECT_EQ(0, values_.top().size());
  617. values_.pop();
  618. // Check IPv6 address
  619. uint8_t expected6[] = { 0x20, 1, 0xd, 0xb8, 0, 0, 0, 0,
  620. 0, 0, 0, 0, 0, 0, 0, 1 };
  621. EXPECT_EQ(16, values_.top().size());
  622. EXPECT_EQ(0, memcmp(expected6, &values_.top()[0], 16));
  623. values_.pop();
  624. // Check IPv4 address
  625. uint8_t expected4[] = { 10, 0, 0, 1 };
  626. EXPECT_EQ(4, values_.top().size());
  627. EXPECT_EQ(0, memcmp(expected4, &values_.top()[0], 4));
  628. // Check that the debug output was correct. Add the strings
  629. // to the test vector in the class and then call checkFile
  630. // for comparison
  631. addString("EVAL_DEBUG_IPADDRESS Pushing IPAddress 0x0A000001");
  632. addString("EVAL_DEBUG_IPADDRESS Pushing IPAddress "
  633. "0x20010DB8000000000000000000000001");
  634. addString("EVAL_DEBUG_IPADDRESS Pushing IPAddress 0x");
  635. addString("EVAL_DEBUG_IPADDRESS Pushing IPAddress 0x");
  636. EXPECT_TRUE(checkFile());
  637. }
  638. // This test checks if a token representing an option value is able to extract
  639. // the option from an IPv4 packet and properly store the option's value.
  640. TEST_F(TokenTest, optionString4) {
  641. TokenPtr found;
  642. TokenPtr not_found;
  643. // The packets we use have option 100 with a string in them.
  644. ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::TEXTUAL)));
  645. ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::TEXTUAL)));
  646. // This should evaluate to the content of the option 100 (i.e. "hundred4")
  647. ASSERT_NO_THROW(found->evaluate(*pkt4_, values_));
  648. // This should evaluate to "" as there is no option 101.
  649. ASSERT_NO_THROW(not_found->evaluate(*pkt4_, values_));
  650. // There should be 2 values evaluated.
  651. ASSERT_EQ(2, values_.size());
  652. // This is a stack, so the pop order is inversed. We should get the empty
  653. // string first.
  654. EXPECT_EQ("", values_.top());
  655. values_.pop();
  656. // Then the content of the option 100.
  657. EXPECT_EQ("hundred4", values_.top());
  658. // Check that the debug output was correct. Add the strings
  659. // to the test vector in the class and then call checkFile
  660. // for comparison
  661. addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'hundred4'");
  662. addString("EVAL_DEBUG_OPTION Pushing option 101 with value ''");
  663. EXPECT_TRUE(checkFile());
  664. }
  665. // This test checks if a token representing option value is able to extract
  666. // the option from an IPv4 packet and properly store its value in a
  667. // hexadecimal format.
  668. TEST_F(TokenTest, optionHexString4) {
  669. TokenPtr found;
  670. TokenPtr not_found;
  671. // The packets we use have option 100 with a string in them.
  672. ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::HEXADECIMAL)));
  673. ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::HEXADECIMAL)));
  674. // This should evaluate to the content of the option 100 (i.e. "hundred4")
  675. ASSERT_NO_THROW(found->evaluate(*pkt4_, values_));
  676. // This should evaluate to "" as there is no option 101.
  677. ASSERT_NO_THROW(not_found->evaluate(*pkt4_, values_));
  678. // There should be 2 values evaluated.
  679. ASSERT_EQ(2, values_.size());
  680. // This is a stack, so the pop order is inversed. We should get the empty
  681. // string first.
  682. EXPECT_EQ("", values_.top());
  683. values_.pop();
  684. // Then the content of the option 100.
  685. EXPECT_EQ("hundred4", values_.top());
  686. // Check that the debug output was correct. Add the strings
  687. // to the test vector in the class and then call checkFile
  688. // for comparison
  689. addString("EVAL_DEBUG_OPTION Pushing option 100 with value 0x68756E6472656434");
  690. addString("EVAL_DEBUG_OPTION Pushing option 101 with value 0x");
  691. EXPECT_TRUE(checkFile());
  692. }
  693. // This test checks if a token representing an option value is able to check
  694. // the existence of the option from an IPv4 packet.
  695. TEST_F(TokenTest, optionExistsString4) {
  696. TokenPtr found;
  697. TokenPtr not_found;
  698. // The packets we use have option 100 with a string in them.
  699. ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::EXISTS)));
  700. ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::EXISTS)));
  701. ASSERT_NO_THROW(found->evaluate(*pkt4_, values_));
  702. ASSERT_NO_THROW(not_found->evaluate(*pkt4_, values_));
  703. // There should be 2 values evaluated.
  704. ASSERT_EQ(2, values_.size());
  705. // This is a stack, so the pop order is inversed.
  706. EXPECT_EQ("false", values_.top());
  707. values_.pop();
  708. EXPECT_EQ("true", values_.top());
  709. // Check that the debug output was correct. Add the strings
  710. // to the test vector in the class and then call checkFile
  711. // for comparison
  712. addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'true'");
  713. addString("EVAL_DEBUG_OPTION Pushing option 101 with value 'false'");
  714. EXPECT_TRUE(checkFile());
  715. }
  716. // This test checks if a token representing an option value is able to extract
  717. // the option from an IPv6 packet and properly store the option's value.
  718. TEST_F(TokenTest, optionString6) {
  719. TokenPtr found;
  720. TokenPtr not_found;
  721. // The packets we use have option 100 with a string in them.
  722. ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::TEXTUAL)));
  723. ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::TEXTUAL)));
  724. // This should evaluate to the content of the option 100 (i.e. "hundred6")
  725. ASSERT_NO_THROW(found->evaluate(*pkt6_, values_));
  726. // This should evaluate to "" as there is no option 101.
  727. ASSERT_NO_THROW(not_found->evaluate(*pkt6_, values_));
  728. // There should be 2 values evaluated.
  729. ASSERT_EQ(2, values_.size());
  730. // This is a stack, so the pop order is inversed. We should get the empty
  731. // string first.
  732. EXPECT_EQ("", values_.top());
  733. values_.pop();
  734. // Then the content of the option 100.
  735. EXPECT_EQ("hundred6", values_.top());
  736. // Check that the debug output was correct. Add the strings
  737. // to the test vector in the class and then call checkFile
  738. // for comparison
  739. addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'hundred6'");
  740. addString("EVAL_DEBUG_OPTION Pushing option 101 with value ''");
  741. EXPECT_TRUE(checkFile());
  742. }
  743. // This test checks if a token representing an option value is able to extract
  744. // the option from an IPv6 packet and properly store its value in hexadecimal
  745. // format.
  746. TEST_F(TokenTest, optionHexString6) {
  747. TokenPtr found;
  748. TokenPtr not_found;
  749. // The packets we use have option 100 with a string in them.
  750. ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::HEXADECIMAL)));
  751. ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::HEXADECIMAL)));
  752. // This should evaluate to the content of the option 100 (i.e. "hundred6")
  753. ASSERT_NO_THROW(found->evaluate(*pkt6_, values_));
  754. // This should evaluate to "" as there is no option 101.
  755. ASSERT_NO_THROW(not_found->evaluate(*pkt6_, values_));
  756. // There should be 2 values evaluated.
  757. ASSERT_EQ(2, values_.size());
  758. // This is a stack, so the pop order is inversed. We should get the empty
  759. // string first.
  760. EXPECT_EQ("", values_.top());
  761. values_.pop();
  762. // Then the content of the option 100.
  763. EXPECT_EQ("hundred6", values_.top());
  764. // Check that the debug output was correct. Add the strings
  765. // to the test vector in the class and then call checkFile
  766. // for comparison
  767. addString("EVAL_DEBUG_OPTION Pushing option 100 with value 0x68756E6472656436");
  768. addString("EVAL_DEBUG_OPTION Pushing option 101 with value 0x");
  769. EXPECT_TRUE(checkFile());
  770. }
  771. // This test checks if a token representing an option value is able to check
  772. // the existence of the option from an IPv6 packet.
  773. TEST_F(TokenTest, optionExistsString6) {
  774. TokenPtr found;
  775. TokenPtr not_found;
  776. // The packets we use have option 100 with a string in them.
  777. ASSERT_NO_THROW(found.reset(new TokenOption(100, TokenOption::EXISTS)));
  778. ASSERT_NO_THROW(not_found.reset(new TokenOption(101, TokenOption::EXISTS)));
  779. ASSERT_NO_THROW(found->evaluate(*pkt6_, values_));
  780. ASSERT_NO_THROW(not_found->evaluate(*pkt6_, values_));
  781. // There should be 2 values evaluated.
  782. ASSERT_EQ(2, values_.size());
  783. // This is a stack, so the pop order is inversed.
  784. EXPECT_EQ("false", values_.top());
  785. values_.pop();
  786. EXPECT_EQ("true", values_.top());
  787. // Check that the debug output was correct. Add the strings
  788. // to the test vector in the class and then call checkFile
  789. // for comparison
  790. addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'true'");
  791. addString("EVAL_DEBUG_OPTION Pushing option 101 with value 'false'");
  792. EXPECT_TRUE(checkFile());
  793. }
  794. // This test checks that the existing relay4 option can be found.
  795. TEST_F(TokenTest, relay4Option) {
  796. // Insert relay option with sub-options 1 and 13
  797. insertRelay4Option();
  798. // Creating the token should be safe.
  799. ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(13, TokenOption::TEXTUAL)));
  800. // We should be able to evaluate it.
  801. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  802. // we should have one value on the stack
  803. ASSERT_EQ(1, values_.size());
  804. // The option should be found and relay4[13] should evaluate to the
  805. // content of that sub-option, i.e. "thirteen"
  806. EXPECT_EQ("thirteen", values_.top());
  807. // Check that the debug output was correct. Add the strings
  808. // to the test vector in the class and then call checkFile
  809. // for comparison
  810. addString("EVAL_DEBUG_OPTION Pushing option 13 with value 'thirteen'");
  811. EXPECT_TRUE(checkFile());
  812. }
  813. // This test checks that the code properly handles cases when
  814. // there is a RAI option, but there's no requested sub-option.
  815. TEST_F(TokenTest, relay4OptionNoSuboption) {
  816. // Insert relay option with sub-options 1 and 13
  817. insertRelay4Option();
  818. // Creating the token should be safe.
  819. ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(15, TokenOption::TEXTUAL)));
  820. // We should be able to evaluate it.
  821. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  822. // we should have one value on the stack
  823. ASSERT_EQ(1, values_.size());
  824. // The option should NOT be found (there is no sub-option 15),
  825. // so the expression should evaluate to ""
  826. EXPECT_EQ("", values_.top());
  827. // Check that the debug output was correct. Add the strings
  828. // to the test vector in the class and then call checkFile
  829. // for comparison
  830. addString("EVAL_DEBUG_OPTION Pushing option 15 with value ''");
  831. EXPECT_TRUE(checkFile());
  832. }
  833. // This test checks that the code properly handles cases when
  834. // there's no RAI option at all.
  835. TEST_F(TokenTest, relay4OptionNoRai) {
  836. // We didn't call insertRelay4Option(), so there's no RAI option.
  837. // Creating the token should be safe.
  838. ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(13, TokenOption::TEXTUAL)));
  839. // We should be able to evaluate it.
  840. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  841. // we should have one value on the stack
  842. ASSERT_EQ(1, values_.size());
  843. // The option should NOT be found (there is no sub-option 13),
  844. // so the expression should evaluate to ""
  845. EXPECT_EQ("", values_.top());
  846. // Check that the debug output was correct. Add the strings
  847. // to the test vector in the class and then call checkFile
  848. // for comparison
  849. addString("EVAL_DEBUG_OPTION Pushing option 13 with value ''");
  850. EXPECT_TRUE(checkFile());
  851. }
  852. // This test checks that only the RAI is searched for the requested
  853. // sub-option.
  854. TEST_F(TokenTest, relay4RAIOnly) {
  855. // Insert relay option with sub-options 1 and 13
  856. insertRelay4Option();
  857. // Add options 13 and 70 to the packet.
  858. OptionPtr opt13(new OptionString(Option::V4, 13, "THIRTEEN"));
  859. OptionPtr opt70(new OptionString(Option::V4, 70, "SEVENTY"));
  860. pkt4_->addOption(opt13);
  861. pkt4_->addOption(opt70);
  862. // The situation is as follows:
  863. // Packet:
  864. // - option 13 (containing "THIRTEEN")
  865. // - option 82 (rai)
  866. // - option 1 (containing "one")
  867. // - option 13 (containing "thirteen")
  868. // Let's try to get option 13. It should get the one from RAI
  869. ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(13, TokenOption::TEXTUAL)));
  870. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  871. ASSERT_EQ(1, values_.size());
  872. EXPECT_EQ("thirteen", values_.top());
  873. // Try to get option 1. It should get the one from RAI
  874. clearStack();
  875. ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(1, TokenOption::TEXTUAL)));
  876. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  877. ASSERT_EQ(1, values_.size());
  878. EXPECT_EQ("one", values_.top());
  879. // Try to get option 70. It should fail, as there's no such
  880. // sub option in RAI.
  881. clearStack();
  882. ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(70, TokenOption::TEXTUAL)));
  883. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  884. ASSERT_EQ(1, values_.size());
  885. EXPECT_EQ("", values_.top());
  886. // Try to check option 1. It should return "true"
  887. clearStack();
  888. ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(1, TokenOption::EXISTS)));
  889. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  890. ASSERT_EQ(1, values_.size());
  891. EXPECT_EQ("true", values_.top());
  892. // Try to check option 70. It should return "false"
  893. clearStack();
  894. ASSERT_NO_THROW(t_.reset(new TokenRelay4Option(70, TokenOption::EXISTS)));
  895. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  896. ASSERT_EQ(1, values_.size());
  897. EXPECT_EQ("false", values_.top());
  898. // Check that the debug output was correct. Add the strings
  899. // to the test vector in the class and then call checkFile
  900. // for comparison
  901. addString("EVAL_DEBUG_OPTION Pushing option 13 with value 'thirteen'");
  902. addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'one'");
  903. addString("EVAL_DEBUG_OPTION Pushing option 70 with value ''");
  904. addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'true'");
  905. addString("EVAL_DEBUG_OPTION Pushing option 70 with value 'false'");
  906. EXPECT_TRUE(checkFile());
  907. }
  908. // This test checks if we can properly extract an option
  909. // from relay encapsulations. Our packet has two relay
  910. // encapsulations. Both include a common option with the
  911. // original message (option 100) and both include their
  912. // own option (101 and 102). We attempt to extract the
  913. // options and compare them to expected values. We also
  914. // try to extract an option from an encapsulation
  915. // that doesn't exist (level 2), this should result in an empty
  916. // string.
  917. TEST_F(TokenTest, relay6Option) {
  918. // We start by adding a set of relay encapsulations to the
  919. // basic v6 packet.
  920. addRelay6Encapsulations();
  921. // Then we work our way through the set of choices
  922. // Level 0 both options it has and the check that
  923. // the checking for an option it doesn't have results
  924. // in an empty string.
  925. verifyRelay6Option(0, 100, TokenOption::TEXTUAL, "hundred.zero");
  926. verifyRelay6Option(0, 100, TokenOption::EXISTS, "true");
  927. verifyRelay6Option(0, 101, TokenOption::TEXTUAL, "hundredone.zero");
  928. verifyRelay6Option(0, 102, TokenOption::TEXTUAL, "");
  929. verifyRelay6Option(0, 102, TokenOption::EXISTS, "false");
  930. // Level 1, again both options it has and the one for level 0
  931. verifyRelay6Option(1, 100, TokenOption::TEXTUAL, "hundred.one");
  932. verifyRelay6Option(1, 101, TokenOption::TEXTUAL, "");
  933. verifyRelay6Option(1, 102, TokenOption::TEXTUAL, "hundredtwo.one");
  934. // Level 2, no encapsulation so no options
  935. verifyRelay6Option(2, 100, TokenOption::TEXTUAL, "");
  936. // Check that the debug output was correct. Add the strings
  937. // to the test vector in the class and then call checkFile
  938. // for comparison
  939. addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'hundred.zero'");
  940. addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'true'");
  941. addString("EVAL_DEBUG_OPTION Pushing option 101 with value 'hundredone.zero'");
  942. addString("EVAL_DEBUG_OPTION Pushing option 102 with value ''");
  943. addString("EVAL_DEBUG_OPTION Pushing option 102 with value 'false'");
  944. addString("EVAL_DEBUG_OPTION Pushing option 100 with value 'hundred.one'");
  945. addString("EVAL_DEBUG_OPTION Pushing option 101 with value ''");
  946. addString("EVAL_DEBUG_OPTION Pushing option 102 with value 'hundredtwo.one'");
  947. addString("EVAL_DEBUG_OPTION Pushing option 100 with value ''");
  948. EXPECT_TRUE(checkFile());
  949. }
  950. // Verifies that relay6 option requires DHCPv6
  951. TEST_F(TokenTest, relay6OptionError) {
  952. // Create a relay6 option token
  953. ASSERT_NO_THROW(t_.reset(new TokenRelay6Option(0, 13, TokenOption::TEXTUAL)));
  954. // A DHCPv6 packet is required
  955. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  956. }
  957. // Verifies that DHCPv4 packet metadata can be extracted.
  958. TEST_F(TokenTest, pkt4MetaData) {
  959. pkt4_->setIface("eth0");
  960. pkt4_->setLocalAddr(IOAddress("10.0.0.1"));
  961. pkt4_->setRemoteAddr(IOAddress("10.0.0.2"));
  962. // Check interface (expect eth0)
  963. ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::IFACE)));
  964. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  965. ASSERT_EQ(1, values_.size());
  966. ASSERT_EQ("eth0", values_.top());
  967. // Check source (expect 10.0.0.2)
  968. clearStack();
  969. ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::SRC)));
  970. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  971. ASSERT_EQ(1, values_.size());
  972. vector<uint8_t> a2 = IOAddress("10.0.0.2").toBytes();
  973. ASSERT_EQ(a2.size(), values_.top().size());
  974. EXPECT_EQ(0, memcmp(&a2[0], &values_.top()[0], a2.size()));
  975. // Check destination (expect 10.0.0.1)
  976. clearStack();
  977. ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::DST)));
  978. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  979. ASSERT_EQ(1, values_.size());
  980. vector<uint8_t> a1 = IOAddress("10.0.0.1").toBytes();
  981. ASSERT_EQ(a1.size(), values_.top().size());
  982. EXPECT_EQ(0, memcmp(&a1[0], &values_.top()[0], a1.size()));
  983. // Check length (expect 249)
  984. clearStack();
  985. ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::LEN)));
  986. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  987. ASSERT_EQ(1, values_.size());
  988. uint32_t length = htonl(static_cast<uint32_t>(pkt4_->len()));
  989. ASSERT_EQ(4, values_.top().size());
  990. EXPECT_EQ(0, memcmp(&length, &values_.top()[0], 4));
  991. // Unknown metadata type fails
  992. clearStack();
  993. ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::MetadataType(100))));
  994. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  995. // Check that the debug output was correct. Add the strings
  996. // to the test vector in the class and then call checkFile
  997. // for comparison
  998. addString("EVAL_DEBUG_PKT Pushing PKT meta data iface with value eth0");
  999. addString("EVAL_DEBUG_PKT Pushing PKT meta data src with value 0x0A000002");
  1000. addString("EVAL_DEBUG_PKT Pushing PKT meta data dst with value 0x0A000001");
  1001. addString("EVAL_DEBUG_PKT Pushing PKT meta data len with value 0x000000F9");
  1002. EXPECT_TRUE(checkFile());
  1003. }
  1004. // Verifies that DHCPv6 packet metadata can be extracted.
  1005. TEST_F(TokenTest, pkt6MetaData) {
  1006. pkt6_->setIface("eth0");
  1007. pkt6_->setLocalAddr(IOAddress("ff02::1:2"));
  1008. pkt6_->setRemoteAddr(IOAddress("fe80::1234"));
  1009. // Check interface (expect eth0)
  1010. ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::IFACE)));
  1011. EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_));
  1012. ASSERT_EQ(1, values_.size());
  1013. ASSERT_EQ("eth0", values_.top());
  1014. // Check source (expect fe80::1234)
  1015. clearStack();
  1016. ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::SRC)));
  1017. EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_));
  1018. ASSERT_EQ(1, values_.size());
  1019. ASSERT_EQ(16, values_.top().size());
  1020. EXPECT_EQ(0xfe, static_cast<uint8_t>(values_.top()[0]));
  1021. EXPECT_EQ(0x80, static_cast<uint8_t>(values_.top()[1]));
  1022. for (unsigned i = 2; i < 14; ++i) {
  1023. EXPECT_EQ(0, values_.top()[i]);
  1024. }
  1025. EXPECT_EQ(0x12, values_.top()[14]);
  1026. EXPECT_EQ(0x34, values_.top()[15]);
  1027. // Check destination (expect ff02::1:2)
  1028. clearStack();
  1029. ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::DST)));
  1030. EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_));
  1031. ASSERT_EQ(1, values_.size());
  1032. vector<uint8_t> ma = IOAddress("ff02::1:2").toBytes();
  1033. ASSERT_EQ(ma.size(), values_.top().size());
  1034. EXPECT_EQ(0, memcmp(&ma[0], &values_.top()[0], ma.size()));
  1035. // Check length (expect 16)
  1036. clearStack();
  1037. ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::LEN)));
  1038. EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_));
  1039. ASSERT_EQ(1, values_.size());
  1040. uint32_t length = htonl(static_cast<uint32_t>(pkt6_->len()));
  1041. ASSERT_EQ(4, values_.top().size());
  1042. EXPECT_EQ(0, memcmp(&length, &values_.top()[0], 4));
  1043. // Unknown meta data type fails
  1044. clearStack();
  1045. ASSERT_NO_THROW(t_.reset(new TokenPkt(TokenPkt::MetadataType(100))));
  1046. EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError);
  1047. // Check that the debug output was correct. Add the strings
  1048. // to the test vector in the class and then call checkFile
  1049. // for comparison
  1050. addString("EVAL_DEBUG_PKT Pushing PKT meta data iface with value eth0");
  1051. addString("EVAL_DEBUG_PKT Pushing PKT meta data src with value "
  1052. "0xFE800000000000000000000000001234");
  1053. addString("EVAL_DEBUG_PKT Pushing PKT meta data dst with value "
  1054. "0xFF020000000000000000000000010002");
  1055. addString("EVAL_DEBUG_PKT Pushing PKT meta data len with value 0x00000010");
  1056. EXPECT_TRUE(checkFile());
  1057. }
  1058. // Verifies if the DHCPv4 packet fields can be extracted.
  1059. TEST_F(TokenTest, pkt4Fields) {
  1060. pkt4_->setGiaddr(IOAddress("192.0.2.1"));
  1061. pkt4_->setCiaddr(IOAddress("192.0.2.2"));
  1062. pkt4_->setYiaddr(IOAddress("192.0.2.3"));
  1063. pkt4_->setSiaddr(IOAddress("192.0.2.4"));
  1064. // We're setting hardware address to uncommon (7 bytes rather than 6 and
  1065. // hardware type 123) HW address. We'll use it in hlen and htype checks.
  1066. HWAddrPtr hw(new HWAddr(HWAddr::fromText("01:02:03:04:05:06:07", 123)));
  1067. pkt4_->setHWAddr(hw);
  1068. // Check hardware address field.
  1069. ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::CHADDR)));
  1070. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1071. ASSERT_EQ(1, values_.size());
  1072. uint8_t expected_hw[] = { 1, 2, 3, 4, 5, 6, 7 };
  1073. ASSERT_EQ(7, values_.top().size());
  1074. EXPECT_EQ(0, memcmp(expected_hw, &values_.top()[0], 7));
  1075. // Check hlen value field.
  1076. clearStack();
  1077. ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::HLEN)));
  1078. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1079. ASSERT_EQ(1, values_.size());
  1080. ASSERT_EQ(4, values_.top().size());
  1081. uint32_t expected_hlen = htonl(7);
  1082. EXPECT_EQ(0, memcmp(&expected_hlen, &values_.top()[0], 4));
  1083. // Check htype value.
  1084. clearStack();
  1085. ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::HTYPE)));
  1086. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1087. ASSERT_EQ(1, values_.size());
  1088. ASSERT_EQ(4, values_.top().size());
  1089. uint32_t expected_htype = htonl(123);
  1090. EXPECT_EQ(0, memcmp(&expected_htype, &values_.top()[0], 4));
  1091. // Check giaddr value.
  1092. clearStack();
  1093. ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::GIADDR)));
  1094. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1095. ASSERT_EQ(1, values_.size());
  1096. uint8_t expected_addr[] = { 192, 0, 2, 1 };
  1097. ASSERT_EQ(4, values_.top().size());
  1098. EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4));
  1099. // Check ciaddr value.
  1100. clearStack();
  1101. ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::CIADDR)));
  1102. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1103. ASSERT_EQ(1, values_.size());
  1104. expected_addr[3] = 2;
  1105. ASSERT_EQ(4, values_.top().size());
  1106. EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4));
  1107. // Check yiaddr value.
  1108. clearStack();
  1109. ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::YIADDR)));
  1110. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1111. ASSERT_EQ(1, values_.size());
  1112. expected_addr[3] = 3;
  1113. ASSERT_EQ(4, values_.top().size());
  1114. EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4));
  1115. // Check siaddr value.
  1116. clearStack();
  1117. ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::SIADDR)));
  1118. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1119. ASSERT_EQ(1, values_.size());
  1120. expected_addr[3] = 4;
  1121. ASSERT_EQ(4, values_.top().size());
  1122. EXPECT_EQ(0, memcmp(expected_addr, &values_.top()[0], 4));
  1123. // Check a DHCPv6 packet throws.
  1124. clearStack();
  1125. ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::HLEN)));
  1126. EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError);
  1127. // Unknown field fails
  1128. clearStack();
  1129. ASSERT_NO_THROW(t_.reset(new TokenPkt4(TokenPkt4::FieldType(100))));
  1130. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  1131. // Check that the debug output was correct. Add the strings
  1132. // to the test vector in the class and then call checkFile
  1133. // for comparison
  1134. addString("EVAL_DEBUG_PKT4 Pushing PKT4 field mac with value 0x01020304050607");
  1135. addString("EVAL_DEBUG_PKT4 Pushing PKT4 field hlen with value 0x00000007");
  1136. addString("EVAL_DEBUG_PKT4 Pushing PKT4 field htype with value 0x0000007B");
  1137. addString("EVAL_DEBUG_PKT4 Pushing PKT4 field giaddr with value 0xC0000201");
  1138. addString("EVAL_DEBUG_PKT4 Pushing PKT4 field ciaddr with value 0xC0000202");
  1139. addString("EVAL_DEBUG_PKT4 Pushing PKT4 field yiaddr with value 0xC0000203");
  1140. addString("EVAL_DEBUG_PKT4 Pushing PKT4 field siaddr with value 0xC0000204");
  1141. EXPECT_TRUE(checkFile());
  1142. }
  1143. // Verifies if the DHCPv6 packet fields can be extracted.
  1144. TEST_F(TokenTest, pkt6Fields) {
  1145. // The default test creates a v6 DHCPV6_SOLICIT packet with a
  1146. // transaction id of 12345.
  1147. // Check the message type
  1148. ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::MSGTYPE)));
  1149. EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_));
  1150. ASSERT_EQ(1, values_.size());
  1151. uint32_t expected = htonl(1);
  1152. EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4));
  1153. // Check the transaction id field
  1154. clearStack();
  1155. ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::TRANSID)));
  1156. EXPECT_NO_THROW(t_->evaluate(*pkt6_, values_));
  1157. ASSERT_EQ(1, values_.size());
  1158. expected = htonl(12345);
  1159. EXPECT_EQ(0, memcmp(&expected, &values_.top()[0], 4));
  1160. // Check that working with a v4 packet generates an error
  1161. clearStack();
  1162. ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::TRANSID)));
  1163. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  1164. // Unknown field fails
  1165. clearStack();
  1166. ASSERT_NO_THROW(t_.reset(new TokenPkt6(TokenPkt6::FieldType(100))));
  1167. EXPECT_THROW(t_->evaluate(*pkt6_, values_), EvalTypeError);
  1168. // Check that the debug output was correct. Add the strings
  1169. // to the test vector in the class and then call checkFile
  1170. // for comparison
  1171. addString("EVAL_DEBUG_PKT6 Pushing PKT6 field msgtype with value 0x00000001");
  1172. addString("EVAL_DEBUG_PKT6 Pushing PKT6 field transid with value 0x00003039");
  1173. EXPECT_TRUE(checkFile());
  1174. }
  1175. // This test checks if we can properly extract the link and peer
  1176. // address fields from relay encapsulations. Our packet has
  1177. // two relay encapsulations. We attempt to extract the two
  1178. // fields from both of the encapsulations and compare them.
  1179. // We also try to extract one of the fields from an encapsulation
  1180. // that doesn't exist (level 2), this should result in an empty
  1181. // string.
  1182. TEST_F(TokenTest, relay6Field) {
  1183. // Values for the address results
  1184. uint8_t zeroaddr[] = { 0, 0, 0, 0, 0, 0, 0, 0,
  1185. 0, 0, 0, 0, 0, 0, 0, 0 };
  1186. uint8_t linkaddr[] = { 0, 1, 0, 0, 0, 0, 0, 0,
  1187. 0, 0, 0, 0, 0, 0, 0, 1 };
  1188. uint8_t peeraddr[] = { 0, 1, 0, 0, 0, 0, 0, 0,
  1189. 0, 0, 0, 0, 0, 0, 0, 2 };
  1190. // We start by adding a set of relay encapsulations to the
  1191. // basic v6 packet.
  1192. addRelay6Encapsulations();
  1193. // Then we work our way through the set of choices
  1194. // Level 0 both link and peer address should be 0::0
  1195. verifyRelay6Eval(0, TokenRelay6Field::LINKADDR, 16, zeroaddr);
  1196. verifyRelay6Eval(0, TokenRelay6Field::PEERADDR, 16, zeroaddr);
  1197. // Level 1 link and peer should have different non-zero addresses
  1198. verifyRelay6Eval(1, TokenRelay6Field::LINKADDR, 16, linkaddr);
  1199. verifyRelay6Eval(1, TokenRelay6Field::PEERADDR, 16, peeraddr);
  1200. // Level 2 has no encapsulation so the address should be zero length
  1201. verifyRelay6Eval(2, TokenRelay6Field::LINKADDR, 0, zeroaddr);
  1202. // Lets check that the layout of the address returned by the
  1203. // token matches that of the TokenIpAddress
  1204. TokenPtr trelay;
  1205. TokenPtr taddr;
  1206. TokenPtr tequal;
  1207. ASSERT_NO_THROW(trelay.reset(new TokenRelay6Field(1, TokenRelay6Field::LINKADDR)));
  1208. ASSERT_NO_THROW(taddr.reset(new TokenIpAddress("1::1")));
  1209. ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
  1210. EXPECT_NO_THROW(trelay->evaluate(*pkt6_, values_));
  1211. EXPECT_NO_THROW(taddr->evaluate(*pkt6_, values_));
  1212. EXPECT_NO_THROW(tequal->evaluate(*pkt6_, values_));
  1213. // We should have a single value on the stack and it should be "true"
  1214. ASSERT_EQ(1, values_.size());
  1215. EXPECT_EQ("true", values_.top());
  1216. // be tidy
  1217. clearStack();
  1218. // Check that the debug output was correct. Add the strings
  1219. // to the test vector in the class and then call checkFile
  1220. // for comparison
  1221. addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field linkaddr nest 0 "
  1222. "with value 0x00000000000000000000000000000000");
  1223. addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field peeraddr nest 0 "
  1224. "with value 0x00000000000000000000000000000000");
  1225. addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field linkaddr nest 1 "
  1226. "with value 0x00010000000000000000000000000001");
  1227. addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field peeraddr nest 1 "
  1228. "with value 0x00010000000000000000000000000002");
  1229. addString("EVAL_DEBUG_RELAY6_RANGE Pushing PKT6 relay field linkaddr nest 2 "
  1230. "with value 0x");
  1231. addString("EVAL_DEBUG_RELAY6 Pushing PKT6 relay field linkaddr nest 1 "
  1232. "with value 0x00010000000000000000000000000001");
  1233. addString("EVAL_DEBUG_IPADDRESS Pushing IPAddress "
  1234. "0x00010000000000000000000000000001");
  1235. addString("EVAL_DEBUG_EQUAL Popping 0x00010000000000000000000000000001 "
  1236. "and 0x00010000000000000000000000000001 pushing result 'true'");
  1237. EXPECT_TRUE(checkFile());
  1238. }
  1239. // This test checks some error cases for a relay6 field token
  1240. TEST_F(TokenTest, relay6FieldError) {
  1241. // Create a valid relay6 field token
  1242. ASSERT_NO_THROW(t_.reset(new TokenRelay6Field(0, TokenRelay6Field::LINKADDR)));
  1243. // a DHCPv6 packet is required
  1244. ASSERT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  1245. // No test for unknown field as it is not (yet) checked?!
  1246. }
  1247. // This test checks if a token representing an == operator is able to
  1248. // compare two values (with incorrectly built stack).
  1249. TEST_F(TokenTest, optionEqualInvalid) {
  1250. ASSERT_NO_THROW(t_.reset(new TokenEqual()));
  1251. // CASE 1: There's not enough values on the stack. == is an operator that
  1252. // takes two parameters. There are 0 on the stack.
  1253. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1254. // CASE 2: One value is still not enough.
  1255. values_.push("foo");
  1256. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1257. }
  1258. // This test checks if a token representing an == operator is able to
  1259. // compare two different values.
  1260. TEST_F(TokenTest, optionEqualFalse) {
  1261. ASSERT_NO_THROW(t_.reset(new TokenEqual()));
  1262. values_.push("foo");
  1263. values_.push("bar");
  1264. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1265. // After evaluation there should be a single value that represents
  1266. // result of "foo" == "bar" comparision.
  1267. ASSERT_EQ(1, values_.size());
  1268. EXPECT_EQ("false", values_.top());
  1269. // Check that the debug output was correct. Add the strings
  1270. // to the test vector in the class and then call checkFile
  1271. // for comparison
  1272. addString("EVAL_DEBUG_EQUAL Popping 0x626172 and 0x666F6F "
  1273. "pushing result 'false'");
  1274. EXPECT_TRUE(checkFile());
  1275. }
  1276. // This test checks if a token representing an == operator is able to
  1277. // compare two identical values.
  1278. TEST_F(TokenTest, optionEqualTrue) {
  1279. ASSERT_NO_THROW(t_.reset(new TokenEqual()));
  1280. values_.push("foo");
  1281. values_.push("foo");
  1282. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1283. // After evaluation there should be a single value that represents
  1284. // result of "foo" == "foo" comparision.
  1285. ASSERT_EQ(1, values_.size());
  1286. EXPECT_EQ("true", values_.top());
  1287. // Check that the debug output was correct. Add the strings
  1288. // to the test vector in the class and then call checkFile
  1289. // for comparison
  1290. addString("EVAL_DEBUG_EQUAL Popping 0x666F6F and 0x666F6F "
  1291. "pushing result 'true'");
  1292. EXPECT_TRUE(checkFile());
  1293. }
  1294. // This test checks if a token representing a substring request
  1295. // throws an exception if there aren't enough values on the stack.
  1296. // The stack from the top is: length, start, string.
  1297. // The actual packet is not used.
  1298. TEST_F(TokenTest, substringNotEnoughValues) {
  1299. ASSERT_NO_THROW(t_.reset(new TokenSubstring()));
  1300. // Subsring requires three values on the stack, try
  1301. // with 0, 1 and 2 all should throw an exception
  1302. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1303. values_.push("");
  1304. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1305. values_.push("0");
  1306. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1307. // Three should work
  1308. values_.push("0");
  1309. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1310. // As we had an empty string to start with we should have an empty
  1311. // one after the evaluate
  1312. ASSERT_EQ(1, values_.size());
  1313. EXPECT_EQ("", values_.top());
  1314. // Check that the debug output was correct. Add the strings
  1315. // to the test vector in the class and then call checkFile
  1316. // for comparison
  1317. addString("EVAL_DEBUG_SUBSTRING_EMPTY Popping length 0, start 0, "
  1318. "string 0x pushing result 0x");
  1319. EXPECT_TRUE(checkFile());
  1320. }
  1321. // Test getting the whole string in different ways
  1322. TEST_F(TokenTest, substringWholeString) {
  1323. // Get the whole string
  1324. verifySubstringEval("foobar", "0", "6", "foobar");
  1325. // Get the whole string with "all"
  1326. verifySubstringEval("foobar", "0", "all", "foobar");
  1327. // Get the whole string with an extra long number
  1328. verifySubstringEval("foobar", "0", "123456", "foobar");
  1329. // Get the whole string counting from the back
  1330. verifySubstringEval("foobar", "-6", "all", "foobar");
  1331. // Check that the debug output was correct. Add the strings
  1332. // to the test vector in the class and then call checkFile
  1333. // for comparison
  1334. addString("EVAL_DEBUG_SUBSTRING Popping length 6, start 0, "
  1335. "string 0x666F6F626172 pushing result 0x666F6F626172");
  1336. addString("EVAL_DEBUG_SUBSTRING Popping length all, start 0, "
  1337. "string 0x666F6F626172 pushing result 0x666F6F626172");
  1338. addString("EVAL_DEBUG_SUBSTRING Popping length 123456, start 0, "
  1339. "string 0x666F6F626172 pushing result 0x666F6F626172");
  1340. addString("EVAL_DEBUG_SUBSTRING Popping length all, start -6, "
  1341. "string 0x666F6F626172 pushing result 0x666F6F626172");
  1342. EXPECT_TRUE(checkFile());
  1343. }
  1344. // Test getting a suffix, in this case the last 3 characters
  1345. TEST_F(TokenTest, substringTrailer) {
  1346. verifySubstringEval("foobar", "3", "3", "bar");
  1347. verifySubstringEval("foobar", "3", "all", "bar");
  1348. verifySubstringEval("foobar", "-3", "all", "bar");
  1349. verifySubstringEval("foobar", "-3", "123", "bar");
  1350. // Check that the debug output was correct. Add the strings
  1351. // to the test vector in the class and then call checkFile
  1352. // for comparison
  1353. addString("EVAL_DEBUG_SUBSTRING Popping length 3, start 3, "
  1354. "string 0x666F6F626172 pushing result 0x626172");
  1355. addString("EVAL_DEBUG_SUBSTRING Popping length all, start 3, "
  1356. "string 0x666F6F626172 pushing result 0x626172");
  1357. addString("EVAL_DEBUG_SUBSTRING Popping length all, start -3, "
  1358. "string 0x666F6F626172 pushing result 0x626172");
  1359. addString("EVAL_DEBUG_SUBSTRING Popping length 123, start -3, "
  1360. "string 0x666F6F626172 pushing result 0x626172");
  1361. EXPECT_TRUE(checkFile());
  1362. }
  1363. // Test getting the middle of the string in different ways
  1364. TEST_F(TokenTest, substringMiddle) {
  1365. verifySubstringEval("foobar", "1", "4", "ooba");
  1366. verifySubstringEval("foobar", "-5", "4", "ooba");
  1367. verifySubstringEval("foobar", "-1", "-4", "ooba");
  1368. verifySubstringEval("foobar", "5", "-4", "ooba");
  1369. // Check that the debug output was correct. Add the strings
  1370. // to the test vector in the class and then call checkFile
  1371. // for comparison
  1372. addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 1, "
  1373. "string 0x666F6F626172 pushing result 0x6F6F6261");
  1374. addString("EVAL_DEBUG_SUBSTRING Popping length 4, start -5, "
  1375. "string 0x666F6F626172 pushing result 0x6F6F6261");
  1376. addString("EVAL_DEBUG_SUBSTRING Popping length -4, start -1, "
  1377. "string 0x666F6F626172 pushing result 0x6F6F6261");
  1378. addString("EVAL_DEBUG_SUBSTRING Popping length -4, start 5, "
  1379. "string 0x666F6F626172 pushing result 0x6F6F6261");
  1380. EXPECT_TRUE(checkFile());
  1381. }
  1382. // Test getting the last letter in different ways
  1383. TEST_F(TokenTest, substringLastLetter) {
  1384. verifySubstringEval("foobar", "5", "all", "r");
  1385. verifySubstringEval("foobar", "5", "1", "r");
  1386. verifySubstringEval("foobar", "5", "5", "r");
  1387. verifySubstringEval("foobar", "-1", "all", "r");
  1388. verifySubstringEval("foobar", "-1", "1", "r");
  1389. verifySubstringEval("foobar", "-1", "5", "r");
  1390. // Check that the debug output was correct. Add the strings
  1391. // to the test vector in the class and then call checkFile
  1392. // for comparison
  1393. addString("EVAL_DEBUG_SUBSTRING Popping length all, start 5, "
  1394. "string 0x666F6F626172 pushing result 0x72");
  1395. addString("EVAL_DEBUG_SUBSTRING Popping length 1, start 5, "
  1396. "string 0x666F6F626172 pushing result 0x72");
  1397. addString("EVAL_DEBUG_SUBSTRING Popping length 5, start 5, "
  1398. "string 0x666F6F626172 pushing result 0x72");
  1399. addString("EVAL_DEBUG_SUBSTRING Popping length all, start -1, "
  1400. "string 0x666F6F626172 pushing result 0x72");
  1401. addString("EVAL_DEBUG_SUBSTRING Popping length 1, start -1, "
  1402. "string 0x666F6F626172 pushing result 0x72");
  1403. addString("EVAL_DEBUG_SUBSTRING Popping length 5, start -1, "
  1404. "string 0x666F6F626172 pushing result 0x72");
  1405. EXPECT_TRUE(checkFile());
  1406. }
  1407. // Test we get only what is available if we ask for a longer string
  1408. TEST_F(TokenTest, substringLength) {
  1409. // Test off the front
  1410. verifySubstringEval("foobar", "0", "-4", "");
  1411. verifySubstringEval("foobar", "1", "-4", "f");
  1412. verifySubstringEval("foobar", "2", "-4", "fo");
  1413. verifySubstringEval("foobar", "3", "-4", "foo");
  1414. // and the back
  1415. verifySubstringEval("foobar", "3", "4", "bar");
  1416. verifySubstringEval("foobar", "4", "4", "ar");
  1417. verifySubstringEval("foobar", "5", "4", "r");
  1418. verifySubstringEval("foobar", "6", "4", "");
  1419. // Check that the debug output was correct. Add the strings
  1420. // to the test vector in the class and then call checkFile
  1421. // for comparison
  1422. addString("EVAL_DEBUG_SUBSTRING Popping length -4, start 0, "
  1423. "string 0x666F6F626172 pushing result 0x");
  1424. addString("EVAL_DEBUG_SUBSTRING Popping length -4, start 1, "
  1425. "string 0x666F6F626172 pushing result 0x66");
  1426. addString("EVAL_DEBUG_SUBSTRING Popping length -4, start 2, "
  1427. "string 0x666F6F626172 pushing result 0x666F");
  1428. addString("EVAL_DEBUG_SUBSTRING Popping length -4, start 3, "
  1429. "string 0x666F6F626172 pushing result 0x666F6F");
  1430. addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 3, "
  1431. "string 0x666F6F626172 pushing result 0x626172");
  1432. addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 4, "
  1433. "string 0x666F6F626172 pushing result 0x6172");
  1434. addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 5, "
  1435. "string 0x666F6F626172 pushing result 0x72");
  1436. addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length 4, start 6, "
  1437. "string 0x666F6F626172 pushing result 0x");
  1438. EXPECT_TRUE(checkFile());
  1439. }
  1440. // Test that we get nothing if the starting postion is out of the string
  1441. TEST_F(TokenTest, substringStartingPosition) {
  1442. // Off the front
  1443. verifySubstringEval("foobar", "-7", "1", "");
  1444. verifySubstringEval("foobar", "-7", "-11", "");
  1445. verifySubstringEval("foobar", "-7", "all", "");
  1446. // and the back
  1447. verifySubstringEval("foobar", "6", "1", "");
  1448. verifySubstringEval("foobar", "6", "-11", "");
  1449. verifySubstringEval("foobar", "6", "all", "");
  1450. // Check that the debug output was correct. Add the strings
  1451. // to the test vector in the class and then call checkFile
  1452. // for comparison
  1453. addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length 1, start -7, "
  1454. "string 0x666F6F626172 pushing result 0x");
  1455. addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length -11, start -7, "
  1456. "string 0x666F6F626172 pushing result 0x");
  1457. addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length all, start -7, "
  1458. "string 0x666F6F626172 pushing result 0x");
  1459. addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length 1, start 6, "
  1460. "string 0x666F6F626172 pushing result 0x");
  1461. addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length -11, start 6, "
  1462. "string 0x666F6F626172 pushing result 0x");
  1463. addString("EVAL_DEBUG_SUBSTRING_RANGE Popping length all, start 6, "
  1464. "string 0x666F6F626172 pushing result 0x");
  1465. EXPECT_TRUE(checkFile());
  1466. }
  1467. // Check what happens if we use strings that aren't numbers for start or length
  1468. // We should return the empty string
  1469. TEST_F(TokenTest, substringBadParams) {
  1470. verifySubstringEval("foobar", "0ick", "all", "", true);
  1471. verifySubstringEval("foobar", "ick0", "all", "", true);
  1472. verifySubstringEval("foobar", "ick", "all", "", true);
  1473. verifySubstringEval("foobar", "0", "ick", "", true);
  1474. verifySubstringEval("foobar", "0", "0ick", "", true);
  1475. verifySubstringEval("foobar", "0", "ick0", "", true);
  1476. verifySubstringEval("foobar", "0", "allaboard", "", true);
  1477. // These should result in a throw which should generate it's own
  1478. // log entry
  1479. }
  1480. // lastly check that we don't get anything if the string is empty or
  1481. // we don't ask for any characters from it.
  1482. TEST_F(TokenTest, substringReturnEmpty) {
  1483. verifySubstringEval("", "0", "all", "");
  1484. verifySubstringEval("foobar", "0", "0", "");
  1485. // Check that the debug output was correct. Add the strings
  1486. // to the test vector in the class and then call checkFile
  1487. // for comparison
  1488. addString("EVAL_DEBUG_SUBSTRING_EMPTY Popping length all, start 0, "
  1489. "string 0x pushing result 0x");
  1490. addString("EVAL_DEBUG_SUBSTRING Popping length 0, start 0, "
  1491. "string 0x666F6F626172 pushing result 0x");
  1492. EXPECT_TRUE(checkFile());
  1493. }
  1494. // Check if we can use the substring and equal tokens together
  1495. // We put the result on the stack first then the substring values
  1496. // then evaluate the substring which should leave the original
  1497. // result on the bottom with the substring result on next.
  1498. // Evaulating the equals should produce true for the first
  1499. // and false for the second.
  1500. // throws an exception if there aren't enough values on the stack.
  1501. // The stack from the top is: length, start, string.
  1502. // The actual packet is not used.
  1503. TEST_F(TokenTest, substringEquals) {
  1504. TokenPtr tequal;
  1505. ASSERT_NO_THROW(t_.reset(new TokenSubstring()));
  1506. ASSERT_NO_THROW(tequal.reset(new TokenEqual()));
  1507. // The final expected value
  1508. values_.push("ooba");
  1509. // The substring values
  1510. values_.push("foobar");
  1511. values_.push("1");
  1512. values_.push("4");
  1513. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1514. // we should have two values on the stack
  1515. ASSERT_EQ(2, values_.size());
  1516. // next the equals eval
  1517. EXPECT_NO_THROW(tequal->evaluate(*pkt4_, values_));
  1518. ASSERT_EQ(1, values_.size());
  1519. EXPECT_EQ("true", values_.top());
  1520. // get rid of the result
  1521. values_.pop();
  1522. // and try it again but with a bad final value
  1523. // The final expected value
  1524. values_.push("foob");
  1525. // The substring values
  1526. values_.push("foobar");
  1527. values_.push("1");
  1528. values_.push("4");
  1529. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1530. // we should have two values on the stack
  1531. ASSERT_EQ(2, values_.size());
  1532. // next the equals eval
  1533. EXPECT_NO_THROW(tequal->evaluate(*pkt4_, values_));
  1534. ASSERT_EQ(1, values_.size());
  1535. EXPECT_EQ("false", values_.top());
  1536. // Check that the debug output was correct. Add the strings
  1537. // to the test vector in the class and then call checkFile
  1538. // for comparison
  1539. addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 1, "
  1540. "string 0x666F6F626172 pushing result 0x6F6F6261");
  1541. addString("EVAL_DEBUG_EQUAL Popping 0x6F6F6261 and 0x6F6F6261 "
  1542. "pushing result 'true'");
  1543. addString("EVAL_DEBUG_SUBSTRING Popping length 4, start 1, "
  1544. "string 0x666F6F626172 pushing result 0x6F6F6261");
  1545. addString("EVAL_DEBUG_EQUAL Popping 0x6F6F6261 and 0x666F6F62 "
  1546. "pushing result 'false'");
  1547. EXPECT_TRUE(checkFile());
  1548. }
  1549. // This test checks if a token representing a concat request
  1550. // throws an exception if there aren't enough values on the stack.
  1551. // The actual packet is not used.
  1552. TEST_F(TokenTest, concat) {
  1553. ASSERT_NO_THROW(t_.reset(new TokenConcat()));
  1554. // Concat requires two values on the stack, try
  1555. // with 0 and 1 both should throw an exception
  1556. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1557. values_.push("foo");
  1558. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1559. // Two should work
  1560. values_.push("bar");
  1561. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1562. // Check the result
  1563. ASSERT_EQ(1, values_.size());
  1564. EXPECT_EQ("foobar", values_.top());
  1565. // Check that the debug output was correct. Add the strings
  1566. // to the test vector in the class and then call checkFile
  1567. // for comparison
  1568. addString("EVAL_DEBUG_CONCAT Popping 0x626172 and 0x666F6F "
  1569. "pushing 0x666F6F626172");
  1570. EXPECT_TRUE(checkFile());
  1571. }
  1572. // This test checks if a token representing a not is able to
  1573. // negate a boolean value (with incorrectly built stack).
  1574. TEST_F(TokenTest, operatorNotInvalid) {
  1575. ASSERT_NO_THROW(t_.reset(new TokenNot()));
  1576. // CASE 1: The stack is empty.
  1577. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1578. // CASE 2: The top value is not a boolean
  1579. values_.push("foo");
  1580. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  1581. }
  1582. // This test checks if a token representing a not operator is able to
  1583. // negate a boolean value.
  1584. TEST_F(TokenTest, operatorNot) {
  1585. ASSERT_NO_THROW(t_.reset(new TokenNot()));
  1586. values_.push("true");
  1587. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1588. // After evaluation there should be the negation of the value.
  1589. ASSERT_EQ(1, values_.size());
  1590. EXPECT_EQ("false", values_.top());
  1591. // Double negation is identity.
  1592. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1593. ASSERT_EQ(1, values_.size());
  1594. EXPECT_EQ("true", values_.top());
  1595. // Check that the debug output was correct. Add the strings
  1596. // to the test vector in the class and then call checkFile
  1597. // for comparison
  1598. addString("EVAL_DEBUG_NOT Popping 'true' pushing 'false'");
  1599. addString("EVAL_DEBUG_NOT Popping 'false' pushing 'true'");
  1600. EXPECT_TRUE(checkFile());
  1601. }
  1602. // This test checks if a token representing an and is able to
  1603. // conjugate two values (with incorrectly built stack).
  1604. TEST_F(TokenTest, operatorAndInvalid) {
  1605. ASSERT_NO_THROW(t_.reset(new TokenAnd()));
  1606. // CASE 1: There's not enough values on the stack. and is an operator that
  1607. // takes two parameters. There are 0 on the stack.
  1608. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1609. // CASE 2: One value is still not enough.
  1610. values_.push("foo");
  1611. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1612. // CASE 3: The two values must be logical
  1613. values_.push("true");
  1614. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  1615. // Swap the 2 values
  1616. values_.push("true");
  1617. values_.push("foo");
  1618. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  1619. }
  1620. // This test checks if a token representing an and operator is able to
  1621. // conjugate false with another logical
  1622. TEST_F(TokenTest, operatorAndFalse) {
  1623. ASSERT_NO_THROW(t_.reset(new TokenAnd()));
  1624. values_.push("true");
  1625. values_.push("false");
  1626. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1627. // After evaluation there should be a single "false" value
  1628. ASSERT_EQ(1, values_.size());
  1629. EXPECT_EQ("false", values_.top());
  1630. // After true and false, check false and true
  1631. values_.push("true");
  1632. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1633. ASSERT_EQ(1, values_.size());
  1634. EXPECT_EQ("false", values_.top());
  1635. // And false and false
  1636. values_.push("false");
  1637. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1638. ASSERT_EQ(1, values_.size());
  1639. EXPECT_EQ("false", values_.top());
  1640. // Check that the debug output was correct. Add the strings
  1641. // to the test vector in the class and then call checkFile
  1642. // for comparison
  1643. addString("EVAL_DEBUG_AND Popping 'false' and 'true' pushing 'false'");
  1644. addString("EVAL_DEBUG_AND Popping 'true' and 'false' pushing 'false'");
  1645. addString("EVAL_DEBUG_AND Popping 'false' and 'false' pushing 'false'");
  1646. EXPECT_TRUE(checkFile());
  1647. }
  1648. // This test checks if a token representing an and is able to
  1649. // conjugate two true values.
  1650. TEST_F(TokenTest, operatorAndTrue) {
  1651. ASSERT_NO_THROW(t_.reset(new TokenAnd()));
  1652. values_.push("true");
  1653. values_.push("true");
  1654. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1655. // After evaluation there should be a single "true" value
  1656. ASSERT_EQ(1, values_.size());
  1657. EXPECT_EQ("true", values_.top());
  1658. // Check that the debug output was correct. Add the strings
  1659. // to the test vector in the class and then call checkFile
  1660. // for comparison
  1661. addString("EVAL_DEBUG_AND Popping 'true' and 'true' pushing 'true'");
  1662. EXPECT_TRUE(checkFile());
  1663. }
  1664. // This test checks if a token representing an or is able to
  1665. // combinate two values (with incorrectly built stack).
  1666. TEST_F(TokenTest, operatorOrInvalid) {
  1667. ASSERT_NO_THROW(t_.reset(new TokenOr()));
  1668. // CASE 1: There's not enough values on the stack. or is an operator that
  1669. // takes two parameters. There are 0 on the stack.
  1670. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1671. // CASE 2: One value is still not enough.
  1672. values_.push("foo");
  1673. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalBadStack);
  1674. // CASE 3: The two values must be logical
  1675. values_.push("true");
  1676. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  1677. // Swap the 2 values
  1678. values_.push("true");
  1679. values_.push("foo");
  1680. EXPECT_THROW(t_->evaluate(*pkt4_, values_), EvalTypeError);
  1681. }
  1682. // This test checks if a token representing an or is able to
  1683. // conjugate two false values.
  1684. TEST_F(TokenTest, operatorOrFalse) {
  1685. ASSERT_NO_THROW(t_.reset(new TokenOr()));
  1686. values_.push("false");
  1687. values_.push("false");
  1688. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1689. // After evaluation there should be a single "false" value
  1690. ASSERT_EQ(1, values_.size());
  1691. EXPECT_EQ("false", values_.top());
  1692. // Check that the debug output was correct. Add the strings
  1693. // to the test vector in the class and then call checkFile
  1694. // for comparison
  1695. addString("EVAL_DEBUG_OR Popping 'false' and 'false' pushing 'false'");
  1696. EXPECT_TRUE(checkFile());
  1697. }
  1698. // This test checks if a token representing an == operator is able to
  1699. // conjugate true with another logical
  1700. TEST_F(TokenTest, operatorOrTrue) {
  1701. ASSERT_NO_THROW(t_.reset(new TokenOr()));
  1702. values_.push("false");
  1703. values_.push("true");
  1704. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1705. // After evaluation there should be a single "true" value
  1706. ASSERT_EQ(1, values_.size());
  1707. EXPECT_EQ("true", values_.top());
  1708. // After false or true, checks true or false
  1709. values_.push("false");
  1710. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1711. ASSERT_EQ(1, values_.size());
  1712. EXPECT_EQ("true", values_.top());
  1713. // And true or true
  1714. values_.push("true");
  1715. EXPECT_NO_THROW(t_->evaluate(*pkt4_, values_));
  1716. ASSERT_EQ(1, values_.size());
  1717. EXPECT_EQ("true", values_.top());
  1718. // Check that the debug output was correct. Add the strings
  1719. // to the test vector in the class and then call checkFile
  1720. // for comparison
  1721. addString("EVAL_DEBUG_OR Popping 'true' and 'false' pushing 'true'");
  1722. addString("EVAL_DEBUG_OR Popping 'false' and 'true' pushing 'true'");
  1723. addString("EVAL_DEBUG_OR Popping 'true' and 'true' pushing 'true'");
  1724. EXPECT_TRUE(checkFile());
  1725. }
  1726. // This test verifies if expression vendor[4491].exists works properly in DHCPv4.
  1727. TEST_F(TokenTest, vendor4SpecificVendorExists) {
  1728. // Case 1: no option, should evaluate to false
  1729. testVendorExists(Option::V4, 4491, 0, "false");
  1730. // Case 2: option present, but uses different enterprise-id, should fail
  1731. testVendorExists(Option::V4, 4491, 1234, "false");
  1732. // Case 3: option present and has matchin enterprise-id, should succeed
  1733. testVendorExists(Option::V4, 4491, 4491, "true");
  1734. // Check if the logged messages are correct.
  1735. addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 125 missing, "
  1736. "pushing result 'false'");
  1737. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, "
  1738. "option had 1234, pushing result 'false'");
  1739. addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 4491 "
  1740. "found, pushing result 'true'");
  1741. EXPECT_TRUE(checkFile());
  1742. }
  1743. // This test verifies if expression vendor[4491].exists works properly in DHCPv6.
  1744. TEST_F(TokenTest, vendor6SpecificVendorExists) {
  1745. // Case 1: no option, should evaluate to false
  1746. testVendorExists(Option::V6, 4491, 0, "false");
  1747. // Case 2: option present, but uses different enterprise-id, should fail
  1748. testVendorExists(Option::V6, 4491, 1234, "false");
  1749. // Case 3: option present and has matchin enterprise-id, should suceed
  1750. testVendorExists(Option::V6, 4491, 4491, "true");
  1751. // Check if the logged messages are correct.
  1752. addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 17 missing, "
  1753. "pushing result 'false'");
  1754. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, "
  1755. "option had 1234, pushing result 'false'");
  1756. addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 4491 "
  1757. "found, pushing result 'true'");
  1758. EXPECT_TRUE(checkFile());
  1759. }
  1760. /// Test if expression vendor[*].exists works properly for DHCPv4.
  1761. TEST_F(TokenTest, vendor4AnyVendorExists) {
  1762. // Case 1: no option, should evaluate to false
  1763. testVendorExists(Option::V4, 0, 0, "false");
  1764. // Case 2: option present with vendor-id 1234, should succeed
  1765. testVendorExists(Option::V4, 0, 1234, "true");
  1766. // Case 3: option present with vendor-id 4491, should succeed
  1767. testVendorExists(Option::V4, 0, 4491, "true");
  1768. // Check if the logged messages are correct.
  1769. addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 125 missing, "
  1770. "pushing result 'false'");
  1771. addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 1234 "
  1772. "found, pushing result 'true'");
  1773. addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 4491 "
  1774. "found, pushing result 'true'");
  1775. EXPECT_TRUE(checkFile());
  1776. }
  1777. // Test if expression vendor[*].exists works properly for DHCPv6.
  1778. TEST_F(TokenTest, vendor6AnyVendorExists) {
  1779. // Case 1: no option, should evaluate to false
  1780. testVendorExists(Option::V6, 0, 0, "false");
  1781. // Case 2: option present with vendor-id 1234, should succeed
  1782. testVendorExists(Option::V6, 0, 1234, "true");
  1783. // Case 3: option present with vendor-id 4491, should succeed
  1784. testVendorExists(Option::V6, 0, 4491, "true");
  1785. // Check if the logged messages are correct.
  1786. addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 17 missing, "
  1787. "pushing result 'false'");
  1788. addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 1234 "
  1789. "found, pushing result 'true'");
  1790. addString("EVAL_DEBUG_VENDOR_EXISTS Option with enterprise-id 4491 "
  1791. "found, pushing result 'true'");
  1792. EXPECT_TRUE(checkFile());
  1793. }
  1794. // Test if expression vendor[*].enterprise works properly for DHCPv4.
  1795. TEST_F(TokenTest, vendor4enterprise) {
  1796. // Case 1: No option present, should return empty string
  1797. testVendorEnterprise(Option::V4, 0, "");
  1798. // Case 2: Option with vendor-id 1234, should return "1234"
  1799. testVendorEnterprise(Option::V4, 1234, encode(1234));
  1800. // Case 3: Option with vendor-id set to maximum value, should still
  1801. // be able to handle it
  1802. testVendorEnterprise(Option::V4, 4294967295, encode(4294967295));
  1803. // Check if the logged messages are correct.
  1804. addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 125 missing, pushing"
  1805. " result ''");
  1806. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID Pushing enterprise-id 1234 as "
  1807. "result 0x000004D2");
  1808. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID Pushing enterprise-id 4294967295"
  1809. " as result 0xFFFFFFFF");
  1810. EXPECT_TRUE(checkFile());
  1811. }
  1812. // Test if expression vendor[*].enterprise works properly for DHCPv6.
  1813. TEST_F(TokenTest, vendor6enterprise) {
  1814. // Case 1: No option present, should return empty string
  1815. testVendorEnterprise(Option::V6, 0, "");
  1816. // Case 2: Option with vendor-id 1234, should return "1234"
  1817. testVendorEnterprise(Option::V6, 1234, encode(1234));
  1818. // Case 3: Option with vendor-id set to maximum value, should still
  1819. // be able to handle it
  1820. testVendorEnterprise(Option::V6, 4294967295, encode(4294967295));
  1821. // Check if the logged messages are correct.
  1822. addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 17 missing, pushing"
  1823. " result ''");
  1824. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID Pushing enterprise-id 1234 as "
  1825. "result 0x000004D2");
  1826. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID Pushing enterprise-id 4294967295 "
  1827. "as result 0xFFFFFFFF");
  1828. EXPECT_TRUE(checkFile());
  1829. }
  1830. // This one tests "vendor[4491].option[1].exists" expression. There are so many
  1831. // wonderful ways in which this could fail: the option could not be there,
  1832. // it could have different enterprise-id, may not have suboption 1. Or may
  1833. // have the suboption with valid type, but enterprise may be different.
  1834. TEST_F(TokenTest, vendor4SuboptionExists) {
  1835. // Case 1: expression vendor[4491].option[1].exists, no option present
  1836. testVendorSuboption(Option::V4, 4491, 1, 0, 0, TokenOption::EXISTS, "false");
  1837. // Case 2: expression vendor[4491].option[1].exists, option with vendor-id = 1234,
  1838. // no suboptions, expected result "false"
  1839. testVendorSuboption(Option::V4, 4491, 1, 1234, 0, TokenOption::EXISTS, "false");
  1840. // Case 3: expression vendor[4491].option[1].exists, option with vendor-id = 1234,
  1841. // suboption 1, expected result "false"
  1842. testVendorSuboption(Option::V4, 4491, 1, 1234, 1, TokenOption::EXISTS, "false");
  1843. // Case 4: expression vendor[4491].option[1].exists, option with vendor-id = 4491,
  1844. // suboption 2, expected result "false"
  1845. testVendorSuboption(Option::V4, 4491, 1, 4491, 2, TokenOption::EXISTS, "false");
  1846. // Case 5: expression vendor[4491].option[1].exists, option with vendor-id = 4491,
  1847. // suboption 1, expected result "true"
  1848. testVendorSuboption(Option::V4, 4491, 1, 4491, 1, TokenOption::EXISTS, "true");
  1849. // Check if the logged messages are correct.
  1850. addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 125 missing, pushing "
  1851. "result 'false'");
  1852. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, "
  1853. "option had 1234, pushing result 'false'");
  1854. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, "
  1855. "option had 1234, pushing result 'false'");
  1856. addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'false'");
  1857. addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'true'");
  1858. EXPECT_TRUE(checkFile());
  1859. }
  1860. // This is similar to the previous one, but tests vendor[4491].option[1].exists
  1861. // for DHCPv6.
  1862. TEST_F(TokenTest, vendor6SuboptionExists) {
  1863. // Case 1: expression vendor[4491].option[1].exists, no option present
  1864. testVendorSuboption(Option::V6, 4491, 1, 0, 0, TokenOption::EXISTS, "false");
  1865. // Case 2: expression vendor[4491].option[1].exists, option with vendor-id = 1234,
  1866. // no suboptions, expected result "false"
  1867. testVendorSuboption(Option::V6, 4491, 1, 1234, 0, TokenOption::EXISTS, "false");
  1868. // Case 3: expression vendor[4491].option[1].exists, option with vendor-id = 1234,
  1869. // suboption 1, expected result "false"
  1870. testVendorSuboption(Option::V6, 4491, 1, 1234, 1, TokenOption::EXISTS, "false");
  1871. // Case 4: expression vendor[4491].option[1].exists, option with vendor-id = 4491,
  1872. // suboption 2, expected result "false"
  1873. testVendorSuboption(Option::V6, 4491, 1, 4491, 2, TokenOption::EXISTS, "false");
  1874. // Case 5: expression vendor[4491].option[1].exists, option with vendor-id = 4491,
  1875. // suboption 1, expected result "true"
  1876. testVendorSuboption(Option::V6, 4491, 1, 4491, 1, TokenOption::EXISTS, "true");
  1877. // Check if the logged messages are correct.
  1878. addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 17 missing, pushing "
  1879. "result 'false'");
  1880. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, "
  1881. "option had 1234, pushing result 'false'");
  1882. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, "
  1883. "option had 1234, pushing result 'false'");
  1884. addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'false'");
  1885. addString("EVAL_DEBUG_OPTION Pushing option 1 with value 'true'");
  1886. EXPECT_TRUE(checkFile());
  1887. }
  1888. // This test verifies if vendor[4491].option[1].hex expression properly returns
  1889. // value of said sub-option or empty string if desired option is not present.
  1890. // This test is for DHCPv4.
  1891. TEST_F(TokenTest, vendor4SuboptionHex) {
  1892. // Case 1: no option present, should return empty string
  1893. testVendorSuboption(Option::V4, 4491, 1, 0, 0, TokenOption::HEXADECIMAL, "");
  1894. // Case 2: option with vendor-id = 1234, no suboptions, expected result ""
  1895. testVendorSuboption(Option::V4, 4491, 1, 1234, 0, TokenOption::HEXADECIMAL, "");
  1896. // Case 3: option with vendor-id = 1234, suboption 1, expected result ""
  1897. testVendorSuboption(Option::V4, 4491, 1, 1234, 1, TokenOption::HEXADECIMAL, "");
  1898. // Case 4: option with vendor-id = 4491, suboption 2, expected result ""
  1899. testVendorSuboption(Option::V4, 4491, 1, 4491, 2, TokenOption::HEXADECIMAL, "");
  1900. // Case 5: option with vendor-id = 4491, suboption 1, expected result content
  1901. // of the option
  1902. testVendorSuboption(Option::V4, 4491, 1, 4491, 1, TokenOption::HEXADECIMAL, "alpha");
  1903. // Check if the logged messages are correct.
  1904. addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 125 missing, pushing "
  1905. "result ''");
  1906. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, "
  1907. "option had 1234, pushing result ''");
  1908. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, "
  1909. "option had 1234, pushing result ''");
  1910. addString("EVAL_DEBUG_OPTION Pushing option 1 with value 0x");
  1911. addString("EVAL_DEBUG_OPTION Pushing option 1 with value 0x616C706861");
  1912. EXPECT_TRUE(checkFile());
  1913. }
  1914. // This test verifies if vendor[4491].option[1].hex expression properly returns
  1915. // value of said sub-option or empty string if desired option is not present.
  1916. // This test is for DHCPv4.
  1917. TEST_F(TokenTest, vendor6SuboptionHex) {
  1918. // Case 1: no option present, should return empty string
  1919. testVendorSuboption(Option::V6, 4491, 1, 0, 0, TokenOption::HEXADECIMAL, "");
  1920. // Case 2: option with vendor-id = 1234, no suboptions, expected result ""
  1921. testVendorSuboption(Option::V6, 4491, 1, 1234, 0, TokenOption::HEXADECIMAL, "");
  1922. // Case 3: option with vendor-id = 1234, suboption 1, expected result ""
  1923. testVendorSuboption(Option::V6, 4491, 1, 1234, 1, TokenOption::HEXADECIMAL, "");
  1924. // Case 4: option with vendor-id = 4491, suboption 2, expected result ""
  1925. testVendorSuboption(Option::V6, 4491, 1, 4491, 2, TokenOption::HEXADECIMAL, "");
  1926. // Case 5: option with vendor-id = 4491, suboption 1, expected result content
  1927. // of the option
  1928. testVendorSuboption(Option::V6, 4491, 1, 4491, 1, TokenOption::HEXADECIMAL, "alpha");
  1929. // Check if the logged messages are correct.
  1930. addString("EVAL_DEBUG_VENDOR_NO_OPTION Option with code 17 missing, pushing "
  1931. "result ''");
  1932. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, "
  1933. "option had 1234, pushing result ''");
  1934. addString("EVAL_DEBUG_VENDOR_ENTERPRISE_ID_MISMATCH Was looking for 4491, "
  1935. "option had 1234, pushing result ''");
  1936. addString("EVAL_DEBUG_OPTION Pushing option 1 with value 0x");
  1937. addString("EVAL_DEBUG_OPTION Pushing option 1 with value 0x616C706861");
  1938. EXPECT_TRUE(checkFile());
  1939. }
  1940. // This test verifies that "vendor-class[4491].exists" expression can be used
  1941. // in DHCPv4.
  1942. TEST_F(TokenTest, vendorClass4SpecificVendorExists) {
  1943. // Case 1: no option present, should fail
  1944. testVendorClassExists(Option::V4, 4491, 0, "false");
  1945. // Case 2: option exists, but has different vendor-id (1234), should fail
  1946. testVendorClassExists(Option::V4, 4491, 1234, "false");
  1947. // Case 3: option exists and has matching vendor-id, should succeed
  1948. testVendorClassExists(Option::V4, 4491, 4491, "true");
  1949. // Check if the logged messages are correct.
  1950. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, "
  1951. "pushing result 'false'");
  1952. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  1953. "4491, option had 1234, pushing result 'false'");
  1954. addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 4491 "
  1955. "found, pushing result 'true'");
  1956. EXPECT_TRUE(checkFile());
  1957. }
  1958. // This test verifies that "vendor-class[4491].exists" expression can be used
  1959. // in DHCPv6.
  1960. TEST_F(TokenTest, vendorClass6SpecificVendorExists) {
  1961. // Case 1: no option present, should fail
  1962. testVendorClassExists(Option::V6, 4491, 0, "false");
  1963. // Case 2: option exists, but has different vendor-id (1234), should fail
  1964. testVendorClassExists(Option::V6, 4491, 1234, "false");
  1965. // Case 3: option exists and has matching vendor-id, should succeed
  1966. testVendorClassExists(Option::V6, 4491, 4491, "true");
  1967. // Check if the logged messages are correct.
  1968. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, pushing "
  1969. "result 'false'");
  1970. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  1971. "4491, option had 1234, pushing result 'false'");
  1972. addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 4491 "
  1973. "found, pushing result 'true'");
  1974. EXPECT_TRUE(checkFile());
  1975. }
  1976. // This test verifies that "vendor-class[*].exists" can be used in DHCPv4
  1977. // and it matches a vendor class option with any vendor-id.
  1978. TEST_F(TokenTest, vendorClass4AnyVendorExists) {
  1979. // Case 1: no option present, should fail
  1980. testVendorClassExists(Option::V4, 0, 0, "false");
  1981. // Case 2: option exists, should succeed, regardless of the vendor-id
  1982. testVendorClassExists(Option::V4, 0, 1234, "true");
  1983. // Case 3: option exists, should succeed, regardless of the vendor-id
  1984. testVendorClassExists(Option::V4, 0, 4491, "true");
  1985. // Check if the logged messages are correct.
  1986. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, "
  1987. "pushing result 'false'");
  1988. addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 1234 "
  1989. "found, pushing result 'true'");
  1990. addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 4491 "
  1991. "found, pushing result 'true'");
  1992. EXPECT_TRUE(checkFile());
  1993. }
  1994. // This test verifies that "vendor-class[*].exists" can be used in DHCPv6
  1995. // and it matches a vendor class option with any vendor-id.
  1996. TEST_F(TokenTest, vendorClass6AnyVendorExists) {
  1997. // Case 1: no option present, should fail
  1998. testVendorClassExists(Option::V6, 0, 0, "false");
  1999. // Case 2: option exists, should succeed, regardless of the vendor-id
  2000. testVendorClassExists(Option::V6, 0, 1234, "true");
  2001. // Case 3: option exists, should succeed, regardless of the vendor-id
  2002. testVendorClassExists(Option::V6, 0, 4491, "true");
  2003. // Check if the logged messages are correct.
  2004. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, pushing "
  2005. "result 'false'");
  2006. addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 1234 "
  2007. "found, pushing result 'true'");
  2008. addString("EVAL_DEBUG_VENDOR_CLASS_EXISTS Option with enterprise-id 4491 "
  2009. "found, pushing result 'true'");
  2010. EXPECT_TRUE(checkFile());
  2011. }
  2012. // Test if expression "vendor-class.enterprise" works properly for DHCPv4.
  2013. TEST_F(TokenTest, vendorClass4enterprise) {
  2014. // Case 1: No option present, should return empty string
  2015. testVendorClassEnterprise(Option::V4, 0, "");
  2016. // Case 2: Option with vendor-id 1234, should return "1234"
  2017. testVendorClassEnterprise(Option::V4, 1234, encode(1234));
  2018. // Case 3: Option with vendor-id set to maximum value, should still
  2019. // be able to handle it
  2020. testVendorClassEnterprise(Option::V4, 4294967295, encode(4294967295));
  2021. // Check if the logged messages are correct.
  2022. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, pushing "
  2023. "result ''");
  2024. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID Pushing enterprise-id "
  2025. "1234 as result 0x000004D2");
  2026. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID Pushing enterprise-id "
  2027. "4294967295 as result 0xFFFFFFFF");
  2028. EXPECT_TRUE(checkFile());
  2029. }
  2030. // Test if expression "vendor-class.enterprise" works properly for DHCPv6.
  2031. TEST_F(TokenTest, vendorClass6enterprise) {
  2032. // Case 1: No option present, should return empty string
  2033. testVendorClassEnterprise(Option::V6, 0, "");
  2034. // Case 2: Option with vendor-id 1234, should return "1234"
  2035. testVendorClassEnterprise(Option::V6, 1234, encode(1234));
  2036. // Case 3: Option with vendor-id set to maximum value, should still
  2037. // be able to handle it.
  2038. testVendorClassEnterprise(Option::V6, 4294967295, encode(4294967295));
  2039. // Check if the logged messages are correct.
  2040. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, pushing "
  2041. "result ''");
  2042. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID Pushing enterprise-id "
  2043. "1234 as result 0x000004D2");
  2044. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID Pushing enterprise-id "
  2045. "4294967295 as result 0xFFFFFFFF");
  2046. EXPECT_TRUE(checkFile());
  2047. }
  2048. // Test that expression "vendor-class[4491].data" is able to retrieve content
  2049. // of the first tuple of the vendor-class option in DHCPv4.
  2050. TEST_F(TokenTest, vendorClass4SpecificVendorData) {
  2051. // Case 1: Expression looks for vendor-id 4491, data[0], there is no
  2052. // vendor-class option at all, expected result is empty string.
  2053. testVendorClassData(Option::V4, 4491, 0, 0, 0, "");
  2054. // Case 2: Expression looks for vendor-id 4491, data[0], there is
  2055. // vendor-class with vendor-id 1234 and no data, expected result is empty string.
  2056. testVendorClassData(Option::V4, 4491, 0, 1234, 0, "");
  2057. // Case 3: Expression looks for vendor-id 4491, data[0], there is
  2058. // vendor-class with vendor-id 4491 and no data, expected result is empty string.
  2059. // Note that vendor option in v4 always have at least one data chunk, even though
  2060. // it may be empty. The OptionVendor code was told to not create any special
  2061. // tuples, but it creates one empty on its own. So the code finds that one
  2062. // tuple and extracts its content (an empty string).
  2063. testVendorClassData(Option::V4, 4491, 0, 4491, 0, "");
  2064. // Case 4: Expression looks for vendor-id 4491, data[0], there is
  2065. // vendor-class with vendor-id 1234 and 1 data tuple, expected result is empty string
  2066. testVendorClassData(Option::V4, 4491, 0, 1234, 1, "");
  2067. // Case 5: Expression looks for vendor-id 4491, data[0], there is
  2068. // vendor-class with vendor-id 4491 and 1 data tuple, expected result is
  2069. // content of that data ("alpha")
  2070. testVendorClassData(Option::V4, 4491, 0, 4491, 1, "alpha");
  2071. // Check if the logged messages are correct.
  2072. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, "
  2073. "pushing result ''");
  2074. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  2075. "4491, option had 1234, pushing result ''");
  2076. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in vendor "
  2077. "class found, pushing result ''");
  2078. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  2079. "4491, option had 1234, pushing result ''");
  2080. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in vendor "
  2081. "class found, pushing result 'alpha'");
  2082. EXPECT_TRUE(checkFile());
  2083. }
  2084. // Test that expression "vendor-class[4491].data" is able to retrieve content
  2085. // of the first tuple of the vendor-class option in DHCPv6.
  2086. TEST_F(TokenTest, vendorClass6SpecificVendorData) {
  2087. // Case 1: Expression looks for vendor-id 4491, data[0], there is no
  2088. // vendor-class option at all, expected result is empty string.
  2089. testVendorClassData(Option::V6, 4491, 0, 0, 0, "");
  2090. // Case 2: Expression looks for vendor-id 4491, data[0], there is
  2091. // vendor-class with vendor-id 1234 and no data, expected result is empty string.
  2092. testVendorClassData(Option::V6, 4491, 0, 1234, 0, "");
  2093. // Case 3: Expression looks for vendor-id 4491, data[0], there is
  2094. // vendor-class with vendor-id 4491 and no data, expected result is empty string
  2095. testVendorClassData(Option::V6, 4491, 0, 4491, 0, "");
  2096. // Case 4: Expression looks for vendor-id 4491, data[0], there is
  2097. // vendor-class with vendor-id 1234 and 1 data tuple, expected result is empty string
  2098. testVendorClassData(Option::V6, 4491, 0, 1234, 1, "");
  2099. // Case 5: Expression looks for vendor-id 4491, data[0], there is
  2100. // vendor-class with vendor-id 4491 and 1 data tuple, expected result is
  2101. // content of that data ("alpha")
  2102. testVendorClassData(Option::V6, 4491, 0, 4491, 1, "alpha");
  2103. // Check if the logged messages are correct.
  2104. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, "
  2105. "pushing result ''");
  2106. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  2107. "4491, option had 1234, pushing result ''");
  2108. addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 0, "
  2109. "but option with enterprise-id 4491 has only 0 data tuple(s), "
  2110. "pushing result ''");
  2111. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  2112. "4491, option had 1234, pushing result ''");
  2113. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in vendor "
  2114. "class found, pushing result 'alpha'");
  2115. EXPECT_TRUE(checkFile());
  2116. }
  2117. // Test that expression "vendor-class[*].data" is able to retrieve content
  2118. // of the first tuple of the vendor-class option in DHCPv4.
  2119. TEST_F(TokenTest, vendorClass4AnyVendorData) {
  2120. // Case 1: Expression looks for any vendor-id (0), data[0], there is no
  2121. // vendor-class option at all, expected result is empty string.
  2122. testVendorClassData(Option::V4, 0, 0, 0, 0, "");
  2123. // Case 2: Expression looks for any vendor-id (0), data[0], there is
  2124. // vendor-class with vendor-id 1234 and no data (one empty tuple), expected
  2125. // result is empty string.
  2126. testVendorClassData(Option::V4, 0, 0, 1234, 0, "");
  2127. // Case 3: Expression looks for any vendor-id (0), data[0], there is
  2128. // vendor-class with vendor-id 4491 and no data (one empty tuple), expected
  2129. // result is empty string.
  2130. testVendorClassData(Option::V4, 0, 0, 4491, 0, "");
  2131. // Case 4: Expression looks for any vendor-id (0), data[0], there is
  2132. // vendor-class with vendor-id 1234 and 1 data tuple, expected result is
  2133. // content of that data ("alpha")
  2134. testVendorClassData(Option::V4, 0, 0, 1234, 1, "alpha");
  2135. // Case 5: Expression looks for any vendor-id (0), data[0], there is
  2136. // vendor-class with vendor-id 4491 and 1 data tuple, expected result is
  2137. // content of that data ("alpha")
  2138. testVendorClassData(Option::V4, 0, 0, 4491, 1, "alpha");
  2139. // Check if the logged messages are correct.
  2140. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, "
  2141. "pushing result ''");
  2142. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in "
  2143. "vendor class found, pushing result ''");
  2144. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in "
  2145. "vendor class found, pushing result ''");
  2146. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in "
  2147. "vendor class found, pushing result 'alpha'");
  2148. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in "
  2149. "vendor class found, pushing result 'alpha'");
  2150. EXPECT_TRUE(checkFile());
  2151. }
  2152. // Test that expression "vendor-class[*].data" is able to retrieve content
  2153. // of the first tuple of the vendor-class option in DHCPv6.
  2154. TEST_F(TokenTest, vendorClass6AnyVendorData) {
  2155. // Case 1: Expression looks for any vendor-id (0), data[0], there is no
  2156. // vendor-class option at all, expected result is empty string.
  2157. testVendorClassData(Option::V6, 0, 0, 0, 0, "");
  2158. // Case 2: Expression looks for any vendor-id (0), data[0], there is
  2159. // vendor-class with vendor-id 1234 and no data, expected result is empty string.
  2160. testVendorClassData(Option::V6, 0, 0, 1234, 0, "");
  2161. // Case 3: Expression looks for any vendor-id (0), data[0], there is
  2162. // vendor-class with vendor-id 4491 and no data, expected result is empty string
  2163. testVendorClassData(Option::V6, 0, 0, 4491, 0, "");
  2164. // Case 4: Expression looks for any vendor-id (0), data[0], there is
  2165. // vendor-class with vendor-id 1234 and 1 data tuple, expected result is
  2166. // content of that data ("alpha")
  2167. testVendorClassData(Option::V6, 0, 0, 1234, 1, "alpha");
  2168. // Case 5: Expression looks for any vendor-id (0), data[0], there is
  2169. // vendor-class with vendor-id 4491 and 1 data tuple, expected result is
  2170. // content of that data ("alpha")
  2171. testVendorClassData(Option::V6, 0, 0, 4491, 1, "alpha");
  2172. // Check if the logged messages are correct.
  2173. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, "
  2174. "pushing result ''");
  2175. addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 0, "
  2176. "but option with enterprise-id 1234 has only 0 data tuple(s), "
  2177. "pushing result ''");
  2178. addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 0, "
  2179. "but option with enterprise-id 4491 has only 0 data tuple(s), "
  2180. "pushing result ''");
  2181. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in vendor "
  2182. "class found, pushing result 'alpha'");
  2183. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 0 (out of 1 received) in vendor "
  2184. "class found, pushing result 'alpha'");
  2185. EXPECT_TRUE(checkFile());
  2186. }
  2187. // This test verifies if expression vendor-class[4491].data[3] is able to access
  2188. // the tuple specified by index. This is a DHCPv4 test.
  2189. TEST_F(TokenTest, vendorClass4DataIndex) {
  2190. // Case 1: Expression looks for vendor-id 4491, data[3], there is no
  2191. // vendor-class option at all, expected result is empty string.
  2192. testVendorClassData(Option::V4, 4491, 3, 0, 0, "");
  2193. // Case 2: Expression looks for vendor-id 4491, data[3], there is
  2194. // vendor-class with vendor-id 1234 and no data, expected result is empty string.
  2195. testVendorClassData(Option::V4, 4491, 3, 1234, 0, "");
  2196. // Case 3: Expression looks for vendor-id 4491, data[3], there is
  2197. // vendor-class with vendor-id 4491 and no data, expected result is empty string
  2198. testVendorClassData(Option::V4, 4491, 3, 4491, 0, "");
  2199. // Case 4: Expression looks for vendor-id 4491, data[3], there is
  2200. // vendor-class with vendor-id 1234 and 1 data tuple, expected result is empty string.
  2201. testVendorClassData(Option::V4, 4491, 3, 1234, 1, "");
  2202. // Case 5: Expression looks for vendor-id 4491, data[3], there is
  2203. // vendor-class with vendor-id 4491, but has only 3 data tuples, expected
  2204. // result is empty string.
  2205. testVendorClassData(Option::V4, 4491, 3, 4491, 3, "");
  2206. // Case 6: Expression looks for vendor-id 4491, data[3], there is
  2207. // vendor-class with vendor-id 4491 and 5 data tuples, expected result is
  2208. // content of that tuple ("gamma")
  2209. testVendorClassData(Option::V4, 4491, 3, 4491, 5, "gamma");
  2210. // Case 6: Expression looks for vendor-id 4491, data[3], there is
  2211. // vendor-class with vendor-id 1234 and 5 data tuples, expected result is
  2212. // empty string, because vendor-id does not match.
  2213. testVendorClassData(Option::V4, 4491, 3, 1234, 5, "");
  2214. // Check if the logged messages are correct.
  2215. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 124 missing, "
  2216. "pushing result ''");
  2217. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  2218. "4491, option had 1234, pushing result ''");
  2219. addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 3, "
  2220. "but option with enterprise-id 4491 has only 1 data tuple(s), "
  2221. "pushing result ''");
  2222. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  2223. "4491, option had 1234, pushing result ''");
  2224. addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 3, "
  2225. "but option with enterprise-id 4491 has only 3 data tuple(s), "
  2226. "pushing result ''");
  2227. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 3 (out of 5 received) in vendor "
  2228. "class found, pushing result 'gamma'");
  2229. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  2230. "4491, option had 1234, pushing result ''");
  2231. EXPECT_TRUE(checkFile());
  2232. }
  2233. // This test verifies if expression vendor-class[4491].data[3] is able to access
  2234. // the tuple specified by index. This is a DHCPv6 test.
  2235. TEST_F(TokenTest, vendorClass6DataIndex) {
  2236. // Case 1: Expression looks for vendor-id 4491, data[3], there is no
  2237. // vendor-class option at all, expected result is empty string.
  2238. testVendorClassData(Option::V6, 4491, 3, 0, 0, "");
  2239. // Case 2: Expression looks for vendor-id 4491, data[3], there is
  2240. // vendor-class with vendor-id 1234 and no data, expected result is empty string.
  2241. testVendorClassData(Option::V6, 4491, 3, 1234, 0, "");
  2242. // Case 3: Expression looks for vendor-id 4491, data[3], there is
  2243. // vendor-class with vendor-id 4491 and no data, expected result is empty string
  2244. testVendorClassData(Option::V6, 4491, 3, 4491, 0, "");
  2245. // Case 4: Expression looks for vendor-id 4491, data[3], there is
  2246. // vendor-class with vendor-id 1234 and 5 data tuples, expected result is empty string.
  2247. testVendorClassData(Option::V6, 4491, 3, 1234, 5, "");
  2248. // Case 5: Expression looks for vendor-id 4491, data[3], there is
  2249. // vendor-class with vendor-id 4491, but has only 3 data tuples, expected
  2250. // result is empty string.
  2251. testVendorClassData(Option::V6, 4491, 3, 4491, 3, "");
  2252. // Case 6: Expression looks for vendor-id 4491, data[3], there is
  2253. // vendor-class with vendor-id 4491 and 5 data tuples, expected result is
  2254. // content of that tuple ("gamma")
  2255. testVendorClassData(Option::V6, 4491, 3, 4491, 5, "gamma");
  2256. // Check if the logged messages are correct.
  2257. addString("EVAL_DEBUG_VENDOR_CLASS_NO_OPTION Option with code 16 missing, "
  2258. "pushing result ''");
  2259. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  2260. "4491, option had 1234, pushing result ''");
  2261. addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 3, "
  2262. "but option with enterprise-id 4491 has only 0 data tuple(s), "
  2263. "pushing result ''");
  2264. addString("EVAL_DEBUG_VENDOR_CLASS_ENTERPRISE_ID_MISMATCH Was looking for "
  2265. "4491, option had 1234, pushing result ''");
  2266. addString("EVAL_DEBUG_VENDOR_CLASS_DATA_NOT_FOUND Requested data index 3, "
  2267. "but option with enterprise-id 4491 has only 3 data tuple(s), "
  2268. "pushing result ''");
  2269. addString("EVAL_DEBUG_VENDOR_CLASS_DATA Data 3 (out of 5 received) in vendor"
  2270. " class found, pushing result 'gamma'");
  2271. EXPECT_TRUE(checkFile());
  2272. }
  2273. };