ares-test-mock.cc 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  1. #include "ares-test.h"
  2. #include "dns-proto.h"
  3. #ifndef WIN32
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #endif
  7. #include <sstream>
  8. #include <vector>
  9. using testing::InvokeWithoutArgs;
  10. using testing::DoAll;
  11. namespace ares {
  12. namespace test {
  13. TEST_P(MockChannelTest, Basic) {
  14. std::vector<byte> reply = {
  15. 0x00, 0x00, // qid
  16. 0x84, // response + query + AA + not-TC + not-RD
  17. 0x00, // not-RA + not-Z + not-AD + not-CD + rc=NoError
  18. 0x00, 0x01, // 1 question
  19. 0x00, 0x01, // 1 answer RRs
  20. 0x00, 0x00, // 0 authority RRs
  21. 0x00, 0x00, // 0 additional RRs
  22. // Question
  23. 0x03, 'w', 'w', 'w',
  24. 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
  25. 0x03, 'c', 'o', 'm',
  26. 0x00,
  27. 0x00, 0x01, // type A
  28. 0x00, 0x01, // class IN
  29. // Answer
  30. 0x03, 'w', 'w', 'w',
  31. 0x06, 'g', 'o', 'o', 'g', 'l', 'e',
  32. 0x03, 'c', 'o', 'm',
  33. 0x00,
  34. 0x00, 0x01, // type A
  35. 0x00, 0x01, // class IN
  36. 0x00, 0x00, 0x01, 0x00, // TTL
  37. 0x00, 0x04, // rdata length
  38. 0x01, 0x02, 0x03, 0x04
  39. };
  40. ON_CALL(server_, OnRequest("www.google.com", T_A))
  41. .WillByDefault(SetReplyData(&server_, reply));
  42. HostResult result;
  43. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  44. Process();
  45. EXPECT_TRUE(result.done_);
  46. std::stringstream ss;
  47. ss << result.host_;
  48. EXPECT_EQ("{'www.google.com' aliases=[] addrs=[1.2.3.4]}", ss.str());
  49. }
  50. // UDP only so mock server doesn't get confused by concatenated requests
  51. TEST_P(MockUDPChannelTest, GetHostByNameParallelLookups) {
  52. DNSPacket rsp1;
  53. rsp1.set_response().set_aa()
  54. .add_question(new DNSQuestion("www.google.com", T_A))
  55. .add_answer(new DNSARR("www.google.com", 100, {2, 3, 4, 5}));
  56. ON_CALL(server_, OnRequest("www.google.com", T_A))
  57. .WillByDefault(SetReply(&server_, &rsp1));
  58. DNSPacket rsp2;
  59. rsp2.set_response().set_aa()
  60. .add_question(new DNSQuestion("www.example.com", T_A))
  61. .add_answer(new DNSARR("www.example.com", 100, {1, 2, 3, 4}));
  62. ON_CALL(server_, OnRequest("www.example.com", T_A))
  63. .WillByDefault(SetReply(&server_, &rsp2));
  64. HostResult result1;
  65. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result1);
  66. HostResult result2;
  67. ares_gethostbyname(channel_, "www.example.com.", AF_INET, HostCallback, &result2);
  68. HostResult result3;
  69. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result3);
  70. Process();
  71. EXPECT_TRUE(result1.done_);
  72. EXPECT_TRUE(result2.done_);
  73. EXPECT_TRUE(result3.done_);
  74. std::stringstream ss1;
  75. ss1 << result1.host_;
  76. EXPECT_EQ("{'www.google.com' aliases=[] addrs=[2.3.4.5]}", ss1.str());
  77. std::stringstream ss2;
  78. ss2 << result2.host_;
  79. EXPECT_EQ("{'www.example.com' aliases=[] addrs=[1.2.3.4]}", ss2.str());
  80. std::stringstream ss3;
  81. ss3 << result3.host_;
  82. EXPECT_EQ("{'www.google.com' aliases=[] addrs=[2.3.4.5]}", ss3.str());
  83. }
  84. // UDP to TCP specific test
  85. TEST_P(MockUDPChannelTest, TruncationRetry) {
  86. DNSPacket rsptruncated;
  87. rsptruncated.set_response().set_aa().set_tc()
  88. .add_question(new DNSQuestion("www.google.com", T_A));
  89. DNSPacket rspok;
  90. rspok.set_response()
  91. .add_question(new DNSQuestion("www.google.com", T_A))
  92. .add_answer(new DNSARR("www.google.com", 100, {1, 2, 3, 4}));
  93. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  94. .WillOnce(SetReply(&server_, &rsptruncated))
  95. .WillOnce(SetReply(&server_, &rspok));
  96. HostResult result;
  97. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  98. Process();
  99. EXPECT_TRUE(result.done_);
  100. std::stringstream ss;
  101. ss << result.host_;
  102. EXPECT_EQ("{'www.google.com' aliases=[] addrs=[1.2.3.4]}", ss.str());
  103. }
  104. static int sock_cb_count = 0;
  105. static int SocketConnectCallback(ares_socket_t fd, int type, void *data) {
  106. int rc = *(int*)data;
  107. if (verbose) std::cerr << "SocketConnectCallback(" << fd << ") invoked" << std::endl;
  108. sock_cb_count++;
  109. return rc;
  110. }
  111. TEST_P(MockChannelTest, SockCallback) {
  112. DNSPacket rsp;
  113. rsp.set_response().set_aa()
  114. .add_question(new DNSQuestion("www.google.com", T_A))
  115. .add_answer(new DNSARR("www.google.com", 100, {2, 3, 4, 5}));
  116. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  117. .WillOnce(SetReply(&server_, &rsp));
  118. // Get notified of new sockets
  119. int rc = ARES_SUCCESS;
  120. ares_set_socket_callback(channel_, SocketConnectCallback, &rc);
  121. HostResult result;
  122. sock_cb_count = 0;
  123. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  124. Process();
  125. EXPECT_EQ(1, sock_cb_count);
  126. EXPECT_TRUE(result.done_);
  127. std::stringstream ss;
  128. ss << result.host_;
  129. EXPECT_EQ("{'www.google.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
  130. }
  131. TEST_P(MockChannelTest, SockFailCallback) {
  132. // Notification of new sockets gives an error.
  133. int rc = -1;
  134. ares_set_socket_callback(channel_, SocketConnectCallback, &rc);
  135. HostResult result;
  136. sock_cb_count = 0;
  137. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  138. Process();
  139. EXPECT_LT(1, sock_cb_count);
  140. EXPECT_TRUE(result.done_);
  141. EXPECT_EQ(ARES_ECONNREFUSED, result.status_);
  142. }
  143. static int sock_config_cb_count = 0;
  144. static int SocketConfigureCallback(ares_socket_t fd, int type, void *data) {
  145. int rc = *(int*)data;
  146. if (verbose) std::cerr << "SocketConfigureCallback(" << fd << ") invoked" << std::endl;
  147. sock_config_cb_count++;
  148. return rc;
  149. }
  150. TEST_P(MockChannelTest, SockConfigureCallback) {
  151. DNSPacket rsp;
  152. rsp.set_response().set_aa()
  153. .add_question(new DNSQuestion("www.google.com", T_A))
  154. .add_answer(new DNSARR("www.google.com", 100, {2, 3, 4, 5}));
  155. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  156. .WillOnce(SetReply(&server_, &rsp));
  157. // Get notified of new sockets
  158. int rc = ARES_SUCCESS;
  159. ares_set_socket_configure_callback(channel_, SocketConfigureCallback, &rc);
  160. HostResult result;
  161. sock_config_cb_count = 0;
  162. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  163. Process();
  164. EXPECT_EQ(1, sock_config_cb_count);
  165. EXPECT_TRUE(result.done_);
  166. std::stringstream ss;
  167. ss << result.host_;
  168. EXPECT_EQ("{'www.google.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
  169. }
  170. TEST_P(MockChannelTest, SockConfigureFailCallback) {
  171. // Notification of new sockets gives an error.
  172. int rc = -1;
  173. ares_set_socket_configure_callback(channel_, SocketConfigureCallback, &rc);
  174. HostResult result;
  175. sock_config_cb_count = 0;
  176. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  177. Process();
  178. EXPECT_LT(1, sock_config_cb_count);
  179. EXPECT_TRUE(result.done_);
  180. EXPECT_EQ(ARES_ECONNREFUSED, result.status_);
  181. }
  182. // TCP only to prevent retries
  183. TEST_P(MockTCPChannelTest, MalformedResponse) {
  184. std::vector<byte> one = {0x01};
  185. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  186. .WillOnce(SetReplyData(&server_, one));
  187. HostResult result;
  188. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  189. Process();
  190. EXPECT_TRUE(result.done_);
  191. EXPECT_EQ(ARES_ETIMEOUT, result.status_);
  192. }
  193. TEST_P(MockTCPChannelTest, FormErrResponse) {
  194. DNSPacket rsp;
  195. rsp.set_response().set_aa()
  196. .add_question(new DNSQuestion("www.google.com", T_A));
  197. rsp.set_rcode(FORMERR);
  198. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  199. .WillOnce(SetReply(&server_, &rsp));
  200. HostResult result;
  201. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  202. Process();
  203. EXPECT_TRUE(result.done_);
  204. EXPECT_EQ(ARES_EFORMERR, result.status_);
  205. }
  206. TEST_P(MockTCPChannelTest, ServFailResponse) {
  207. DNSPacket rsp;
  208. rsp.set_response().set_aa()
  209. .add_question(new DNSQuestion("www.google.com", T_A));
  210. rsp.set_rcode(SERVFAIL);
  211. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  212. .WillOnce(SetReply(&server_, &rsp));
  213. HostResult result;
  214. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  215. Process();
  216. EXPECT_TRUE(result.done_);
  217. // ARES_FLAG_NOCHECKRESP not set, so SERVFAIL consumed
  218. EXPECT_EQ(ARES_ECONNREFUSED, result.status_);
  219. }
  220. TEST_P(MockTCPChannelTest, NotImplResponse) {
  221. DNSPacket rsp;
  222. rsp.set_response().set_aa()
  223. .add_question(new DNSQuestion("www.google.com", T_A));
  224. rsp.set_rcode(NOTIMP);
  225. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  226. .WillOnce(SetReply(&server_, &rsp));
  227. HostResult result;
  228. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  229. Process();
  230. EXPECT_TRUE(result.done_);
  231. // ARES_FLAG_NOCHECKRESP not set, so NOTIMP consumed
  232. EXPECT_EQ(ARES_ECONNREFUSED, result.status_);
  233. }
  234. TEST_P(MockTCPChannelTest, RefusedResponse) {
  235. DNSPacket rsp;
  236. rsp.set_response().set_aa()
  237. .add_question(new DNSQuestion("www.google.com", T_A));
  238. rsp.set_rcode(REFUSED);
  239. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  240. .WillOnce(SetReply(&server_, &rsp));
  241. HostResult result;
  242. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  243. Process();
  244. EXPECT_TRUE(result.done_);
  245. // ARES_FLAG_NOCHECKRESP not set, so REFUSED consumed
  246. EXPECT_EQ(ARES_ECONNREFUSED, result.status_);
  247. }
  248. TEST_P(MockTCPChannelTest, YXDomainResponse) {
  249. DNSPacket rsp;
  250. rsp.set_response().set_aa()
  251. .add_question(new DNSQuestion("www.google.com", T_A));
  252. rsp.set_rcode(YXDOMAIN);
  253. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  254. .WillOnce(SetReply(&server_, &rsp));
  255. HostResult result;
  256. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  257. Process();
  258. EXPECT_TRUE(result.done_);
  259. EXPECT_EQ(ARES_ENODATA, result.status_);
  260. }
  261. class MockExtraOptsTest
  262. : public MockChannelOptsTest,
  263. public ::testing::WithParamInterface< std::pair<int, bool> > {
  264. public:
  265. MockExtraOptsTest()
  266. : MockChannelOptsTest(1, GetParam().first, GetParam().second,
  267. FillOptions(&opts_),
  268. ARES_OPT_SOCK_SNDBUF|ARES_OPT_SOCK_RCVBUF) {}
  269. static struct ares_options* FillOptions(struct ares_options * opts) {
  270. memset(opts, 0, sizeof(struct ares_options));
  271. // Set a few options that affect socket communications
  272. opts->socket_send_buffer_size = 514;
  273. opts->socket_receive_buffer_size = 514;
  274. return opts;
  275. }
  276. private:
  277. struct ares_options opts_;
  278. };
  279. TEST_P(MockExtraOptsTest, SimpleQuery) {
  280. ares_set_local_ip4(channel_, 0x7F000001);
  281. byte addr6[16] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  282. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
  283. ares_set_local_ip6(channel_, addr6);
  284. ares_set_local_dev(channel_, "dummy");
  285. DNSPacket rsp;
  286. rsp.set_response().set_aa()
  287. .add_question(new DNSQuestion("www.google.com", T_A))
  288. .add_answer(new DNSARR("www.google.com", 100, {2, 3, 4, 5}));
  289. ON_CALL(server_, OnRequest("www.google.com", T_A))
  290. .WillByDefault(SetReply(&server_, &rsp));
  291. HostResult result;
  292. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  293. Process();
  294. EXPECT_TRUE(result.done_);
  295. std::stringstream ss;
  296. ss << result.host_;
  297. EXPECT_EQ("{'www.google.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
  298. }
  299. class MockFlagsChannelOptsTest
  300. : public MockChannelOptsTest,
  301. public ::testing::WithParamInterface< std::pair<int, bool> > {
  302. public:
  303. MockFlagsChannelOptsTest(int flags)
  304. : MockChannelOptsTest(1, GetParam().first, GetParam().second,
  305. FillOptions(&opts_, flags), ARES_OPT_FLAGS) {}
  306. static struct ares_options* FillOptions(struct ares_options * opts, int flags) {
  307. memset(opts, 0, sizeof(struct ares_options));
  308. opts->flags = flags;
  309. return opts;
  310. }
  311. private:
  312. struct ares_options opts_;
  313. };
  314. class MockNoCheckRespChannelTest : public MockFlagsChannelOptsTest {
  315. public:
  316. MockNoCheckRespChannelTest() : MockFlagsChannelOptsTest(ARES_FLAG_NOCHECKRESP) {}
  317. };
  318. TEST_P(MockNoCheckRespChannelTest, ServFailResponse) {
  319. DNSPacket rsp;
  320. rsp.set_response().set_aa()
  321. .add_question(new DNSQuestion("www.google.com", T_A));
  322. rsp.set_rcode(SERVFAIL);
  323. ON_CALL(server_, OnRequest("www.google.com", T_A))
  324. .WillByDefault(SetReply(&server_, &rsp));
  325. HostResult result;
  326. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  327. Process();
  328. EXPECT_TRUE(result.done_);
  329. EXPECT_EQ(ARES_ESERVFAIL, result.status_);
  330. }
  331. TEST_P(MockNoCheckRespChannelTest, NotImplResponse) {
  332. DNSPacket rsp;
  333. rsp.set_response().set_aa()
  334. .add_question(new DNSQuestion("www.google.com", T_A));
  335. rsp.set_rcode(NOTIMP);
  336. ON_CALL(server_, OnRequest("www.google.com", T_A))
  337. .WillByDefault(SetReply(&server_, &rsp));
  338. HostResult result;
  339. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  340. Process();
  341. EXPECT_TRUE(result.done_);
  342. EXPECT_EQ(ARES_ENOTIMP, result.status_);
  343. }
  344. TEST_P(MockNoCheckRespChannelTest, RefusedResponse) {
  345. DNSPacket rsp;
  346. rsp.set_response().set_aa()
  347. .add_question(new DNSQuestion("www.google.com", T_A));
  348. rsp.set_rcode(REFUSED);
  349. ON_CALL(server_, OnRequest("www.google.com", T_A))
  350. .WillByDefault(SetReply(&server_, &rsp));
  351. HostResult result;
  352. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  353. Process();
  354. EXPECT_TRUE(result.done_);
  355. EXPECT_EQ(ARES_EREFUSED, result.status_);
  356. }
  357. class MockEDNSChannelTest : public MockFlagsChannelOptsTest {
  358. public:
  359. MockEDNSChannelTest() : MockFlagsChannelOptsTest(ARES_FLAG_EDNS) {}
  360. };
  361. TEST_P(MockEDNSChannelTest, RetryWithoutEDNS) {
  362. DNSPacket rspfail;
  363. rspfail.set_response().set_aa().set_rcode(FORMERR)
  364. .add_question(new DNSQuestion("www.google.com", T_A));
  365. DNSPacket rspok;
  366. rspok.set_response()
  367. .add_question(new DNSQuestion("www.google.com", T_A))
  368. .add_answer(new DNSARR("www.google.com", 100, {1, 2, 3, 4}));
  369. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  370. .WillOnce(SetReply(&server_, &rspfail))
  371. .WillOnce(SetReply(&server_, &rspok));
  372. HostResult result;
  373. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  374. Process();
  375. EXPECT_TRUE(result.done_);
  376. std::stringstream ss;
  377. ss << result.host_;
  378. EXPECT_EQ("{'www.google.com' aliases=[] addrs=[1.2.3.4]}", ss.str());
  379. }
  380. TEST_P(MockChannelTest, SearchDomains) {
  381. DNSPacket nofirst;
  382. nofirst.set_response().set_aa().set_rcode(NXDOMAIN)
  383. .add_question(new DNSQuestion("www.first.com", T_A));
  384. ON_CALL(server_, OnRequest("www.first.com", T_A))
  385. .WillByDefault(SetReply(&server_, &nofirst));
  386. DNSPacket nosecond;
  387. nosecond.set_response().set_aa().set_rcode(NXDOMAIN)
  388. .add_question(new DNSQuestion("www.second.org", T_A));
  389. ON_CALL(server_, OnRequest("www.second.org", T_A))
  390. .WillByDefault(SetReply(&server_, &nosecond));
  391. DNSPacket yesthird;
  392. yesthird.set_response().set_aa()
  393. .add_question(new DNSQuestion("www.third.gov", T_A))
  394. .add_answer(new DNSARR("www.third.gov", 0x0200, {2, 3, 4, 5}));
  395. ON_CALL(server_, OnRequest("www.third.gov", T_A))
  396. .WillByDefault(SetReply(&server_, &yesthird));
  397. HostResult result;
  398. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  399. Process();
  400. EXPECT_TRUE(result.done_);
  401. std::stringstream ss;
  402. ss << result.host_;
  403. EXPECT_EQ("{'www.third.gov' aliases=[] addrs=[2.3.4.5]}", ss.str());
  404. }
  405. // Relies on retries so is UDP-only
  406. TEST_P(MockUDPChannelTest, SearchDomainsWithResentReply) {
  407. DNSPacket nofirst;
  408. nofirst.set_response().set_aa().set_rcode(NXDOMAIN)
  409. .add_question(new DNSQuestion("www.first.com", T_A));
  410. EXPECT_CALL(server_, OnRequest("www.first.com", T_A))
  411. .WillOnce(SetReply(&server_, &nofirst));
  412. DNSPacket nosecond;
  413. nosecond.set_response().set_aa().set_rcode(NXDOMAIN)
  414. .add_question(new DNSQuestion("www.second.org", T_A));
  415. EXPECT_CALL(server_, OnRequest("www.second.org", T_A))
  416. .WillOnce(SetReply(&server_, &nosecond));
  417. DNSPacket yesthird;
  418. yesthird.set_response().set_aa()
  419. .add_question(new DNSQuestion("www.third.gov", T_A))
  420. .add_answer(new DNSARR("www.third.gov", 0x0200, {2, 3, 4, 5}));
  421. // Before sending the real answer, resend an earlier reply
  422. EXPECT_CALL(server_, OnRequest("www.third.gov", T_A))
  423. .WillOnce(DoAll(SetReply(&server_, &nofirst),
  424. SetReplyQID(&server_, 123)))
  425. .WillOnce(DoAll(SetReply(&server_, &yesthird),
  426. SetReplyQID(&server_, -1)));
  427. HostResult result;
  428. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  429. Process();
  430. EXPECT_TRUE(result.done_);
  431. std::stringstream ss;
  432. ss << result.host_;
  433. EXPECT_EQ("{'www.third.gov' aliases=[] addrs=[2.3.4.5]}", ss.str());
  434. }
  435. TEST_P(MockChannelTest, SearchDomainsBare) {
  436. DNSPacket nofirst;
  437. nofirst.set_response().set_aa().set_rcode(NXDOMAIN)
  438. .add_question(new DNSQuestion("www.first.com", T_A));
  439. ON_CALL(server_, OnRequest("www.first.com", T_A))
  440. .WillByDefault(SetReply(&server_, &nofirst));
  441. DNSPacket nosecond;
  442. nosecond.set_response().set_aa().set_rcode(NXDOMAIN)
  443. .add_question(new DNSQuestion("www.second.org", T_A));
  444. ON_CALL(server_, OnRequest("www.second.org", T_A))
  445. .WillByDefault(SetReply(&server_, &nosecond));
  446. DNSPacket nothird;
  447. nothird.set_response().set_aa().set_rcode(NXDOMAIN)
  448. .add_question(new DNSQuestion("www.third.gov", T_A));
  449. ON_CALL(server_, OnRequest("www.third.gov", T_A))
  450. .WillByDefault(SetReply(&server_, &nothird));
  451. DNSPacket yesbare;
  452. yesbare.set_response().set_aa()
  453. .add_question(new DNSQuestion("www", T_A))
  454. .add_answer(new DNSARR("www", 0x0200, {2, 3, 4, 5}));
  455. ON_CALL(server_, OnRequest("www", T_A))
  456. .WillByDefault(SetReply(&server_, &yesbare));
  457. HostResult result;
  458. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  459. Process();
  460. EXPECT_TRUE(result.done_);
  461. std::stringstream ss;
  462. ss << result.host_;
  463. EXPECT_EQ("{'www' aliases=[] addrs=[2.3.4.5]}", ss.str());
  464. }
  465. TEST_P(MockChannelTest, SearchNoDataThenSuccess) {
  466. // First two search domains recognize the name but have no A records.
  467. DNSPacket nofirst;
  468. nofirst.set_response().set_aa()
  469. .add_question(new DNSQuestion("www.first.com", T_A));
  470. ON_CALL(server_, OnRequest("www.first.com", T_A))
  471. .WillByDefault(SetReply(&server_, &nofirst));
  472. DNSPacket nosecond;
  473. nosecond.set_response().set_aa()
  474. .add_question(new DNSQuestion("www.second.org", T_A));
  475. ON_CALL(server_, OnRequest("www.second.org", T_A))
  476. .WillByDefault(SetReply(&server_, &nosecond));
  477. DNSPacket yesthird;
  478. yesthird.set_response().set_aa()
  479. .add_question(new DNSQuestion("www.third.gov", T_A))
  480. .add_answer(new DNSARR("www.third.gov", 0x0200, {2, 3, 4, 5}));
  481. ON_CALL(server_, OnRequest("www.third.gov", T_A))
  482. .WillByDefault(SetReply(&server_, &yesthird));
  483. HostResult result;
  484. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  485. Process();
  486. EXPECT_TRUE(result.done_);
  487. std::stringstream ss;
  488. ss << result.host_;
  489. EXPECT_EQ("{'www.third.gov' aliases=[] addrs=[2.3.4.5]}", ss.str());
  490. }
  491. TEST_P(MockChannelTest, SearchNoDataThenNoDataBare) {
  492. // First two search domains recognize the name but have no A records.
  493. DNSPacket nofirst;
  494. nofirst.set_response().set_aa()
  495. .add_question(new DNSQuestion("www.first.com", T_A));
  496. ON_CALL(server_, OnRequest("www.first.com", T_A))
  497. .WillByDefault(SetReply(&server_, &nofirst));
  498. DNSPacket nosecond;
  499. nosecond.set_response().set_aa()
  500. .add_question(new DNSQuestion("www.second.org", T_A));
  501. ON_CALL(server_, OnRequest("www.second.org", T_A))
  502. .WillByDefault(SetReply(&server_, &nosecond));
  503. DNSPacket nothird;
  504. nothird.set_response().set_aa()
  505. .add_question(new DNSQuestion("www.third.gov", T_A));
  506. ON_CALL(server_, OnRequest("www.third.gov", T_A))
  507. .WillByDefault(SetReply(&server_, &nothird));
  508. DNSPacket nobare;
  509. nobare.set_response().set_aa()
  510. .add_question(new DNSQuestion("www", T_A));
  511. ON_CALL(server_, OnRequest("www", T_A))
  512. .WillByDefault(SetReply(&server_, &nobare));
  513. HostResult result;
  514. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  515. Process();
  516. EXPECT_TRUE(result.done_);
  517. EXPECT_EQ(ARES_ENODATA, result.status_);
  518. }
  519. TEST_P(MockChannelTest, SearchNoDataThenFail) {
  520. // First two search domains recognize the name but have no A records.
  521. DNSPacket nofirst;
  522. nofirst.set_response().set_aa()
  523. .add_question(new DNSQuestion("www.first.com", T_A));
  524. ON_CALL(server_, OnRequest("www.first.com", T_A))
  525. .WillByDefault(SetReply(&server_, &nofirst));
  526. DNSPacket nosecond;
  527. nosecond.set_response().set_aa()
  528. .add_question(new DNSQuestion("www.second.org", T_A));
  529. ON_CALL(server_, OnRequest("www.second.org", T_A))
  530. .WillByDefault(SetReply(&server_, &nosecond));
  531. DNSPacket nothird;
  532. nothird.set_response().set_aa()
  533. .add_question(new DNSQuestion("www.third.gov", T_A));
  534. ON_CALL(server_, OnRequest("www.third.gov", T_A))
  535. .WillByDefault(SetReply(&server_, &nothird));
  536. DNSPacket nobare;
  537. nobare.set_response().set_aa().set_rcode(NXDOMAIN)
  538. .add_question(new DNSQuestion("www", T_A));
  539. ON_CALL(server_, OnRequest("www", T_A))
  540. .WillByDefault(SetReply(&server_, &nobare));
  541. HostResult result;
  542. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  543. Process();
  544. EXPECT_TRUE(result.done_);
  545. EXPECT_EQ(ARES_ENODATA, result.status_);
  546. }
  547. TEST_P(MockChannelTest, SearchAllocFailure) {
  548. SearchResult result;
  549. SetAllocFail(1);
  550. ares_search(channel_, "fully.qualified.", C_IN, T_A, SearchCallback, &result);
  551. /* Already done */
  552. EXPECT_TRUE(result.done_);
  553. EXPECT_EQ(ARES_ENOMEM, result.status_);
  554. }
  555. TEST_P(MockChannelTest, SearchHighNdots) {
  556. DNSPacket nobare;
  557. nobare.set_response().set_aa().set_rcode(NXDOMAIN)
  558. .add_question(new DNSQuestion("a.b.c.w.w.w", T_A));
  559. ON_CALL(server_, OnRequest("a.b.c.w.w.w", T_A))
  560. .WillByDefault(SetReply(&server_, &nobare));
  561. DNSPacket yesfirst;
  562. yesfirst.set_response().set_aa()
  563. .add_question(new DNSQuestion("a.b.c.w.w.w.first.com", T_A))
  564. .add_answer(new DNSARR("a.b.c.w.w.w.first.com", 0x0200, {2, 3, 4, 5}));
  565. ON_CALL(server_, OnRequest("a.b.c.w.w.w.first.com", T_A))
  566. .WillByDefault(SetReply(&server_, &yesfirst));
  567. SearchResult result;
  568. ares_search(channel_, "a.b.c.w.w.w", C_IN, T_A, SearchCallback, &result);
  569. Process();
  570. EXPECT_TRUE(result.done_);
  571. EXPECT_EQ(ARES_SUCCESS, result.status_);
  572. std::stringstream ss;
  573. ss << PacketToString(result.data_);
  574. EXPECT_EQ("RSP QRY AA NOERROR Q:{'a.b.c.w.w.w.first.com' IN A} "
  575. "A:{'a.b.c.w.w.w.first.com' IN A TTL=512 2.3.4.5}",
  576. ss.str());
  577. }
  578. TEST_P(MockChannelTest, UnspecifiedFamilyV6) {
  579. DNSPacket rsp6;
  580. rsp6.set_response().set_aa()
  581. .add_question(new DNSQuestion("example.com", T_AAAA))
  582. .add_answer(new DNSAaaaRR("example.com", 100,
  583. {0x21, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  584. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03}));
  585. ON_CALL(server_, OnRequest("example.com", T_AAAA))
  586. .WillByDefault(SetReply(&server_, &rsp6));
  587. DNSPacket rsp4;
  588. rsp4.set_response().set_aa()
  589. .add_question(new DNSQuestion("example.com", T_A));
  590. ON_CALL(server_, OnRequest("example.com", T_A))
  591. .WillByDefault(SetReply(&server_, &rsp4));
  592. HostResult result;
  593. ares_gethostbyname(channel_, "example.com.", AF_UNSPEC, HostCallback, &result);
  594. Process();
  595. EXPECT_TRUE(result.done_);
  596. std::stringstream ss;
  597. ss << result.host_;
  598. // Default to IPv6 when both are available.
  599. EXPECT_EQ("{'example.com' aliases=[] addrs=[2121:0000:0000:0000:0000:0000:0000:0303]}", ss.str());
  600. }
  601. TEST_P(MockChannelTest, UnspecifiedFamilyV4) {
  602. DNSPacket rsp6;
  603. rsp6.set_response().set_aa()
  604. .add_question(new DNSQuestion("example.com", T_AAAA));
  605. ON_CALL(server_, OnRequest("example.com", T_AAAA))
  606. .WillByDefault(SetReply(&server_, &rsp6));
  607. DNSPacket rsp4;
  608. rsp4.set_response().set_aa()
  609. .add_question(new DNSQuestion("example.com", T_A))
  610. .add_answer(new DNSARR("example.com", 100, {2, 3, 4, 5}));
  611. ON_CALL(server_, OnRequest("example.com", T_A))
  612. .WillByDefault(SetReply(&server_, &rsp4));
  613. HostResult result;
  614. ares_gethostbyname(channel_, "example.com.", AF_UNSPEC, HostCallback, &result);
  615. Process();
  616. EXPECT_TRUE(result.done_);
  617. std::stringstream ss;
  618. ss << result.host_;
  619. EXPECT_EQ("{'example.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
  620. }
  621. TEST_P(MockChannelTest, UnspecifiedFamilyNoData) {
  622. DNSPacket rsp6;
  623. rsp6.set_response().set_aa()
  624. .add_question(new DNSQuestion("example.com", T_AAAA))
  625. .add_answer(new DNSCnameRR("example.com", 100, "elsewhere.com"));
  626. ON_CALL(server_, OnRequest("example.com", T_AAAA))
  627. .WillByDefault(SetReply(&server_, &rsp6));
  628. DNSPacket rsp4;
  629. rsp4.set_response().set_aa()
  630. .add_question(new DNSQuestion("example.com", T_A));
  631. ON_CALL(server_, OnRequest("example.com", T_A))
  632. .WillByDefault(SetReply(&server_, &rsp4));
  633. HostResult result;
  634. ares_gethostbyname(channel_, "example.com.", AF_UNSPEC, HostCallback, &result);
  635. Process();
  636. EXPECT_TRUE(result.done_);
  637. std::stringstream ss;
  638. ss << result.host_;
  639. EXPECT_EQ("{'' aliases=[] addrs=[]}", ss.str());
  640. }
  641. TEST_P(MockChannelTest, UnspecifiedFamilyCname6A4) {
  642. DNSPacket rsp6;
  643. rsp6.set_response().set_aa()
  644. .add_question(new DNSQuestion("example.com", T_AAAA))
  645. .add_answer(new DNSCnameRR("example.com", 100, "elsewhere.com"));
  646. ON_CALL(server_, OnRequest("example.com", T_AAAA))
  647. .WillByDefault(SetReply(&server_, &rsp6));
  648. DNSPacket rsp4;
  649. rsp4.set_response().set_aa()
  650. .add_question(new DNSQuestion("example.com", T_A))
  651. .add_answer(new DNSARR("example.com", 100, {1, 2, 3, 4}));
  652. ON_CALL(server_, OnRequest("example.com", T_A))
  653. .WillByDefault(SetReply(&server_, &rsp4));
  654. HostResult result;
  655. ares_gethostbyname(channel_, "example.com.", AF_UNSPEC, HostCallback, &result);
  656. Process();
  657. EXPECT_TRUE(result.done_);
  658. std::stringstream ss;
  659. ss << result.host_;
  660. EXPECT_EQ("{'example.com' aliases=[] addrs=[1.2.3.4]}", ss.str());
  661. }
  662. TEST_P(MockChannelTest, ExplicitIP) {
  663. HostResult result;
  664. ares_gethostbyname(channel_, "1.2.3.4", AF_INET, HostCallback, &result);
  665. EXPECT_TRUE(result.done_); // Immediate return
  666. EXPECT_EQ(ARES_SUCCESS, result.status_);
  667. std::stringstream ss;
  668. ss << result.host_;
  669. EXPECT_EQ("{'1.2.3.4' aliases=[] addrs=[1.2.3.4]}", ss.str());
  670. }
  671. TEST_P(MockChannelTest, ExplicitIPAllocFail) {
  672. HostResult result;
  673. SetAllocSizeFail(strlen("1.2.3.4") + 1);
  674. ares_gethostbyname(channel_, "1.2.3.4", AF_INET, HostCallback, &result);
  675. EXPECT_TRUE(result.done_); // Immediate return
  676. EXPECT_EQ(ARES_ENOMEM, result.status_);
  677. }
  678. TEST_P(MockChannelTest, SortListV4) {
  679. DNSPacket rsp;
  680. rsp.set_response().set_aa()
  681. .add_question(new DNSQuestion("example.com", T_A))
  682. .add_answer(new DNSARR("example.com", 100, {22, 23, 24, 25}))
  683. .add_answer(new DNSARR("example.com", 100, {12, 13, 14, 15}))
  684. .add_answer(new DNSARR("example.com", 100, {2, 3, 4, 5}));
  685. ON_CALL(server_, OnRequest("example.com", T_A))
  686. .WillByDefault(SetReply(&server_, &rsp));
  687. {
  688. EXPECT_EQ(ARES_SUCCESS, ares_set_sortlist(channel_, "12.13.0.0/255.255.0.0 1234::5678"));
  689. HostResult result;
  690. ares_gethostbyname(channel_, "example.com.", AF_INET, HostCallback, &result);
  691. Process();
  692. EXPECT_TRUE(result.done_);
  693. std::stringstream ss;
  694. ss << result.host_;
  695. EXPECT_EQ("{'example.com' aliases=[] addrs=[12.13.14.15, 22.23.24.25, 2.3.4.5]}", ss.str());
  696. }
  697. {
  698. EXPECT_EQ(ARES_SUCCESS, ares_set_sortlist(channel_, "2.3.0.0/16 130.140.150.160/26"));
  699. HostResult result;
  700. ares_gethostbyname(channel_, "example.com.", AF_INET, HostCallback, &result);
  701. Process();
  702. EXPECT_TRUE(result.done_);
  703. std::stringstream ss;
  704. ss << result.host_;
  705. EXPECT_EQ("{'example.com' aliases=[] addrs=[2.3.4.5, 22.23.24.25, 12.13.14.15]}", ss.str());
  706. }
  707. struct ares_options options;
  708. memset(&options, 0, sizeof(options));
  709. int optmask = 0;
  710. EXPECT_EQ(ARES_SUCCESS, ares_save_options(channel_, &options, &optmask));
  711. EXPECT_TRUE((optmask & ARES_OPT_SORTLIST) == ARES_OPT_SORTLIST);
  712. ares_destroy_options(&options);
  713. }
  714. TEST_P(MockChannelTest, SortListV6) {
  715. DNSPacket rsp;
  716. rsp.set_response().set_aa()
  717. .add_question(new DNSQuestion("example.com", T_AAAA))
  718. .add_answer(new DNSAaaaRR("example.com", 100,
  719. {0x11, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  720. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x02}))
  721. .add_answer(new DNSAaaaRR("example.com", 100,
  722. {0x21, 0x21, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  723. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0x03}));
  724. ON_CALL(server_, OnRequest("example.com", T_AAAA))
  725. .WillByDefault(SetReply(&server_, &rsp));
  726. {
  727. ares_set_sortlist(channel_, "1111::/16 2.3.0.0/255.255.0.0");
  728. HostResult result;
  729. ares_gethostbyname(channel_, "example.com.", AF_INET6, HostCallback, &result);
  730. Process();
  731. EXPECT_TRUE(result.done_);
  732. std::stringstream ss;
  733. ss << result.host_;
  734. EXPECT_EQ("{'example.com' aliases=[] addrs=[1111:0000:0000:0000:0000:0000:0000:0202, "
  735. "2121:0000:0000:0000:0000:0000:0000:0303]}", ss.str());
  736. }
  737. {
  738. ares_set_sortlist(channel_, "2121::/8");
  739. HostResult result;
  740. ares_gethostbyname(channel_, "example.com.", AF_INET6, HostCallback, &result);
  741. Process();
  742. EXPECT_TRUE(result.done_);
  743. std::stringstream ss;
  744. ss << result.host_;
  745. EXPECT_EQ("{'example.com' aliases=[] addrs=[2121:0000:0000:0000:0000:0000:0000:0303, "
  746. "1111:0000:0000:0000:0000:0000:0000:0202]}", ss.str());
  747. }
  748. }
  749. // Relies on retries so is UDP-only
  750. TEST_P(MockUDPChannelTest, SearchDomainsAllocFail) {
  751. DNSPacket nofirst;
  752. nofirst.set_response().set_aa().set_rcode(NXDOMAIN)
  753. .add_question(new DNSQuestion("www.first.com", T_A));
  754. ON_CALL(server_, OnRequest("www.first.com", T_A))
  755. .WillByDefault(SetReply(&server_, &nofirst));
  756. DNSPacket nosecond;
  757. nosecond.set_response().set_aa().set_rcode(NXDOMAIN)
  758. .add_question(new DNSQuestion("www.second.org", T_A));
  759. ON_CALL(server_, OnRequest("www.second.org", T_A))
  760. .WillByDefault(SetReply(&server_, &nosecond));
  761. DNSPacket yesthird;
  762. yesthird.set_response().set_aa()
  763. .add_question(new DNSQuestion("www.third.gov", T_A))
  764. .add_answer(new DNSARR("www.third.gov", 0x0200, {2, 3, 4, 5}));
  765. ON_CALL(server_, OnRequest("www.third.gov", T_A))
  766. .WillByDefault(SetReply(&server_, &yesthird));
  767. // Fail a variety of different memory allocations, and confirm
  768. // that the operation either fails with ENOMEM or succeeds
  769. // with the expected result.
  770. const int kCount = 34;
  771. HostResult results[kCount];
  772. for (int ii = 1; ii <= kCount; ii++) {
  773. HostResult* result = &(results[ii - 1]);
  774. ClearFails();
  775. SetAllocFail(ii);
  776. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, result);
  777. Process();
  778. EXPECT_TRUE(result->done_);
  779. if (result->status_ == ARES_SUCCESS) {
  780. std::stringstream ss;
  781. ss << result->host_;
  782. EXPECT_EQ("{'www.third.gov' aliases=[] addrs=[2.3.4.5]}", ss.str()) << " failed alloc #" << ii;
  783. if (verbose) std::cerr << "Succeeded despite failure of alloc #" << ii << std::endl;
  784. }
  785. }
  786. // Explicitly destroy the channel now, so that the HostResult objects
  787. // are still valid (in case any pending work refers to them).
  788. ares_destroy(channel_);
  789. channel_ = nullptr;
  790. }
  791. // Relies on retries so is UDP-only
  792. TEST_P(MockUDPChannelTest, Resend) {
  793. std::vector<byte> nothing;
  794. DNSPacket reply;
  795. reply.set_response().set_aa()
  796. .add_question(new DNSQuestion("www.google.com", T_A))
  797. .add_answer(new DNSARR("www.google.com", 0x0100, {0x01, 0x02, 0x03, 0x04}));
  798. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  799. .WillOnce(SetReplyData(&server_, nothing))
  800. .WillOnce(SetReplyData(&server_, nothing))
  801. .WillOnce(SetReply(&server_, &reply));
  802. HostResult result;
  803. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  804. Process();
  805. EXPECT_TRUE(result.done_);
  806. EXPECT_EQ(2, result.timeouts_);
  807. std::stringstream ss;
  808. ss << result.host_;
  809. EXPECT_EQ("{'www.google.com' aliases=[] addrs=[1.2.3.4]}", ss.str());
  810. }
  811. TEST_P(MockChannelTest, CancelImmediate) {
  812. HostResult result;
  813. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  814. ares_cancel(channel_);
  815. EXPECT_TRUE(result.done_);
  816. EXPECT_EQ(ARES_ECANCELLED, result.status_);
  817. EXPECT_EQ(0, result.timeouts_);
  818. }
  819. TEST_P(MockChannelTest, CancelImmediateGetHostByAddr) {
  820. HostResult result;
  821. struct in_addr addr;
  822. addr.s_addr = htonl(0x08080808);
  823. ares_gethostbyaddr(channel_, &addr, sizeof(addr), AF_INET, HostCallback, &result);
  824. ares_cancel(channel_);
  825. EXPECT_TRUE(result.done_);
  826. EXPECT_EQ(ARES_ECANCELLED, result.status_);
  827. EXPECT_EQ(0, result.timeouts_);
  828. }
  829. // Relies on retries so is UDP-only
  830. TEST_P(MockUDPChannelTest, CancelLater) {
  831. std::vector<byte> nothing;
  832. // On second request, cancel the channel.
  833. EXPECT_CALL(server_, OnRequest("www.google.com", T_A))
  834. .WillOnce(SetReplyData(&server_, nothing))
  835. .WillOnce(CancelChannel(&server_, channel_));
  836. HostResult result;
  837. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  838. Process();
  839. EXPECT_TRUE(result.done_);
  840. EXPECT_EQ(ARES_ECANCELLED, result.status_);
  841. EXPECT_EQ(0, result.timeouts_);
  842. }
  843. TEST_P(MockChannelTest, GetHostByNameDestroyAbsolute) {
  844. HostResult result;
  845. ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
  846. ares_destroy(channel_);
  847. channel_ = nullptr;
  848. EXPECT_TRUE(result.done_); // Synchronous
  849. EXPECT_EQ(ARES_EDESTRUCTION, result.status_);
  850. EXPECT_EQ(0, result.timeouts_);
  851. }
  852. TEST_P(MockChannelTest, GetHostByNameDestroyRelative) {
  853. HostResult result;
  854. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  855. ares_destroy(channel_);
  856. channel_ = nullptr;
  857. EXPECT_TRUE(result.done_); // Synchronous
  858. EXPECT_EQ(ARES_EDESTRUCTION, result.status_);
  859. EXPECT_EQ(0, result.timeouts_);
  860. }
  861. TEST_P(MockChannelTest, GetHostByNameCNAMENoData) {
  862. DNSPacket response;
  863. response.set_response().set_aa()
  864. .add_question(new DNSQuestion("cname.first.com", T_A))
  865. .add_answer(new DNSCnameRR("cname.first.com", 100, "a.first.com"));
  866. ON_CALL(server_, OnRequest("cname.first.com", T_A))
  867. .WillByDefault(SetReply(&server_, &response));
  868. HostResult result;
  869. ares_gethostbyname(channel_, "cname.first.com.", AF_INET, HostCallback, &result);
  870. Process();
  871. EXPECT_TRUE(result.done_);
  872. EXPECT_EQ(ARES_ENODATA, result.status_);
  873. }
  874. TEST_P(MockChannelTest, GetHostByAddrDestroy) {
  875. unsigned char gdns_addr4[4] = {0x08, 0x08, 0x08, 0x08};
  876. HostResult result;
  877. ares_gethostbyaddr(channel_, gdns_addr4, sizeof(gdns_addr4), AF_INET, HostCallback, &result);
  878. ares_destroy(channel_);
  879. channel_ = nullptr;
  880. EXPECT_TRUE(result.done_); // Synchronous
  881. EXPECT_EQ(ARES_EDESTRUCTION, result.status_);
  882. EXPECT_EQ(0, result.timeouts_);
  883. }
  884. #ifndef WIN32
  885. TEST_P(MockChannelTest, HostAlias) {
  886. DNSPacket reply;
  887. reply.set_response().set_aa()
  888. .add_question(new DNSQuestion("www.google.com", T_A))
  889. .add_answer(new DNSARR("www.google.com", 0x0100, {0x01, 0x02, 0x03, 0x04}));
  890. ON_CALL(server_, OnRequest("www.google.com", T_A))
  891. .WillByDefault(SetReply(&server_, &reply));
  892. TempFile aliases("\n\n# www commentedout\nwww www.google.com\n");
  893. EnvValue with_env("HOSTALIASES", aliases.filename());
  894. HostResult result;
  895. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  896. Process();
  897. EXPECT_TRUE(result.done_);
  898. std::stringstream ss;
  899. ss << result.host_;
  900. EXPECT_EQ("{'www.google.com' aliases=[] addrs=[1.2.3.4]}", ss.str());
  901. }
  902. TEST_P(MockChannelTest, HostAliasMissing) {
  903. DNSPacket yesfirst;
  904. yesfirst.set_response().set_aa()
  905. .add_question(new DNSQuestion("www.first.com", T_A))
  906. .add_answer(new DNSARR("www.first.com", 0x0200, {2, 3, 4, 5}));
  907. ON_CALL(server_, OnRequest("www.first.com", T_A))
  908. .WillByDefault(SetReply(&server_, &yesfirst));
  909. TempFile aliases("\n\n# www commentedout\nww www.google.com\n");
  910. EnvValue with_env("HOSTALIASES", aliases.filename());
  911. HostResult result;
  912. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  913. Process();
  914. EXPECT_TRUE(result.done_);
  915. std::stringstream ss;
  916. ss << result.host_;
  917. EXPECT_EQ("{'www.first.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
  918. }
  919. TEST_P(MockChannelTest, HostAliasMissingFile) {
  920. DNSPacket yesfirst;
  921. yesfirst.set_response().set_aa()
  922. .add_question(new DNSQuestion("www.first.com", T_A))
  923. .add_answer(new DNSARR("www.first.com", 0x0200, {2, 3, 4, 5}));
  924. ON_CALL(server_, OnRequest("www.first.com", T_A))
  925. .WillByDefault(SetReply(&server_, &yesfirst));
  926. EnvValue with_env("HOSTALIASES", "bogus.mcfile");
  927. HostResult result;
  928. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  929. Process();
  930. EXPECT_TRUE(result.done_);
  931. std::stringstream ss;
  932. ss << result.host_;
  933. EXPECT_EQ("{'www.first.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
  934. }
  935. TEST_P(MockChannelTest, HostAliasUnreadable) {
  936. TempFile aliases("www www.google.com\n");
  937. EXPECT_EQ(chmod(aliases.filename(), 0), 0);
  938. /* Perform OS sanity checks. We are observing on Debian after the chmod(fn, 0)
  939. * that we are still able to fopen() the file which is unexpected. Skip the
  940. * test if we observe this behavior */
  941. struct stat st;
  942. EXPECT_EQ(stat(aliases.filename(), &st), 0);
  943. EXPECT_EQ(st.st_mode & (S_IRWXU|S_IRWXG|S_IRWXO), 0);
  944. FILE *fp = fopen(aliases.filename(), "r");
  945. if (fp != NULL) {
  946. if (verbose) std::cerr << "Skipping Test due to OS incompatibility (open file caching)" << std::endl;
  947. fclose(fp);
  948. return;
  949. }
  950. EnvValue with_env("HOSTALIASES", aliases.filename());
  951. HostResult result;
  952. ares_gethostbyname(channel_, "www", AF_INET, HostCallback, &result);
  953. Process();
  954. EXPECT_TRUE(result.done_);
  955. EXPECT_EQ(ARES_EFILE, result.status_);
  956. chmod(aliases.filename(), 0777);
  957. }
  958. #endif
  959. class MockMultiServerChannelTest
  960. : public MockChannelOptsTest,
  961. public ::testing::WithParamInterface< std::pair<int, bool> > {
  962. public:
  963. MockMultiServerChannelTest(bool rotate)
  964. : MockChannelOptsTest(3, GetParam().first, GetParam().second, nullptr, rotate ? ARES_OPT_ROTATE : ARES_OPT_NOROTATE) {}
  965. void CheckExample() {
  966. HostResult result;
  967. ares_gethostbyname(channel_, "www.example.com.", AF_INET, HostCallback, &result);
  968. Process();
  969. EXPECT_TRUE(result.done_);
  970. std::stringstream ss;
  971. ss << result.host_;
  972. EXPECT_EQ("{'www.example.com' aliases=[] addrs=[2.3.4.5]}", ss.str());
  973. }
  974. };
  975. class RotateMultiMockTest : public MockMultiServerChannelTest {
  976. public:
  977. RotateMultiMockTest() : MockMultiServerChannelTest(true) {}
  978. };
  979. class NoRotateMultiMockTest : public MockMultiServerChannelTest {
  980. public:
  981. NoRotateMultiMockTest() : MockMultiServerChannelTest(false) {}
  982. };
  983. TEST_P(RotateMultiMockTest, ThirdServer) {
  984. struct ares_options opts = {0};
  985. int optmask = 0;
  986. EXPECT_EQ(ARES_SUCCESS, ares_save_options(channel_, &opts, &optmask));
  987. EXPECT_EQ(0, (optmask & ARES_OPT_NOROTATE));
  988. ares_destroy_options(&opts);
  989. DNSPacket servfailrsp;
  990. servfailrsp.set_response().set_aa().set_rcode(SERVFAIL)
  991. .add_question(new DNSQuestion("www.example.com", T_A));
  992. DNSPacket notimplrsp;
  993. notimplrsp.set_response().set_aa().set_rcode(NOTIMP)
  994. .add_question(new DNSQuestion("www.example.com", T_A));
  995. DNSPacket okrsp;
  996. okrsp.set_response().set_aa()
  997. .add_question(new DNSQuestion("www.example.com", T_A))
  998. .add_answer(new DNSARR("www.example.com", 100, {2,3,4,5}));
  999. EXPECT_CALL(*servers_[0], OnRequest("www.example.com", T_A))
  1000. .WillOnce(SetReply(servers_[0].get(), &servfailrsp));
  1001. EXPECT_CALL(*servers_[1], OnRequest("www.example.com", T_A))
  1002. .WillOnce(SetReply(servers_[1].get(), &notimplrsp));
  1003. EXPECT_CALL(*servers_[2], OnRequest("www.example.com", T_A))
  1004. .WillOnce(SetReply(servers_[2].get(), &okrsp));
  1005. CheckExample();
  1006. // Second time around, starts from server [1].
  1007. EXPECT_CALL(*servers_[1], OnRequest("www.example.com", T_A))
  1008. .WillOnce(SetReply(servers_[1].get(), &servfailrsp));
  1009. EXPECT_CALL(*servers_[2], OnRequest("www.example.com", T_A))
  1010. .WillOnce(SetReply(servers_[2].get(), &notimplrsp));
  1011. EXPECT_CALL(*servers_[0], OnRequest("www.example.com", T_A))
  1012. .WillOnce(SetReply(servers_[0].get(), &okrsp));
  1013. CheckExample();
  1014. // Third time around, starts from server [2].
  1015. EXPECT_CALL(*servers_[2], OnRequest("www.example.com", T_A))
  1016. .WillOnce(SetReply(servers_[2].get(), &servfailrsp));
  1017. EXPECT_CALL(*servers_[0], OnRequest("www.example.com", T_A))
  1018. .WillOnce(SetReply(servers_[0].get(), &notimplrsp));
  1019. EXPECT_CALL(*servers_[1], OnRequest("www.example.com", T_A))
  1020. .WillOnce(SetReply(servers_[1].get(), &okrsp));
  1021. CheckExample();
  1022. }
  1023. TEST_P(NoRotateMultiMockTest, ThirdServer) {
  1024. struct ares_options opts = {0};
  1025. int optmask = 0;
  1026. EXPECT_EQ(ARES_SUCCESS, ares_save_options(channel_, &opts, &optmask));
  1027. EXPECT_EQ(ARES_OPT_NOROTATE, (optmask & ARES_OPT_NOROTATE));
  1028. ares_destroy_options(&opts);
  1029. DNSPacket servfailrsp;
  1030. servfailrsp.set_response().set_aa().set_rcode(SERVFAIL)
  1031. .add_question(new DNSQuestion("www.example.com", T_A));
  1032. DNSPacket notimplrsp;
  1033. notimplrsp.set_response().set_aa().set_rcode(NOTIMP)
  1034. .add_question(new DNSQuestion("www.example.com", T_A));
  1035. DNSPacket okrsp;
  1036. okrsp.set_response().set_aa()
  1037. .add_question(new DNSQuestion("www.example.com", T_A))
  1038. .add_answer(new DNSARR("www.example.com", 100, {2,3,4,5}));
  1039. EXPECT_CALL(*servers_[0], OnRequest("www.example.com", T_A))
  1040. .WillOnce(SetReply(servers_[0].get(), &servfailrsp));
  1041. EXPECT_CALL(*servers_[1], OnRequest("www.example.com", T_A))
  1042. .WillOnce(SetReply(servers_[1].get(), &notimplrsp));
  1043. EXPECT_CALL(*servers_[2], OnRequest("www.example.com", T_A))
  1044. .WillOnce(SetReply(servers_[2].get(), &okrsp));
  1045. CheckExample();
  1046. // Second time around, still starts from server [0].
  1047. EXPECT_CALL(*servers_[0], OnRequest("www.example.com", T_A))
  1048. .WillOnce(SetReply(servers_[0].get(), &servfailrsp));
  1049. EXPECT_CALL(*servers_[1], OnRequest("www.example.com", T_A))
  1050. .WillOnce(SetReply(servers_[1].get(), &notimplrsp));
  1051. EXPECT_CALL(*servers_[2], OnRequest("www.example.com", T_A))
  1052. .WillOnce(SetReply(servers_[2].get(), &okrsp));
  1053. CheckExample();
  1054. // Third time around, still starts from server [0].
  1055. EXPECT_CALL(*servers_[0], OnRequest("www.example.com", T_A))
  1056. .WillOnce(SetReply(servers_[0].get(), &servfailrsp));
  1057. EXPECT_CALL(*servers_[1], OnRequest("www.example.com", T_A))
  1058. .WillOnce(SetReply(servers_[1].get(), &notimplrsp));
  1059. EXPECT_CALL(*servers_[2], OnRequest("www.example.com", T_A))
  1060. .WillOnce(SetReply(servers_[2].get(), &okrsp));
  1061. CheckExample();
  1062. }
  1063. INSTANTIATE_TEST_SUITE_P(AddressFamilies, MockChannelTest, ::testing::ValuesIn(ares::test::families_modes));
  1064. INSTANTIATE_TEST_SUITE_P(AddressFamilies, MockUDPChannelTest, ::testing::ValuesIn(ares::test::families));
  1065. INSTANTIATE_TEST_SUITE_P(AddressFamilies, MockTCPChannelTest, ::testing::ValuesIn(ares::test::families));
  1066. INSTANTIATE_TEST_SUITE_P(AddressFamilies, MockExtraOptsTest, ::testing::ValuesIn(ares::test::families_modes));
  1067. INSTANTIATE_TEST_SUITE_P(AddressFamilies, MockNoCheckRespChannelTest, ::testing::ValuesIn(ares::test::families_modes));
  1068. INSTANTIATE_TEST_SUITE_P(AddressFamilies, MockEDNSChannelTest, ::testing::ValuesIn(ares::test::families_modes));
  1069. INSTANTIATE_TEST_SUITE_P(TransportModes, RotateMultiMockTest, ::testing::ValuesIn(ares::test::families_modes));
  1070. INSTANTIATE_TEST_SUITE_P(TransportModes, NoRotateMultiMockTest, ::testing::ValuesIn(ares::test::families_modes));
  1071. } // namespace test
  1072. } // namespace ares