• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include "ares-test.h"
2 #include "dns-proto.h"
3 
4 #include <string>
5 #include <vector>
6 
7 namespace ares {
8 namespace test {
9 
TEST_F(DefaultChannelTest,GetServers)10 TEST_F(DefaultChannelTest, GetServers) {
11   std::vector<std::string> servers = GetNameServers(channel_);
12   if (verbose) {
13     for (const std::string& server : servers) {
14       std::cerr << "Nameserver: " << server << std::endl;
15     }
16   }
17 }
18 
TEST_F(DefaultChannelTest,GetServersFailures)19 TEST_F(DefaultChannelTest, GetServersFailures) {
20   EXPECT_EQ(ARES_SUCCESS,
21             ares_set_servers_csv(channel_, "1.2.3.4,2.3.4.5"));
22   struct ares_addr_node* servers = nullptr;
23   SetAllocFail(1);
24   EXPECT_EQ(ARES_ENOMEM, ares_get_servers(channel_, &servers));
25   SetAllocFail(2);
26   EXPECT_EQ(ARES_ENOMEM, ares_get_servers(channel_, &servers));
27   EXPECT_EQ(ARES_ENODATA, ares_get_servers(nullptr, &servers));
28 }
29 
TEST_F(DefaultChannelTest,SetServers)30 TEST_F(DefaultChannelTest, SetServers) {
31   EXPECT_EQ(ARES_SUCCESS, ares_set_servers(channel_, nullptr));
32   std::vector<std::string> empty;
33   EXPECT_EQ(empty, GetNameServers(channel_));
34 
35   struct ares_addr_node server1;
36   struct ares_addr_node server2;
37   server1.next = &server2;
38   server1.family = AF_INET;
39   server1.addr.addr4.s_addr = htonl(0x01020304);
40   server2.next = nullptr;
41   server2.family = AF_INET;
42   server2.addr.addr4.s_addr = htonl(0x02030405);
43   EXPECT_EQ(ARES_ENODATA, ares_set_servers(nullptr, &server1));
44 
45   EXPECT_EQ(ARES_SUCCESS, ares_set_servers(channel_, &server1));
46   std::vector<std::string> expected = {"1.2.3.4", "2.3.4.5"};
47   EXPECT_EQ(expected, GetNameServers(channel_));
48 
49   // Change not allowed while request is pending
50   HostResult result;
51   ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
52   EXPECT_EQ(ARES_ENOTIMP, ares_set_servers(channel_, &server1));
53   ares_cancel(channel_);
54 }
55 
TEST_F(DefaultChannelTest,SetServersPorts)56 TEST_F(DefaultChannelTest, SetServersPorts) {
57   EXPECT_EQ(ARES_SUCCESS, ares_set_servers_ports(channel_, nullptr));
58   std::vector<std::string> empty;
59   EXPECT_EQ(empty, GetNameServers(channel_));
60 
61   struct ares_addr_port_node server1;
62   struct ares_addr_port_node server2;
63   server1.next = &server2;
64   server1.family = AF_INET;
65   server1.addr.addr4.s_addr = htonl(0x01020304);
66   server1.udp_port = 111;
67   server1.tcp_port = 111;
68   server2.next = nullptr;
69   server2.family = AF_INET;
70   server2.addr.addr4.s_addr = htonl(0x02030405);
71   server2.udp_port = 0;
72   server2.tcp_port = 0;;
73   EXPECT_EQ(ARES_ENODATA, ares_set_servers_ports(nullptr, &server1));
74 
75   EXPECT_EQ(ARES_SUCCESS, ares_set_servers_ports(channel_, &server1));
76   std::vector<std::string> expected = {"1.2.3.4:111", "2.3.4.5"};
77   EXPECT_EQ(expected, GetNameServers(channel_));
78 
79   // Change not allowed while request is pending
80   HostResult result;
81   ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
82   EXPECT_EQ(ARES_ENOTIMP, ares_set_servers_ports(channel_, &server1));
83   ares_cancel(channel_);
84 }
85 
TEST_F(DefaultChannelTest,SetServersCSV)86 TEST_F(DefaultChannelTest, SetServersCSV) {
87   EXPECT_EQ(ARES_ENODATA, ares_set_servers_csv(nullptr, "1.2.3.4"));
88   EXPECT_EQ(ARES_ENODATA, ares_set_servers_csv(nullptr, "xyzzy,plugh"));
89   EXPECT_EQ(ARES_ENODATA, ares_set_servers_csv(nullptr, "256.1.2.3"));
90   EXPECT_EQ(ARES_ENODATA, ares_set_servers_csv(nullptr, "1.2.3.4.5"));
91   EXPECT_EQ(ARES_ENODATA, ares_set_servers_csv(nullptr, "1:2:3:4:5"));
92 
93   EXPECT_EQ(ARES_SUCCESS,
94             ares_set_servers_csv(channel_, "1.2.3.4,0102:0304:0506:0708:0910:1112:1314:1516,2.3.4.5"));
95   std::vector<std::string> expected = {"1.2.3.4", "0102:0304:0506:0708:0910:1112:1314:1516", "2.3.4.5"};
96   EXPECT_EQ(expected, GetNameServers(channel_));
97 
98   // Same, with spaces
99   EXPECT_EQ(ARES_EBADSTR,
100             ares_set_servers_csv(channel_, "1.2.3.4 , 0102:0304:0506:0708:0910:1112:1314:1516, 2.3.4.5"));
101 
102   // Same, with ports
103   EXPECT_EQ(ARES_SUCCESS,
104             ares_set_servers_csv(channel_, "1.2.3.4:54,[0102:0304:0506:0708:0910:1112:1314:1516]:80,2.3.4.5:55"));
105   EXPECT_EQ(expected, GetNameServers(channel_));
106   EXPECT_EQ(ARES_SUCCESS,
107             ares_set_servers_ports_csv(channel_, "1.2.3.4:54,[0102:0304:0506:0708:0910:1112:1314:1516]:80,2.3.4.5:55"));
108   std::vector<std::string> expected2 = {"1.2.3.4:54", "[0102:0304:0506:0708:0910:1112:1314:1516]:80", "2.3.4.5:55"};
109   EXPECT_EQ(expected2, GetNameServers(channel_));
110 
111   // Change not allowed while request is pending
112   HostResult result;
113   ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
114   EXPECT_EQ(ARES_ENOTIMP, ares_set_servers_csv(channel_, "1.2.3.4,2.3.4.5"));
115   EXPECT_EQ(ARES_ENOTIMP, ares_set_servers_ports_csv(channel_, "1.2.3.4:56,2.3.4.5:67"));
116   ares_cancel(channel_);
117 
118   // Should survive duplication
119   ares_channel channel2;
120   EXPECT_EQ(ARES_SUCCESS, ares_dup(&channel2, channel_));
121   EXPECT_EQ(expected2, GetNameServers(channel2));
122   ares_destroy(channel2);
123 
124   // Allocation failure cases
125   for (int fail = 1; fail <= 5; fail++) {
126     SetAllocFail(fail);
127     EXPECT_EQ(ARES_ENOMEM,
128               ares_set_servers_csv(channel_, "1.2.3.4,0102:0304:0506:0708:0910:1112:1314:1516,2.3.4.5"));
129   }
130 
131   // Blank servers
132   EXPECT_EQ(ARES_SUCCESS, ares_set_servers_csv(channel_, ""));
133   std::vector<std::string> none;
134   EXPECT_EQ(none, GetNameServers(channel_));
135 
136   EXPECT_EQ(ARES_EBADSTR, ares_set_servers_csv(channel_, "2.3.4.5,1.2.3.4:,3.4.5.6"));
137   EXPECT_EQ(ARES_EBADSTR, ares_set_servers_csv(channel_, "2.3.4.5,1.2.3.4:Z,3.4.5.6"));
138 }
139 
TEST_F(DefaultChannelTest,TimeoutValue)140 TEST_F(DefaultChannelTest, TimeoutValue) {
141   struct timeval tinfo;
142   tinfo.tv_sec = 0;
143   tinfo.tv_usec = 0;
144   struct timeval tmax;
145   tmax.tv_sec = 0;
146   tmax.tv_usec = 10;
147   struct timeval* pt;
148 
149   // No timers => get max back.
150   pt = ares_timeout(channel_, &tmax, &tinfo);
151   EXPECT_EQ(&tmax, pt);
152   EXPECT_EQ(0, pt->tv_sec);
153   EXPECT_EQ(10, pt->tv_usec);
154 
155   pt = ares_timeout(channel_, nullptr, &tinfo);
156   EXPECT_EQ(nullptr, pt);
157 
158   HostResult result;
159   ares_gethostbyname(channel_, "www.google.com.", AF_INET, HostCallback, &result);
160 
161   // Now there's a timer running.
162   pt = ares_timeout(channel_, &tmax, &tinfo);
163   EXPECT_EQ(&tmax, pt);
164   EXPECT_EQ(0, pt->tv_sec);
165   EXPECT_EQ(10, pt->tv_usec);
166 
167   tmax.tv_sec = 100;
168   pt = ares_timeout(channel_, &tmax, &tinfo);
169   EXPECT_EQ(&tinfo, pt);
170 
171   pt = ares_timeout(channel_, nullptr, &tinfo);
172   EXPECT_EQ(&tinfo, pt);
173 
174   Process();
175 }
176 
TEST_F(LibraryTest,InetNtoP)177 TEST_F(LibraryTest, InetNtoP) {
178   struct in_addr addr;
179   addr.s_addr = htonl(0x01020304);
180   char buffer[256];
181   EXPECT_EQ(buffer, ares_inet_ntop(AF_INET, &addr, buffer, sizeof(buffer)));
182   EXPECT_EQ("1.2.3.4", std::string(buffer));
183 }
184 
TEST_F(LibraryTest,Mkquery)185 TEST_F(LibraryTest, Mkquery) {
186   byte* p;
187   int len;
188   ares_mkquery("example.com", ns_c_in, ns_t_a, 0x1234, 0, &p, &len);
189   std::vector<byte> data(p, p + len);
190   ares_free_string(p);
191 
192   std::string actual = PacketToString(data);
193   DNSPacket pkt;
194   pkt.set_qid(0x1234).add_question(new DNSQuestion("example.com", ns_t_a));
195   std::string expected = PacketToString(pkt.data());
196   EXPECT_EQ(expected, actual);
197 }
198 
TEST_F(LibraryTest,CreateQuery)199 TEST_F(LibraryTest, CreateQuery) {
200   byte* p;
201   int len;
202   EXPECT_EQ(ARES_SUCCESS,
203             ares_create_query("exam\\@le.com", ns_c_in, ns_t_a, 0x1234, 0,
204                               &p, &len, 0));
205   std::vector<byte> data(p, p + len);
206   ares_free_string(p);
207 
208   std::string actual = PacketToString(data);
209   DNSPacket pkt;
210   pkt.set_qid(0x1234).add_question(new DNSQuestion("exam@le.com", ns_t_a));
211   std::string expected = PacketToString(pkt.data());
212   EXPECT_EQ(expected, actual);
213 }
214 
TEST_F(LibraryTest,CreateQueryTrailingEscapedDot)215 TEST_F(LibraryTest, CreateQueryTrailingEscapedDot) {
216   byte* p;
217   int len;
218   EXPECT_EQ(ARES_SUCCESS,
219             ares_create_query("example.com\\.", ns_c_in, ns_t_a, 0x1234, 0,
220                               &p, &len, 0));
221   std::vector<byte> data(p, p + len);
222   ares_free_string(p);
223 
224   std::string actual = PacketToString(data);
225   EXPECT_EQ("REQ QRY  Q:{'example.com\\.' IN A}", actual);
226 }
227 
TEST_F(LibraryTest,CreateQueryNameTooLong)228 TEST_F(LibraryTest, CreateQueryNameTooLong) {
229   byte* p;
230   int len;
231   EXPECT_EQ(ARES_EBADNAME,
232             ares_create_query(
233               "a1234567890123456789.b1234567890123456789.c1234567890123456789.d1234567890123456789."
234               "a1234567890123456789.b1234567890123456789.c1234567890123456789.d1234567890123456789."
235               "a1234567890123456789.b1234567890123456789.c1234567890123456789.d1234567890123456789."
236               "x1234567890123456789.y1234567890123456789.",
237               ns_c_in, ns_t_a, 0x1234, 0, &p, &len, 0));
238 }
239 
TEST_F(LibraryTest,CreateQueryFailures)240 TEST_F(LibraryTest, CreateQueryFailures) {
241   byte* p;
242   int len;
243   // RC1035 has a 255 byte limit on names.
244   std::string longname;
245   for (int ii = 0; ii < 17; ii++) {
246     longname += "fedcba9876543210";
247   }
248   p = nullptr;
249   EXPECT_EQ(ARES_EBADNAME,
250             ares_create_query(longname.c_str(), ns_c_in, ns_t_a, 0x1234, 0,
251                     &p, &len, 0));
252   if (p) ares_free_string(p);
253 
254   SetAllocFail(1);
255 
256   p = nullptr;
257   EXPECT_EQ(ARES_ENOMEM,
258             ares_create_query("example.com", ns_c_in, ns_t_a, 0x1234, 0,
259                     &p, &len, 0));
260   if (p) ares_free_string(p);
261 
262   // 63-char limit on a single label
263   std::string longlabel = "a.a123456789b123456789c123456789d123456789e123456789f123456789g123456789.org";
264   p = nullptr;
265   EXPECT_EQ(ARES_EBADNAME,
266             ares_create_query(longlabel.c_str(), ns_c_in, ns_t_a, 0x1234, 0,
267                     &p, &len, 0));
268   if (p) ares_free_string(p);
269 
270   // Empty non-terminal label
271   p = nullptr;
272   EXPECT_EQ(ARES_EBADNAME,
273             ares_create_query("example..com", ns_c_in, ns_t_a, 0x1234, 0,
274                     &p, &len, 0));
275   if (p) ares_free_string(p);
276 }
277 
TEST_F(LibraryTest,CreateQueryOnionDomain)278 TEST_F(LibraryTest, CreateQueryOnionDomain) {
279   byte* p;
280   int len;
281   EXPECT_EQ(ARES_ENOTFOUND,
282             ares_create_query("dontleak.onion", ns_c_in, ns_t_a, 0x1234, 0,
283                               &p, &len, 0));
284 }
285 
TEST_F(DefaultChannelTest,HostByNameOnionDomain)286 TEST_F(DefaultChannelTest, HostByNameOnionDomain) {
287   HostResult result;
288   ares_gethostbyname(channel_, "dontleak.onion", AF_INET, HostCallback, &result);
289   EXPECT_TRUE(result.done_);
290   EXPECT_EQ(ARES_ENOTFOUND, result.status_);
291 }
292 
TEST_F(DefaultChannelTest,HostByNameFileOnionDomain)293 TEST_F(DefaultChannelTest, HostByNameFileOnionDomain) {
294   struct hostent *h;
295   EXPECT_EQ(ARES_ENOTFOUND,
296             ares_gethostbyname_file(channel_, "dontleak.onion", AF_INET, &h));
297 }
298 
299 // Interesting question: should tacking on a search domain let the query
300 // through? It seems safer to reject it because "supersecret.onion.search"
301 // still leaks information about the query to malicious resolvers.
TEST_F(DefaultChannelTest,SearchOnionDomain)302 TEST_F(DefaultChannelTest, SearchOnionDomain) {
303   SearchResult result;
304   ares_search(channel_, "dontleak.onion", ns_c_in, ns_t_a,
305               SearchCallback, &result);
306   EXPECT_TRUE(result.done_);
307   EXPECT_EQ(ARES_ENOTFOUND, result.status_);
308 }
309 
TEST_F(DefaultChannelTest,SendFailure)310 TEST_F(DefaultChannelTest, SendFailure) {
311   unsigned char buf[2];
312   SearchResult result;
313   ares_send(channel_, buf, sizeof(buf), SearchCallback, &result);
314   EXPECT_TRUE(result.done_);
315   EXPECT_EQ(ARES_EBADQUERY, result.status_);
316 }
317 
ExpandName(const std::vector<byte> & data,int offset,long * enclen)318 std::string ExpandName(const std::vector<byte>& data, int offset,
319                        long *enclen) {
320   char *name = nullptr;
321   int rc = ares_expand_name(data.data() + offset, data.data(), data.size(),
322                             &name, enclen);
323   EXPECT_EQ(ARES_SUCCESS, rc);
324   std::string result;
325   if (rc == ARES_SUCCESS) {
326     result = name;
327   } else {
328     result = "<error>";
329   }
330   ares_free_string(name);
331   return result;
332 }
333 
TEST_F(LibraryTest,ExpandName)334 TEST_F(LibraryTest, ExpandName) {
335   long enclen;
336   std::vector<byte> data1 = {1, 'a', 2, 'b', 'c', 3, 'd', 'e', 'f', 0};
337   EXPECT_EQ("a.bc.def", ExpandName(data1, 0, &enclen));
338   EXPECT_EQ(data1.size(), enclen);
339 
340   std::vector<byte> data2 = {0};
341   EXPECT_EQ("", ExpandName(data2, 0, &enclen));
342   EXPECT_EQ(1, enclen);
343 
344   // Complete name indirection
345   std::vector<byte> data3 = {0x12, 0x23,
346                              3, 'd', 'e', 'f', 0,
347                              0xC0, 2};
348   EXPECT_EQ("def", ExpandName(data3, 2, &enclen));
349   EXPECT_EQ(5, enclen);
350   EXPECT_EQ("def", ExpandName(data3, 7, &enclen));
351   EXPECT_EQ(2, enclen);
352 
353   // One label then indirection
354   std::vector<byte> data4 = {0x12, 0x23,
355                              3, 'd', 'e', 'f', 0,
356                              1, 'a', 0xC0, 2};
357   EXPECT_EQ("def", ExpandName(data4, 2, &enclen));
358   EXPECT_EQ(5, enclen);
359   EXPECT_EQ("a.def", ExpandName(data4, 7, &enclen));
360   EXPECT_EQ(4, enclen);
361 
362   // Two labels then indirection
363   std::vector<byte> data5 = {0x12, 0x23,
364                              3, 'd', 'e', 'f', 0,
365                              1, 'a', 1, 'b', 0xC0, 2};
366   EXPECT_EQ("def", ExpandName(data5, 2, &enclen));
367   EXPECT_EQ(5, enclen);
368   EXPECT_EQ("a.b.def", ExpandName(data5, 7, &enclen));
369   EXPECT_EQ(6, enclen);
370 
371   // Empty name, indirection to empty name
372   std::vector<byte> data6 = {0x12, 0x23,
373                              0,
374                              0xC0, 2};
375   EXPECT_EQ("", ExpandName(data6, 2, &enclen));
376   EXPECT_EQ(1, enclen);
377   EXPECT_EQ("", ExpandName(data6, 3, &enclen));
378   EXPECT_EQ(2, enclen);
379 }
380 
TEST_F(LibraryTest,ExpandNameFailure)381 TEST_F(LibraryTest, ExpandNameFailure) {
382   std::vector<byte> data1 = {0x03, 'c', 'o', 'm', 0x00};
383   char *name = nullptr;
384   long enclen;
385   SetAllocFail(1);
386   EXPECT_EQ(ARES_ENOMEM,
387             ares_expand_name(data1.data(), data1.data(), data1.size(),
388                              &name, &enclen));
389 
390   // Empty packet
391   EXPECT_EQ(ARES_EBADNAME,
392             ares_expand_name(data1.data(), data1.data(), 0, &name, &enclen));
393 
394   // Start beyond enclosing data
395   EXPECT_EQ(ARES_EBADNAME,
396             ares_expand_name(data1.data() + data1.size(), data1.data(), data1.size(),
397                              &name, &enclen));
398 
399   // Length beyond size of enclosing data
400   std::vector<byte> data2a = {0x13, 'c', 'o', 'm', 0x00};
401   EXPECT_EQ(ARES_EBADNAME,
402             ares_expand_name(data2a.data(), data2a.data(), data2a.size(),
403                              &name, &enclen));
404   std::vector<byte> data2b = {0x1};
405   EXPECT_EQ(ARES_EBADNAME,
406             ares_expand_name(data2b.data(), data2b.data(), data2b.size(),
407                              &name, &enclen));
408   std::vector<byte> data2c = {0xC0};
409   EXPECT_EQ(ARES_EBADNAME,
410             ares_expand_name(data2c.data(), data2c.data(), data2c.size(),
411                              &name, &enclen));
412 
413   // Indirection beyond enclosing data
414   std::vector<byte> data3a = {0xC0, 0x02};
415   EXPECT_EQ(ARES_EBADNAME,
416             ares_expand_name(data3a.data(), data3a.data(), data3a.size(),
417                              &name, &enclen));
418   std::vector<byte> data3b = {0xC0, 0x0A, 'c', 'o', 'm', 0x00};
419   EXPECT_EQ(ARES_EBADNAME,
420             ares_expand_name(data3b.data(), data3b.data(), data3b.size(),
421                              &name, &enclen));
422 
423   // Invalid top bits in label length
424   std::vector<byte> data4 = {0x03, 'c', 'o', 'm', 0x00, 0x80, 0x00};
425   EXPECT_EQ(ARES_EBADNAME,
426             ares_expand_name(data4.data() + 5, data4.data(), data4.size(),
427                              &name, &enclen));
428 
429   // Label too long: 64-byte label, with invalid top 2 bits of length (01).
430   std::vector<byte> data5 = {0x40,
431                              '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
432                              '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
433                              '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
434                              '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
435                              0x00};
436   EXPECT_EQ(ARES_EBADNAME,
437             ares_expand_name(data5.data(), data5.data(), data5.size(),
438                              &name, &enclen)) << name;
439 
440   // Incomplete indirect length
441   std::vector<byte> data6 = {0x03, 'c', 'o', 'm', 0x00, 0xC0};
442   EXPECT_EQ(ARES_EBADNAME,
443             ares_expand_name(data6.data() + 5, data6.data(), data6.size(),
444                              &name, &enclen));
445 
446   // Indirection loops
447   std::vector<byte> data7 = {0xC0, 0x02, 0xC0, 0x00};
448   EXPECT_EQ(ARES_EBADNAME,
449             ares_expand_name(data7.data(), data7.data(), data7.size(),
450                              &name, &enclen));
451   std::vector<byte> data8 = {3, 'd', 'e', 'f', 0xC0, 0x08, 0x00, 0x00,
452                              3, 'a', 'b', 'c', 0xC0, 0x00};
453   EXPECT_EQ(ARES_EBADNAME,
454             ares_expand_name(data8.data(), data8.data(), data8.size(),
455                              &name, &enclen));
456   std::vector<byte> data9 = {0x12, 0x23,  // start 2 bytes in
457                              3, 'd', 'e', 'f', 0xC0, 0x02};
458   EXPECT_EQ(ARES_EBADNAME,
459             ares_expand_name(data9.data() + 2, data9.data(), data9.size(),
460                              &name, &enclen));
461 }
462 
TEST_F(LibraryTest,CreateEDNSQuery)463 TEST_F(LibraryTest, CreateEDNSQuery) {
464   byte* p;
465   int len;
466   EXPECT_EQ(ARES_SUCCESS,
467             ares_create_query("example.com", ns_c_in, ns_t_a, 0x1234, 0,
468                               &p, &len, 1280));
469   std::vector<byte> data(p, p + len);
470   ares_free_string(p);
471 
472   std::string actual = PacketToString(data);
473   DNSPacket pkt;
474   pkt.set_qid(0x1234).add_question(new DNSQuestion("example.com", ns_t_a))
475     .add_additional(new DNSOptRR(0, 1280));
476   std::string expected = PacketToString(pkt.data());
477   EXPECT_EQ(expected, actual);
478 }
479 
TEST_F(LibraryTest,CreateRootQuery)480 TEST_F(LibraryTest, CreateRootQuery) {
481   byte* p;
482   int len;
483   ares_create_query(".", ns_c_in, ns_t_a, 0x1234, 0, &p, &len, 0);
484   std::vector<byte> data(p, p + len);
485   ares_free_string(p);
486 
487   std::string actual = PacketToString(data);
488   DNSPacket pkt;
489   pkt.set_qid(0x1234).add_question(new DNSQuestion("", ns_t_a));
490   std::string expected = PacketToString(pkt.data());
491   EXPECT_EQ(expected, actual);
492 }
493 
TEST_F(LibraryTest,Version)494 TEST_F(LibraryTest, Version) {
495   // Assume linked to same version
496   EXPECT_EQ(std::string(ARES_VERSION_STR),
497             std::string(ares_version(nullptr)));
498   int version;
499   ares_version(&version);
500   EXPECT_EQ(ARES_VERSION, version);
501 }
502 
TEST_F(LibraryTest,Strerror)503 TEST_F(LibraryTest, Strerror) {
504   EXPECT_EQ("Successful completion",
505             std::string(ares_strerror(ARES_SUCCESS)));
506   EXPECT_EQ("DNS query cancelled",
507             std::string(ares_strerror(ARES_ECANCELLED)));
508   EXPECT_EQ("unknown",
509             std::string(ares_strerror(99)));
510 }
511 
TEST_F(LibraryTest,ExpandString)512 TEST_F(LibraryTest, ExpandString) {
513   std::vector<byte> s1 = { 3, 'a', 'b', 'c'};
514   char* result = nullptr;
515   long len;
516   EXPECT_EQ(ARES_SUCCESS,
517             ares_expand_string(s1.data(), s1.data(), s1.size(),
518                                (unsigned char**)&result, &len));
519   EXPECT_EQ("abc", std::string(result));
520   EXPECT_EQ(1 + 3, len);  // amount of data consumed includes 1 byte len
521   ares_free_string(result);
522   result = nullptr;
523   EXPECT_EQ(ARES_EBADSTR,
524             ares_expand_string(s1.data() + 1, s1.data(), s1.size(),
525                                (unsigned char**)&result, &len));
526   EXPECT_EQ(ARES_EBADSTR,
527             ares_expand_string(s1.data() + 4, s1.data(), s1.size(),
528                                (unsigned char**)&result, &len));
529   SetAllocSizeFail(3 + 1);
530   EXPECT_EQ(ARES_ENOMEM,
531             ares_expand_string(s1.data(), s1.data(), s1.size(),
532                                (unsigned char**)&result, &len));
533 }
534 
535 }  // namespace test
536 }  // namespace ares
537