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 #ifndef CHROME_BROWSER_DEBUGGER_DEVTOOLS_REMOTE_LISTEN_SOCKET_UNITTEST_H_ 6 #define CHROME_BROWSER_DEBUGGER_DEVTOOLS_REMOTE_LISTEN_SOCKET_UNITTEST_H_ 7 #pragma once 8 9 #include "build/build_config.h" 10 11 #include <deque> 12 #include <string> 13 14 #if defined(OS_WIN) 15 #include <winsock2.h> 16 #elif defined(OS_POSIX) 17 #include <sys/socket.h> 18 #include <errno.h> 19 #include <semaphore.h> 20 #include <arpa/inet.h> 21 #endif 22 23 #include "base/threading/thread.h" 24 #include "base/basictypes.h" 25 #include "base/memory/scoped_ptr.h" 26 #include "base/message_loop.h" 27 #include "base/string_util.h" 28 #include "base/threading/thread.h" 29 #include "chrome/browser/debugger/devtools_remote.h" 30 #include "chrome/browser/debugger/devtools_remote_listen_socket.h" 31 #include "chrome/browser/debugger/devtools_remote_message.h" 32 #include "net/base/net_util.h" 33 #include "net/base/listen_socket.h" 34 #include "net/base/winsock_init.h" 35 #include "testing/gtest/include/gtest/gtest.h" 36 37 #if defined(OS_POSIX) 38 // Used same name as in Windows to avoid #ifdef where refrenced 39 #define SOCKET int 40 const int INVALID_SOCKET = -1; 41 const int SOCKET_ERROR = -1; 42 #endif 43 44 enum ActionType { 45 ACTION_NONE = 0, 46 ACTION_LISTEN = 1, 47 ACTION_ACCEPT = 2, 48 ACTION_READ = 3, 49 ACTION_READ_MESSAGE = 4, 50 ACTION_SEND = 5, 51 ACTION_CLOSE = 6, 52 ACTION_SHUTDOWN = 7 53 }; 54 55 class ListenSocketTestAction { 56 public: 57 ListenSocketTestAction(); 58 explicit ListenSocketTestAction(ActionType action); 59 ListenSocketTestAction(ActionType action, std::string data); 60 ListenSocketTestAction(ActionType action, 61 const DevToolsRemoteMessage& message); 62 ~ListenSocketTestAction(); 63 64 const std::string data() const { return data_; } 65 const DevToolsRemoteMessage message() { return message_; } 66 ActionType type() const { return action_; } 67 68 private: 69 ActionType action_; 70 std::string data_; 71 DevToolsRemoteMessage message_; 72 }; 73 74 75 // This had to be split out into a separate class because I couldn't 76 // make a the testing::Test class refcounted. 77 class DevToolsRemoteListenSocketTester : 78 public DevToolsRemoteListener { 79 public: 80 DevToolsRemoteListenSocketTester(); 81 82 virtual void SetUp(); 83 virtual void TearDown(); 84 85 void ReportAction(const ListenSocketTestAction& action); 86 bool NextAction(int timeout); 87 88 // DevToolsRemoteMessageHandler interface 89 virtual void HandleMessage(const DevToolsRemoteMessage& message); 90 virtual void OnAcceptConnection(ListenSocket* connection); 91 virtual void OnConnectionLost(); 92 93 // read all pending data from the test socket 94 int ClearTestSocket(); 95 // Release the connection and server sockets 96 void Shutdown(); 97 void Listen(); 98 void SendFromTester(); 99 virtual bool Send(SOCKET sock, const std::string& str); 100 // verify the send/read from client to server 101 void TestClientSend(); 102 // verify a send/read from server to client 103 void TestServerSend(); 104 105 #if defined(OS_WIN) 106 CRITICAL_SECTION lock_; 107 HANDLE semaphore_; 108 #elif defined(OS_POSIX) 109 pthread_mutex_t lock_; 110 sem_t* semaphore_; 111 #endif 112 113 scoped_ptr<base::Thread> thread_; 114 MessageLoopForIO* loop_; 115 ListenSocket* server_; 116 ListenSocket* connection_; 117 ListenSocketTestAction last_action_; 118 std::deque<ListenSocketTestAction> queue_; 119 SOCKET test_socket_; 120 static const int kTestPort; 121 122 protected: 123 virtual ListenSocket* DoListen(); 124 125 private: 126 virtual ~DevToolsRemoteListenSocketTester(); 127 }; 128 129 #endif // CHROME_BROWSER_DEBUGGER_DEVTOOLS_REMOTE_LISTEN_SOCKET_UNITTEST_H_ 130