1 // Copyright (c) 2012 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.
5 #include "ppapi/tests/test_network_monitor_private.h"
9 #include "ppapi/cpp/instance_handle.h"
10 #include "ppapi/cpp/module.h"
11 #include "ppapi/cpp/private/net_address_private.h"
12 #include "ppapi/cpp/private/network_list_private.h"
13 #include "ppapi/cpp/private/network_monitor_private.h"
14 #include "ppapi/tests/testing_instance.h"
15 #include "ppapi/tests/test_utils.h"
16 #include "ppapi/utility/private/network_list_observer_private.h"
18 REGISTER_TEST_CASE(NetworkMonitorPrivate
);
23 explicit CallbackData(PP_Instance instance
)
26 delete_monitor(false),
33 pp::NetworkListPrivate network_list
;
35 pp::NetworkMonitorPrivate
* monitor
;
38 void TestCallback(void* user_data
, PP_Resource pp_network_list
) {
39 CallbackData
* data
= static_cast<CallbackData
*>(user_data
);
42 data
->network_list
= pp::NetworkListPrivate(pp::PASS_REF
, pp_network_list
);
44 if (data
->delete_monitor
)
47 if (data
->call_counter
== 1)
51 class TestNetworkListObserver
: public pp::NetworkListObserverPrivate
{
53 explicit TestNetworkListObserver(const pp::InstanceHandle
& instance
)
54 : pp::NetworkListObserverPrivate(instance
),
55 event(instance
.pp_instance()) {
57 virtual void OnNetworkListChanged(const pp::NetworkListPrivate
& list
) {
62 pp::NetworkListPrivate current_list
;
68 TestNetworkMonitorPrivate::TestNetworkMonitorPrivate(TestingInstance
* instance
)
69 : TestCase(instance
) {
72 bool TestNetworkMonitorPrivate::Init() {
73 if (!pp::NetworkMonitorPrivate::IsAvailable())
76 return CheckTestingInterface();
79 void TestNetworkMonitorPrivate::RunTests(const std::string
& filter
) {
80 RUN_TEST_FORCEASYNC_AND_NOT(Basic
, filter
);
81 RUN_TEST_FORCEASYNC_AND_NOT(2Monitors
, filter
);
82 RUN_TEST_FORCEASYNC_AND_NOT(DeleteInCallback
, filter
);
83 RUN_TEST_FORCEASYNC_AND_NOT(ListObserver
, filter
);
86 std::string
TestNetworkMonitorPrivate::VerifyNetworkList(
87 const pp::NetworkListPrivate
& network_list
) {
88 // Verify that there is at least one network interface.
89 size_t count
= network_list
.GetCount();
90 ASSERT_TRUE(count
>= 1U);
92 // Iterate over all interfaces and verify their properties.
93 for (size_t iface
= 0; iface
< count
; ++iface
) {
94 // Verify that the first interface has at least one address.
95 std::vector
<PP_NetAddress_Private
> addresses
;
96 network_list
.GetIpAddresses(iface
, &addresses
);
97 ASSERT_TRUE(addresses
.size() >= 1U);
98 // Verify that the addresses are valid.
99 for (size_t i
= 0; i
< addresses
.size(); ++i
) {
100 PP_NetAddressFamily_Private family
=
101 pp::NetAddressPrivate::GetFamily(addresses
[i
]);
103 ASSERT_TRUE(family
== PP_NETADDRESSFAMILY_IPV4
||
104 family
== PP_NETADDRESSFAMILY_IPV6
);
107 ASSERT_TRUE(pp::NetAddressPrivate::GetAddress(
108 addresses
[i
], ip
, sizeof(ip
)));
110 // Verify that the address is not zero.
112 for (j
= 0; j
< sizeof(ip
); ++j
) {
116 ASSERT_TRUE(j
!= addresses
[i
].size
);
118 // Verify that port is set to 0.
119 ASSERT_TRUE(pp::NetAddressPrivate::GetPort(addresses
[i
]) == 0);
122 // Verify that each interface has a unique name and a display name.
123 ASSERT_FALSE(network_list
.GetName(iface
).empty());
124 ASSERT_FALSE(network_list
.GetDisplayName(iface
).empty());
126 PP_NetworkListType_Private type
= network_list
.GetType(iface
);
127 ASSERT_TRUE(type
>= PP_NETWORKLIST_UNKNOWN
);
128 ASSERT_TRUE(type
<= PP_NETWORKLIST_CELLULAR
);
130 PP_NetworkListState_Private state
= network_list
.GetState(iface
);
131 ASSERT_TRUE(state
>= PP_NETWORKLIST_DOWN
);
132 ASSERT_TRUE(state
<= PP_NETWORKLIST_UP
);
135 // Try to call GetIpAddresses() without C++ wrapper and verify that
136 // it always returns correct value.
137 const PPB_NetworkList_Private
* interface
=
138 static_cast<const PPB_NetworkList_Private
*>(
139 pp::Module::Get()->GetBrowserInterface(
140 PPB_NETWORKLIST_PRIVATE_INTERFACE
));
141 ASSERT_TRUE(interface
);
142 std::vector
<PP_NetAddress_Private
> addresses
;
143 network_list
.GetIpAddresses(0, &addresses
);
144 size_t address_count
= addresses
.size();
145 addresses
.resize(addresses
.size() + 3);
146 for (size_t i
= 0; i
< addresses
.size(); ++i
) {
147 const char kFillValue
= 123;
148 memset(&addresses
.front(), kFillValue
,
149 addresses
.size() * sizeof(PP_NetAddress_Private
));
150 int result
= interface
->GetIpAddresses(network_list
.pp_resource(), 0,
151 &addresses
.front(), i
);
152 ASSERT_EQ(result
, static_cast<int>(address_count
));
154 // Verify that nothing outside the buffer was touched.
155 for (char* pos
= reinterpret_cast<char*>(&addresses
[result
]);
156 pos
!= reinterpret_cast<char*>(&addresses
[0] + addresses
.size());
158 ASSERT_TRUE(*pos
== kFillValue
);
165 std::string
TestNetworkMonitorPrivate::TestBasic() {
166 CallbackData
callback_data(instance_
->pp_instance());
168 pp::NetworkMonitorPrivate
network_monitor(
169 instance_
, &TestCallback
, &callback_data
);
170 callback_data
.event
.Wait();
171 ASSERT_EQ(callback_data
.call_counter
, 1);
173 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data
.network_list
));
178 std::string
TestNetworkMonitorPrivate::Test2Monitors() {
179 CallbackData
callback_data(instance_
->pp_instance());
181 pp::NetworkMonitorPrivate
network_monitor(
182 instance_
, &TestCallback
, &callback_data
);
183 callback_data
.event
.Wait();
184 ASSERT_EQ(callback_data
.call_counter
, 1);
186 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data
.network_list
));
188 CallbackData
callback_data_2(instance_
->pp_instance());
190 pp::NetworkMonitorPrivate
network_monitor_2(
191 instance_
, &TestCallback
, &callback_data_2
);
192 callback_data_2
.event
.Wait();
193 ASSERT_EQ(callback_data_2
.call_counter
, 1);
195 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data_2
.network_list
));
200 std::string
TestNetworkMonitorPrivate::TestDeleteInCallback() {
201 CallbackData
callback_data(instance_
->pp_instance());
203 pp::NetworkMonitorPrivate
* network_monitor
= new pp::NetworkMonitorPrivate(
204 instance_
, &TestCallback
, &callback_data
);
205 callback_data
.delete_monitor
= true;
206 callback_data
.monitor
= network_monitor
;
208 callback_data
.event
.Wait();
209 ASSERT_EQ(callback_data
.call_counter
, 1);
211 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(callback_data
.network_list
));
216 std::string
TestNetworkMonitorPrivate::TestListObserver() {
217 TestNetworkListObserver
observer(instance_
);
218 observer
.event
.Wait();
219 ASSERT_SUBTEST_SUCCESS(VerifyNetworkList(observer
.current_list
));