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

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

mercurial