test_tcp_socket.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright 2013 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. #ifndef PPAPI_TESTS_TEST_TCP_SOCKET_H_
  5. #define PPAPI_TESTS_TEST_TCP_SOCKET_H_
  6. #include <stddef.h>
  7. #include <string>
  8. #include "ppapi/c/pp_stdint.h"
  9. #include "ppapi/c/ppb_tcp_socket.h"
  10. #include "ppapi/cpp/net_address.h"
  11. #include "ppapi/tests/test_case.h"
  12. namespace pp {
  13. class TCPSocket;
  14. }
  15. class TestTCPSocket: public TestCase {
  16. public:
  17. explicit TestTCPSocket(TestingInstance* instance);
  18. // TestCase implementation.
  19. virtual bool Init();
  20. virtual void RunTests(const std::string& filter);
  21. private:
  22. std::string TestConnect();
  23. std::string TestReadWrite();
  24. std::string TestSetOption();
  25. std::string TestListen();
  26. std::string TestBacklog();
  27. std::string TestInterface_1_0();
  28. std::string TestUnexpectedCalls();
  29. // The higher level test fixture is responsible for making socket methods
  30. // behave in the expected manner. The *Fails tests expect the specified even
  31. // to fail with PP_ERROR_FAILED, and the *Hangs test expect the specified
  32. // operation to never complete, at least until teardown starts.
  33. std::string TestConnectFails();
  34. std::string TestConnectHangs();
  35. std::string TestWriteFails();
  36. std::string TestReadFails();
  37. std::string TestSetSendBufferSizeFails();
  38. std::string TestSetReceiveBufferSizeFails();
  39. std::string TestSetNoDelayFails();
  40. // When a bind call fails, normally the socket is reuseable.
  41. std::string TestBindFailsConnectSucceeds();
  42. // This is needed in addition to the above test in the case where a bind
  43. // failure is simulated in a way that also closes the NetworkContext pipe.
  44. std::string TestBindFails();
  45. std::string TestBindHangs();
  46. std::string TestListenFails();
  47. std::string TestListenHangs();
  48. std::string TestAcceptFails();
  49. std::string TestAcceptHangs();
  50. std::string TestAcceptedSocketWriteFails();
  51. std::string TestAcceptedSocketReadFails();
  52. std::string TestBindConnectFails();
  53. std::string TestBindConnectHangs();
  54. std::string ReadFirstLineFromSocket(pp::TCPSocket* socket, std::string* s);
  55. std::string ReadFirstLineFromSocket_1_0(PP_Resource socket,
  56. std::string* s);
  57. // Expects to read exactly |num_bytes| from the socket. Stops once exactly
  58. // |num_bytes| have been read.
  59. std::string ReadFromSocket(pp::TCPSocket* socket,
  60. char* buffer,
  61. size_t num_bytes);
  62. // Reads from |socket| until a read error occurs, and sets |read_data| and
  63. // |error| accordingly. Only fails if a Read() call returns more data than the
  64. // buffer that was passed in to it.
  65. std::string ReadFromSocketUntilError(pp::TCPSocket* socket,
  66. std::string* read_data,
  67. int* error);
  68. std::string WriteToSocket(pp::TCPSocket* socket, const std::string& s);
  69. std::string WriteToSocket_1_0(PP_Resource socket, const std::string& s);
  70. // Sets |address| to an address usable for Bind(). Returned address uses the
  71. // IP address used to talk to |test_server_addr_| and a port of 0, so Bind()
  72. // calls to it should succeed.
  73. std::string GetAddressToBind(pp::NetAddress* address);
  74. std::string StartListen(pp::TCPSocket* socket, int32_t backlog);
  75. enum Command {
  76. kBind = 0x1,
  77. kListen = 0x2,
  78. kAccept = 0x4,
  79. kConnect = 0x8,
  80. kReadWrite = 0x10,
  81. kAllCommands = -1,
  82. };
  83. // Runs |commands|, consisting of one or more Command values on |socket|,
  84. // expecting all of them to fail with PP_ERROR_FAILED. Useful for testing
  85. // invalid state transitions.
  86. std::string RunCommandsExpendingFailures(pp::TCPSocket* socket, int commands);
  87. // Address of the EmbeddedTestServer set up by the browser test fixture.
  88. pp::NetAddress test_server_addr_;
  89. const PPB_TCPSocket_1_0* socket_interface_1_0_;
  90. };
  91. #endif // PPAPI_TESTS_TEST_TCP_SOCKET_H_