"Fossies" - the Fresh Open Source Software Archive

Member "ntp-4.2.8p15/tests/libntp/decodenetnum.c" (23 Jun 2020, 5463 Bytes) of package /linux/misc/ntp-4.2.8p15.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "decodenetnum.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 4.2.8p14_vs_4.2.8p15.

    1 #include "config.h"
    2 #include "ntp_stdlib.h"
    3 #include "sockaddrtest.h"
    4 
    5 #include "unity.h"
    6 
    7 void setUp(void);
    8 extern void test_IPv4AddressOnly(void);
    9 extern void test_IPv4AddressWithPort(void);
   10 extern void test_IPv6AddressOnly(void);
   11 extern void test_IPv6AddressWithPort(void);
   12 extern void test_IPv6AddressWithScope(void);
   13 extern void test_IPv6AddressWithPortAndScope(void);
   14 extern void test_IllegalAddress(void);
   15 extern void test_IllegalCharInPort(void);
   16 extern void test_NameBufOverflow(void);
   17 
   18 /*
   19  * NOTE: The IPv6 specific tests are reduced to stubs when IPv6 is
   20  * disabled.
   21  *
   22  * ISC_PLATFORM_HAVEIPV6 checks if system has IPV6 capabilies. WANTIPV6
   23  * ISC_PLATFORM_WANTIPV6 can be changed with build --disable-ipv6.
   24  *
   25  * If we want IPv6 but don't have it, the tests should fail, I think.
   26  */
   27 void
   28 setUp(void)
   29 {
   30     init_lib();
   31 }
   32 
   33 
   34 void
   35 test_IPv4AddressOnly(void)
   36 {
   37     const char *str = "192.0.2.1";
   38     sockaddr_u actual;
   39 
   40     sockaddr_u expected;
   41     memset(&expected, 0, sizeof(expected));
   42     expected.sa4.sin_family = AF_INET;
   43     expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
   44     SET_PORT(&expected, NTP_PORT);
   45 
   46     TEST_ASSERT_TRUE(decodenetnum(str, &actual));
   47     TEST_ASSERT_TRUE(IsEqual(expected, actual));
   48 }
   49 
   50 void
   51 test_IPv4AddressWithPort(void)
   52 {
   53     const char *str = "192.0.2.2:2000";
   54     sockaddr_u actual;
   55 
   56     sockaddr_u expected;
   57     memset(&expected, 0, sizeof(expected));
   58     expected.sa4.sin_family = AF_INET;
   59     expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.2");
   60     SET_PORT(&expected, 2000);
   61 
   62     TEST_ASSERT_TRUE(decodenetnum(str, &actual));
   63     TEST_ASSERT_TRUE(IsEqual(expected, actual));
   64 }
   65 
   66 
   67 void
   68 test_IPv6AddressOnly(void)
   69 {
   70 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
   71 
   72     const struct in6_addr address = {
   73         0x20, 0x01, 0x0d, 0xb8,
   74         0x85, 0xa3, 0x08, 0xd3,
   75         0x13, 0x19, 0x8a, 0x2e,
   76         0x03, 0x70, 0x73, 0x34
   77     };
   78 
   79     const char *str1 = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334";
   80     const char *str2 = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]";
   81     sockaddr_u actual;
   82 
   83     sockaddr_u expected;
   84     memset(&expected, 0, sizeof(expected));
   85     expected.sa6.sin6_family = AF_INET6;
   86     expected.sa6.sin6_addr = address;
   87     SET_PORT(&expected, NTP_PORT);
   88 
   89     TEST_ASSERT_TRUE(decodenetnum(str1, &actual));
   90     TEST_ASSERT_TRUE(IsEqual(expected, actual));
   91 
   92     TEST_ASSERT_TRUE(decodenetnum(str2, &actual));
   93     TEST_ASSERT_TRUE(IsEqual(expected, actual));
   94 
   95 #else
   96 
   97     TEST_IGNORE_MESSAGE("IPV6 disabled in build");
   98 
   99 #endif
  100 }
  101 
  102 
  103 void
  104 test_IPv6AddressWithPort(void)
  105 {
  106 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
  107 
  108     const struct in6_addr address = {
  109         0x20, 0x01, 0x0d, 0xb8,
  110         0x85, 0xa3, 0x08, 0xd3,
  111         0x13, 0x19, 0x8a, 0x2e,
  112         0x03, 0x70, 0x73, 0x34
  113     };
  114 
  115     const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334]:3000";
  116     sockaddr_u actual;
  117 
  118     sockaddr_u expected;
  119     memset(&expected, 0, sizeof(expected));
  120     expected.sa6.sin6_family = AF_INET6;
  121     expected.sa6.sin6_addr = address;
  122     SET_PORT(&expected, 3000);
  123 
  124     TEST_ASSERT_TRUE(decodenetnum(str, &actual));
  125     TEST_ASSERT_TRUE(IsEqual(expected, actual));
  126 
  127 #else
  128 
  129     TEST_IGNORE_MESSAGE("IPV6 disabled in build");
  130 
  131 #endif
  132 }
  133 
  134 void test_IPv6AddressWithScope(void)
  135 {
  136 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
  137 
  138     const struct in6_addr address = {
  139         0x20, 0x01, 0x0d, 0xb8,
  140         0x85, 0xa3, 0x08, 0xd3,
  141         0x13, 0x19, 0x8a, 0x2e,
  142         0x03, 0x70, 0x73, 0x34
  143     };
  144 
  145     const char *str1 = "2001:0db8:85a3:08d3:1319:8a2e:0370:7334%42";
  146     const char *str2 = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334%42]";
  147     sockaddr_u actual;
  148 
  149     sockaddr_u expected;
  150     memset(&expected, 0, sizeof(expected));
  151     expected.sa6.sin6_family = AF_INET6;
  152     expected.sa6.sin6_addr = address;
  153     expected.sa6.sin6_scope_id = 42;
  154     SET_PORT(&expected, NTP_PORT);
  155 
  156     TEST_ASSERT_TRUE(decodenetnum(str1, &actual));
  157     TEST_ASSERT_TRUE(IsEqual(expected, actual));
  158 
  159     TEST_ASSERT_TRUE(decodenetnum(str2, &actual));
  160     TEST_ASSERT_TRUE(IsEqual(expected, actual));
  161 
  162 #else
  163 
  164     TEST_IGNORE_MESSAGE("IPV6 disabled in build");
  165 
  166 #endif
  167 }
  168 
  169 void test_IPv6AddressWithPortAndScope(void)
  170 {
  171 #if defined(ISC_PLATFORM_HAVEIPV6) && defined(WANT_IPV6)
  172 
  173     const struct in6_addr address = {
  174         0x20, 0x01, 0x0d, 0xb8,
  175         0x85, 0xa3, 0x08, 0xd3,
  176         0x13, 0x19, 0x8a, 0x2e,
  177         0x03, 0x70, 0x73, 0x34
  178     };
  179 
  180     const char *str = "[2001:0db8:85a3:08d3:1319:8a2e:0370:7334%42]:3000";
  181     sockaddr_u actual;
  182 
  183     sockaddr_u expected;
  184     memset(&expected, 0, sizeof(expected));
  185     expected.sa6.sin6_family = AF_INET6;
  186     expected.sa6.sin6_addr = address;
  187     expected.sa6.sin6_scope_id = 42;
  188     SET_PORT(&expected, 3000);
  189 
  190     TEST_ASSERT_TRUE(decodenetnum(str, &actual));
  191     TEST_ASSERT_TRUE(IsEqual(expected, actual));
  192 
  193 #else
  194 
  195     TEST_IGNORE_MESSAGE("IPV6 disabled in build");
  196 
  197 #endif
  198 }
  199 
  200 void
  201 test_IllegalAddress(void)
  202 {
  203     const char *str = "192.0.2.270:2000";
  204     sockaddr_u actual;
  205 
  206     TEST_ASSERT_FALSE(decodenetnum(str, &actual));
  207 }
  208 
  209 
  210 void
  211 test_IllegalCharInPort(void)
  212 {
  213     /* An illegal port does not make the decodenetnum fail, but instead
  214      * makes it use the standard port.
  215      */
  216     const char *str = "192.0.2.1:a700";
  217     sockaddr_u actual;
  218 
  219     sockaddr_u expected;
  220     memset(&expected, 0, sizeof(expected));
  221     expected.sa4.sin_family = AF_INET;
  222     expected.sa4.sin_addr.s_addr = inet_addr("192.0.2.1");
  223     SET_PORT(&expected, NTP_PORT);
  224 
  225     TEST_ASSERT_TRUE(decodenetnum(str, &actual));
  226     TEST_ASSERT_TRUE(IsEqual(expected, actual));
  227 }
  228 
  229 void
  230 test_NameBufOverflow(void)
  231 {
  232     const char *str =
  233         "loremipsumloremipsumloremipsumloremipsumloremipsum"
  234         "loremipsumloremipsumloremipsumloremipsum";
  235 
  236     sockaddr_u actual;
  237     TEST_ASSERT_FALSE(decodenetnum(str, &actual));
  238 }