test_utils.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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.
  4. #include "ppapi/tests/test_utils.h"
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #if defined(_MSC_VER)
  9. #include <windows.h>
  10. #else
  11. #include <unistd.h>
  12. #endif
  13. #include "ppapi/c/pp_errors.h"
  14. #include "ppapi/cpp/instance_handle.h"
  15. #include "ppapi/cpp/module.h"
  16. #include "ppapi/cpp/net_address.h"
  17. #include "ppapi/cpp/private/host_resolver_private.h"
  18. #include "ppapi/cpp/private/net_address_private.h"
  19. #include "ppapi/cpp/url_loader.h"
  20. #include "ppapi/cpp/var.h"
  21. namespace {
  22. bool IsBigEndian() {
  23. union {
  24. uint32_t integer32;
  25. uint8_t integer8[4];
  26. } data = { 0x01020304 };
  27. return data.integer8[0] == 1;
  28. }
  29. void DoNothing(void* user_data, int32_t result) {}
  30. } // namespace
  31. const int kActionTimeoutMs = 10000;
  32. const PPB_Testing_Private* GetTestingInterface() {
  33. static const PPB_Testing_Private* g_testing_interface =
  34. static_cast<const PPB_Testing_Private*>(
  35. pp::Module::Get()->GetBrowserInterface(
  36. PPB_TESTING_PRIVATE_INTERFACE));
  37. return g_testing_interface;
  38. }
  39. std::string ReportError(const char* method, int32_t error) {
  40. char error_as_string[12];
  41. snprintf(error_as_string, sizeof(error_as_string), "%d",
  42. static_cast<int>(error));
  43. std::string result = method + std::string(" failed with error: ") +
  44. error_as_string;
  45. return result;
  46. }
  47. void PlatformSleep(int duration_ms) {
  48. #if defined(_MSC_VER)
  49. ::Sleep(duration_ms);
  50. #else
  51. usleep(duration_ms * 1000);
  52. #endif
  53. }
  54. bool GetLocalHostPort(PP_Instance instance, std::string* host, uint16_t* port) {
  55. if (!host || !port)
  56. return false;
  57. const PPB_Testing_Private* testing = GetTestingInterface();
  58. if (!testing)
  59. return false;
  60. PP_URLComponents_Dev components;
  61. pp::Var pp_url(pp::PASS_REF,
  62. testing->GetDocumentURL(instance, &components));
  63. if (!pp_url.is_string())
  64. return false;
  65. std::string url = pp_url.AsString();
  66. if (components.host.len < 0)
  67. return false;
  68. host->assign(url.substr(components.host.begin, components.host.len));
  69. if (components.port.len <= 0)
  70. return false;
  71. int i = atoi(url.substr(components.port.begin, components.port.len).c_str());
  72. if (i < 0 || i > 65535)
  73. return false;
  74. *port = static_cast<uint16_t>(i);
  75. return true;
  76. }
  77. uint16_t ConvertFromNetEndian16(uint16_t x) {
  78. if (IsBigEndian())
  79. return x;
  80. else
  81. return (x << 8) | (x >> 8);
  82. }
  83. uint16_t ConvertToNetEndian16(uint16_t x) {
  84. if (IsBigEndian())
  85. return x;
  86. else
  87. return (x << 8) | (x >> 8);
  88. }
  89. bool EqualNetAddress(const pp::NetAddress& addr1, const pp::NetAddress& addr2) {
  90. if (addr1.GetFamily() == PP_NETADDRESS_FAMILY_UNSPECIFIED ||
  91. addr2.GetFamily() == PP_NETADDRESS_FAMILY_UNSPECIFIED) {
  92. return false;
  93. }
  94. if (addr1.GetFamily() == PP_NETADDRESS_FAMILY_IPV4) {
  95. PP_NetAddress_IPv4 ipv4_addr1, ipv4_addr2;
  96. if (!addr1.DescribeAsIPv4Address(&ipv4_addr1) ||
  97. !addr2.DescribeAsIPv4Address(&ipv4_addr2)) {
  98. return false;
  99. }
  100. return ipv4_addr1.port == ipv4_addr2.port &&
  101. !memcmp(ipv4_addr1.addr, ipv4_addr2.addr, sizeof(ipv4_addr1.addr));
  102. } else {
  103. PP_NetAddress_IPv6 ipv6_addr1, ipv6_addr2;
  104. if (!addr1.DescribeAsIPv6Address(&ipv6_addr1) ||
  105. !addr2.DescribeAsIPv6Address(&ipv6_addr2)) {
  106. return false;
  107. }
  108. return ipv6_addr1.port == ipv6_addr2.port &&
  109. !memcmp(ipv6_addr1.addr, ipv6_addr2.addr, sizeof(ipv6_addr1.addr));
  110. }
  111. }
  112. bool ResolveHost(PP_Instance instance,
  113. const std::string& host,
  114. uint16_t port,
  115. pp::NetAddress* addr) {
  116. // TODO(yzshen): Change to use the public host resolver once it is supported.
  117. pp::InstanceHandle instance_handle(instance);
  118. pp::HostResolverPrivate host_resolver(instance_handle);
  119. PP_HostResolver_Private_Hint hint =
  120. { PP_NETADDRESSFAMILY_PRIVATE_UNSPECIFIED, 0 };
  121. TestCompletionCallback callback(instance);
  122. callback.WaitForResult(
  123. host_resolver.Resolve(host, port, hint, callback.GetCallback()));
  124. PP_NetAddress_Private addr_private;
  125. if (callback.result() != PP_OK || host_resolver.GetSize() == 0 ||
  126. !host_resolver.GetNetAddress(0, &addr_private)) {
  127. return false;
  128. }
  129. switch (pp::NetAddressPrivate::GetFamily(addr_private)) {
  130. case PP_NETADDRESSFAMILY_PRIVATE_IPV4: {
  131. PP_NetAddress_IPv4 ipv4_addr;
  132. ipv4_addr.port = ConvertToNetEndian16(
  133. pp::NetAddressPrivate::GetPort(addr_private));
  134. if (!pp::NetAddressPrivate::GetAddress(addr_private, ipv4_addr.addr,
  135. sizeof(ipv4_addr.addr))) {
  136. return false;
  137. }
  138. *addr = pp::NetAddress(instance_handle, ipv4_addr);
  139. return true;
  140. }
  141. case PP_NETADDRESSFAMILY_PRIVATE_IPV6: {
  142. PP_NetAddress_IPv6 ipv6_addr;
  143. ipv6_addr.port = ConvertToNetEndian16(
  144. pp::NetAddressPrivate::GetPort(addr_private));
  145. if (!pp::NetAddressPrivate::GetAddress(addr_private, ipv6_addr.addr,
  146. sizeof(ipv6_addr.addr))) {
  147. return false;
  148. }
  149. *addr = pp::NetAddress(instance_handle, ipv6_addr);
  150. return true;
  151. }
  152. default: {
  153. return false;
  154. }
  155. }
  156. }
  157. bool ReplacePort(PP_Instance instance,
  158. const pp::NetAddress& input_addr,
  159. uint16_t port,
  160. pp::NetAddress* output_addr) {
  161. switch (input_addr.GetFamily()) {
  162. case PP_NETADDRESS_FAMILY_IPV4: {
  163. PP_NetAddress_IPv4 ipv4_addr;
  164. if (!input_addr.DescribeAsIPv4Address(&ipv4_addr))
  165. return false;
  166. ipv4_addr.port = ConvertToNetEndian16(port);
  167. *output_addr = pp::NetAddress(pp::InstanceHandle(instance), ipv4_addr);
  168. return true;
  169. }
  170. case PP_NETADDRESS_FAMILY_IPV6: {
  171. PP_NetAddress_IPv6 ipv6_addr;
  172. if (!input_addr.DescribeAsIPv6Address(&ipv6_addr))
  173. return false;
  174. ipv6_addr.port = ConvertToNetEndian16(port);
  175. *output_addr = pp::NetAddress(pp::InstanceHandle(instance), ipv6_addr);
  176. return true;
  177. }
  178. default: {
  179. return false;
  180. }
  181. }
  182. }
  183. uint16_t GetPort(const pp::NetAddress& addr) {
  184. switch (addr.GetFamily()) {
  185. case PP_NETADDRESS_FAMILY_IPV4: {
  186. PP_NetAddress_IPv4 ipv4_addr;
  187. if (!addr.DescribeAsIPv4Address(&ipv4_addr))
  188. return 0;
  189. return ConvertFromNetEndian16(ipv4_addr.port);
  190. }
  191. case PP_NETADDRESS_FAMILY_IPV6: {
  192. PP_NetAddress_IPv6 ipv6_addr;
  193. if (!addr.DescribeAsIPv6Address(&ipv6_addr))
  194. return 0;
  195. return ConvertFromNetEndian16(ipv6_addr.port);
  196. }
  197. default: {
  198. return 0;
  199. }
  200. }
  201. }
  202. void NestedEvent::Wait() {
  203. PP_DCHECK(pp::Module::Get()->core()->IsMainThread());
  204. // Don't allow nesting more than once; it doesn't work with the code as-is,
  205. // and probably is a bad idea most of the time anyway.
  206. PP_DCHECK(!waiting_);
  207. if (signalled_)
  208. return;
  209. waiting_ = true;
  210. while (!signalled_)
  211. GetTestingInterface()->RunMessageLoop(instance_);
  212. waiting_ = false;
  213. }
  214. void NestedEvent::Signal() {
  215. if (pp::Module::Get()->core()->IsMainThread())
  216. SignalOnMainThread();
  217. else
  218. PostSignal(0);
  219. }
  220. void NestedEvent::PostSignal(int32_t wait_ms) {
  221. pp::Module::Get()->core()->CallOnMainThread(
  222. wait_ms,
  223. pp::CompletionCallback(&SignalThunk, this),
  224. 0);
  225. }
  226. void NestedEvent::Reset() {
  227. PP_DCHECK(pp::Module::Get()->core()->IsMainThread());
  228. // It doesn't make sense to reset when we're still waiting.
  229. PP_DCHECK(!waiting_);
  230. signalled_ = false;
  231. }
  232. void NestedEvent::SignalOnMainThread() {
  233. PP_DCHECK(pp::Module::Get()->core()->IsMainThread());
  234. signalled_ = true;
  235. if (waiting_)
  236. GetTestingInterface()->QuitMessageLoop(instance_);
  237. }
  238. void NestedEvent::SignalThunk(void* event, int32_t /* result */) {
  239. static_cast<NestedEvent*>(event)->SignalOnMainThread();
  240. }
  241. pp::CompletionCallback DoNothingCallback() {
  242. return pp::CompletionCallback(&DoNothing, NULL,
  243. PP_COMPLETIONCALLBACK_FLAG_OPTIONAL);
  244. }
  245. TestCompletionCallback::TestCompletionCallback(PP_Instance instance)
  246. : wait_for_result_called_(false),
  247. have_result_(false),
  248. result_(PP_OK_COMPLETIONPENDING),
  249. // TODO(dmichael): The default should probably be PP_REQUIRED, but this is
  250. // what the tests currently expect.
  251. callback_type_(PP_OPTIONAL),
  252. post_quit_task_(false),
  253. instance_(instance),
  254. delegate_(NULL) {
  255. }
  256. TestCompletionCallback::TestCompletionCallback(PP_Instance instance,
  257. bool force_async)
  258. : wait_for_result_called_(false),
  259. have_result_(false),
  260. result_(PP_OK_COMPLETIONPENDING),
  261. callback_type_(force_async ? PP_REQUIRED : PP_OPTIONAL),
  262. post_quit_task_(false),
  263. instance_(instance),
  264. delegate_(NULL) {
  265. }
  266. TestCompletionCallback::TestCompletionCallback(PP_Instance instance,
  267. CallbackType callback_type)
  268. : wait_for_result_called_(false),
  269. have_result_(false),
  270. result_(PP_OK_COMPLETIONPENDING),
  271. callback_type_(callback_type),
  272. post_quit_task_(false),
  273. instance_(instance),
  274. delegate_(NULL) {
  275. }
  276. void TestCompletionCallback::WaitForResult(int32_t result) {
  277. PP_DCHECK(!wait_for_result_called_);
  278. wait_for_result_called_ = true;
  279. errors_.clear();
  280. if (result == PP_OK_COMPLETIONPENDING) {
  281. if (!have_result_) {
  282. post_quit_task_ = true;
  283. RunMessageLoop();
  284. }
  285. if (callback_type_ == PP_BLOCKING) {
  286. errors_.assign(
  287. ReportError("TestCompletionCallback: Call did not run synchronously "
  288. "when passed a blocking completion callback!",
  289. result_));
  290. return;
  291. }
  292. } else {
  293. result_ = result;
  294. have_result_ = true;
  295. if (callback_type_ == PP_REQUIRED) {
  296. errors_.assign(
  297. ReportError("TestCompletionCallback: Call ran synchronously when "
  298. "passed a required completion callback!",
  299. result_));
  300. return;
  301. }
  302. }
  303. PP_DCHECK(have_result_ == true);
  304. }
  305. void TestCompletionCallback::WaitForAbortResult(int32_t result) {
  306. WaitForResult(result);
  307. int32_t final_result = result_;
  308. if (result == PP_OK_COMPLETIONPENDING) {
  309. if (final_result != PP_ERROR_ABORTED) {
  310. errors_.assign(
  311. ReportError("TestCompletionCallback: Expected PP_ERROR_ABORTED or "
  312. "PP_OK. Ran asynchronously.",
  313. final_result));
  314. return;
  315. }
  316. } else if (result < PP_OK) {
  317. errors_.assign(
  318. ReportError("TestCompletionCallback: Expected PP_ERROR_ABORTED or "
  319. "non-error response. Ran synchronously.",
  320. result));
  321. return;
  322. }
  323. }
  324. pp::CompletionCallback TestCompletionCallback::GetCallback() {
  325. Reset();
  326. int32_t flags = 0;
  327. if (callback_type_ == PP_BLOCKING)
  328. return pp::CompletionCallback();
  329. else if (callback_type_ == PP_OPTIONAL)
  330. flags = PP_COMPLETIONCALLBACK_FLAG_OPTIONAL;
  331. target_loop_ = pp::MessageLoop::GetCurrent();
  332. return pp::CompletionCallback(&TestCompletionCallback::Handler,
  333. const_cast<TestCompletionCallback*>(this),
  334. flags);
  335. }
  336. void TestCompletionCallback::Reset() {
  337. wait_for_result_called_ = false;
  338. result_ = PP_OK_COMPLETIONPENDING;
  339. have_result_ = false;
  340. post_quit_task_ = false;
  341. delegate_ = NULL;
  342. errors_.clear();
  343. }
  344. // static
  345. void TestCompletionCallback::Handler(void* user_data, int32_t result) {
  346. TestCompletionCallback* callback =
  347. static_cast<TestCompletionCallback*>(user_data);
  348. // If this check fails, it means that the callback was invoked twice or that
  349. // the PPAPI call completed synchronously, but also ran the callback.
  350. PP_DCHECK(!callback->have_result_);
  351. callback->result_ = result;
  352. callback->have_result_ = true;
  353. if (callback->delegate_)
  354. callback->delegate_->OnCallback(user_data, result);
  355. if (callback->post_quit_task_) {
  356. callback->post_quit_task_ = false;
  357. callback->QuitMessageLoop();
  358. }
  359. if (callback->target_loop_ != pp::MessageLoop::GetCurrent()) {
  360. // Note, in-process, loop_ and GetCurrent() will both be NULL, so should
  361. // still be equal.
  362. callback->errors_.assign(
  363. ReportError("TestCompletionCallback: Callback ran on the wrong message "
  364. "loop!",
  365. result));
  366. }
  367. }
  368. void TestCompletionCallback::RunMessageLoop() {
  369. pp::MessageLoop loop(pp::MessageLoop::GetCurrent());
  370. // If we don't have a message loop, we're probably running in process, where
  371. // PPB_MessageLoop is not supported. Just use the Testing message loop.
  372. if (loop.is_null() || loop == pp::MessageLoop::GetForMainThread())
  373. GetTestingInterface()->RunMessageLoop(instance_);
  374. else
  375. loop.Run();
  376. }
  377. void TestCompletionCallback::QuitMessageLoop() {
  378. pp::MessageLoop loop(pp::MessageLoop::GetCurrent());
  379. // If we don't have a message loop, we're probably running in process, where
  380. // PPB_MessageLoop is not supported. Just use the Testing message loop.
  381. if (loop.is_null() || loop == pp::MessageLoop::GetForMainThread()) {
  382. GetTestingInterface()->QuitMessageLoop(instance_);
  383. } else {
  384. const bool should_quit = false;
  385. loop.PostQuit(should_quit);
  386. }
  387. }
  388. int32_t OpenURLRequest(PP_Instance instance,
  389. pp::URLLoader* loader,
  390. const pp::URLRequestInfo& request,
  391. CallbackType callback_type,
  392. std::string* response_body) {
  393. {
  394. TestCompletionCallback open_callback(instance, callback_type);
  395. open_callback.WaitForResult(
  396. loader->Open(request, open_callback.GetCallback()));
  397. if (open_callback.result() != PP_OK)
  398. return open_callback.result();
  399. }
  400. int32_t bytes_read = 0;
  401. do {
  402. char buffer[1024];
  403. TestCompletionCallback read_callback(instance, callback_type);
  404. read_callback.WaitForResult(loader->ReadResponseBody(
  405. &buffer, sizeof(buffer), read_callback.GetCallback()));
  406. bytes_read = read_callback.result();
  407. if (bytes_read < 0)
  408. return bytes_read;
  409. if (response_body)
  410. response_body->append(std::string(buffer, bytes_read));
  411. } while (bytes_read > 0);
  412. return PP_OK;
  413. }