TESTS: Add test for getaddrinfo.
authorRobin Hack <hack.robin@gmail.com>
Mon, 30 Mar 2015 12:36:29 +0000 (14:36 +0200)
committerMichael Adam <obnox@samba.org>
Wed, 7 Oct 2015 13:58:32 +0000 (15:58 +0200)
Test tries to get multiple and mixed (ipv4 and ipv6) records.

Signed-off-by: Robin Hack <hack.robin@gmail.com>
Reviewed-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
tests/hosts.in
tests/test_getaddrinfo.c

index 59195aa39da5d00bef84c0a27a0e0fe890653f7a..3b03783fa9e3959ae31dcbff1e2c71618d44016d 100644 (file)
@@ -5,3 +5,8 @@
 ::13            beteigeuze.galaxy.site beteigeuze mail   
 127.0.0.14 krikkit.galaxy.site
 ::14 krikkit.galaxy.site
+127.1.1.1              pumpkin.bunny.net
+127.0.0.66             pumpkin.bunny.net
+2666::22               pumpkin.bunny.net
+DEAD:BEEF:1:2:3::4     pumpkin.bunny.net
+B00B:5::4              pumpkin.bunny.net
index 548910245d0669893dc4697adc384c45425a21fb..a13cc511bd7c9c6526eab4d795fe3034bb8d261e 100644 (file)
@@ -389,6 +389,78 @@ static void test_nwrap_getaddrinfo_ipv6(void **state)
        freeaddrinfo(res);
 }
 
+static void test_nwrap_getaddrinfo_multiple_mixed(void **state)
+{
+       struct addrinfo *res, *res_head;
+       struct addrinfo hints;
+       unsigned int ipv6_count = 0;
+       unsigned int ipv4_count = 0;
+       int rc;
+       int p;
+
+       struct sockaddr_in *r_addr;
+       struct sockaddr_in6 *r_addr6;
+
+       const char *result;
+       const char *value;
+
+       /* For inet_ntop call */
+       char buf[4096];
+
+       /* 2 - ipv4 and 3 ipv6 addresses */
+       const char *ipvX_results[] = {"127.1.1.1", "127.0.0.66", "2666::22", "B00B:5::4", "DEAD:BEEF:1:2:3::4", NULL};
+
+       (void) state; /* unused */
+
+        memset(&hints, '\0', sizeof(struct addrinfo));
+        hints.ai_protocol = IPPROTO_TCP;
+        hints.ai_family = AF_UNSPEC;
+        hints.ai_socktype = SOCK_STREAM;
+
+       rc = getaddrinfo("pumpkin.bunny.net", NULL, &hints, &res_head);
+       assert_return_code(rc, 0);
+       assert_non_null(res_head);
+
+       for (res = res_head; res != NULL; res = res->ai_next) {
+               if (res->ai_family == AF_INET) {
+                       r_addr = (struct sockaddr_in *) res->ai_addr;
+                       assert_non_null(r_addr);
+                       ++ipv4_count;
+                       result = inet_ntop(AF_INET,
+                                          &r_addr->sin_addr,
+                                          buf,
+                                          4096);
+               } else if (res->ai_family == AF_INET6) {
+                       r_addr6 = (struct sockaddr_in6 *) res->ai_addr;
+                       assert_non_null(r_addr6);
+                       ++ipv6_count;
+                       result = inet_ntop(AF_INET6,
+                                          &r_addr6->sin6_addr,
+                                          buf,
+                                          4096);
+               } else {
+                       /* Unknown family type */
+                       assert_int_equal(1,0);
+               }
+
+               /* Important part */
+               assert_non_null(result);
+
+               /* This could be part of cmocka library */
+               for (value = ipvX_results[0], p = 0; value != NULL; value = ipvX_results[++p]) {
+                       if (strcasecmp(value, result) == 0) {
+                               break;
+                       }
+               }
+               assert_non_null(value);
+       }
+
+       assert_int_equal(ipv6_count, 3);
+       assert_int_equal(ipv4_count, 2);
+
+       freeaddrinfo(res_head);
+}
+
 int main(void) {
        int rc;
 
@@ -401,6 +473,7 @@ int main(void) {
                cmocka_unit_test(test_nwrap_getaddrinfo_null),
                cmocka_unit_test(test_nwrap_getaddrinfo_dot),
                cmocka_unit_test(test_nwrap_getaddrinfo_ipv6),
+               cmocka_unit_test(test_nwrap_getaddrinfo_multiple_mixed),
        };
 
        rc = cmocka_run_group_tests(tests, NULL, NULL);