nc_add_unittests.cc 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790
  1. // Copyright (C) 2013-2016 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. #include <config.h>
  7. #include <asiolink/io_service.h>
  8. #include <d2/d2_cfg_mgr.h>
  9. #include <d2/d2_cfg_mgr.h>
  10. #include <d2/nc_add.h>
  11. #include <dns/messagerenderer.h>
  12. #include <nc_test_utils.h>
  13. #include <boost/function.hpp>
  14. #include <boost/bind.hpp>
  15. #include <gtest/gtest.h>
  16. using namespace std;
  17. using namespace isc;
  18. using namespace isc::d2;
  19. using namespace isc::util;
  20. namespace {
  21. /// @brief Test class derived from NameAddTransaction to provide visibility
  22. // to protected methods.
  23. class NameAddStub : public NameAddTransaction {
  24. public:
  25. NameAddStub(asiolink::IOServicePtr& io_service,
  26. dhcp_ddns::NameChangeRequestPtr& ncr,
  27. DdnsDomainPtr& forward_domain,
  28. DdnsDomainPtr& reverse_domain,
  29. D2CfgMgrPtr& cfg_mgr)
  30. : NameAddTransaction(io_service, ncr, forward_domain, reverse_domain,
  31. cfg_mgr),
  32. simulate_send_exception_(false),
  33. simulate_build_request_exception_(false) {
  34. }
  35. virtual ~NameAddStub() {
  36. }
  37. /// @brief Simulates sending update requests to the DNS server
  38. ///
  39. /// This method simulates the initiation of an asynchronous send of
  40. /// a DNS update request. It overrides the actual sendUpdate method in
  41. /// the base class, thus avoiding an actual send, yet still increments
  42. /// the update attempt count and posts a next event of NOP_EVT.
  43. ///
  44. /// It will also simulate an exception-based failure of sendUpdate, if
  45. /// the simulate_send_exception_ flag is true.
  46. ///
  47. /// @param comment Parameter is unused, but present in base class method.
  48. ///
  49. virtual void sendUpdate(const std::string& /*comment*/) {
  50. if (simulate_send_exception_) {
  51. // Make the flag a one-shot by resetting it.
  52. simulate_send_exception_ = false;
  53. // Transition to failed.
  54. transition(PROCESS_TRANS_FAILED_ST, UPDATE_FAILED_EVT);
  55. return;
  56. }
  57. // Update send attempt count and post a NOP_EVT.
  58. setUpdateAttempts(getUpdateAttempts() + 1);
  59. postNextEvent(StateModel::NOP_EVT);
  60. }
  61. /// @brief Prepares the initial D2UpdateMessage
  62. ///
  63. /// This method overrides the NameChangeTransactio implementation to
  64. /// provide the ability to simulate an exception throw in the build
  65. /// request logic.
  66. /// If the one-shot flag, simulate_build_request_exception_ is true,
  67. /// this method will throw an exception, otherwise it will invoke the
  68. /// base class method, providing normal functionality.
  69. ///
  70. /// For parameter description see the NameChangeTransaction implementation.
  71. virtual D2UpdateMessagePtr prepNewRequest(DdnsDomainPtr domain) {
  72. if (simulate_build_request_exception_) {
  73. simulate_build_request_exception_ = false;
  74. isc_throw (NameAddTransactionError,
  75. "Simulated build requests exception");
  76. }
  77. return (NameChangeTransaction::prepNewRequest(domain));
  78. }
  79. /// @brief Simulates receiving a response
  80. ///
  81. /// This method simulates the completion of a DNSClient send. This allows
  82. /// the state handler logic devoted to dealing with IO completion to be
  83. /// fully exercised without requiring any actual IO. The two primary
  84. /// pieces of information gleaned from IO completion are the DNSClient
  85. /// status which indicates whether or not the IO exchange was successful
  86. /// and the rcode, which indicates the server's reaction to the request.
  87. ///
  88. /// This method updates the transaction's DNS status value to that of the
  89. /// given parameter, and then constructs and DNS update response message
  90. /// with the given rcode value. To complete the simulation it then posts
  91. /// a next event of IO_COMPLETED_EVT.
  92. ///
  93. /// @param status simulated DNSClient status
  94. /// @param rcode simulated server response code
  95. void fakeResponse(const DNSClient::Status& status,
  96. const dns::Rcode& rcode) {
  97. // Set the DNS update status. This is normally set in
  98. // DNSClient IO completion handler.
  99. setDnsUpdateStatus(status);
  100. // Construct an empty message with the given Rcode.
  101. D2UpdateMessagePtr msg(new D2UpdateMessage(D2UpdateMessage::OUTBOUND));
  102. msg->setRcode(rcode);
  103. // Set the update response to the message.
  104. setDnsUpdateResponse(msg);
  105. // Post the IO completion event.
  106. postNextEvent(NameChangeTransaction::IO_COMPLETED_EVT);
  107. }
  108. /// @brief Selects the first forward server.
  109. /// Some state handlers require a server to have been selected.
  110. /// This selects a server without going through the state
  111. /// transition(s) to do so.
  112. bool selectFwdServer() {
  113. if (getForwardDomain()) {
  114. initServerSelection(getForwardDomain());
  115. selectNextServer();
  116. return (getCurrentServer().get() != 0);
  117. }
  118. return (false);
  119. }
  120. /// @brief Selects the first reverse server.
  121. /// Some state handlers require a server to have been selected.
  122. /// This selects a server without going through the state
  123. /// transition(s) to do so.
  124. bool selectRevServer() {
  125. if (getReverseDomain()) {
  126. initServerSelection(getReverseDomain());
  127. selectNextServer();
  128. return (getCurrentServer().get() != 0);
  129. }
  130. return (false);
  131. }
  132. /// @brief One-shot flag which will simulate sendUpdate failure if true.
  133. bool simulate_send_exception_;
  134. /// @brief One-shot flag which will simulate an exception when sendUpdate
  135. /// failure if true.
  136. bool simulate_build_request_exception_;
  137. using StateModel::postNextEvent;
  138. using StateModel::setState;
  139. using StateModel::initDictionaries;
  140. using NameAddTransaction::defineEvents;
  141. using NameAddTransaction::verifyEvents;
  142. using NameAddTransaction::defineStates;
  143. using NameAddTransaction::verifyStates;
  144. using NameAddTransaction::readyHandler;
  145. using NameAddTransaction::selectingFwdServerHandler;
  146. using NameAddTransaction::getCurrentServer;
  147. using NameAddTransaction::addingFwdAddrsHandler;
  148. using NameAddTransaction::setDnsUpdateStatus;
  149. using NameAddTransaction::replacingFwdAddrsHandler;
  150. using NameAddTransaction::selectingRevServerHandler;
  151. using NameAddTransaction::replacingRevPtrsHandler;
  152. using NameAddTransaction::processAddOkHandler;
  153. using NameAddTransaction::processAddFailedHandler;
  154. using NameAddTransaction::buildAddFwdAddressRequest;
  155. using NameAddTransaction::buildReplaceFwdAddressRequest;
  156. using NameAddTransaction::buildReplaceRevPtrsRequest;
  157. };
  158. typedef boost::shared_ptr<NameAddStub> NameAddStubPtr;
  159. /// @brief Test fixture for testing NameAddTransaction
  160. ///
  161. /// Note this class uses NameAddStub class to exercise non-public
  162. /// aspects of NameAddTransaction.
  163. class NameAddTransactionTest : public TransactionTest {
  164. public:
  165. NameAddTransactionTest() {
  166. }
  167. virtual ~NameAddTransactionTest() {
  168. }
  169. /// @brief Creates a transaction which requests an IPv4 DNS update.
  170. ///
  171. /// The transaction is constructed around a predefined (i.e. "canned")
  172. /// IPv4 NameChangeRequest. The request has both forward and reverse DNS
  173. /// changes requested. Based upon the change mask, the transaction
  174. /// will have either the forward, reverse, or both domains populated.
  175. ///
  176. /// @param change_mask determines which change directions are requested
  177. NameAddStubPtr makeTransaction4(int change_mask = FWD_AND_REV_CHG) {
  178. // Creates IPv4 remove request, forward, and reverse domains.
  179. setupForIPv4Transaction(dhcp_ddns::CHG_ADD, change_mask);
  180. // Now create the test transaction as would occur in update manager.
  181. return (NameAddStubPtr(new NameAddStub(io_service_, ncr_,
  182. forward_domain_,
  183. reverse_domain_, cfg_mgr_)));
  184. }
  185. /// @brief Creates a transaction which requests an IPv6 DNS update.
  186. ///
  187. /// The transaction is constructed around a predefined (i.e. "canned")
  188. /// IPv6 NameChangeRequest. The request has both forward and reverse DNS
  189. /// changes requested. Based upon the change mask, the transaction
  190. /// will have either the forward, reverse, or both domains populated.
  191. ///
  192. /// @param change_mask determines which change directions are requested
  193. NameAddStubPtr makeTransaction6(int change_mask = FWD_AND_REV_CHG) {
  194. // Creates IPv6 remove request, forward, and reverse domains.
  195. setupForIPv6Transaction(dhcp_ddns::CHG_ADD, change_mask);
  196. // Now create the test transaction as would occur in update manager.
  197. return (NameAddStubPtr(new NameAddStub(io_service_, ncr_,
  198. forward_domain_,
  199. reverse_domain_,
  200. cfg_mgr_)));
  201. }
  202. /// @brief Create a test transaction at a known point in the state model.
  203. ///
  204. /// Method prepares a new test transaction and sets its state and next
  205. /// event values to those given. This makes the transaction appear to
  206. /// be at that point in the state model without having to transition it
  207. /// through prerequisite states. It also provides the ability to set
  208. /// which change directions are requested: forward change only, reverse
  209. /// change only, or both.
  210. ///
  211. /// @param state value to set as the current state
  212. /// @param event value to post as the next event
  213. /// @param change_mask determines which change directions are requested
  214. /// @param family selects between an IPv4 (AF_INET) and IPv6 (AF_INET6)
  215. /// transaction.
  216. NameAddStubPtr prepHandlerTest(unsigned int state, unsigned int event,
  217. unsigned int change_mask = FWD_AND_REV_CHG,
  218. short family = AF_INET) {
  219. NameAddStubPtr name_add = (family == AF_INET ?
  220. makeTransaction4(change_mask) :
  221. makeTransaction4(change_mask));
  222. name_add->initDictionaries();
  223. name_add->postNextEvent(event);
  224. name_add->setState(state);
  225. return (name_add);
  226. }
  227. };
  228. /// @brief Tests NameAddTransaction construction.
  229. /// This test verifies that:
  230. /// 1. Construction with invalid type of request
  231. /// 2. Valid construction functions properly
  232. TEST(NameAddTransaction, construction) {
  233. asiolink::IOServicePtr io_service(new isc::asiolink::IOService());
  234. D2CfgMgrPtr cfg_mgr(new D2CfgMgr());
  235. const char* msg_str =
  236. "{"
  237. " \"change-type\" : 1 , "
  238. " \"forward-change\" : true , "
  239. " \"reverse-change\" : true , "
  240. " \"fqdn\" : \"example.com.\" , "
  241. " \"ip-address\" : \"192.168.2.1\" , "
  242. " \"dhcid\" : \"0102030405060708\" , "
  243. " \"lease-expires-on\" : \"20130121132405\" , "
  244. " \"lease-length\" : 1300 "
  245. "}";
  246. dhcp_ddns::NameChangeRequestPtr ncr;
  247. DnsServerInfoStoragePtr servers;
  248. DdnsDomainPtr forward_domain;
  249. DdnsDomainPtr reverse_domain;
  250. DdnsDomainPtr empty_domain;
  251. ASSERT_NO_THROW(ncr = dhcp_ddns::NameChangeRequest::fromJSON(msg_str));
  252. ASSERT_NO_THROW(forward_domain.reset(new DdnsDomain("*", servers)));
  253. ASSERT_NO_THROW(reverse_domain.reset(new DdnsDomain("*", servers)));
  254. // Verify that construction with wrong change type fails.
  255. EXPECT_THROW(NameAddTransaction(io_service, ncr,
  256. forward_domain, reverse_domain, cfg_mgr),
  257. NameAddTransactionError);
  258. // Verify that a valid construction attempt works.
  259. ncr->setChangeType(isc::dhcp_ddns::CHG_ADD);
  260. EXPECT_NO_THROW(NameAddTransaction(io_service, ncr,
  261. forward_domain, reverse_domain,
  262. cfg_mgr));
  263. }
  264. /// @brief Tests event and state dictionary construction and verification.
  265. TEST_F(NameAddTransactionTest, dictionaryCheck) {
  266. NameAddStubPtr name_add;
  267. ASSERT_NO_THROW(name_add = makeTransaction4());
  268. // Verify that the event and state dictionary validation fails prior
  269. // dictionary construction.
  270. ASSERT_THROW(name_add->verifyEvents(), StateModelError);
  271. ASSERT_THROW(name_add->verifyStates(), StateModelError);
  272. // Construct both dictionaries.
  273. ASSERT_NO_THROW(name_add->defineEvents());
  274. ASSERT_NO_THROW(name_add->defineStates());
  275. // Verify both event and state dictionaries now pass validation.
  276. ASSERT_NO_THROW(name_add->verifyEvents());
  277. ASSERT_NO_THROW(name_add->verifyStates());
  278. }
  279. /// @brief Tests construction of a DNS update request for adding a forward
  280. /// dns entry.
  281. TEST_F(NameAddTransactionTest, buildForwardAdd) {
  282. // Create a IPv4 forward add transaction.
  283. // Verify the request builds without error.
  284. // and then verify the request contents.
  285. NameAddStubPtr name_add;
  286. ASSERT_NO_THROW(name_add = makeTransaction4());
  287. ASSERT_NO_THROW(name_add->buildAddFwdAddressRequest());
  288. checkAddFwdAddressRequest(*name_add);
  289. // Create a IPv6 forward add transaction.
  290. // Verify the request builds without error.
  291. // and then verify the request contents.
  292. ASSERT_NO_THROW(name_add = makeTransaction6());
  293. ASSERT_NO_THROW(name_add->buildAddFwdAddressRequest());
  294. checkAddFwdAddressRequest(*name_add);
  295. }
  296. /// @brief Tests construction of a DNS update request for replacing a forward
  297. /// dns entry.
  298. TEST_F(NameAddTransactionTest, buildReplaceFwdAddressRequest) {
  299. // Create a IPv4 forward replace transaction.
  300. // Verify the request builds without error.
  301. // and then verify the request contents.
  302. NameAddStubPtr name_add;
  303. ASSERT_NO_THROW(name_add = makeTransaction4());
  304. ASSERT_NO_THROW(name_add->buildReplaceFwdAddressRequest());
  305. checkReplaceFwdAddressRequest(*name_add);
  306. // Create a IPv6 forward replace transaction.
  307. // Verify the request builds without error.
  308. // and then verify the request contents.
  309. ASSERT_NO_THROW(name_add = makeTransaction6());
  310. ASSERT_NO_THROW(name_add->buildReplaceFwdAddressRequest());
  311. checkReplaceFwdAddressRequest(*name_add);
  312. }
  313. /// @brief Tests the construction of a DNS update request for replacing a
  314. /// reverse dns entry.
  315. TEST_F(NameAddTransactionTest, buildReplaceRevPtrsRequest) {
  316. // Create a IPv4 reverse replace transaction.
  317. // Verify the request builds without error.
  318. // and then verify the request contents.
  319. NameAddStubPtr name_add;
  320. ASSERT_NO_THROW(name_add = makeTransaction4());
  321. ASSERT_NO_THROW(name_add->buildReplaceRevPtrsRequest());
  322. checkReplaceRevPtrsRequest(*name_add);
  323. // Create a IPv6 reverse replace transaction.
  324. // Verify the request builds without error.
  325. // and then verify the request contents.
  326. ASSERT_NO_THROW(name_add = makeTransaction6());
  327. ASSERT_NO_THROW(name_add->buildReplaceRevPtrsRequest());
  328. checkReplaceRevPtrsRequest(*name_add);
  329. }
  330. // Tests the readyHandler functionality.
  331. // It verifies behavior for the following scenarios:
  332. //
  333. // 1. Posted event is START_EVT and request includes only a forward change
  334. // 2. Posted event is START_EVT and request includes both a forward and a
  335. // reverse change
  336. // 3. Posted event is START_EVT and request includes only a reverse change
  337. // 4. Posted event is invalid
  338. //
  339. TEST_F(NameAddTransactionTest, readyHandler) {
  340. NameAddStubPtr name_add;
  341. // Create a transaction which includes only a forward change.
  342. ASSERT_NO_THROW(name_add =
  343. prepHandlerTest(NameChangeTransaction::READY_ST,
  344. StateModel::START_EVT, FORWARD_CHG));
  345. // Run readyHandler.
  346. EXPECT_NO_THROW(name_add->readyHandler());
  347. // Verify that a request requiring only a forward change, transitions to
  348. // selecting a forward server.
  349. EXPECT_EQ(NameChangeTransaction::SELECTING_FWD_SERVER_ST,
  350. name_add->getCurrState());
  351. EXPECT_EQ(NameChangeTransaction::SELECT_SERVER_EVT,
  352. name_add->getNextEvent());
  353. // Create a transaction which includes both a forward and a reverse change.
  354. ASSERT_NO_THROW(name_add =
  355. prepHandlerTest(NameChangeTransaction::READY_ST,
  356. StateModel::START_EVT, FWD_AND_REV_CHG));
  357. // Run readyHandler.
  358. EXPECT_NO_THROW(name_add->readyHandler());
  359. // Verify that a request requiring both forward and reverse, starts with
  360. // the forward change by transitioning to selecting a forward server.
  361. EXPECT_EQ(NameChangeTransaction::SELECTING_FWD_SERVER_ST,
  362. name_add->getCurrState());
  363. EXPECT_EQ(NameChangeTransaction::SELECT_SERVER_EVT,
  364. name_add->getNextEvent());
  365. // Create and prep a reverse only transaction.
  366. ASSERT_NO_THROW(name_add =
  367. prepHandlerTest(NameChangeTransaction::READY_ST,
  368. StateModel::START_EVT, REVERSE_CHG));
  369. // Run readyHandler.
  370. EXPECT_NO_THROW(name_add->readyHandler());
  371. // Verify that a request requiring only a reverse change, transitions to
  372. // selecting a reverse server.
  373. EXPECT_EQ(NameChangeTransaction::SELECTING_REV_SERVER_ST,
  374. name_add->getCurrState());
  375. EXPECT_EQ(NameChangeTransaction::SELECT_SERVER_EVT,
  376. name_add->getNextEvent());
  377. // Create and prep transaction, poised to run the handler but with an
  378. // invalid event.
  379. ASSERT_NO_THROW(name_add =
  380. prepHandlerTest(NameChangeTransaction::READY_ST,
  381. StateModel::NOP_EVT));
  382. // Running the readyHandler should throw.
  383. EXPECT_THROW(name_add->readyHandler(), NameAddTransactionError);
  384. }
  385. // Tests the selectingFwdServerHandler functionality.
  386. // It verifies behavior for the following scenarios:
  387. //
  388. // 1. Posted event is SELECT_SERVER_EVT
  389. // 2. Posted event is SERVER_IO_ERROR_EVT
  390. // 3. Posted event is invalid
  391. //
  392. TEST_F(NameAddTransactionTest, selectingFwdServerHandler) {
  393. NameAddStubPtr name_add;
  394. // Create and prep a transaction, poised to run the handler.
  395. ASSERT_NO_THROW(name_add =
  396. prepHandlerTest(NameChangeTransaction::
  397. SELECTING_FWD_SERVER_ST,
  398. NameChangeTransaction::SELECT_SERVER_EVT));
  399. // Call selectingFwdServerHandler enough times to select all of the
  400. // servers in it's current domain. The first time, it will be with
  401. // next event of SELECT_SERVER_EVT. Thereafter it will be with a next
  402. // event of SERVER_IO_ERROR_EVT.
  403. int num_servers = name_add->getForwardDomain()->getServers()->size();
  404. for (int i = 0; i < num_servers; ++i) {
  405. // Run selectingFwdServerHandler.
  406. ASSERT_NO_THROW(name_add->selectingFwdServerHandler())
  407. << " num_servers: " << num_servers
  408. << " selections: " << i;
  409. // Verify that a server was selected.
  410. ASSERT_TRUE(name_add->getCurrentServer())
  411. << " num_servers: " << num_servers << " selections: " << i;
  412. // Verify that we transitioned correctly.
  413. ASSERT_EQ(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  414. name_add->getCurrState())
  415. << " num_servers: " << num_servers << " selections: " << i;
  416. ASSERT_EQ(NameChangeTransaction::SERVER_SELECTED_EVT,
  417. name_add->getNextEvent())
  418. << " num_servers: " << num_servers << " selections: " << i;
  419. // Post a server IO error event. This simulates an IO error occuring
  420. // and a need to select the new server.
  421. ASSERT_NO_THROW(name_add->postNextEvent(NameChangeTransaction::
  422. SERVER_IO_ERROR_EVT))
  423. << " num_servers: " << num_servers
  424. << " selections: " << i;
  425. }
  426. // We should have exhausted the list of servers. Processing another
  427. // SERVER_IO_ERROR_EVT should transition us to failure.
  428. EXPECT_NO_THROW(name_add->selectingFwdServerHandler());
  429. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  430. name_add->getCurrState());
  431. EXPECT_EQ(NameChangeTransaction::NO_MORE_SERVERS_EVT,
  432. name_add->getNextEvent());
  433. // Create and prep transaction, poised to run the handler but with an
  434. // invalid event.
  435. ASSERT_NO_THROW(name_add =
  436. prepHandlerTest(NameChangeTransaction::
  437. SELECTING_FWD_SERVER_ST,
  438. StateModel::NOP_EVT));
  439. // Running the handler should throw.
  440. EXPECT_THROW(name_add->selectingFwdServerHandler(),
  441. NameAddTransactionError);
  442. }
  443. // ************************ addingFwdAddrHandler Tests *****************
  444. // Tests that addingFwdAddrsHandler rejects invalid events.
  445. TEST_F(NameAddTransactionTest, addingFwdAddrsHandler_InvalidEvent) {
  446. NameAddStubPtr name_add;
  447. // Create and prep a transaction, poised to run the handler but with
  448. // an invalid event.
  449. ASSERT_NO_THROW(name_add =
  450. prepHandlerTest(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  451. NameChangeTransaction::
  452. StateModel::NOP_EVT));
  453. // Running the handler should throw.
  454. EXPECT_THROW(name_add->addingFwdAddrsHandler(),
  455. NameAddTransactionError);
  456. }
  457. // Tests addingFwdAddrsHandler with the following scenario:
  458. //
  459. // The request includes only a forward change.
  460. // Initial posted event is SERVER_SELECTED_EVT.
  461. // The update request is sent without error.
  462. // A server response is received which indicates successful update
  463. //
  464. TEST_F(NameAddTransactionTest, addingFwdAddrsHandler_FwdOnlyAddOK) {
  465. NameAddStubPtr name_add;
  466. // Create and prep a transaction, poised to run the handler.
  467. ASSERT_NO_THROW(name_add =
  468. prepHandlerTest(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  469. NameChangeTransaction::
  470. SERVER_SELECTED_EVT, FORWARD_CHG));
  471. // Should not be an update message yet.
  472. D2UpdateMessagePtr update_msg = name_add->getDnsUpdateRequest();
  473. ASSERT_FALSE(update_msg);
  474. // At this point completion flags should be false.
  475. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  476. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  477. // Run addingFwdAddrsHandler to construct and send the request.
  478. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  479. // Verify that an update message was constructed properly.
  480. checkAddFwdAddressRequest(*name_add);
  481. // Verify that we are still in this state and next event is NOP_EVT.
  482. // This indicates we "sent" the message and are waiting for IO completion.
  483. EXPECT_EQ(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  484. name_add->getCurrState());
  485. EXPECT_EQ(NameChangeTransaction::NOP_EVT,
  486. name_add->getNextEvent());
  487. // Simulate receiving a successful update response.
  488. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::NOERROR());
  489. // Run addingFwdAddrsHandler again to process the response.
  490. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  491. // Forward completion should be true, reverse should be false.
  492. EXPECT_TRUE(name_add->getForwardChangeCompleted());
  493. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  494. // Since it is a forward only change, we should be done.
  495. // Verify that we transitioned correctly.
  496. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_OK_ST,
  497. name_add->getCurrState());
  498. EXPECT_EQ(NameChangeTransaction::UPDATE_OK_EVT,
  499. name_add->getNextEvent());
  500. }
  501. // Tests addingFwdAddrsHandler with the following scenario:
  502. //
  503. // The request includes a forward and reverse change.
  504. // Initial posted event is SERVER_SELECTED_EVT.
  505. // The update request is sent without error.
  506. // A server response is received which indicates successful update.
  507. //
  508. TEST_F(NameAddTransactionTest, addingFwdAddrsHandler_fwdAndRevAddOK) {
  509. NameAddStubPtr name_add;
  510. // Create and prep a transaction, poised to run the handler.
  511. ASSERT_NO_THROW(name_add =
  512. prepHandlerTest(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  513. NameChangeTransaction::
  514. SERVER_SELECTED_EVT, FWD_AND_REV_CHG));
  515. // Run addingFwdAddrsHandler to construct and send the request.
  516. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  517. // Simulate receiving a successful update response.
  518. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::NOERROR());
  519. // Run addingFwdAddrsHandler again to process the response.
  520. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  521. // Forward change completion should be true, reverse flag should be false.
  522. EXPECT_TRUE(name_add->getForwardChangeCompleted());
  523. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  524. // Since the request also includes a reverse change we should
  525. // be poised to start it. Verify that we transitioned correctly.
  526. EXPECT_EQ(NameChangeTransaction::SELECTING_REV_SERVER_ST,
  527. name_add->getCurrState());
  528. EXPECT_EQ(NameChangeTransaction::SELECT_SERVER_EVT,
  529. name_add->getNextEvent());
  530. }
  531. // Tests addingFwdAddrsHandler with the following scenario:
  532. //
  533. // The request includes a forward and reverse change.
  534. // Initial posted event is SERVER_SELECTED_EVT.
  535. // The update request is sent without error.
  536. // A server response is received which indicates the FQDN is in use.
  537. //
  538. TEST_F(NameAddTransactionTest, addingFwdAddrsHandler_FqdnInUse) {
  539. NameAddStubPtr name_add;
  540. // Create and prep a transaction, poised to run the handler.
  541. ASSERT_NO_THROW(name_add =
  542. prepHandlerTest(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  543. NameChangeTransaction::
  544. SERVER_SELECTED_EVT));
  545. // Run addingFwdAddrsHandler to construct and send the request.
  546. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  547. // Simulate receiving a FQDN in use response.
  548. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::YXDOMAIN());
  549. // Run addingFwdAddrsHandler again to process the response.
  550. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  551. // Completion flags should still be false.
  552. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  553. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  554. // Since the FQDN is in use, per the RFC we must attempt to replace it.
  555. // Verify that we transitioned correctly.
  556. EXPECT_EQ(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  557. name_add->getCurrState());
  558. EXPECT_EQ(NameAddTransaction::FQDN_IN_USE_EVT,
  559. name_add->getNextEvent());
  560. }
  561. // Tests addingFwdAddrsHandler with the following scenario:
  562. //
  563. // The request includes a forward and reverse change.
  564. // Initial posted event is SERVER_SELECTED_EVT.
  565. // The update request is sent without error.
  566. // A server response is received which indicates the update was rejected.
  567. //
  568. TEST_F(NameAddTransactionTest, addingFwdAddrsHandler_OtherRcode) {
  569. NameAddStubPtr name_add;
  570. // Create and prep a transaction, poised to run the handler.
  571. ASSERT_NO_THROW(name_add =
  572. prepHandlerTest(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  573. NameChangeTransaction::
  574. SERVER_SELECTED_EVT));
  575. // Select a server to satisfy log statements.
  576. ASSERT_TRUE(name_add->selectFwdServer());
  577. // Run addingFwdAddrsHandler to construct and send the request.
  578. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  579. // Simulate receiving server rejection response. Per RFC, anything other
  580. // than no error or FQDN in use is failure. Arbitrarily choosing refused.
  581. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::REFUSED());
  582. // Run addingFwdAddrsHandler again to process the response.
  583. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  584. // Completion flags should still be false.
  585. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  586. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  587. // We should have failed the transaction. Verify that we transitioned
  588. // correctly.
  589. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  590. name_add->getCurrState());
  591. EXPECT_EQ(NameChangeTransaction::UPDATE_FAILED_EVT,
  592. name_add->getNextEvent());
  593. }
  594. // Tests addingFwdAddrsHandler with the following scenario:
  595. //
  596. // The request includes a forward and reverse change.
  597. // Initial posted event is SERVER_SELECTED_EVT.
  598. // The update request send times out MAX_UPDATE_TRIES_PER_SERVER times.
  599. //
  600. TEST_F(NameAddTransactionTest, addingFwdAddrsHandler_Timeout) {
  601. NameAddStubPtr name_add;
  602. // Create and prep a transaction, poised to run the handler.
  603. // The log message issued when this test succeeds, displays the
  604. // selected server, so we need to select a server before running this
  605. // test.
  606. ASSERT_NO_THROW(name_add =
  607. prepHandlerTest(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  608. NameChangeTransaction::
  609. SERVER_SELECTED_EVT));
  610. // Select a server to satisfy log statements.
  611. ASSERT_TRUE(name_add->selectFwdServer());
  612. // Verify that we can make maximum number of update attempts permitted
  613. // and then transition to selecting a new server.
  614. int max_tries = NameChangeTransaction::MAX_UPDATE_TRIES_PER_SERVER;
  615. for (int i = 1; i <= max_tries; ++i) {
  616. const D2UpdateMessagePtr prev_msg = name_add->getDnsUpdateRequest();
  617. // Run addingFwdAddrsHandler to send the request.
  618. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  619. const D2UpdateMessagePtr curr_msg = name_add->getDnsUpdateRequest();
  620. if (i == 1) {
  621. // First time out we should build the message.
  622. EXPECT_FALSE(prev_msg);
  623. EXPECT_TRUE(curr_msg);
  624. } else {
  625. // Subsequent passes should reuse the request. We are only
  626. // looking to check that we have not replaced the pointer value
  627. // with a new pointer. This tests the on_entry() logic which
  628. // clears the request ONLY upon initial entry into the state.
  629. EXPECT_TRUE(prev_msg == curr_msg);
  630. }
  631. // Simulate a server IO timeout.
  632. name_add->setDnsUpdateStatus(DNSClient::TIMEOUT);
  633. name_add->postNextEvent(NameChangeTransaction::IO_COMPLETED_EVT);
  634. // Run addingFwdAddrsHandler again to process the response.
  635. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  636. // Completion flags should be false.
  637. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  638. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  639. if (i < max_tries) {
  640. // We should be ready to try again.
  641. EXPECT_EQ(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  642. name_add->getCurrState());
  643. EXPECT_EQ(NameChangeTransaction::SERVER_SELECTED_EVT,
  644. name_add->getNextEvent());
  645. } else {
  646. // Server retries should be exhausted, time for a new server.
  647. EXPECT_EQ(NameAddTransaction::SELECTING_FWD_SERVER_ST,
  648. name_add->getCurrState());
  649. EXPECT_EQ(NameChangeTransaction::SERVER_IO_ERROR_EVT,
  650. name_add->getNextEvent());
  651. }
  652. }
  653. }
  654. // Tests addingFwdAddrsHandler with the following scenario:
  655. //
  656. // The request includes a forward and reverse change.
  657. // Initial posted event is SERVER_SELECTED_EVT.
  658. // The update request is sent but a corrupt response is received, this occurs
  659. // MAX_UPDATE_TRIES_PER_SERVER times.
  660. //
  661. TEST_F(NameAddTransactionTest, addingFwdAddrsHandler_InvalidResponse) {
  662. NameAddStubPtr name_add;
  663. // Create and prep a transaction, poised to run the handler.
  664. // The log message issued when this test succeeds, displays the
  665. // selected server, so we need to select a server before running this
  666. // test.
  667. ASSERT_NO_THROW(name_add =
  668. prepHandlerTest(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  669. NameChangeTransaction::
  670. SERVER_SELECTED_EVT));
  671. // Select a server to satisfy log statements.
  672. ASSERT_TRUE(name_add->selectFwdServer());
  673. // Verify that we can make maximum number of update attempts permitted
  674. // and then transition to selecting a new server.
  675. int max_tries = NameChangeTransaction::MAX_UPDATE_TRIES_PER_SERVER;
  676. for (int i = 1; i <= max_tries; ++i) {
  677. // Run addingFwdAddrsHandler to construct send the request.
  678. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  679. // Simulate a corrupt server response.
  680. name_add->setDnsUpdateStatus(DNSClient::INVALID_RESPONSE);
  681. name_add->postNextEvent(NameChangeTransaction::IO_COMPLETED_EVT);
  682. // Run addingFwdAddrsHandler again to process the response.
  683. EXPECT_NO_THROW(name_add->addingFwdAddrsHandler());
  684. // Completion flags should be false.
  685. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  686. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  687. if (i < max_tries) {
  688. // We should be ready to try again.
  689. EXPECT_EQ(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  690. name_add->getCurrState());
  691. EXPECT_EQ(NameChangeTransaction::SERVER_SELECTED_EVT,
  692. name_add->getNextEvent());
  693. } else {
  694. // Server retries should be exhausted, time for a new server.
  695. EXPECT_EQ(NameAddTransaction::SELECTING_FWD_SERVER_ST,
  696. name_add->getCurrState());
  697. EXPECT_EQ(NameChangeTransaction::SERVER_IO_ERROR_EVT,
  698. name_add->getNextEvent());
  699. }
  700. }
  701. }
  702. // ************************ replacingFwdAddrHandler Tests *****************
  703. // Tests that replacingFwdAddrsHandler rejects invalid events.
  704. TEST_F(NameAddTransactionTest, replacingFwdAddrsHandler_InvalidEvent) {
  705. NameAddStubPtr name_add;
  706. // Create and prep a transaction, poised to run the handler but with
  707. // an invalid event.
  708. ASSERT_NO_THROW(name_add =
  709. prepHandlerTest(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  710. NameChangeTransaction::
  711. StateModel::NOP_EVT));
  712. // Running the handler should throw.
  713. EXPECT_THROW(name_add->replacingFwdAddrsHandler(),
  714. NameAddTransactionError);
  715. }
  716. // Tests replacingFwdAddrsHandler with the following scenario:
  717. //
  718. // The request includes only a forward change.
  719. // Initial posted event is FQDN_IN_USE_EVT.
  720. // The update request is sent without error.
  721. // A server response is received which indicates successful update.
  722. //
  723. TEST_F(NameAddTransactionTest, replacingFwdAddrsHandler_FwdOnlyAddOK) {
  724. NameAddStubPtr name_add;
  725. // Create and prep a transaction, poised to run the handler.
  726. ASSERT_NO_THROW(name_add =
  727. prepHandlerTest(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  728. NameAddTransaction::
  729. FQDN_IN_USE_EVT, FORWARD_CHG));
  730. // Should not be an update message yet.
  731. D2UpdateMessagePtr update_msg = name_add->getDnsUpdateRequest();
  732. ASSERT_FALSE(update_msg);
  733. // At this point completion flags should be false.
  734. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  735. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  736. // Run replacingFwdAddrsHandler to construct and send the request.
  737. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  738. // Verify that an update message was constructed properly.
  739. checkReplaceFwdAddressRequest(*name_add);
  740. // Verify that we are still in this state and next event is NOP_EVT.
  741. // This indicates we "sent" the message and are waiting for IO completion.
  742. EXPECT_EQ(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  743. name_add->getCurrState());
  744. EXPECT_EQ(NameChangeTransaction::NOP_EVT,
  745. name_add->getNextEvent());
  746. // Simulate receiving a successful update response.
  747. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::NOERROR());
  748. // Run replacingFwdAddrsHandler again to process the response.
  749. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  750. // Forward completion should be true, reverse should be false.
  751. EXPECT_TRUE(name_add->getForwardChangeCompleted());
  752. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  753. // Since it is a forward only change, we should be done.
  754. // Verify that we transitioned correctly.
  755. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_OK_ST,
  756. name_add->getCurrState());
  757. EXPECT_EQ(NameChangeTransaction::UPDATE_OK_EVT,
  758. name_add->getNextEvent());
  759. }
  760. // Tests replacingFwdAddrsHandler with the following scenario:
  761. //
  762. // The request includes only a forward change.
  763. // Initial posted event is SERVER_SELECTED_EVT.
  764. // The update request is sent without error.
  765. // A server response is received which indicates successful update.
  766. //
  767. TEST_F(NameAddTransactionTest, replacingFwdAddrsHandler_FwdOnlyAddOK2) {
  768. NameAddStubPtr name_add;
  769. // Create and prep a transaction, poised to run the handler.
  770. ASSERT_NO_THROW(name_add =
  771. prepHandlerTest(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  772. NameChangeTransaction::
  773. SERVER_SELECTED_EVT, FORWARD_CHG));
  774. // Run replacingFwdAddrsHandler to construct and send the request.
  775. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  776. // Simulate receiving a successful update response.
  777. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::NOERROR());
  778. // Run replacingFwdAddrsHandler again to process the response.
  779. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  780. // Forward completion should be true, reverse should be false.
  781. EXPECT_TRUE(name_add->getForwardChangeCompleted());
  782. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  783. // Since it is a forward only change, we should be done.
  784. // Verify that we transitioned correctly.
  785. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_OK_ST,
  786. name_add->getCurrState());
  787. EXPECT_EQ(NameChangeTransaction::UPDATE_OK_EVT,
  788. name_add->getNextEvent());
  789. }
  790. // Tests replacingFwdAddrsHandler with the following scenario:
  791. //
  792. // The request includes a forward and reverse change.
  793. // Initial posted event is FQDN_IN_USE_EVT.
  794. // The update request is sent without error.
  795. // A server response is received which indicates successful update.
  796. //
  797. TEST_F(NameAddTransactionTest, replacingFwdAddrsHandler_FwdAndRevAddOK) {
  798. NameAddStubPtr name_add;
  799. // Create and prep a transaction, poised to run the handler.
  800. ASSERT_NO_THROW(name_add =
  801. prepHandlerTest(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  802. NameAddTransaction::
  803. FQDN_IN_USE_EVT, FWD_AND_REV_CHG));
  804. // Run replacingFwdAddrsHandler to construct and send the request.
  805. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  806. // Simulate receiving a successful update response.
  807. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::NOERROR());
  808. // Run replacingFwdAddrsHandler again to process the response.
  809. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  810. // Forward change completion should be true, reverse flag should be false.
  811. EXPECT_TRUE(name_add->getForwardChangeCompleted());
  812. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  813. // Since the request also includes a reverse change we should
  814. // be poised to start it. Verify that we transitioned correctly.
  815. EXPECT_EQ(NameChangeTransaction::SELECTING_REV_SERVER_ST,
  816. name_add->getCurrState());
  817. EXPECT_EQ(NameChangeTransaction::SELECT_SERVER_EVT,
  818. name_add->getNextEvent());
  819. }
  820. // Tests replacingFwdAddrsHandler with the following scenario:
  821. //
  822. // The request includes a forward and reverse change.
  823. // Initial posted event is FQDN_IN_USE_EVT.
  824. // The update request is sent without error.
  825. // A server response is received which indicates the FQDN is NOT in use.
  826. //
  827. TEST_F(NameAddTransactionTest, replacingFwdAddrsHandler_FqdnNotInUse) {
  828. NameAddStubPtr name_add;
  829. // Create and prep a transaction, poised to run the handler.
  830. ASSERT_NO_THROW(name_add =
  831. prepHandlerTest(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  832. NameAddTransaction::
  833. FQDN_IN_USE_EVT, FWD_AND_REV_CHG));
  834. // Run replacingFwdAddrsHandler to construct and send the request.
  835. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  836. // Simulate receiving a FQDN not in use response.
  837. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::NXDOMAIN());
  838. // Run replacingFwdAddrsHandler again to process the response.
  839. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  840. // Completion flags should still be false.
  841. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  842. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  843. // Since the FQDN is no longer in use, per the RFC, try to add it.
  844. // Verify that we transitioned correctly.
  845. EXPECT_EQ(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  846. name_add->getCurrState());
  847. EXPECT_EQ(NameChangeTransaction::SERVER_SELECTED_EVT,
  848. name_add->getNextEvent());
  849. }
  850. // Tests replacingFwdAddrsHandler with the following scenario:
  851. //
  852. // The request includes a forward and reverse change.
  853. // The update request is sent without error.
  854. // A server response is received which indicates the update was rejected.
  855. //
  856. TEST_F(NameAddTransactionTest, replacingFwdAddrsHandler_OtherRcode) {
  857. NameAddStubPtr name_add;
  858. // Create the transaction.
  859. ASSERT_NO_THROW(name_add =
  860. prepHandlerTest(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  861. NameAddTransaction::
  862. FQDN_IN_USE_EVT, FWD_AND_REV_CHG));
  863. // Select a server to satisfy log statements.
  864. ASSERT_TRUE(name_add->selectFwdServer());
  865. // Run replacingFwdAddrsHandler to construct and send the request.
  866. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  867. // Simulate receiving server rejection response. Per RFC, anything other
  868. // than no error or FQDN in use is failure. Arbitrarily choosing refused.
  869. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::REFUSED());
  870. // Run replacingFwdAddrsHandler again to process the response.
  871. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  872. // Completion flags should still be false.
  873. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  874. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  875. // We should have failed the transaction. Verify that we transitioned
  876. // correctly.
  877. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  878. name_add->getCurrState());
  879. EXPECT_EQ(NameChangeTransaction::UPDATE_FAILED_EVT,
  880. name_add->getNextEvent());
  881. }
  882. // Tests replacingFwdAddrsHandler with the following scenario:
  883. //
  884. // The request includes a forward and reverse change.
  885. // Initial posted event is FQDN_IN_USE_EVT.
  886. // The update request send times out MAX_UPDATE_TRIES_PER_SERVER times.
  887. //
  888. TEST_F(NameAddTransactionTest, replacingFwdAddrsHandler_Timeout) {
  889. NameAddStubPtr name_add;
  890. // Create the transaction.
  891. ASSERT_NO_THROW(name_add =
  892. prepHandlerTest(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  893. NameAddTransaction::
  894. FQDN_IN_USE_EVT, FWD_AND_REV_CHG));
  895. // Select a server to satisfy log statements.
  896. ASSERT_TRUE(name_add->selectFwdServer());
  897. // Verify that we can make maximum number of update attempts permitted
  898. // and then transition to selecting a new server.
  899. int max_tries = NameChangeTransaction::MAX_UPDATE_TRIES_PER_SERVER;
  900. for (int i = 1; i <= max_tries; ++i) {
  901. const D2UpdateMessagePtr prev_msg = name_add->getDnsUpdateRequest();
  902. // Run replacingFwdAddrsHandler to send the request.
  903. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  904. const D2UpdateMessagePtr curr_msg = name_add->getDnsUpdateRequest();
  905. if (i == 1) {
  906. // First time out we should build the message.
  907. EXPECT_FALSE(prev_msg);
  908. EXPECT_TRUE(curr_msg);
  909. } else {
  910. // Subsequent passes should reuse the request. We are only
  911. // looking to check that we have not replaced the pointer value
  912. // with a new pointer. This tests the on_entry() logic which
  913. // clears the request ONLY upon initial entry into the state.
  914. EXPECT_TRUE(prev_msg == curr_msg);
  915. }
  916. // Simulate a server IO timeout.
  917. name_add->setDnsUpdateStatus(DNSClient::TIMEOUT);
  918. name_add->postNextEvent(NameChangeTransaction::IO_COMPLETED_EVT);
  919. // Run replacingFwdAddrsHandler again to process the response.
  920. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  921. // Completion flags should be false.
  922. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  923. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  924. if (i < max_tries) {
  925. // We should be ready to try again.
  926. EXPECT_EQ(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  927. name_add->getCurrState());
  928. EXPECT_EQ(NameChangeTransaction::SERVER_SELECTED_EVT,
  929. name_add->getNextEvent());
  930. } else {
  931. // Server retries should be exhausted, time for a new server.
  932. EXPECT_EQ(NameAddTransaction::SELECTING_FWD_SERVER_ST,
  933. name_add->getCurrState());
  934. EXPECT_EQ(NameChangeTransaction::SERVER_IO_ERROR_EVT,
  935. name_add->getNextEvent());
  936. }
  937. }
  938. }
  939. // Tests replacingFwdAddrsHandler with the following scenario:
  940. //
  941. // The request includes a forward and reverse change.
  942. // Initial posted event is FQDN_IN_USE_EVT.
  943. // The update request is sent but a corrupt response is received, this occurs
  944. // MAX_UPDATE_TRIES_PER_SERVER times.
  945. //
  946. TEST_F(NameAddTransactionTest, replacingFwdAddrsHandler_CorruptResponse) {
  947. NameAddStubPtr name_add;
  948. // Create the transaction.
  949. ASSERT_NO_THROW(name_add =
  950. prepHandlerTest(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  951. NameAddTransaction::
  952. FQDN_IN_USE_EVT, FWD_AND_REV_CHG));
  953. // Select a server to satisfy log statements.
  954. ASSERT_TRUE(name_add->selectFwdServer());
  955. // Verify that we can make maximum number of update attempts permitted
  956. // and then transition to selecting a new server.
  957. int max_tries = NameChangeTransaction::MAX_UPDATE_TRIES_PER_SERVER;
  958. for (int i = 1; i <= max_tries; ++i) {
  959. const D2UpdateMessagePtr prev_msg = name_add->getDnsUpdateRequest();
  960. // Run replacingFwdAddrsHandler to send the request.
  961. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  962. const D2UpdateMessagePtr curr_msg = name_add->getDnsUpdateRequest();
  963. if (i == 1) {
  964. // First time out we should build the message.
  965. EXPECT_FALSE(prev_msg);
  966. EXPECT_TRUE(curr_msg);
  967. } else {
  968. // Subsequent passes should reuse the request. We are only
  969. // looking to check that we have not replaced the pointer value
  970. // with a new pointer. This tests the on_entry() logic which
  971. // clears the request ONLY upon initial entry into the state.
  972. EXPECT_TRUE(prev_msg == curr_msg);
  973. }
  974. // Simulate a corrupt server response.
  975. name_add->setDnsUpdateStatus(DNSClient::INVALID_RESPONSE);
  976. name_add->postNextEvent(NameChangeTransaction::IO_COMPLETED_EVT);
  977. // Run replacingFwdAddrsHandler again to process the response.
  978. EXPECT_NO_THROW(name_add->replacingFwdAddrsHandler());
  979. // Completion flags should be false.
  980. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  981. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  982. if (i < max_tries) {
  983. // We should be ready to try again.
  984. EXPECT_EQ(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  985. name_add->getCurrState());
  986. EXPECT_EQ(NameChangeTransaction::SERVER_SELECTED_EVT,
  987. name_add->getNextEvent());
  988. } else {
  989. // Server retries should be exhausted, time for a new server.
  990. EXPECT_EQ(NameAddTransaction::SELECTING_FWD_SERVER_ST,
  991. name_add->getCurrState());
  992. EXPECT_EQ(NameChangeTransaction::SERVER_IO_ERROR_EVT,
  993. name_add->getNextEvent());
  994. }
  995. }
  996. }
  997. // Tests the selectingRevServerHandler functionality.
  998. // It verifies behavior for the following scenarios:
  999. //
  1000. // 1. Posted event is SELECT_SERVER_EVT
  1001. // 2. Posted event is SERVER_IO_ERROR_EVT
  1002. // 3. Posted event is invalid
  1003. //
  1004. TEST_F(NameAddTransactionTest, selectingRevServerHandler) {
  1005. NameAddStubPtr name_add;
  1006. // Create and prep a transaction, poised to run the handler.
  1007. ASSERT_NO_THROW(name_add =
  1008. prepHandlerTest(NameChangeTransaction::
  1009. SELECTING_REV_SERVER_ST,
  1010. NameChangeTransaction::SELECT_SERVER_EVT));
  1011. // Call selectingRevServerHandler enough times to select all of the
  1012. // servers in it's current domain. The first time, it will be with
  1013. // next event of SELECT_SERVER_EVT. Thereafter it will be with a next
  1014. // event of SERVER_IO_ERROR_EVT.
  1015. int num_servers = name_add->getReverseDomain()->getServers()->size();
  1016. for (int i = 0; i < num_servers; ++i) {
  1017. // Run selectingRevServerHandler.
  1018. ASSERT_NO_THROW(name_add->selectingRevServerHandler())
  1019. << " num_servers: " << num_servers
  1020. << " selections: " << i;
  1021. // Verify that a server was selected.
  1022. ASSERT_TRUE(name_add->getCurrentServer())
  1023. << " num_servers: " << num_servers
  1024. << " selections: " << i;
  1025. // Verify that we transitioned correctly.
  1026. ASSERT_EQ(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1027. name_add->getCurrState())
  1028. << " num_servers: " << num_servers << " selections: " << i;
  1029. ASSERT_EQ(NameChangeTransaction::SERVER_SELECTED_EVT,
  1030. name_add->getNextEvent())
  1031. << " num_servers: " << num_servers << " selections: " << i;
  1032. // Post a server IO error event. This simulates an IO error occuring
  1033. // and a need to select the new server.
  1034. ASSERT_NO_THROW(name_add->postNextEvent(NameChangeTransaction::
  1035. SERVER_IO_ERROR_EVT))
  1036. << " num_servers: " << num_servers
  1037. << " selections: " << i;
  1038. }
  1039. // We should have exhausted the list of servers. Processing another
  1040. // SERVER_IO_ERROR_EVT should transition us to failure.
  1041. EXPECT_NO_THROW(name_add->selectingRevServerHandler());
  1042. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  1043. name_add->getCurrState());
  1044. EXPECT_EQ(NameChangeTransaction::NO_MORE_SERVERS_EVT,
  1045. name_add->getNextEvent());
  1046. // Create and prep transaction, poised to run the handler but with an
  1047. // invalid event.
  1048. ASSERT_NO_THROW(name_add =
  1049. prepHandlerTest(NameChangeTransaction::
  1050. SELECTING_REV_SERVER_ST,
  1051. StateModel::NOP_EVT));
  1052. // Running the handler should throw.
  1053. EXPECT_THROW(name_add->selectingRevServerHandler(),
  1054. NameAddTransactionError);
  1055. }
  1056. //************************** replacingRevPtrsHandler tests *****************
  1057. // Tests that replacingRevPtrsHandler rejects invalid events.
  1058. TEST_F(NameAddTransactionTest, replacingRevPtrsHandler_InvalidEvent) {
  1059. NameAddStubPtr name_add;
  1060. // Create and prep a transaction, poised to run the handler but with
  1061. // an invalid event.
  1062. ASSERT_NO_THROW(name_add =
  1063. prepHandlerTest(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1064. NameChangeTransaction::
  1065. StateModel::NOP_EVT));
  1066. // Running the handler should throw.
  1067. EXPECT_THROW(name_add->replacingRevPtrsHandler(),
  1068. NameAddTransactionError);
  1069. }
  1070. // Tests replacingRevPtrsHandler with the following scenario:
  1071. //
  1072. // The request includes only a reverse change.
  1073. // Initial posted event is SERVER_SELECTED_EVT.
  1074. // The update request is sent without error.
  1075. // A server response is received which indicates successful update.
  1076. //
  1077. TEST_F(NameAddTransactionTest, replacingRevPtrsHandler_FwdOnlyAddOK) {
  1078. NameAddStubPtr name_add;
  1079. // Create and prep a transaction, poised to run the handler.
  1080. ASSERT_NO_THROW(name_add =
  1081. prepHandlerTest(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1082. NameAddTransaction::
  1083. SERVER_SELECTED_EVT, REVERSE_CHG));
  1084. // Should not be an update message yet.
  1085. D2UpdateMessagePtr update_msg = name_add->getDnsUpdateRequest();
  1086. ASSERT_FALSE(update_msg);
  1087. // At this point completion flags should be false.
  1088. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1089. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  1090. // Run replacingRevPtrsHandler to construct and send the request.
  1091. EXPECT_NO_THROW(name_add->replacingRevPtrsHandler());
  1092. // Verify that an update message was constructed properly.
  1093. checkReplaceRevPtrsRequest(*name_add);
  1094. // Verify that we are still in this state and next event is NOP_EVT.
  1095. // This indicates we "sent" the message and are waiting for IO completion.
  1096. EXPECT_EQ(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1097. name_add->getCurrState());
  1098. EXPECT_EQ(NameChangeTransaction::NOP_EVT,
  1099. name_add->getNextEvent());
  1100. // Simulate receiving a successful update response.
  1101. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::NOERROR());
  1102. // Run replacingRevPtrsHandler again to process the response.
  1103. EXPECT_NO_THROW(name_add->replacingRevPtrsHandler());
  1104. // Forward completion should be false, reverse should be true.
  1105. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1106. EXPECT_TRUE(name_add->getReverseChangeCompleted());
  1107. // Since it is a reverse change, we should be done.
  1108. // Verify that we transitioned correctly.
  1109. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_OK_ST,
  1110. name_add->getCurrState());
  1111. EXPECT_EQ(NameChangeTransaction::UPDATE_OK_EVT,
  1112. name_add->getNextEvent());
  1113. }
  1114. // Tests replacingRevPtrsHandler with the following scenario:
  1115. //
  1116. // The request includes only a reverse change.
  1117. // Initial posted event is SERVER_SELECTED_EVT.
  1118. // The update request is sent without error.
  1119. // A server response is received which indicates the update was rejected.
  1120. //
  1121. TEST_F(NameAddTransactionTest, replacingRevPtrsHandler_OtherRcode) {
  1122. NameAddStubPtr name_add;
  1123. // Create the transaction.
  1124. ASSERT_NO_THROW(name_add =
  1125. prepHandlerTest(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1126. NameAddTransaction::
  1127. SERVER_SELECTED_EVT, REVERSE_CHG));
  1128. // Select a server to satisfy log statements.
  1129. ASSERT_TRUE(name_add->selectRevServer());
  1130. // Run replacingRevPtrsHandler to construct and send the request.
  1131. EXPECT_NO_THROW(name_add->replacingRevPtrsHandler());
  1132. // Simulate receiving server rejection response. Per RFC, anything other
  1133. // than no error is failure. Arbitrarily choosing refused.
  1134. name_add->fakeResponse(DNSClient::SUCCESS, dns::Rcode::REFUSED());
  1135. // Run replacingRevPtrsHandler again to process the response.
  1136. EXPECT_NO_THROW(name_add->replacingRevPtrsHandler());
  1137. // Completion flags should still be false.
  1138. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1139. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  1140. // We should have failed the transaction. Verify that we transitioned
  1141. // correctly.
  1142. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  1143. name_add->getCurrState());
  1144. EXPECT_EQ(NameChangeTransaction::UPDATE_FAILED_EVT,
  1145. name_add->getNextEvent());
  1146. }
  1147. // Tests replacingRevPtrsHandler with the following scenario:
  1148. //
  1149. // The request includes only a reverse change.
  1150. // Initial posted event is SERVER_SELECTED_EVT.
  1151. // The update request send times out MAX_UPDATE_TRIES_PER_SERVER times.
  1152. //
  1153. TEST_F(NameAddTransactionTest, replacingRevPtrsHandler_Timeout) {
  1154. NameAddStubPtr name_add;
  1155. // Create the transaction.
  1156. ASSERT_NO_THROW(name_add =
  1157. prepHandlerTest(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1158. NameAddTransaction::
  1159. SERVER_SELECTED_EVT, REVERSE_CHG));
  1160. // Select a server to satisfy log statements.
  1161. ASSERT_TRUE(name_add->selectRevServer());
  1162. // Verify that we can make maximum number of update attempts permitted
  1163. // and then transition to selecting a new server.
  1164. int max_tries = NameChangeTransaction::MAX_UPDATE_TRIES_PER_SERVER;
  1165. for (int i = 1; i <= max_tries; ++i) {
  1166. const D2UpdateMessagePtr prev_msg = name_add->getDnsUpdateRequest();
  1167. // Run replacingRevPtrsHandler to send the request.
  1168. EXPECT_NO_THROW(name_add->replacingRevPtrsHandler());
  1169. const D2UpdateMessagePtr curr_msg = name_add->getDnsUpdateRequest();
  1170. if (i == 1) {
  1171. // First time out we should build the message.
  1172. EXPECT_FALSE(prev_msg);
  1173. EXPECT_TRUE(curr_msg);
  1174. } else {
  1175. // Subsequent passes should reuse the request. We are only
  1176. // looking to check that we have not replaced the pointer value
  1177. // with a new pointer. This tests the on_entry() logic which
  1178. // clears the request ONLY upon initial entry into the state.
  1179. EXPECT_TRUE(prev_msg == curr_msg);
  1180. }
  1181. // Simulate a server IO timeout.
  1182. name_add->setDnsUpdateStatus(DNSClient::TIMEOUT);
  1183. name_add->postNextEvent(NameChangeTransaction::IO_COMPLETED_EVT);
  1184. // Run replacingRevPtrsHandler again to process the response.
  1185. EXPECT_NO_THROW(name_add->replacingRevPtrsHandler());
  1186. // Completion flags should be false.
  1187. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1188. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  1189. if (i < max_tries) {
  1190. // We should be ready to try again.
  1191. EXPECT_EQ(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1192. name_add->getCurrState());
  1193. EXPECT_EQ(NameChangeTransaction::SERVER_SELECTED_EVT,
  1194. name_add->getNextEvent());
  1195. } else {
  1196. // Server retries should be exhausted, time for a new server.
  1197. EXPECT_EQ(NameAddTransaction::SELECTING_REV_SERVER_ST,
  1198. name_add->getCurrState());
  1199. EXPECT_EQ(NameChangeTransaction::SERVER_IO_ERROR_EVT,
  1200. name_add->getNextEvent());
  1201. }
  1202. }
  1203. }
  1204. // Tests replacingRevPtrsHandler with the following scenario:
  1205. //
  1206. // The request includes only a reverse change.
  1207. // Initial posted event is SERVER_SELECTED_EVT.
  1208. // The update request is sent but a corrupt response is received, this occurs
  1209. // MAX_UPDATE_TRIES_PER_SERVER times.
  1210. //
  1211. TEST_F(NameAddTransactionTest, replacingRevPtrsHandler_CorruptResponse) {
  1212. NameAddStubPtr name_add;
  1213. // Create the transaction.
  1214. ASSERT_NO_THROW(name_add =
  1215. prepHandlerTest(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1216. NameAddTransaction::
  1217. SERVER_SELECTED_EVT, REVERSE_CHG));
  1218. // Select a server to satisfy log statements.
  1219. ASSERT_TRUE(name_add->selectRevServer());
  1220. // Verify that we can make maximum number of update attempts permitted
  1221. // and then transition to selecting a new server.
  1222. int max_tries = NameChangeTransaction::MAX_UPDATE_TRIES_PER_SERVER;
  1223. for (int i = 1; i <= max_tries; ++i) {
  1224. const D2UpdateMessagePtr prev_msg = name_add->getDnsUpdateRequest();
  1225. // Run replacingRevPtrsHandler to send the request.
  1226. EXPECT_NO_THROW(name_add->replacingRevPtrsHandler());
  1227. const D2UpdateMessagePtr curr_msg = name_add->getDnsUpdateRequest();
  1228. if (i == 1) {
  1229. // First time out we should build the message.
  1230. EXPECT_FALSE(prev_msg);
  1231. EXPECT_TRUE(curr_msg);
  1232. } else {
  1233. // Subsequent passes should reuse the request. We are only
  1234. // looking to check that we have not replaced the pointer value
  1235. // with a new pointer. This tests the on_entry() logic which
  1236. // clears the request ONLY upon initial entry into the state.
  1237. EXPECT_TRUE(prev_msg == curr_msg);
  1238. }
  1239. // Simulate a server corrupt response.
  1240. name_add->setDnsUpdateStatus(DNSClient::INVALID_RESPONSE);
  1241. name_add->postNextEvent(NameChangeTransaction::IO_COMPLETED_EVT);
  1242. // Run replacingRevPtrsHandler again to process the response.
  1243. EXPECT_NO_THROW(name_add->replacingRevPtrsHandler());
  1244. // Completion flags should be false.
  1245. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1246. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  1247. if (i < max_tries) {
  1248. // We should be ready to try again.
  1249. EXPECT_EQ(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1250. name_add->getCurrState());
  1251. EXPECT_EQ(NameChangeTransaction::SERVER_SELECTED_EVT,
  1252. name_add->getNextEvent());
  1253. } else {
  1254. // Server retries should be exhausted, time for a new server.
  1255. EXPECT_EQ(NameAddTransaction::SELECTING_REV_SERVER_ST,
  1256. name_add->getCurrState());
  1257. EXPECT_EQ(NameChangeTransaction::SERVER_IO_ERROR_EVT,
  1258. name_add->getNextEvent());
  1259. }
  1260. }
  1261. }
  1262. // Tests the processAddOkHandler functionality.
  1263. // It verifies behavior for the following scenarios:
  1264. //
  1265. // 1. Posted event is UPDATE_OK_EVT
  1266. // 2. Posted event is invalid
  1267. //
  1268. TEST_F(NameAddTransactionTest, processAddOkHandler) {
  1269. NameAddStubPtr name_add;
  1270. // Create and prep a transaction, poised to run the handler.
  1271. ASSERT_NO_THROW(name_add =
  1272. prepHandlerTest(NameChangeTransaction::PROCESS_TRANS_OK_ST,
  1273. NameChangeTransaction::UPDATE_OK_EVT));
  1274. // Run processAddOkHandler.
  1275. EXPECT_NO_THROW(name_add->processAddOkHandler());
  1276. // Verify that a server was selected.
  1277. EXPECT_EQ(dhcp_ddns::ST_COMPLETED, name_add->getNcrStatus());
  1278. // Verify that the model has ended.
  1279. EXPECT_EQ(StateModel::END_ST, name_add->getCurrState());
  1280. EXPECT_EQ(StateModel::END_EVT, name_add->getNextEvent());
  1281. // Create and prep transaction, poised to run the handler but with an
  1282. // invalid event.
  1283. ASSERT_NO_THROW(name_add =
  1284. prepHandlerTest(NameChangeTransaction::PROCESS_TRANS_OK_ST,
  1285. StateModel::NOP_EVT));
  1286. // Running the handler should throw.
  1287. EXPECT_THROW(name_add->processAddOkHandler(), NameAddTransactionError);
  1288. }
  1289. // Tests the processAddFailedHandler functionality.
  1290. // It verifies behavior for the following scenarios:
  1291. //
  1292. // 1. Posted event is UPDATE_FAILED_EVT
  1293. // 2. Posted event is invalid
  1294. //
  1295. TEST_F(NameAddTransactionTest, processAddFailedHandler) {
  1296. NameAddStubPtr name_add;
  1297. // Create and prep a transaction, poised to run the handler.
  1298. ASSERT_NO_THROW(name_add =
  1299. prepHandlerTest(NameChangeTransaction::
  1300. PROCESS_TRANS_FAILED_ST,
  1301. NameChangeTransaction::UPDATE_FAILED_EVT));
  1302. // Run processAddFailedHandler.
  1303. EXPECT_NO_THROW(name_add->processAddFailedHandler());
  1304. // Verify that a server was selected.
  1305. EXPECT_EQ(dhcp_ddns::ST_FAILED, name_add->getNcrStatus());
  1306. // Verify that the model has ended. (Remember, the transaction failed NOT
  1307. // the model. The model should have ended normally.)
  1308. EXPECT_EQ(StateModel::END_ST, name_add->getCurrState());
  1309. EXPECT_EQ(StateModel::END_EVT, name_add->getNextEvent());
  1310. // Create and prep transaction, poised to run the handler but with an
  1311. // invalid event.
  1312. ASSERT_NO_THROW(name_add =
  1313. prepHandlerTest(NameChangeTransaction::
  1314. PROCESS_TRANS_FAILED_ST,
  1315. StateModel::NOP_EVT));
  1316. // Running the handler should throw.
  1317. EXPECT_THROW(name_add->processAddFailedHandler(), NameAddTransactionError);
  1318. }
  1319. // Tests the processAddFailedHandler functionality.
  1320. // It verifies behavior for posted event of NO_MORE_SERVERS_EVT.
  1321. TEST_F(NameAddTransactionTest, processAddFailedHandler_NoMoreServers) {
  1322. NameAddStubPtr name_remove;
  1323. // Create and prep a transaction, poised to run the handler.
  1324. ASSERT_NO_THROW(name_remove =
  1325. prepHandlerTest(NameChangeTransaction::
  1326. PROCESS_TRANS_FAILED_ST,
  1327. NameChangeTransaction::
  1328. NO_MORE_SERVERS_EVT));
  1329. // Run processAddFailedHandler.
  1330. EXPECT_NO_THROW(name_remove->processAddFailedHandler());
  1331. // Verify that a server was selected.
  1332. EXPECT_EQ(dhcp_ddns::ST_FAILED, name_remove->getNcrStatus());
  1333. // Verify that the model has ended. (Remember, the transaction failed NOT
  1334. // the model. The model should have ended normally.)
  1335. EXPECT_EQ(StateModel::END_ST, name_remove->getCurrState());
  1336. EXPECT_EQ(StateModel::END_EVT, name_remove->getNextEvent());
  1337. }
  1338. // Tests addingFwdAddrsHandler with the following scenario:
  1339. //
  1340. // The request includes only a forward change.
  1341. // Initial posted event is SERVER_SELECTED_EVT.
  1342. // The send update request fails due to an unexpected exception.
  1343. //
  1344. TEST_F(NameAddTransactionTest, addingFwdAddrsHandler_sendUpdateException) {
  1345. NameAddStubPtr name_add;
  1346. // Create and prep a transaction, poised to run the handler.
  1347. ASSERT_NO_THROW(name_add =
  1348. prepHandlerTest(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  1349. NameChangeTransaction::
  1350. SERVER_SELECTED_EVT, FORWARD_CHG));
  1351. name_add->simulate_send_exception_ = true;
  1352. // Run replacingFwdAddrsHandler to construct and send the request.
  1353. ASSERT_NO_THROW(name_add->addingFwdAddrsHandler());
  1354. // Completion flags should be false.
  1355. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1356. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  1357. // Since IO exceptions should be gracefully handled, any that occur
  1358. // are unanticipated, and deemed unrecoverable, so the transaction should
  1359. // be transitioned to failure.
  1360. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  1361. name_add->getCurrState());
  1362. EXPECT_EQ(NameChangeTransaction::UPDATE_FAILED_EVT,
  1363. name_add->getNextEvent());
  1364. }
  1365. // Tests replacingFwdAddrsHandler with the following scenario:
  1366. //
  1367. // The request includes only a forward change.
  1368. // Initial posted event is SERVER_SELECTED_EVT.
  1369. // The send update request fails due to an unexpected exception.
  1370. //
  1371. TEST_F(NameAddTransactionTest, replacingFwdAddrsHandler_SendUpdateException) {
  1372. NameAddStubPtr name_add;
  1373. // Create and prep a transaction, poised to run the handler.
  1374. ASSERT_NO_THROW(name_add =
  1375. prepHandlerTest(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  1376. NameChangeTransaction::
  1377. SERVER_SELECTED_EVT, FORWARD_CHG));
  1378. name_add->simulate_send_exception_ = true;
  1379. // Run replacingFwdAddrsHandler to construct and send the request.
  1380. ASSERT_NO_THROW(name_add->replacingFwdAddrsHandler());
  1381. // Completion flags should be false.
  1382. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1383. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  1384. // Since IO exceptions should be gracefully handled, any that occur
  1385. // are unanticipated, and deemed unrecoverable, so the transaction should
  1386. // be transitioned to failure.
  1387. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  1388. name_add->getCurrState());
  1389. EXPECT_EQ(NameChangeTransaction::UPDATE_FAILED_EVT,
  1390. name_add->getNextEvent());
  1391. }
  1392. // Tests replacingRevPtrHandler with the following scenario:
  1393. //
  1394. // The request includes only a reverse change.
  1395. // Initial posted event is SERVER_SELECTED_EVT.
  1396. // The send update request fails due to an unexpected exception.
  1397. //
  1398. TEST_F(NameAddTransactionTest, replacingRevPtrsHandler_SendUpdateException) {
  1399. NameAddStubPtr name_add;
  1400. // Create and prep a transaction, poised to run the handler.
  1401. ASSERT_NO_THROW(name_add =
  1402. prepHandlerTest(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1403. NameChangeTransaction::
  1404. SERVER_SELECTED_EVT, REVERSE_CHG));
  1405. name_add->simulate_send_exception_ = true;
  1406. // Run replacingRevPtrsHandler to construct and send the request.
  1407. ASSERT_NO_THROW(name_add->replacingRevPtrsHandler());
  1408. // Completion flags should be false.
  1409. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1410. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  1411. // Since IO exceptions should be gracefully handled, any that occur
  1412. // are unanticipated, and deemed unrecoverable, so the transaction should
  1413. // be transitioned to failure.
  1414. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  1415. name_add->getCurrState());
  1416. EXPECT_EQ(NameChangeTransaction::UPDATE_FAILED_EVT,
  1417. name_add->getNextEvent());
  1418. }
  1419. // Tests addingFwdAddrsHandler with the following scenario:
  1420. //
  1421. // The request includes only a forward change.
  1422. // Initial posted event is SERVER_SELECTED_EVT.
  1423. // The request build fails due to an unexpected exception.
  1424. //
  1425. TEST_F(NameAddTransactionTest, addingFwdAddrsHandler_BuildRequestException) {
  1426. NameAddStubPtr name_add;
  1427. // Create and prep a transaction, poised to run the handler.
  1428. ASSERT_NO_THROW(name_add =
  1429. prepHandlerTest(NameAddTransaction::ADDING_FWD_ADDRS_ST,
  1430. NameChangeTransaction::
  1431. SERVER_SELECTED_EVT, FORWARD_CHG));
  1432. // Set the one-shot exception simulation flag.
  1433. name_add->simulate_build_request_exception_ = true;
  1434. // Run replacingRevPtrsHandler to construct and send the request.
  1435. // This should fail with a build request throw which should be caught
  1436. // in the state handler.
  1437. ASSERT_NO_THROW(name_add->addingFwdAddrsHandler());
  1438. // Verify we did not attempt to send anything.
  1439. EXPECT_EQ(0, name_add->getUpdateAttempts());
  1440. // Completion flags should be false.
  1441. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1442. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  1443. // Since IO exceptions should be gracefully handled, any that occur
  1444. // are unanticipated, and deemed unrecoverable, so the transaction should
  1445. // be transitioned to failure.
  1446. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  1447. name_add->getCurrState());
  1448. EXPECT_EQ(NameChangeTransaction::UPDATE_FAILED_EVT,
  1449. name_add->getNextEvent());
  1450. }
  1451. // Tests replacingFwdAddrsHandler with the following scenario:
  1452. //
  1453. // The request includes only a forward change.
  1454. // Initial posted event is SERVER_SELECTED_EVT.
  1455. // The request build fails due to an unexpected exception.
  1456. //
  1457. TEST_F(NameAddTransactionTest, replacingFwdAddrsHandler_BuildRequestException) {
  1458. NameAddStubPtr name_add;
  1459. // Create and prep a transaction, poised to run the handler.
  1460. ASSERT_NO_THROW(name_add =
  1461. prepHandlerTest(NameAddTransaction::REPLACING_FWD_ADDRS_ST,
  1462. NameChangeTransaction::
  1463. SERVER_SELECTED_EVT, FORWARD_CHG));
  1464. // Set the one-shot exception simulation flag.
  1465. name_add->simulate_build_request_exception_ = true;
  1466. // Run replacingFwdAddrsHandler to construct and send the request.
  1467. // This should fail with a build request throw which should be caught
  1468. // in the state handler.
  1469. ASSERT_NO_THROW(name_add->replacingFwdAddrsHandler());
  1470. // Verify we did not attempt to send anything.
  1471. EXPECT_EQ(0, name_add->getUpdateAttempts());
  1472. // Completion flags should be false.
  1473. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1474. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  1475. // Since IO exceptions should be gracefully handled, any that occur
  1476. // are unanticipated, and deemed unrecoverable, so the transaction should
  1477. // be transitioned to failure.
  1478. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  1479. name_add->getCurrState());
  1480. EXPECT_EQ(NameChangeTransaction::UPDATE_FAILED_EVT,
  1481. name_add->getNextEvent());
  1482. }
  1483. // Tests replacingRevPtrHandler with the following scenario:
  1484. //
  1485. // The request includes only a reverse change.
  1486. // Initial posted event is SERVER_SELECTED_EVT.
  1487. // The request build fails due to an unexpected exception.
  1488. //
  1489. TEST_F(NameAddTransactionTest, replacingRevPtrsHandler_BuildRequestException) {
  1490. NameAddStubPtr name_add;
  1491. // Create and prep a transaction, poised to run the handler.
  1492. ASSERT_NO_THROW(name_add =
  1493. prepHandlerTest(NameAddTransaction::REPLACING_REV_PTRS_ST,
  1494. NameChangeTransaction::
  1495. SERVER_SELECTED_EVT, REVERSE_CHG));
  1496. // Set the one-shot exception simulation flag.
  1497. name_add->simulate_build_request_exception_ = true;
  1498. // Run replacingRevPtrsHandler to construct and send the request.
  1499. // This should fail with a build request throw which should be caught
  1500. // in the state handler.
  1501. ASSERT_NO_THROW(name_add->replacingRevPtrsHandler());
  1502. // Verify we did not attempt to send anything.
  1503. EXPECT_EQ(0, name_add->getUpdateAttempts());
  1504. // Completion flags should be false.
  1505. EXPECT_FALSE(name_add->getForwardChangeCompleted());
  1506. EXPECT_FALSE(name_add->getReverseChangeCompleted());
  1507. // Since IO exceptions should be gracefully handled, any that occur
  1508. // are unanticipated, and deemed unrecoverable, so the transaction should
  1509. // be transitioned to failure.
  1510. EXPECT_EQ(NameChangeTransaction::PROCESS_TRANS_FAILED_ST,
  1511. name_add->getCurrState());
  1512. EXPECT_EQ(NameChangeTransaction::UPDATE_FAILED_EVT,
  1513. name_add->getNextEvent());
  1514. }
  1515. }