netwerk/test/TestOpen.cpp

Wed, 31 Dec 2014 06:55:46 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:55:46 +0100
changeset 1
ca08bd8f51b2
permissions
-rw-r--r--

Added tag TORBROWSER_REPLICA for changeset 6474c204b198

     1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "TestCommon.h"
     7 #include "nsCOMPtr.h"
     8 #include "nsStringAPI.h"
     9 #include "nsIURI.h"
    10 #include "nsIChannel.h"
    11 #include "nsIHttpChannel.h"
    12 #include "nsIInputStream.h"
    13 #include "nsNetUtil.h"
    14 #include "mozilla/unused.h"
    16 #include <stdio.h>
    18 using namespace mozilla;
    20 /*
    21  * Test synchronous Open.
    22  */
    24 #define RETURN_IF_FAILED(rv, what) \
    25     PR_BEGIN_MACRO \
    26     if (NS_FAILED(rv)) { \
    27         printf(what ": failed - %08x\n", static_cast<uint32_t>(rv)); \
    28         return -1; \
    29     } \
    30     PR_END_MACRO
    32 int
    33 main(int argc, char **argv)
    34 {
    35     if (test_common_init(&argc, &argv) != 0)
    36         return -1;
    38     nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
    39     if (NS_FAILED(rv)) return -1;
    41     char buf[256];
    43     if (argc != 3) {
    44         printf("Usage: TestOpen url filename\nLoads a URL using ::Open, writing it to a file\n");
    45         return -1;
    46     }
    48     nsCOMPtr<nsIURI> uri;
    49     nsCOMPtr<nsIInputStream> stream;
    51     rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
    52     RETURN_IF_FAILED(rv, "NS_NewURI");
    54     rv = NS_OpenURI(getter_AddRefs(stream), uri);
    55     RETURN_IF_FAILED(rv, "NS_OpenURI");
    57     FILE* outfile = fopen(argv[2], "wb");
    58     if (!outfile) {
    59       printf("error opening %s\n", argv[2]);
    60       return 1;
    61     }
    63     uint32_t read;
    64     while (NS_SUCCEEDED(stream->Read(buf, sizeof(buf), &read)) && read) {
    65       unused << fwrite(buf, 1, read, outfile);
    66     }
    67     printf("Done\n");
    69     fclose(outfile);
    71     NS_ShutdownXPCOM(nullptr);
    72     return 0;
    73 }

mercurial