Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "TestCommon.h" |
michael@0 | 6 | #include "nsIComponentRegistrar.h" |
michael@0 | 7 | #include "nsISocketTransportService.h" |
michael@0 | 8 | #include "nsISocketTransport.h" |
michael@0 | 9 | #include "nsIServiceManager.h" |
michael@0 | 10 | #include "nsIComponentManager.h" |
michael@0 | 11 | #include "nsCOMPtr.h" |
michael@0 | 12 | #include "nsStringAPI.h" |
michael@0 | 13 | #include "nsIFile.h" |
michael@0 | 14 | #include "nsNetUtil.h" |
michael@0 | 15 | #include "prlog.h" |
michael@0 | 16 | #include "prenv.h" |
michael@0 | 17 | #include "prthread.h" |
michael@0 | 18 | #include <stdlib.h> |
michael@0 | 19 | |
michael@0 | 20 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 21 | |
michael@0 | 22 | #if defined(PR_LOGGING) |
michael@0 | 23 | // |
michael@0 | 24 | // set NSPR_LOG_MODULES=Test:5 |
michael@0 | 25 | // |
michael@0 | 26 | static PRLogModuleInfo *gTestLog = nullptr; |
michael@0 | 27 | #endif |
michael@0 | 28 | #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) |
michael@0 | 29 | |
michael@0 | 30 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 31 | |
michael@0 | 32 | static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID); |
michael@0 | 33 | |
michael@0 | 34 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 35 | |
michael@0 | 36 | static nsresult |
michael@0 | 37 | RunBlockingTest(const nsACString &host, int32_t port, nsIFile *file) |
michael@0 | 38 | { |
michael@0 | 39 | nsresult rv; |
michael@0 | 40 | |
michael@0 | 41 | LOG(("RunBlockingTest\n")); |
michael@0 | 42 | |
michael@0 | 43 | nsCOMPtr<nsISocketTransportService> sts = |
michael@0 | 44 | do_GetService(kSocketTransportServiceCID, &rv); |
michael@0 | 45 | if (NS_FAILED(rv)) return rv; |
michael@0 | 46 | |
michael@0 | 47 | nsCOMPtr<nsIInputStream> input; |
michael@0 | 48 | rv = NS_NewLocalFileInputStream(getter_AddRefs(input), file); |
michael@0 | 49 | if (NS_FAILED(rv)) return rv; |
michael@0 | 50 | |
michael@0 | 51 | nsCOMPtr<nsISocketTransport> trans; |
michael@0 | 52 | rv = sts->CreateTransport(nullptr, 0, host, port, nullptr, getter_AddRefs(trans)); |
michael@0 | 53 | if (NS_FAILED(rv)) return rv; |
michael@0 | 54 | |
michael@0 | 55 | nsCOMPtr<nsIOutputStream> output; |
michael@0 | 56 | rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(output)); |
michael@0 | 57 | if (NS_FAILED(rv)) return rv; |
michael@0 | 58 | |
michael@0 | 59 | char buf[120]; |
michael@0 | 60 | uint32_t nr, nw; |
michael@0 | 61 | for (;;) { |
michael@0 | 62 | rv = input->Read(buf, sizeof(buf), &nr); |
michael@0 | 63 | if (NS_FAILED(rv) || (nr == 0)) return rv; |
michael@0 | 64 | |
michael@0 | 65 | /* |
michael@0 | 66 | const char *p = buf; |
michael@0 | 67 | while (nr) { |
michael@0 | 68 | rv = output->Write(p, nr, &nw); |
michael@0 | 69 | if (NS_FAILED(rv)) return rv; |
michael@0 | 70 | |
michael@0 | 71 | nr -= nw; |
michael@0 | 72 | p += nw; |
michael@0 | 73 | } |
michael@0 | 74 | */ |
michael@0 | 75 | |
michael@0 | 76 | rv = output->Write(buf, nr, &nw); |
michael@0 | 77 | if (NS_FAILED(rv)) return rv; |
michael@0 | 78 | |
michael@0 | 79 | NS_ASSERTION(nr == nw, "not all written"); |
michael@0 | 80 | } |
michael@0 | 81 | |
michael@0 | 82 | LOG((" done copying data.\n")); |
michael@0 | 83 | return NS_OK; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | //////////////////////////////////////////////////////////////////////////////// |
michael@0 | 87 | |
michael@0 | 88 | int |
michael@0 | 89 | main(int argc, char* argv[]) |
michael@0 | 90 | { |
michael@0 | 91 | if (test_common_init(&argc, &argv) != 0) |
michael@0 | 92 | return -1; |
michael@0 | 93 | |
michael@0 | 94 | nsresult rv; |
michael@0 | 95 | |
michael@0 | 96 | if (argc < 4) { |
michael@0 | 97 | printf("usage: %s <host> <port> <file-to-read>\n", argv[0]); |
michael@0 | 98 | return -1; |
michael@0 | 99 | } |
michael@0 | 100 | char* hostName = argv[1]; |
michael@0 | 101 | int32_t port = atoi(argv[2]); |
michael@0 | 102 | char* fileName = argv[3]; |
michael@0 | 103 | { |
michael@0 | 104 | nsCOMPtr<nsIServiceManager> servMan; |
michael@0 | 105 | NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); |
michael@0 | 106 | |
michael@0 | 107 | #if defined(PR_LOGGING) |
michael@0 | 108 | gTestLog = PR_NewLogModule("Test"); |
michael@0 | 109 | #endif |
michael@0 | 110 | |
michael@0 | 111 | nsCOMPtr<nsIFile> file; |
michael@0 | 112 | rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file)); |
michael@0 | 113 | if (NS_FAILED(rv)) return -1; |
michael@0 | 114 | |
michael@0 | 115 | rv = RunBlockingTest(nsDependentCString(hostName), port, file); |
michael@0 | 116 | #if defined(PR_LOGGING) |
michael@0 | 117 | if (NS_FAILED(rv)) |
michael@0 | 118 | LOG(("RunBlockingTest failed [rv=%x]\n", rv)); |
michael@0 | 119 | #endif |
michael@0 | 120 | |
michael@0 | 121 | // give background threads a chance to finish whatever work they may |
michael@0 | 122 | // be doing. |
michael@0 | 123 | LOG(("sleeping for 5 seconds...\n")); |
michael@0 | 124 | PR_Sleep(PR_SecondsToInterval(5)); |
michael@0 | 125 | } // this scopes the nsCOMPtrs |
michael@0 | 126 | // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM |
michael@0 | 127 | rv = NS_ShutdownXPCOM(nullptr); |
michael@0 | 128 | NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); |
michael@0 | 129 | return 0; |
michael@0 | 130 | } |