Bumping gaia.json for 2 gaia revision(s) a=gaia-bump
[gecko.git] / netwerk / test / TestBlockingSocket.cpp
blobe4812faf61f0f6652c373119dd33f9a216082d2e
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #include "TestCommon.h"
6 #include "nsIComponentRegistrar.h"
7 #include "nsISocketTransportService.h"
8 #include "nsISocketTransport.h"
9 #include "nsIServiceManager.h"
10 #include "nsIComponentManager.h"
11 #include "nsCOMPtr.h"
12 #include "nsStringAPI.h"
13 #include "nsIFile.h"
14 #include "nsNetUtil.h"
15 #include "prlog.h"
16 #include "prenv.h"
17 #include "prthread.h"
18 #include <stdlib.h>
20 ////////////////////////////////////////////////////////////////////////////////
22 #if defined(PR_LOGGING)
24 // set NSPR_LOG_MODULES=Test:5
26 static PRLogModuleInfo *gTestLog = nullptr;
27 #endif
28 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
30 ////////////////////////////////////////////////////////////////////////////////
32 static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
34 ////////////////////////////////////////////////////////////////////////////////
36 static nsresult
37 RunBlockingTest(const nsACString &host, int32_t port, nsIFile *file)
39 nsresult rv;
41 LOG(("RunBlockingTest\n"));
43 nsCOMPtr<nsISocketTransportService> sts =
44 do_GetService(kSocketTransportServiceCID, &rv);
45 if (NS_FAILED(rv)) return rv;
47 nsCOMPtr<nsIInputStream> input;
48 rv = NS_NewLocalFileInputStream(getter_AddRefs(input), file);
49 if (NS_FAILED(rv)) return rv;
51 nsCOMPtr<nsISocketTransport> trans;
52 rv = sts->CreateTransport(nullptr, 0, host, port, nullptr, getter_AddRefs(trans));
53 if (NS_FAILED(rv)) return rv;
55 nsCOMPtr<nsIOutputStream> output;
56 rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(output));
57 if (NS_FAILED(rv)) return rv;
59 char buf[120];
60 uint32_t nr, nw;
61 for (;;) {
62 rv = input->Read(buf, sizeof(buf), &nr);
63 if (NS_FAILED(rv) || (nr == 0)) return rv;
66 const char *p = buf;
67 while (nr) {
68 rv = output->Write(p, nr, &nw);
69 if (NS_FAILED(rv)) return rv;
71 nr -= nw;
72 p += nw;
76 rv = output->Write(buf, nr, &nw);
77 if (NS_FAILED(rv)) return rv;
79 NS_ASSERTION(nr == nw, "not all written");
82 LOG((" done copying data.\n"));
83 return NS_OK;
86 ////////////////////////////////////////////////////////////////////////////////
88 int
89 main(int argc, char* argv[])
91 if (test_common_init(&argc, &argv) != 0)
92 return -1;
94 nsresult rv;
96 if (argc < 4) {
97 printf("usage: %s <host> <port> <file-to-read>\n", argv[0]);
98 return -1;
100 char* hostName = argv[1];
101 int32_t port = atoi(argv[2]);
102 char* fileName = argv[3];
104 nsCOMPtr<nsIServiceManager> servMan;
105 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
107 #if defined(PR_LOGGING)
108 gTestLog = PR_NewLogModule("Test");
109 #endif
111 nsCOMPtr<nsIFile> file;
112 rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file));
113 if (NS_FAILED(rv)) return -1;
115 rv = RunBlockingTest(nsDependentCString(hostName), port, file);
116 #if defined(PR_LOGGING)
117 if (NS_FAILED(rv))
118 LOG(("RunBlockingTest failed [rv=%x]\n", rv));
119 #endif
121 // give background threads a chance to finish whatever work they may
122 // be doing.
123 LOG(("sleeping for 5 seconds...\n"));
124 PR_Sleep(PR_SecondsToInterval(5));
125 } // this scopes the nsCOMPtrs
126 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
127 rv = NS_ShutdownXPCOM(nullptr);
128 NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
129 return 0;