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

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: addressed review comments 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
« no previous file with comments | « ppapi/tests/test_net_address_private.h ('k') | ppapi/tests/test_uma.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/c/private/ppb_net_address_private.h"
9 #include "ppapi/tests/testing_instance.h"
10
11 // Other than |GetAnyAddress()|, there's no way to actually get
12 // |PP_NetAddress_Private| structs from just this interface. We'll cheat and
13 // synthesize some.
14 // TODO(viettrungluu): This is very fragile and implementation-dependent. :(
15 #if defined(_WIN32)
16 #define OS_WIN
17 #elif defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__) || \
18 defined(__OpenBSD__) || defined(__sun) || defined(__native_client__)
19 #define OS_POSIX
20 #else
21 #error "Unsupported platform."
22 #endif
23
24 #if defined(OS_WIN)
25 #include <ws2tcpip.h>
26 #elif defined(OS_POSIX)
27 #include <arpa/inet.h>
28 #include <netdb.h>
29 #include <netinet/in.h>
30 #include <sys/socket.h>
31 #endif
32
33 using pp::NetAddressPrivate;
34
35 namespace {
36
37 PP_NetAddress_Private MakeIPv4NetAddress(const char* host, int port) {
38 PP_NetAddress_Private addr = PP_NetAddress_Private();
39 addr.size = sizeof(sockaddr_in);
40 sockaddr_in* a = reinterpret_cast<sockaddr_in*>(addr.data);
41 a->sin_family = AF_INET;
42 a->sin_port = htons(port);
43 a->sin_addr.s_addr = inet_addr(host);
44 return addr;
45 }
46
47 // TODO(viettrungluu): Also add IPv6 tests.
48
49 } // namespace
50
51 REGISTER_TEST_CASE(NetAddressPrivate);
52
53 TestNetAddressPrivate::TestNetAddressPrivate(TestingInstance* instance)
54 : TestCase(instance) {
55 }
56
57 bool TestNetAddressPrivate::Init() {
58 return NetAddressPrivate::IsAvailable();
59 }
60
61 void TestNetAddressPrivate::RunTest() {
62 RUN_TEST(AreEqual);
63 RUN_TEST(AreHostsEqual);
64 RUN_TEST(Describe);
65 RUN_TEST(ReplacePort);
66 RUN_TEST(GetAnyAddress);
67 }
68
69 std::string TestNetAddressPrivate::TestAreEqual() {
70 // No comparisons should ever be done with invalid addresses.
71 PP_NetAddress_Private invalid = PP_NetAddress_Private();
72 ASSERT_FALSE(NetAddressPrivate::AreEqual(invalid, invalid));
73
74 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
75 ASSERT_TRUE(NetAddressPrivate::AreEqual(localhost_80, localhost_80));
76 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, invalid));
77
78 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
79 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, localhost_1234));
80
81 PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80);
82 ASSERT_FALSE(NetAddressPrivate::AreEqual(localhost_80, other_80));
83
84 PASS();
85 }
86
87 std::string TestNetAddressPrivate::TestAreHostsEqual() {
88 // No comparisons should ever be done with invalid addresses.
89 PP_NetAddress_Private invalid = PP_NetAddress_Private();
90 ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(invalid, invalid));
91
92 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
93 ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(localhost_80, localhost_80));
94 ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(localhost_80, invalid));
95
96 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
97 ASSERT_TRUE(NetAddressPrivate::AreHostsEqual(localhost_80, localhost_1234));
98
99 PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80);
100 ASSERT_FALSE(NetAddressPrivate::AreHostsEqual(localhost_80, other_80));
101
102 PASS();
103 }
104
105 std::string TestNetAddressPrivate::TestDescribe() {
106 PP_NetAddress_Private invalid = PP_NetAddress_Private();
107 ASSERT_EQ("", NetAddressPrivate::Describe(invalid, false));
108 ASSERT_EQ("", NetAddressPrivate::Describe(invalid, true));
109
110 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
111 ASSERT_EQ("127.0.0.1", NetAddressPrivate::Describe(localhost_80, false));
112 ASSERT_EQ("127.0.0.1:80", NetAddressPrivate::Describe(localhost_80, true));
113
114 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
115 ASSERT_EQ("127.0.0.1", NetAddressPrivate::Describe(localhost_1234, false));
116 ASSERT_EQ("127.0.0.1:1234", NetAddressPrivate::Describe(localhost_1234,
117 true));
118
119 PP_NetAddress_Private other_80 = MakeIPv4NetAddress("192.168.0.1", 80);
120 ASSERT_EQ("192.168.0.1", NetAddressPrivate::Describe(other_80, false));
121 ASSERT_EQ("192.168.0.1:80", NetAddressPrivate::Describe(other_80, true));
122
123 PASS();
124 }
125
126 std::string TestNetAddressPrivate::TestReplacePort() {
127 // Assume that |AreEqual()| works correctly.
128 PP_NetAddress_Private result = PP_NetAddress_Private();
129
130 PP_NetAddress_Private invalid = PP_NetAddress_Private();
131 ASSERT_FALSE(NetAddressPrivate::ReplacePort(invalid, 1234, &result));
132
133 PP_NetAddress_Private localhost_80 = MakeIPv4NetAddress("127.0.0.1", 80);
134 ASSERT_TRUE(NetAddressPrivate::ReplacePort(localhost_80, 1234, &result));
135 PP_NetAddress_Private localhost_1234 = MakeIPv4NetAddress("127.0.0.1", 1234);
136 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, localhost_1234));
137
138 // Test that having the out param being the same as the in param works
139 // properly.
140 ASSERT_TRUE(NetAddressPrivate::ReplacePort(result, 80, &result));
141 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, localhost_80));
142
143 PASS();
144 }
145
146 std::string TestNetAddressPrivate::TestGetAnyAddress() {
147 // Just make sure it doesn't crash and such.
148 PP_NetAddress_Private result = PP_NetAddress_Private();
149
150 NetAddressPrivate::GetAnyAddress(false, &result);
151 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result));
152
153 NetAddressPrivate::GetAnyAddress(true, &result);
154 ASSERT_TRUE(NetAddressPrivate::AreEqual(result, result));
155
156 PASS();
157 }
OLDNEW
« no previous file with comments | « ppapi/tests/test_net_address_private.h ('k') | ppapi/tests/test_uma.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698