File: TestUDPListener.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (91 lines) | stat: -rw-r--r-- 2,319 bytes parent folder | download | duplicates (3)
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

#include "System/Net/UDPListener.h"
#include "System/Log/ILog.h"


#define CATCH_CONFIG_MAIN
#include "lib/catch.hpp"

class SocketTest {
public:
	SocketTest(){
	}
	void TestPort(int port, bool result = true)
	{
#ifndef NO_IPV6
		const std::string& err = netcode::UDPListener::TryBindSocket(port, socket, "::");
#else
		const std::string& err = netcode::UDPListener::TryBindSocket(port, socket, "127.0.0.1");
#endif
		INFO(err);
		CHECK(err.empty() == result);

	}
	void TestHost(const char* address, bool result = true)
	{
		const std::string& err = netcode::UDPListener::TryBindSocket(11111, socket, address);
		INFO(err);
		CHECK(err.empty() == result);
	}
private:
	std::shared_ptr<asio::ip::udp::socket> socket;
};

TEST_CASE("TryBindSocket")
{
	SocketTest t;

	// IP v4 & v6 addresses
	LOG("\nIP v4 & v6 addresses");
	t.TestHost("127.0.0.1");
	t.TestHost("0.0.0.0");
#ifndef NO_IPV6
	t.TestHost("::");
	t.TestHost("::1");

	// badly named invalid IP v6 addresses
	LOG("\nbadly named invalid IP v6 addresses");
	t.TestHost("::2", false);
	t.TestHost("fe80::224:1dff:fecf:df44/64", false);
	t.TestHost("fe80::224:1dff:fecf:df44", false);
	t.TestHost("::224:1dff:fecf:df44/64", false);
	t.TestHost("fe80::", false);
	t.TestHost("fe80", false);
	/*
		FIXME: for some reason this test works on windows (binds to ipv4 0.0.0.224) and fails on linux/osx

		:8 is reserved. :96 was IPv4 compatible IPv6 addresses, so ::2 would be 0.0.0.2.
		No implementation is required to support this sheme sheme any longer. That's why ::2 doesn't work
		::ffff:x:y/96 is new IPv4-Mapped IPv6 Address
		http://tools.ietf.org/html/rfc4291 sections 2.5.5.1 and 2.5.5.2
	*/
	//	TestHost("224:1dff:fecf:df44/64", false);
#endif

	// host-names
	LOG("\nhost-names");
	t.TestHost("localhost");
	t.TestHost("local.lan", false);
	t.TestHost("google.com", false);

	// normal ports
	LOG("\nnormal ports");
	t.TestPort(1024);
	t.TestPort(11111);
	t.TestPort(32000);
	t.TestPort(65535);

	// special ports (reserved for core services)
/*
	t.TestPort(0, false); //port 0 binds to a random port
	t.TestPort(1, false); // <1024 usally requires root permissions
	t.TestPort(128, false);
	t.TestPort(1023, false);
*/
	// out-of-range ports
	LOG("\nout-of-range ports");
	t.TestPort(65536, false);
	t.TestPort(65537, false);
	t.TestPort(-1, false);
}