Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(232)

Side by Side Diff: ppapi/tests/test_net_address_private.cc

Issue 8527017: Pepper: Add tests for the PPB_NetAddress_Private interface. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: oops Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "ppapi/tests/test_net_address_private.h"
6
7 #include "ppapi/cpp/private/net_address_private.h"
8 #include "ppapi/tests/testing_instance.h"
9
10 // Other than |GetAnyAddress()|, there's no way to actually get
11 // |PP_NetAddress_Private| structs from just this interface. We'll cheat and
yzshen1 2011/11/11 18:55:28 [not a review comment] Are we going to provide API
viettrungluu 2011/11/11 19:07:40 It's not my immediate intention to, though I guess
12 // synthesize some.
13 // TODO(viettrungluu): This is very fragile and implementation-dependent. :(
14 #if defined(_WIN32)
15 #define OS_WIN
16 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
17 defined(__OpenBSD__) || defined(__sun) || defined(__native_client__)
18 #define OS_POSIX
19 #else
20 #error "Unsupported platform."
21 #endif
22
23 #if defined(OS_WIN)
24 #include <ws2tcpip.h>
25 #elif defined(OS_POSIX)
26 #include <netdb.h>
27 #include <netinet/in.h>
28 #include <sys/socket.h>
29 #include <arpa/inet.h>
yzshen1 2011/11/11 18:55:28 Sort it, please.
viettrungluu 2011/11/11 19:07:40 Done.
30 #endif
31
32 using pp::NetAddressPrivate;
33
34 namespace {
35
36 PP_NetAddress_Private MakeIPv4NetAddress(const char* host, int port) {
37 PP_NetAddress_Private addr = PP_NetAddress_Private();
38 addr.size = sizeof(sockaddr_in);
39 sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr.data);
40 a->sin_family = AF_INET;
41 a->sin_port = htons(port);
42 a->sin_addr.s_addr = inet_addr(host);
43 return addr;
44 }
45
46 // TODO(viettrungluu): Also add IPv6 tests.
47
48 } // namespace
49
50 REGISTER_TEST_CASE(NetAddressPrivate);
51
52 TestNetAddressPrivate::TestNetAddressPrivate(TestingInstance* instance)
53 : TestCase(instance) {
54 }
55
56 bool TestNetAddressPrivate::Init() {
57 if (!NetAddressPrivate::IsAvailable())
yzshen1 2011/11/11 18:55:28 Maybe directly return NetAddressPrivate::IsAvailab
viettrungluu 2011/11/11 19:07:40 Done.
58 return false;
59
60 return true;
61 }
62
63 void TestNetAddressPrivate::RunTest() {
64 instance_->LogTest("AreEqual", TestAreEqual());
yzshen1 2011/11/11 18:55:28 Please consider using RUN_TEST
viettrungluu 2011/11/11 19:07:40 Done. I also converted test_uma.cc (where I copie
65 instance_->LogTest("AreHostsEqual", TestAreHostsEqual());
66 instance_->LogTest("Describe", TestDescribe());
67 instance_->LogTest("ReplacePort", TestReplacePort());
68 instance_->LogTest("GetAnyAddress", TestGetAnyAddress());
69 }
70
71 std::string TestNetAddressPrivate::TestAreEqual() {
72 // No comparisons should ever be done with invalid addresses.
73 PP_NetAddress_Private invalid = PP_NetAddress_Private();
74 ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid));
75
76 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
77 ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80));
78 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid));
79
80 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
81 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, localhost_1234));
82
83 PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80);
84 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, other_80));
85
86 PASS();
87 }
88
89 std::string TestNetAddressPrivate::TestAreHostsEqual() {
90 // No comparisons should ever be done with invalid addresses.
91 PP_NetAddress_Private invalid = PP_NetAddress_Private();
92 ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(invalid, invalid));
93
94 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
95 ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(localhost_80, localhost_80));
96 ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(localhost_80, invalid));
97
98 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
99 ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(localhost_80, localhost_1234));
100
101 PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80);
102 ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(localhost_80, other_80));
103
104 PASS();
105 }
106
107 std::string TestNetAddressPrivate::TestDescribe() {
108 PP_NetAddress_Private invalid = PP_NetAddress_Private();
109 ASSERT_EQ("", NetAddressPrivate::Describe(invalid, false));
110 ASSERT_EQ("", NetAddressPrivate::Describe(invalid, true));
111
112 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
113 ASSERT_EQ("127.0.0.1", NetAddressPrivate::Describe(localhost_80, false));
114 ASSERT_EQ("127.0.0.1:80", NetAddressPrivate::Describe(localhost_80, true));
115
116 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
117 ASSERT_EQ("127.0.0.1", NetAddressPrivate::Describe(localhost_1234, false));
118 ASSERT_EQ("127.0.0.1:1234", NetAddressPrivate::Describe(localhost_1234,
119 true));
120
121 PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80);
122 ASSERT_EQ("192.168.0.1", NetAddressPrivate::Describe(other_80, false));
123 ASSERT_EQ("192.168.0.1:80", NetAddressPrivate::Describe(other_80, true));
124
125 PASS();
126 }
127
128 std::string TestNetAddressPrivate::TestReplacePort() {
129 // Assume that |AreEqual()| works correctly.
130 PP_NetAddress_Private result = PP_NetAddress_Private();
131
132 PP_NetAddress_Private invalid = PP_NetAddress_Private();
133 ASSERT_FALSE(NetAddressPrivate::ReplacePort(invalid, 1234, &result));
134
135 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
136 ASSERT_TRUE(NetAddressPrivate::ReplacePort(localhost_80, 1234, &result));
137 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
138 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, localhost_1234));
139
140 // Test that having the out param being the same as the in param works
141 // properly.
142 ASSERT_TRUE(NetAddressPrivate::ReplacePort(result, 80, &result));
143 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, localhost_80));
144
145 PASS();
146 }
147
148 std::string TestNetAddressPrivate::TestGetAnyAddress() {
149 // Just make sure it doesn't crash and such.
150 PP_NetAddress_Private result = PP_NetAddress_Private();
151
152 NetAddressPrivate::GetAnyAddress(false, &result);
153 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result));
154
155 NetAddressPrivate::GetAnyAddress(true, &result);
156 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result));
157
158 PASS();
159 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698