netwerk/test/TestBlockingSocket.cpp

changeset 1
ca08bd8f51b2
equal deleted inserted replaced
-1:000000000000 0:bd3733cb5554
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/. */
4
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>
19
20 ////////////////////////////////////////////////////////////////////////////////
21
22 #if defined(PR_LOGGING)
23 //
24 // set NSPR_LOG_MODULES=Test:5
25 //
26 static PRLogModuleInfo *gTestLog = nullptr;
27 #endif
28 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
29
30 ////////////////////////////////////////////////////////////////////////////////
31
32 static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
33
34 ////////////////////////////////////////////////////////////////////////////////
35
36 static nsresult
37 RunBlockingTest(const nsACString &host, int32_t port, nsIFile *file)
38 {
39 nsresult rv;
40
41 LOG(("RunBlockingTest\n"));
42
43 nsCOMPtr<nsISocketTransportService> sts =
44 do_GetService(kSocketTransportServiceCID, &rv);
45 if (NS_FAILED(rv)) return rv;
46
47 nsCOMPtr<nsIInputStream> input;
48 rv = NS_NewLocalFileInputStream(getter_AddRefs(input), file);
49 if (NS_FAILED(rv)) return rv;
50
51 nsCOMPtr<nsISocketTransport> trans;
52 rv = sts->CreateTransport(nullptr, 0, host, port, nullptr, getter_AddRefs(trans));
53 if (NS_FAILED(rv)) return rv;
54
55 nsCOMPtr<nsIOutputStream> output;
56 rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(output));
57 if (NS_FAILED(rv)) return rv;
58
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;
64
65 /*
66 const char *p = buf;
67 while (nr) {
68 rv = output->Write(p, nr, &nw);
69 if (NS_FAILED(rv)) return rv;
70
71 nr -= nw;
72 p += nw;
73 }
74 */
75
76 rv = output->Write(buf, nr, &nw);
77 if (NS_FAILED(rv)) return rv;
78
79 NS_ASSERTION(nr == nw, "not all written");
80 }
81
82 LOG((" done copying data.\n"));
83 return NS_OK;
84 }
85
86 ////////////////////////////////////////////////////////////////////////////////
87
88 int
89 main(int argc, char* argv[])
90 {
91 if (test_common_init(&argc, &argv) != 0)
92 return -1;
93
94 nsresult rv;
95
96 if (argc < 4) {
97 printf("usage: %s <host> <port> <file-to-read>\n", argv[0]);
98 return -1;
99 }
100 char* hostName = argv[1];
101 int32_t port = atoi(argv[2]);
102 char* fileName = argv[3];
103 {
104 nsCOMPtr<nsIServiceManager> servMan;
105 NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
106
107 #if defined(PR_LOGGING)
108 gTestLog = PR_NewLogModule("Test");
109 #endif
110
111 nsCOMPtr<nsIFile> file;
112 rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file));
113 if (NS_FAILED(rv)) return -1;
114
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
120
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;
130 }

mercurial