source: filezilla/trunk/fuentes/tests/ipaddress.cpp @ 130

Last change on this file since 130 was 130, checked in by jrpelegrina, 4 years ago

First release to xenial

File size: 8.7 KB
Line 
1#include <filezilla.h>
2#include "directorylistingparser.h"
3#include <cppunit/extensions/HelperMacros.h>
4#include <list>
5
6/*
7 * This testsuite asserts the correctness of the
8 * functions handling IP addresses
9 */
10
11class CIPAddressTest : public CppUnit::TestFixture
12{
13        CPPUNIT_TEST_SUITE(CIPAddressTest);
14        CPPUNIT_TEST(testIsIpAddress);
15        CPPUNIT_TEST(testIPV6LongForm);
16        CPPUNIT_TEST(testIsRoutableAddress4);
17        CPPUNIT_TEST(testIsRoutableAddress6);
18        CPPUNIT_TEST_SUITE_END();
19
20public:
21        void setUp() {}
22        void tearDown() {}
23
24        void testIsIpAddress();
25        void testIPV6LongForm();
26        void testIsRoutableAddress4();
27        void testIsRoutableAddress6();
28
29protected:
30};
31
32CPPUNIT_TEST_SUITE_REGISTRATION(CIPAddressTest);
33
34void CIPAddressTest::testIsIpAddress()
35{
36        CPPUNIT_ASSERT(IsIpAddress(_T("255.255.255.255")));
37        CPPUNIT_ASSERT(IsIpAddress(_T("127.0.0.1")));
38        CPPUNIT_ASSERT(!IsIpAddress(_T("127.0.0.0.1")));
39        CPPUNIT_ASSERT(!IsIpAddress(_T("127.-0.0.1")));
40        CPPUNIT_ASSERT(!IsIpAddress(_T("127.a.0.1")));
41        CPPUNIT_ASSERT(!IsIpAddress(_T("127.256.0.1")));
42        CPPUNIT_ASSERT(!IsIpAddress(_T("127.0.1")));
43        CPPUNIT_ASSERT(!IsIpAddress(_T("127.0..1")));
44        CPPUNIT_ASSERT(!IsIpAddress(_T("127.0..0.1")));
45}
46
47void CIPAddressTest::testIPV6LongForm()
48{
49        // Valid addresses
50        CPPUNIT_ASSERT_EQUAL( wxString("0000:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("::1")) );
51        CPPUNIT_ASSERT_EQUAL( wxString("1234:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("1234::1")) );
52        CPPUNIT_ASSERT_EQUAL( wxString("0004:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("4::1")) );
53        CPPUNIT_ASSERT_EQUAL( wxString("1234:abcd:0000:0000:0000:0000:1234:ef01"), GetIPV6LongForm(_T("1234:abcd::1234:ef01")) );
54        CPPUNIT_ASSERT_EQUAL( wxString("1234:abcd:0000:0000:0000:0000:1234:ef01"), GetIPV6LongForm(_T("1234:ABCD::1234:ef01")) );
55        CPPUNIT_ASSERT_EQUAL( wxString("0000:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("0:0:0:0:0:0:0:1")) );
56        CPPUNIT_ASSERT_EQUAL( wxString("0000:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("0:0:0::0:0:0:0:1")) );
57        CPPUNIT_ASSERT_EQUAL( wxString("0000:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("::0:0:0:0:0:0:0:1")) );
58        CPPUNIT_ASSERT_EQUAL( wxString("0000:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("0000:0000:0000:0000:0000:0000:0000:0001")) );
59
60        CPPUNIT_ASSERT_EQUAL( wxString("0000:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("[::1]")) );
61        CPPUNIT_ASSERT_EQUAL( wxString("1234:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("[1234::1]")) );
62        CPPUNIT_ASSERT_EQUAL( wxString("0004:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("[4::1]")) );
63        CPPUNIT_ASSERT_EQUAL( wxString("1234:abcd:0000:0000:0000:0000:1234:ef01"), GetIPV6LongForm(_T("[1234:abcd::1234:ef01]")) );
64        CPPUNIT_ASSERT_EQUAL( wxString("1234:abcd:0000:0000:0000:0000:1234:ef01"), GetIPV6LongForm(_T("[1234:ABCD::1234:ef01]")) );
65        CPPUNIT_ASSERT_EQUAL( wxString("0000:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("[0:0:0:0:0:0:0:1]")) );
66        CPPUNIT_ASSERT_EQUAL( wxString("0000:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("[0:0:0::0:0:0:0:1]")) );
67        CPPUNIT_ASSERT_EQUAL( wxString("0000:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("[::0:0:0:0:0:0:0:1]")) );
68        CPPUNIT_ASSERT_EQUAL( wxString("0000:0000:0000:0000:0000:0000:0000:0001"), GetIPV6LongForm(_T("[0000:0000:0000:0000:0000:0000:0000:0001]")) );
69
70        // Invalid ones
71        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("::")));
72        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T(":::")));
73        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T(":1234:abcd::1234:ef01")));
74        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("1234:abcd::1234:ef01:")));
75        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("1234:abcd::1234::ef01")));
76        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("[1234:abcd::1234::ef01")));
77        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("1234:abcd::1234::ef01]")));
78        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("[[1234:abcd::1234::ef01]]")));
79        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("1234:abcde:1234::ef01")));
80        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("1234:abcg:1234::ef01")));
81        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T(":::1")));
82        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("0:0:0:0:0:0:0:1:2")));
83        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("0:0:0:0:0:0:0:1:2:0:0:0:0:0:0:1:2")));
84        CPPUNIT_ASSERT_EQUAL( wxString(), GetIPV6LongForm(_T("0::0:0:0:0:0:0:1:2:0:0:0:0:0:0:1")));
85}
86
87void CIPAddressTest::testIsRoutableAddress4()
88{
89        // 127.0.0.0/8
90        CPPUNIT_ASSERT(IsRoutableAddress(_T("126.255.255.255"), CSocket::ipv4));
91        CPPUNIT_ASSERT(!IsRoutableAddress(_T("127.0.0.0"), CSocket::ipv4));
92        CPPUNIT_ASSERT(!IsRoutableAddress(_T("127.255.255.255"), CSocket::ipv4));
93        CPPUNIT_ASSERT(IsRoutableAddress(_T("128.0.0.0"), CSocket::ipv4));
94
95        // 10.0.0.0/8
96        CPPUNIT_ASSERT(IsRoutableAddress(_T("9.255.255.255"), CSocket::ipv4));
97        CPPUNIT_ASSERT(!IsRoutableAddress(_T("10.0.0.0"), CSocket::ipv4));
98        CPPUNIT_ASSERT(!IsRoutableAddress(_T("10.255.255.255"), CSocket::ipv4));
99        CPPUNIT_ASSERT(IsRoutableAddress(_T("11.0.0.0"), CSocket::ipv4));
100
101        // 169.254.0.0/16
102        CPPUNIT_ASSERT(IsRoutableAddress(_T("169.253.255.255"), CSocket::ipv4));
103        CPPUNIT_ASSERT(!IsRoutableAddress(_T("169.254.0.0"), CSocket::ipv4));
104        CPPUNIT_ASSERT(!IsRoutableAddress(_T("169.254.255.255"), CSocket::ipv4));
105        CPPUNIT_ASSERT(IsRoutableAddress(_T("169.255.0.0"), CSocket::ipv4));
106
107        // 192.168.0.0/16
108        CPPUNIT_ASSERT(IsRoutableAddress(_T("192.167.255.255"), CSocket::ipv4));
109        CPPUNIT_ASSERT(!IsRoutableAddress(_T("192.168.0.0"), CSocket::ipv4));
110        CPPUNIT_ASSERT(!IsRoutableAddress(_T("192.168.255.255"), CSocket::ipv4));
111        CPPUNIT_ASSERT(IsRoutableAddress(_T("102.169.0.0"), CSocket::ipv4));
112
113        // 172.16.0.0/20
114        CPPUNIT_ASSERT(IsRoutableAddress(_T("172.15.255.255"), CSocket::ipv4));
115        CPPUNIT_ASSERT(!IsRoutableAddress(_T("172.16.0.0"), CSocket::ipv4));
116        CPPUNIT_ASSERT(!IsRoutableAddress(_T("172.31.255.255"), CSocket::ipv4));
117        CPPUNIT_ASSERT(IsRoutableAddress(_T("172.32.0.0"), CSocket::ipv4));
118}
119
120void CIPAddressTest::testIsRoutableAddress6()
121{
122        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::1"), CSocket::ipv6));
123        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::0"), CSocket::ipv6));
124        CPPUNIT_ASSERT(IsRoutableAddress(_T("::2"), CSocket::ipv6));
125        CPPUNIT_ASSERT(IsRoutableAddress(_T("1234:ABCD::1234:ef01"), CSocket::ipv6));
126
127        // fe80::/10 (link local)
128        CPPUNIT_ASSERT(IsRoutableAddress(_T("fe7f:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), CSocket::ipv6));
129        CPPUNIT_ASSERT(!IsRoutableAddress(_T("fe80:0000:0000:0000:0000:0000:0000:0000"), CSocket::ipv6));
130        CPPUNIT_ASSERT(!IsRoutableAddress(_T("febf:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), CSocket::ipv6));
131        CPPUNIT_ASSERT(IsRoutableAddress(_T("fec0:0000:0000:0000:0000:0000:0000:0000"), CSocket::ipv6));
132
133        // fc00::/7 (site local)
134        CPPUNIT_ASSERT(IsRoutableAddress(_T("fbff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), CSocket::ipv6));
135        CPPUNIT_ASSERT(!IsRoutableAddress(_T("fc00:0000:0000:0000:0000:0000:0000:0000"), CSocket::ipv6));
136        CPPUNIT_ASSERT(!IsRoutableAddress(_T("fdff:ffff:ffff:ffff:ffff:ffff:ffff:ffff"), CSocket::ipv6));
137        CPPUNIT_ASSERT(IsRoutableAddress(_T("fe00:0000:0000:0000:0000:0000:0000:0000"), CSocket::ipv6));
138
139        // IPv4 mapped
140
141        // 127.0.0.0/8
142        CPPUNIT_ASSERT(IsRoutableAddress(_T("::ffff:7eff:ffff"), CSocket::ipv6));
143        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::ffff:7f00:0000"), CSocket::ipv6));
144        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::ffff:7fff:ffff"), CSocket::ipv6));
145        CPPUNIT_ASSERT(IsRoutableAddress(_T("::ffff:8000:0000"), CSocket::ipv6));
146
147        // 10.0.0.0/8
148        CPPUNIT_ASSERT(IsRoutableAddress(_T("::ffff:9ff:ffff"), CSocket::ipv6));
149        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::ffff:0a00:0000"), CSocket::ipv6));
150        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::ffff:0aff:ffff"), CSocket::ipv6));
151        CPPUNIT_ASSERT(IsRoutableAddress(_T("::ffff:0b00:0000"), CSocket::ipv6));
152
153        // 169.254.0.0/16
154        CPPUNIT_ASSERT(IsRoutableAddress(_T("::ffff:a9fd:ffff"), CSocket::ipv6));
155        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::ffff:a9fe:0000"), CSocket::ipv6));
156        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::ffff:a9fe:ffff"), CSocket::ipv6));
157        CPPUNIT_ASSERT(IsRoutableAddress(_T("::ffff:a9ff:0000"), CSocket::ipv6));
158
159        // 192.168.0.0/16
160        CPPUNIT_ASSERT(IsRoutableAddress(_T("::ffff:c0a7:ffff"), CSocket::ipv6));
161        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::ffff:c0a8:0000"), CSocket::ipv6));
162        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::ffff:c0a8:ffff"), CSocket::ipv6));
163        CPPUNIT_ASSERT(IsRoutableAddress(_T("::ffff:c0a9:0000"), CSocket::ipv6));
164
165        // 172.16.0.0/20
166        CPPUNIT_ASSERT(IsRoutableAddress(_T("::ffff:ac0f:ffff"), CSocket::ipv6));
167        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::ffff:ac10:0000"), CSocket::ipv6));
168        CPPUNIT_ASSERT(!IsRoutableAddress(_T("::ffff:ac1f:ffff"), CSocket::ipv6));
169        CPPUNIT_ASSERT(IsRoutableAddress(_T("::ffff:ac20:0000"), CSocket::ipv6));
170}
Note: See TracBrowser for help on using the repository browser.