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"
12 #include "nsStringAPI.h"
14 #include "nsNetUtil.h"
20 ////////////////////////////////////////////////////////////////////////////////
22 #if defined(PR_LOGGING)
24 // set NSPR_LOG_MODULES=Test:5
26 static PRLogModuleInfo
*gTestLog
= nullptr;
28 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
30 ////////////////////////////////////////////////////////////////////////////////
32 static NS_DEFINE_CID(kSocketTransportServiceCID
, NS_SOCKETTRANSPORTSERVICE_CID
);
34 ////////////////////////////////////////////////////////////////////////////////
37 RunBlockingTest(const nsACString
&host
, int32_t port
, nsIFile
*file
)
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
;
62 rv
= input
->Read(buf
, sizeof(buf
), &nr
);
63 if (NS_FAILED(rv
) || (nr
== 0)) return rv
;
68 rv = output->Write(p, nr, &nw);
69 if (NS_FAILED(rv)) return rv;
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"));
86 ////////////////////////////////////////////////////////////////////////////////
89 main(int argc
, char* argv
[])
91 if (test_common_init(&argc
, &argv
) != 0)
97 printf("usage: %s <host> <port> <file-to-read>\n", argv
[0]);
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");
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)
118 LOG(("RunBlockingTest failed [rv=%x]\n", rv
));
121 // give background threads a chance to finish whatever work they may
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");