netwerk/test/TestBlockingSocket.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/TestBlockingSocket.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,130 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +#include "TestCommon.h"
     1.9 +#include "nsIComponentRegistrar.h"
    1.10 +#include "nsISocketTransportService.h"
    1.11 +#include "nsISocketTransport.h"
    1.12 +#include "nsIServiceManager.h"
    1.13 +#include "nsIComponentManager.h"
    1.14 +#include "nsCOMPtr.h"
    1.15 +#include "nsStringAPI.h"
    1.16 +#include "nsIFile.h"
    1.17 +#include "nsNetUtil.h"
    1.18 +#include "prlog.h"
    1.19 +#include "prenv.h"
    1.20 +#include "prthread.h"
    1.21 +#include <stdlib.h>
    1.22 +
    1.23 +////////////////////////////////////////////////////////////////////////////////
    1.24 +
    1.25 +#if defined(PR_LOGGING)
    1.26 +//
    1.27 +// set NSPR_LOG_MODULES=Test:5
    1.28 +//
    1.29 +static PRLogModuleInfo *gTestLog = nullptr;
    1.30 +#endif
    1.31 +#define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
    1.32 +
    1.33 +////////////////////////////////////////////////////////////////////////////////
    1.34 +
    1.35 +static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
    1.36 +
    1.37 +////////////////////////////////////////////////////////////////////////////////
    1.38 +
    1.39 +static nsresult
    1.40 +RunBlockingTest(const nsACString &host, int32_t port, nsIFile *file)
    1.41 +{
    1.42 +    nsresult rv;
    1.43 +
    1.44 +    LOG(("RunBlockingTest\n"));
    1.45 +
    1.46 +    nsCOMPtr<nsISocketTransportService> sts =
    1.47 +        do_GetService(kSocketTransportServiceCID, &rv);
    1.48 +    if (NS_FAILED(rv)) return rv;
    1.49 +
    1.50 +    nsCOMPtr<nsIInputStream> input;
    1.51 +    rv = NS_NewLocalFileInputStream(getter_AddRefs(input), file);
    1.52 +    if (NS_FAILED(rv)) return rv;
    1.53 +
    1.54 +    nsCOMPtr<nsISocketTransport> trans;
    1.55 +    rv = sts->CreateTransport(nullptr, 0, host, port, nullptr, getter_AddRefs(trans));
    1.56 +    if (NS_FAILED(rv)) return rv;
    1.57 +
    1.58 +    nsCOMPtr<nsIOutputStream> output;
    1.59 +    rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 100, 10, getter_AddRefs(output));
    1.60 +    if (NS_FAILED(rv)) return rv;
    1.61 +
    1.62 +    char buf[120];
    1.63 +    uint32_t nr, nw;
    1.64 +    for (;;) {
    1.65 +        rv = input->Read(buf, sizeof(buf), &nr);
    1.66 +        if (NS_FAILED(rv) || (nr == 0)) return rv;
    1.67 +
    1.68 +/*
    1.69 +        const char *p = buf;
    1.70 +        while (nr) {
    1.71 +            rv = output->Write(p, nr, &nw);
    1.72 +            if (NS_FAILED(rv)) return rv;
    1.73 +
    1.74 +            nr -= nw;
    1.75 +            p  += nw;
    1.76 +        }
    1.77 +*/
    1.78 +        
    1.79 +        rv = output->Write(buf, nr, &nw);
    1.80 +        if (NS_FAILED(rv)) return rv;
    1.81 +
    1.82 +        NS_ASSERTION(nr == nw, "not all written");
    1.83 +    }
    1.84 +
    1.85 +    LOG(("  done copying data.\n"));
    1.86 +    return NS_OK;
    1.87 +}
    1.88 +
    1.89 +////////////////////////////////////////////////////////////////////////////////
    1.90 +
    1.91 +int
    1.92 +main(int argc, char* argv[])
    1.93 +{
    1.94 +    if (test_common_init(&argc, &argv) != 0)
    1.95 +        return -1;
    1.96 +
    1.97 +    nsresult rv;
    1.98 +
    1.99 +    if (argc < 4) {
   1.100 +        printf("usage: %s <host> <port> <file-to-read>\n", argv[0]);
   1.101 +        return -1;
   1.102 +    }
   1.103 +    char* hostName = argv[1];
   1.104 +    int32_t port = atoi(argv[2]);
   1.105 +    char* fileName = argv[3];
   1.106 +    {
   1.107 +        nsCOMPtr<nsIServiceManager> servMan;
   1.108 +        NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
   1.109 +
   1.110 +#if defined(PR_LOGGING)
   1.111 +        gTestLog = PR_NewLogModule("Test");
   1.112 +#endif
   1.113 +
   1.114 +        nsCOMPtr<nsIFile> file;
   1.115 +        rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file));
   1.116 +        if (NS_FAILED(rv)) return -1;
   1.117 +
   1.118 +        rv = RunBlockingTest(nsDependentCString(hostName), port, file);
   1.119 +#if defined(PR_LOGGING)
   1.120 +        if (NS_FAILED(rv))
   1.121 +            LOG(("RunBlockingTest failed [rv=%x]\n", rv));
   1.122 +#endif
   1.123 +
   1.124 +        // give background threads a chance to finish whatever work they may
   1.125 +        // be doing.
   1.126 +        LOG(("sleeping for 5 seconds...\n"));
   1.127 +        PR_Sleep(PR_SecondsToInterval(5));
   1.128 +    } // this scopes the nsCOMPtrs
   1.129 +    // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
   1.130 +    rv = NS_ShutdownXPCOM(nullptr);
   1.131 +    NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
   1.132 +    return 0;
   1.133 +}

mercurial