"Fossies" - the Fresh Open Source Software Archive

Member "ntp-4.2.8p15/tests/libntp/netof.c" (23 Jun 2020, 2261 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 "netof.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 
    3 #include "ntp_stdlib.h"
    4 #include "ntp_calendar.h"
    5 
    6 #include "unity.h"
    7 
    8 #include "sockaddrtest.h"
    9 
   10 
   11 void setUp(void);
   12 void test_ClassBAddress(void);
   13 void test_ClassCAddress(void);
   14 void test_ClassAAddress(void);
   15 void test_IPv6Address(void);
   16 
   17 
   18 void
   19 setUp(void)
   20 {
   21     init_lib();
   22 
   23     return;
   24 }
   25 
   26 
   27 void
   28 test_ClassBAddress(void)
   29 {
   30     sockaddr_u input = CreateSockaddr4("172.16.2.1", NTP_PORT);
   31     sockaddr_u expected = CreateSockaddr4("172.16.0.0", NTP_PORT);
   32 
   33     sockaddr_u* actual = netof(&input);
   34 
   35     TEST_ASSERT_TRUE(actual != NULL);
   36     TEST_ASSERT_TRUE(IsEqual(expected, *actual));
   37 
   38     return;
   39 }
   40 
   41 void
   42 test_ClassCAddress(void)
   43 {
   44     sockaddr_u input = CreateSockaddr4("192.0.2.255", NTP_PORT);
   45     sockaddr_u expected = CreateSockaddr4("192.0.2.0", NTP_PORT);
   46 
   47     sockaddr_u* actual = netof(&input);
   48 
   49     TEST_ASSERT_TRUE(actual != NULL);
   50     TEST_ASSERT_TRUE(IsEqual(expected, *actual));
   51 
   52     return;
   53 }
   54 
   55 
   56 void
   57 test_ClassAAddress(void)
   58 {
   59     /* Class A addresses are assumed to be classless,
   60      * thus the same address should be returned.
   61      */
   62     sockaddr_u input = CreateSockaddr4("10.20.30.40", NTP_PORT);
   63     sockaddr_u expected = CreateSockaddr4("10.20.30.40", NTP_PORT);
   64 
   65     sockaddr_u* actual = netof(&input);
   66 
   67     TEST_ASSERT_TRUE(actual != NULL);
   68     TEST_ASSERT_TRUE(IsEqual(expected, *actual));
   69 
   70     return;
   71 }
   72 
   73 void
   74 test_IPv6Address(void)
   75 {
   76     /* IPv6 addresses are assumed to have 64-bit host- and 64-bit network parts. */
   77     const struct in6_addr input_address = { { {
   78         0x20, 0x01, 0x0d, 0xb8,
   79         0x85, 0xa3, 0x08, 0xd3, 
   80         0x13, 0x19, 0x8a, 0x2e,
   81         0x03, 0x70, 0x73, 0x34
   82     } } }; // 2001:0db8:85a3:08d3:1319:8a2e:0370:7334
   83 
   84     const struct in6_addr expected_address = { { {
   85         0x20, 0x01, 0x0d, 0xb8,
   86         0x85, 0xa3, 0x08, 0xd3, 
   87         0x00, 0x00, 0x00, 0x00,
   88         0x00, 0x00, 0x00, 0x00
   89     } } }; // 2001:0db8:85a3:08d3:0000:0000:0000:0000
   90 
   91     sockaddr_u input;
   92     memset(&input, 0, sizeof(input));
   93     input.sa6.sin6_family = AF_INET6;
   94     input.sa6.sin6_addr = input_address;
   95     SET_PORT(&input, 3000);
   96 
   97     sockaddr_u expected;
   98     memset(&expected, 0, sizeof(expected));
   99     expected.sa6.sin6_family = AF_INET6;
  100     expected.sa6.sin6_addr = expected_address;
  101     SET_PORT(&expected, 3000);
  102 
  103     sockaddr_u* actual = netof(&input);
  104 
  105     TEST_ASSERT_TRUE(actual != NULL);
  106     TEST_ASSERT_TRUE(IsEqual(expected, *actual));
  107 
  108     return;
  109 }