File: NetworkUtilsTest.cpp

package info (click to toggle)
ola 0.10.8.nojsmin-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 16,656 kB
  • sloc: cpp: 132,274; python: 14,082; javascript: 6,774; sh: 4,616; ansic: 2,189; java: 518; xml: 253; makefile: 183
file content (174 lines) | stat: -rw-r--r-- 4,897 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 *
 * NetworkUtilsTest.cpp
 * Test fixture for the NetworkUtils class
 * Copyright (C) 2005 Simon Newton
 */

#include <cppunit/extensions/HelperMacros.h>

#ifdef _WIN32
#include <ola/win/CleanWinSock2.h>
#endif  // _WIN32

#include <string>
#include <vector>

#include "ola/network/NetworkUtils.h"
#include "ola/Logging.h"
#include "ola/testing/TestUtils.h"

using ola::network::FQDN;
using ola::network::DomainNameFromFQDN;
using ola::network::Hostname;
using ola::network::HostnameFromFQDN;
using ola::network::HostToLittleEndian;
using ola::network::HostToNetwork;
using ola::network::IPV4Address;
using ola::network::LittleEndianToHost;
using ola::network::NameServers;
using ola::network::DefaultRoute;
using ola::network::NetworkToHost;

using std::string;
using std::vector;

class NetworkUtilsTest: public CppUnit::TestFixture {
  CPPUNIT_TEST_SUITE(NetworkUtilsTest);
  CPPUNIT_TEST(testToFromNetwork);
  CPPUNIT_TEST(testToFromLittleEndian);
  CPPUNIT_TEST(testNameProcessing);
  CPPUNIT_TEST(testNameServers);
  CPPUNIT_TEST(testDefaultRoute);
  CPPUNIT_TEST_SUITE_END();

 public:
    void setUp();
    void tearDown();
    void testToFromNetwork();
    void testToFromLittleEndian();
    void testNameProcessing();
    void testNameServers();
    void testDefaultRoute();
};

CPPUNIT_TEST_SUITE_REGISTRATION(NetworkUtilsTest);

/*
 * Setup networking subsystem
 */
void NetworkUtilsTest::setUp() {
#if _WIN32
  WSADATA wsa_data;
  int result = WSAStartup(MAKEWORD(2, 0), &wsa_data);
  OLA_ASSERT_EQ(result, 0);
#endif  // _WIN32
}


/*
 * Cleanup the networking subsystem
 */
void NetworkUtilsTest::tearDown() {
#ifdef _WIN32
  WSACleanup();
#endif  // _WIN32
}

/*
 * Check that we can convert to/from network byte order
 */
void NetworkUtilsTest::testToFromNetwork() {
  uint8_t v1 = 10;
  OLA_ASSERT_EQ(v1, HostToNetwork(v1));
  OLA_ASSERT_EQ(v1, NetworkToHost(HostToNetwork(v1)));

  uint16_t v2 = 0x0102;
  OLA_ASSERT_EQ(v2, NetworkToHost(HostToNetwork(v2)));

  uint32_t v3 = 0x01020304;
  OLA_ASSERT_EQ(v3, NetworkToHost(HostToNetwork(v3)));
}


/*
 * Check that we can convert to/from little endian order
 */
void NetworkUtilsTest::testToFromLittleEndian() {
  uint8_t v1 = 10;
  OLA_ASSERT_EQ(v1, HostToLittleEndian(v1));
  OLA_ASSERT_EQ(v1, LittleEndianToHost(HostToLittleEndian(v1)));

  uint16_t v2 = 0x0102;
  OLA_ASSERT_EQ(v2, LittleEndianToHost(HostToLittleEndian(v2)));

  uint32_t v3 = 0x01020304;
  OLA_ASSERT_EQ(v3, LittleEndianToHost(HostToLittleEndian(v3)));

  int8_t v4 = -10;
  OLA_ASSERT_EQ(v4, HostToLittleEndian(v4));
  OLA_ASSERT_EQ(v4, LittleEndianToHost(HostToLittleEndian(v4)));

  int16_t v5 = -0x0102;
  OLA_ASSERT_EQ(v5, LittleEndianToHost(HostToLittleEndian(v5)));

  int32_t v6 = -0x01020304;
  OLA_ASSERT_EQ(v6, LittleEndianToHost(HostToLittleEndian(v6)));
}


/*
 * Check that name processing works
 */
void NetworkUtilsTest::testNameProcessing() {
  // HostnameFromFQDN
  OLA_ASSERT_EQ(string(""), HostnameFromFQDN(""));
  OLA_ASSERT_EQ(string("foo"), HostnameFromFQDN("foo"));
  OLA_ASSERT_EQ(string("foo"), HostnameFromFQDN("foo.bar"));
  OLA_ASSERT_EQ(string("foo"), HostnameFromFQDN("foo.barbaz"));
  OLA_ASSERT_EQ(string("foo"), HostnameFromFQDN("foo.bar.com"));

  // DomainNameFromFQDN
  OLA_ASSERT_EQ(string(""), DomainNameFromFQDN(""));
  OLA_ASSERT_EQ(string(""), DomainNameFromFQDN("foo"));
  OLA_ASSERT_EQ(string("bar"), DomainNameFromFQDN("foo.bar"));
  OLA_ASSERT_EQ(string("barbaz"), DomainNameFromFQDN("foo.barbaz"));
  OLA_ASSERT_EQ(string("bar.com"), DomainNameFromFQDN("foo.bar.com"));

  // Check we were able to get the hostname
  OLA_ASSERT_GT(FQDN().length(), 0);
  OLA_ASSERT_GT(Hostname().length(), 0);
}


/*
 * Check that name server fetching returns true (it may not actually return any)
 */
void NetworkUtilsTest::testNameServers() {
  vector<IPV4Address> name_servers;
  OLA_ASSERT_TRUE(NameServers(&name_servers));
}


/*
 * Check that default route fetching returns true (it may not actually return
 * one)
 */
void NetworkUtilsTest::testDefaultRoute() {
  int32_t if_index;
  IPV4Address default_gateway;
  OLA_ASSERT_TRUE(DefaultRoute(&if_index, &default_gateway));
}