token_unittest.cc 116 KB

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