-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path_test_net_util.cpp
More file actions
218 lines (181 loc) · 3.83 KB
/
_test_net_util.cpp
File metadata and controls
218 lines (181 loc) · 3.83 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* @file Tests for net_util module
* @brief
* @ref
* @author Yonhgwhan, Roh (fixbrain@gmail.com)
* @date 2018/09/19 created.
* @copyright All rights reserved by Yonghwan, Roh.
**/
#include "stdafx.h"
#include <vector>
#include "_MyLib/src/Win32Utils.h"
#include "_MyLib/src/net_util.h"
void dump_adapter(_In_ const PInetAdapter adapter)
{
//
// Aapter information
//
log_dbg
"\nAdapter, type=%u, friendly name=%ws, name=%s, desc=%ws, mac=%s",
adapter->interface_type,
adapter->friendly_name.c_str(),
adapter->name.c_str(),
adapter->desc.c_str(),
mac_to_str(adapter->physical_address).c_str()
log_end;
//
// DNS list
//
log_dbg "+ Dump DNS lists" log_end;
for (auto& dns : adapter->dns_list)
{
log_dbg " - dns=%s", ipv4_to_str(dns).c_str() log_end;
}
//
// Gateway list
//
log_dbg "+ Dump gateway lists" log_end;
for (auto& gw : adapter->gateway_list)
{
log_dbg " - gw=%s", ipv4_to_str(gw).c_str() log_end;
}
//
// IP information list
//
log_dbg "+ Dump IP assignments" log_end;
for (auto& ip : adapter->ip_info_list)
{
log_dbg
" - %s/%s",
ipv4_to_str(ip->ip).c_str(),
ipv4_to_str(ip->mask).c_str()
log_end;
}
}
bool test_get_adapters()
{
_mem_check_begin
{
init_net_util();
do
{
std::list<PInetAdapter> adapters;
_ASSERTE(true == get_inet_adapters(adapters));
for (auto& adapter : adapters)
{
dump_adapter(adapter);
}
for (auto& item : adapters)
{
delete item;
}
adapters.clear();
} while (false);
cleanup_net_util();
}
_mem_check_end;
return true;
}
bool test_get_addr_info()
{
_mem_check_begin
{
wchar_t* host_names[] ={
L"DESKTOP-IQPVKKP",
L"update.somma.kr",
L"api.somma.kr",
L"www.naver.com",
L"www.google.com"
};
for (int i = 0; i < sizeof(host_names) / sizeof(wchar_t*); ++i)
{
std::list<in_addr> ipv4_list;
std::list<in_addr6> ipv6_list;
_ASSERT(true == get_addr_infow(host_names[i],
&ipv4_list,
&ipv6_list));
log_info "host=%ws", host_names[i] log_end;
for (auto& ip4 : ipv4_list)
{
log_info " ipv4=%s", ipv4_to_str(ip4).c_str() log_end;
}
for (auto& ip6 : ipv6_list)
{
log_info " ipv6=%s", ipv6_to_str(ip6).c_str() log_end;
}
log_info "" log_end;
}
}
_mem_check_end;
return true;
}
bool test_is_reserved_ipv4()
{
const static struct ip_and_result
{
const char* ip;
const bool result;
} iar[] = {
{"10.0.0.2", true},
{"10.0.1.1", false},
{"192.168.0.121", true},
{"192.168.11.112", true},
{"192.167.11.112", false},
{"172.16.0.22", true},
{"172.16.8.22", true},
{"172.16.253.22", false},
{"224.0.0.8", true},
{"224.0.0.250", false},
{"255.255.255.255", true},
{"127.0.0.1", true},
{"169.254.0.1", true},
{"169.254.2.1", true},
{"169.253.2.1", false},
{"100.64.0.9", true},
{"240.0.0.1", true},
{"240.0.1.1", false},
{"0.0.0.2", true}
};
_mem_dump_console
_mem_check_begin
{
for (int i = 0; i < sizeof(iar) / sizeof(ip_and_result); ++i)
{
uint32_t netb_ip = 0xffffffff;
_ASSERTE(true == str_to_ipv4(MbsToWcsEx(iar[i].ip).c_str(), netb_ip));
bool r = is_reserved_ipv4(netb_ip);
_ASSERTE(iar[i].result == r);
}
}
_mem_check_end;
return true;
}
/// @biref
bool test_is_valid_ipv4()
{
const static struct ip_and_result
{
const char* ip;
const bool result;
} iar[] = {
{"10.0.0.2", true},
{"192.168.1.1", true},
{"1.0.0.-1", false},
{"1.0.0.0", true},
{"-1.0.2.1", false},
{"255.255.255.255", true},
{"2555.52.2.2", false},
{"255.255.1.256", false}
};
_mem_dump_console
_mem_check_begin
{
for (int i = 0; i < sizeof(iar) / sizeof(ip_and_result); ++i)
{
_ASSERTE(is_valid_ipv4(iar[i].ip) == iar[i].result);
log_dbg "(%s), ip=%s ", iar[i].result == true ? "valid" : "invalid", iar[i].ip log_end;
}
}
_mem_check_end;
return true;
}