michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "TestCommon.h" michael@0: #include "nsIComponentRegistrar.h" michael@0: #include "nsISocketTransportService.h" michael@0: #include "nsISocketTransport.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIComponentManager.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsStringAPI.h" michael@0: #include "nsIFile.h" michael@0: #include "nsNetUtil.h" michael@0: #include "prlog.h" michael@0: #include "prenv.h" michael@0: #include "prthread.h" michael@0: #include michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #if defined(PR_LOGGING) michael@0: // michael@0: // set NSPR_LOG_MODULES=Test:5 michael@0: // michael@0: static PRLogModuleInfo *gTestLog = nullptr; michael@0: #endif michael@0: #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args) michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID); michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: static nsresult michael@0: RunBlockingTest(const nsACString &host, int32_t port, nsIFile *file) michael@0: { michael@0: nsresult rv; michael@0: michael@0: LOG(("RunBlockingTest\n")); michael@0: michael@0: nsCOMPtr sts = michael@0: do_GetService(kSocketTransportServiceCID, &rv); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsCOMPtr input; michael@0: rv = NS_NewLocalFileInputStream(getter_AddRefs(input), file); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsCOMPtr trans; michael@0: rv = sts->CreateTransport(nullptr, 0, host, port, nullptr, getter_AddRefs(trans)); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nsCOMPtr output; michael@0: rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(output)); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: char buf[120]; michael@0: uint32_t nr, nw; michael@0: for (;;) { michael@0: rv = input->Read(buf, sizeof(buf), &nr); michael@0: if (NS_FAILED(rv) || (nr == 0)) return rv; michael@0: michael@0: /* michael@0: const char *p = buf; michael@0: while (nr) { michael@0: rv = output->Write(p, nr, &nw); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: nr -= nw; michael@0: p += nw; michael@0: } michael@0: */ michael@0: michael@0: rv = output->Write(buf, nr, &nw); michael@0: if (NS_FAILED(rv)) return rv; michael@0: michael@0: NS_ASSERTION(nr == nw, "not all written"); michael@0: } michael@0: michael@0: LOG((" done copying data.\n")); michael@0: return NS_OK; michael@0: } michael@0: michael@0: //////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: int michael@0: main(int argc, char* argv[]) michael@0: { michael@0: if (test_common_init(&argc, &argv) != 0) michael@0: return -1; michael@0: michael@0: nsresult rv; michael@0: michael@0: if (argc < 4) { michael@0: printf("usage: %s \n", argv[0]); michael@0: return -1; michael@0: } michael@0: char* hostName = argv[1]; michael@0: int32_t port = atoi(argv[2]); michael@0: char* fileName = argv[3]; michael@0: { michael@0: nsCOMPtr servMan; michael@0: NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); michael@0: michael@0: #if defined(PR_LOGGING) michael@0: gTestLog = PR_NewLogModule("Test"); michael@0: #endif michael@0: michael@0: nsCOMPtr file; michael@0: rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file)); michael@0: if (NS_FAILED(rv)) return -1; michael@0: michael@0: rv = RunBlockingTest(nsDependentCString(hostName), port, file); michael@0: #if defined(PR_LOGGING) michael@0: if (NS_FAILED(rv)) michael@0: LOG(("RunBlockingTest failed [rv=%x]\n", rv)); michael@0: #endif michael@0: michael@0: // give background threads a chance to finish whatever work they may michael@0: // be doing. michael@0: LOG(("sleeping for 5 seconds...\n")); michael@0: PR_Sleep(PR_SecondsToInterval(5)); michael@0: } // this scopes the nsCOMPtrs michael@0: // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM michael@0: rv = NS_ShutdownXPCOM(nullptr); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); michael@0: return 0; michael@0: }