]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - contrib/ntp/tests/libntp/g_sockaddrtest.h
Fix a regression with SA-15:24 patch that prevented NIS from
[FreeBSD/releng/10.2.git] / contrib / ntp / tests / libntp / g_sockaddrtest.h
1 #ifndef TESTS_SOCKADDRTEST_H
2 #define TESTS_SOCKADDRTEST_H
3
4 #include "g_libntptest.h"
5
6 extern "C" {
7 #include "ntp.h"
8 };
9
10 class sockaddrtest : public libntptest {
11 protected:
12         ::testing::AssertionResult IsEqual(const sockaddr_u &expected, const sockaddr_u &actual) {
13                 if (expected.sa.sa_family != actual.sa.sa_family) {
14                         return ::testing::AssertionFailure()
15                                 << "Expected sa_family: " << expected.sa.sa_family
16                                 << " but got: " << actual.sa.sa_family;
17                 }
18
19                 if (actual.sa.sa_family == AF_INET) { // IPv4
20                         if (expected.sa4.sin_port == actual.sa4.sin_port &&
21                                 memcmp(&expected.sa4.sin_addr, &actual.sa4.sin_addr,
22                                            sizeof(in_addr)) == 0) {
23                                 return ::testing::AssertionSuccess();
24                         } else {
25                                 return ::testing::AssertionFailure()
26                                         << "IPv4 comparision failed, expected: "
27                                         << expected.sa4.sin_addr.s_addr
28                                         << "(" << socktoa(&expected) << ")"
29                                         << " but was: "
30                                         << actual.sa4.sin_addr.s_addr
31                                         << "(" << socktoa(&actual) << ")";
32                         }
33                 } else if (actual.sa.sa_family == AF_INET6) { //IPv6
34                         if (expected.sa6.sin6_port == actual.sa6.sin6_port &&
35                                 memcmp(&expected.sa6.sin6_addr, &actual.sa6.sin6_addr,
36                                            sizeof(in6_addr)) == 0) {
37                                 return ::testing::AssertionSuccess();
38                         } else {
39                                 return ::testing::AssertionFailure()
40                                         << "IPv6 comparision failed";
41                         }
42                 } else { // Unknown family
43                         return ::testing::AssertionFailure()
44                                 << "Unknown sa_family: " << actual.sa.sa_family;
45                 }
46         }
47
48         sockaddr_u CreateSockaddr4(const char* address, unsigned int port) {
49                 sockaddr_u s;
50                 s.sa4.sin_family = AF_INET;
51                 s.sa4.sin_addr.s_addr = inet_addr(address);
52                 SET_PORT(&s, port);
53
54                 return s;
55         }
56 };
57
58 #endif // TESTS_SOCKADDRTEST_H
59