|
| 1 | +#include "unittest.h" |
| 2 | + |
| 3 | +#include <sys/socket.h> |
| 4 | +#include <sys/types.h> |
| 5 | +#include <ifaddrs.h> |
| 6 | +#include <netdb.h> |
| 7 | + |
| 8 | + |
| 9 | +#include "../../src/mdnsd.h" |
| 10 | + |
| 11 | + |
| 12 | +/* |
| 13 | + * Mock-ups |
| 14 | + */ |
| 15 | +int __wrap_getifaddrs(struct ifaddrs **ifap) |
| 16 | +{ |
| 17 | + *ifap = mock_ptr_type(struct ifaddrs *); |
| 18 | + return mock_type(int); |
| 19 | +} |
| 20 | + |
| 21 | +void __wrap_freeifaddrs(struct ifaddrs *ifa) |
| 22 | +{ |
| 23 | + /* Nothing allocated, nothing to free. */ |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +struct sockaddr_in ipv4_10_0_20_1 = { AF_INET, 0, { 0x0114000a } }; |
| 29 | +struct sockaddr_in ipv4_255_255_255_0 = { AF_INET, 0, { 0x00ffffff } }; |
| 30 | +struct sockaddr_in ipv4_LL_169_254_100_32 = { AF_INET, 0, { 0x2064fea9 } }; |
| 31 | + |
| 32 | +struct ifaddrs one_ifc_one_ip4 = { |
| 33 | + NULL, |
| 34 | + "if0c1ip4", IFF_UP | IFF_BROADCAST | IFF_MULTICAST, |
| 35 | + (struct sockaddr*)&ipv4_10_0_20_1, (struct sockaddr*)&ipv4_255_255_255_0, NULL, NULL |
| 36 | +}; |
| 37 | + |
| 38 | + |
| 39 | +struct ifaddrs one_ifc_global_ip4_ll_ip4[] = { |
| 40 | + { |
| 41 | + one_ifc_global_ip4_ll_ip4 + 1, |
| 42 | + "if0Gip4LLip4", IFF_UP | IFF_BROADCAST | IFF_MULTICAST, |
| 43 | + (struct sockaddr*)&ipv4_10_0_20_1, (struct sockaddr*)&ipv4_255_255_255_0, NULL, NULL |
| 44 | + }, |
| 45 | + { |
| 46 | + NULL, |
| 47 | + "if0Gip4LLip4", IFF_UP | IFF_BROADCAST | IFF_MULTICAST, |
| 48 | + (struct sockaddr*)&ipv4_LL_169_254_100_32, (struct sockaddr*)&ipv4_255_255_255_0, NULL, NULL |
| 49 | + } |
| 50 | +}; |
| 51 | + |
| 52 | + |
| 53 | +struct ifaddrs one_ifc_ll_ip4_global_ip4[] = { |
| 54 | + { |
| 55 | + one_ifc_ll_ip4_global_ip4 + 1, |
| 56 | + "if0LLip4Gip4", IFF_UP | IFF_BROADCAST | IFF_MULTICAST, |
| 57 | + (struct sockaddr*)&ipv4_LL_169_254_100_32, (struct sockaddr*)&ipv4_255_255_255_0, NULL, NULL |
| 58 | + }, |
| 59 | + { |
| 60 | + NULL, |
| 61 | + "if0LLip4Gip4", IFF_UP | IFF_BROADCAST | IFF_MULTICAST, |
| 62 | + (struct sockaddr*)&ipv4_10_0_20_1, (struct sockaddr*)&ipv4_255_255_255_0, NULL, NULL |
| 63 | + } |
| 64 | +}; |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | +/* |
| 70 | + * Tests |
| 71 | + */ |
| 72 | + |
| 73 | +static void test_iface_init_no_ifc(__attribute__((__unused__)) void **state) |
| 74 | +{ |
| 75 | + will_return(__wrap_getifaddrs, NULL); |
| 76 | + will_return(__wrap_getifaddrs, 0); |
| 77 | + |
| 78 | + iface_init(NULL); |
| 79 | + |
| 80 | + struct iface *iface = iface_iterator(1); |
| 81 | + assert_null(iface); |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | + |
| 86 | +/* Helper function testing that one IPv4 is set on one interface. */ |
| 87 | +static void check_one_iface_one_global_ipv4(char* ifname) |
| 88 | +{ |
| 89 | + struct iface *iface = iface_iterator(1); |
| 90 | + assert_non_null(iface); |
| 91 | + |
| 92 | + /* Ignoring the (undeterminable) ifindex. */ |
| 93 | + |
| 94 | + assert_int_equal(0, iface->unused); |
| 95 | + assert_int_equal(1, iface->changed); |
| 96 | + assert_string_equal(ifname, iface->ifname); |
| 97 | + |
| 98 | + assert_int_equal(ipv4_10_0_20_1.sin_addr.s_addr, iface->inaddr.s_addr); |
| 99 | + assert_int_equal(0x00000000, iface->inaddr_old.s_addr); |
| 100 | + |
| 101 | + assert_int_equal(-1, iface->sd); |
| 102 | + assert_null(iface->mdns); |
| 103 | + assert_int_equal(1, iface->hostid); |
| 104 | + |
| 105 | + |
| 106 | + iface = iface_iterator(0); |
| 107 | + assert_null(iface); |
| 108 | +} |
| 109 | + |
| 110 | +/* |
| 111 | + * Test behaviour when no interfaces exist. |
| 112 | + */ |
| 113 | +static void test_iface_init_one_ifc_ipv4(__attribute__((__unused__)) void **state) |
| 114 | +{ |
| 115 | + will_return(__wrap_getifaddrs, &one_ifc_one_ip4); |
| 116 | + will_return(__wrap_getifaddrs, 0); |
| 117 | + |
| 118 | + iface_init(NULL); |
| 119 | + |
| 120 | + check_one_iface_one_global_ipv4(one_ifc_one_ip4.ifa_name); |
| 121 | +} |
| 122 | + |
| 123 | +/* |
| 124 | + * Test that a global IPv4 address is not overwritten with a link local one. |
| 125 | + * One interface, only IPv4. |
| 126 | + */ |
| 127 | +static void test_iface_init_one_ifc_global_ipv4_LL_ipv4(__attribute__((__unused__)) void **state) |
| 128 | +{ |
| 129 | + will_return(__wrap_getifaddrs, one_ifc_global_ip4_ll_ip4); |
| 130 | + will_return(__wrap_getifaddrs, 0); |
| 131 | + |
| 132 | + iface_init(NULL); |
| 133 | + |
| 134 | + check_one_iface_one_global_ipv4(one_ifc_global_ip4_ll_ip4->ifa_name); |
| 135 | +} |
| 136 | + |
| 137 | +/* |
| 138 | + * Test that a link local IPv4 address is overwritten by a global one. |
| 139 | + * One interface, only IPv4. |
| 140 | + */ |
| 141 | +static void test_iface_init_one_ifc_LL_ipv4_global_ipv4(__attribute__((__unused__)) void **state) |
| 142 | +{ |
| 143 | + will_return(__wrap_getifaddrs, one_ifc_ll_ip4_global_ip4); |
| 144 | + will_return(__wrap_getifaddrs, 0); |
| 145 | + |
| 146 | + iface_init(NULL); |
| 147 | + |
| 148 | + check_one_iface_one_global_ipv4(one_ifc_ll_ip4_global_ip4->ifa_name); |
| 149 | +} |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | +static int teardown(__attribute__((__unused__)) void **state) |
| 156 | +{ |
| 157 | + iface_exit(); |
| 158 | +} |
| 159 | + |
| 160 | + |
| 161 | +int main(void) |
| 162 | +{ |
| 163 | + const struct CMUnitTest tests[] = { |
| 164 | + cmocka_unit_test(test_iface_init_no_ifc), |
| 165 | + cmocka_unit_test_teardown(test_iface_init_one_ifc_ipv4, teardown), |
| 166 | + cmocka_unit_test_teardown(test_iface_init_one_ifc_global_ipv4_LL_ipv4, teardown), |
| 167 | + cmocka_unit_test_teardown(test_iface_init_one_ifc_LL_ipv4_global_ipv4, teardown), |
| 168 | + }; |
| 169 | + |
| 170 | + return cmocka_run_group_tests(tests, NULL, NULL); |
| 171 | +} |
0 commit comments