Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set ts=2 et sw=2 tw=80: */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
michael@0 | 5 | * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | // Original author: ekr@rtfm.com |
michael@0 | 8 | #include <iostream> |
michael@0 | 9 | |
michael@0 | 10 | #include "prio.h" |
michael@0 | 11 | |
michael@0 | 12 | #include "nsCOMPtr.h" |
michael@0 | 13 | #include "nsNetCID.h" |
michael@0 | 14 | #include "nsXPCOM.h" |
michael@0 | 15 | #include "nsXPCOMGlue.h" |
michael@0 | 16 | |
michael@0 | 17 | #include "nsIComponentManager.h" |
michael@0 | 18 | #include "nsIComponentRegistrar.h" |
michael@0 | 19 | #include "nsIIOService.h" |
michael@0 | 20 | #include "nsIServiceManager.h" |
michael@0 | 21 | #include "nsISocketTransportService.h" |
michael@0 | 22 | |
michael@0 | 23 | #include "nsASocketHandler.h" |
michael@0 | 24 | #include "nsServiceManagerUtils.h" |
michael@0 | 25 | #include "nsThreadUtils.h" |
michael@0 | 26 | |
michael@0 | 27 | #include "mtransport_test_utils.h" |
michael@0 | 28 | |
michael@0 | 29 | #define GTEST_HAS_RTTI 0 |
michael@0 | 30 | #include "gtest/gtest.h" |
michael@0 | 31 | #include "gtest_utils.h" |
michael@0 | 32 | |
michael@0 | 33 | MtransportTestUtils *test_utils; |
michael@0 | 34 | |
michael@0 | 35 | namespace { |
michael@0 | 36 | class SocketTransportServiceTest : public ::testing::Test { |
michael@0 | 37 | public: |
michael@0 | 38 | SocketTransportServiceTest() : received_(0), |
michael@0 | 39 | readpipe_(nullptr), |
michael@0 | 40 | writepipe_(nullptr), |
michael@0 | 41 | registered_(false) { |
michael@0 | 42 | } |
michael@0 | 43 | |
michael@0 | 44 | ~SocketTransportServiceTest() { |
michael@0 | 45 | if (readpipe_) |
michael@0 | 46 | PR_Close(readpipe_); |
michael@0 | 47 | if (writepipe_) |
michael@0 | 48 | PR_Close(writepipe_); |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | void SetUp(); |
michael@0 | 52 | void RegisterHandler(); |
michael@0 | 53 | void SendEvent(); |
michael@0 | 54 | void SendPacket(); |
michael@0 | 55 | |
michael@0 | 56 | void ReceivePacket() { |
michael@0 | 57 | ++received_; |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | void ReceiveEvent() { |
michael@0 | 61 | ++received_; |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | size_t Received() { |
michael@0 | 65 | return received_; |
michael@0 | 66 | } |
michael@0 | 67 | |
michael@0 | 68 | private: |
michael@0 | 69 | nsCOMPtr<nsISocketTransportService> stservice_; |
michael@0 | 70 | nsCOMPtr<nsIEventTarget> target_; |
michael@0 | 71 | size_t received_; |
michael@0 | 72 | PRFileDesc *readpipe_; |
michael@0 | 73 | PRFileDesc *writepipe_; |
michael@0 | 74 | bool registered_; |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | |
michael@0 | 78 | // Received an event. |
michael@0 | 79 | class EventReceived : public nsRunnable { |
michael@0 | 80 | public: |
michael@0 | 81 | EventReceived(SocketTransportServiceTest *test) : |
michael@0 | 82 | test_(test) {} |
michael@0 | 83 | |
michael@0 | 84 | NS_IMETHOD Run() { |
michael@0 | 85 | test_->ReceiveEvent(); |
michael@0 | 86 | return NS_OK; |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | SocketTransportServiceTest *test_; |
michael@0 | 90 | }; |
michael@0 | 91 | |
michael@0 | 92 | |
michael@0 | 93 | // Register our listener on the socket |
michael@0 | 94 | class RegisterEvent : public nsRunnable { |
michael@0 | 95 | public: |
michael@0 | 96 | RegisterEvent(SocketTransportServiceTest *test) : |
michael@0 | 97 | test_(test) {} |
michael@0 | 98 | |
michael@0 | 99 | NS_IMETHOD Run() { |
michael@0 | 100 | test_->RegisterHandler(); |
michael@0 | 101 | return NS_OK; |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | SocketTransportServiceTest *test_; |
michael@0 | 105 | }; |
michael@0 | 106 | |
michael@0 | 107 | |
michael@0 | 108 | class SocketHandler : public nsASocketHandler { |
michael@0 | 109 | public: |
michael@0 | 110 | SocketHandler(SocketTransportServiceTest *test) : test_(test) { |
michael@0 | 111 | } |
michael@0 | 112 | virtual ~SocketHandler() {} |
michael@0 | 113 | |
michael@0 | 114 | void OnSocketReady(PRFileDesc *fd, int16_t outflags) { |
michael@0 | 115 | unsigned char buf[1600]; |
michael@0 | 116 | |
michael@0 | 117 | int32_t rv; |
michael@0 | 118 | rv = PR_Recv(fd, buf, sizeof(buf), 0, PR_INTERVAL_NO_WAIT); |
michael@0 | 119 | if (rv > 0) { |
michael@0 | 120 | std::cerr << "Read " << rv << " bytes" << std::endl; |
michael@0 | 121 | test_->ReceivePacket(); |
michael@0 | 122 | } |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | void OnSocketDetached(PRFileDesc *fd) {} |
michael@0 | 126 | |
michael@0 | 127 | void IsLocal(bool *aIsLocal) { |
michael@0 | 128 | // TODO(jesup): better check? Does it matter? (likely no) |
michael@0 | 129 | *aIsLocal = false; |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | virtual uint64_t ByteCountSent() { return 0; } |
michael@0 | 133 | virtual uint64_t ByteCountReceived() { return 0; } |
michael@0 | 134 | |
michael@0 | 135 | NS_DECL_ISUPPORTS |
michael@0 | 136 | |
michael@0 | 137 | private: |
michael@0 | 138 | SocketTransportServiceTest *test_; |
michael@0 | 139 | }; |
michael@0 | 140 | |
michael@0 | 141 | NS_IMPL_ISUPPORTS0(SocketHandler) |
michael@0 | 142 | |
michael@0 | 143 | void SocketTransportServiceTest::SetUp() { |
michael@0 | 144 | // Get the transport service as a dispatch target |
michael@0 | 145 | nsresult rv; |
michael@0 | 146 | target_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv); |
michael@0 | 147 | ASSERT_TRUE(NS_SUCCEEDED(rv)); |
michael@0 | 148 | |
michael@0 | 149 | // Get the transport service as a transport service |
michael@0 | 150 | stservice_ = do_GetService(NS_SOCKETTRANSPORTSERVICE_CONTRACTID, &rv); |
michael@0 | 151 | ASSERT_TRUE(NS_SUCCEEDED(rv)); |
michael@0 | 152 | |
michael@0 | 153 | // Create a loopback pipe |
michael@0 | 154 | PRStatus status = PR_CreatePipe(&readpipe_, &writepipe_); |
michael@0 | 155 | ASSERT_EQ(status, PR_SUCCESS); |
michael@0 | 156 | |
michael@0 | 157 | // Register ourselves as a listener for the read side of the |
michael@0 | 158 | // socket. The registration has to happen on the STS thread, |
michael@0 | 159 | // hence this event stuff. |
michael@0 | 160 | rv = target_->Dispatch(new RegisterEvent(this), 0); |
michael@0 | 161 | ASSERT_TRUE(NS_SUCCEEDED(rv)); |
michael@0 | 162 | ASSERT_TRUE_WAIT(registered_, 10000); |
michael@0 | 163 | |
michael@0 | 164 | } |
michael@0 | 165 | |
michael@0 | 166 | void SocketTransportServiceTest::RegisterHandler() { |
michael@0 | 167 | nsresult rv; |
michael@0 | 168 | |
michael@0 | 169 | rv = stservice_->AttachSocket(readpipe_, new SocketHandler(this)); |
michael@0 | 170 | ASSERT_TRUE(NS_SUCCEEDED(rv)); |
michael@0 | 171 | |
michael@0 | 172 | registered_ = true; |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | void SocketTransportServiceTest::SendEvent() { |
michael@0 | 176 | nsresult rv; |
michael@0 | 177 | |
michael@0 | 178 | rv = target_->Dispatch(new EventReceived(this), 0); |
michael@0 | 179 | ASSERT_TRUE(NS_SUCCEEDED(rv)); |
michael@0 | 180 | ASSERT_TRUE_WAIT(Received() == 1, 10000); |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | void SocketTransportServiceTest::SendPacket() { |
michael@0 | 184 | unsigned char buffer[1024]; |
michael@0 | 185 | memset(buffer, 0, sizeof(buffer)); |
michael@0 | 186 | |
michael@0 | 187 | int32_t status = PR_Write(writepipe_, buffer, sizeof(buffer)); |
michael@0 | 188 | uint32_t size = status & 0xffff; |
michael@0 | 189 | ASSERT_EQ(sizeof(buffer), size); |
michael@0 | 190 | } |
michael@0 | 191 | |
michael@0 | 192 | |
michael@0 | 193 | |
michael@0 | 194 | // The unit tests themselves |
michael@0 | 195 | TEST_F(SocketTransportServiceTest, SendEvent) { |
michael@0 | 196 | SendEvent(); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | TEST_F(SocketTransportServiceTest, SendPacket) { |
michael@0 | 200 | SendPacket(); |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | |
michael@0 | 204 | } // end namespace |
michael@0 | 205 | |
michael@0 | 206 | |
michael@0 | 207 | int main(int argc, char **argv) { |
michael@0 | 208 | test_utils = new MtransportTestUtils(); |
michael@0 | 209 | |
michael@0 | 210 | // Start the tests |
michael@0 | 211 | ::testing::InitGoogleTest(&argc, argv); |
michael@0 | 212 | |
michael@0 | 213 | int rv = RUN_ALL_TESTS(); |
michael@0 | 214 | delete test_utils; |
michael@0 | 215 | return rv; |
michael@0 | 216 | } |