Bumping manifests a=b2g-bump
[gecko.git] / netwerk / test / TestOpen.cpp
blob9b404976c6f58f2296a03f50f7397a72ea305710
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"
15 #include "nsIScriptSecurityManager.h"
17 #include <stdio.h>
19 using namespace mozilla;
22 * Test synchronous Open.
25 #define RETURN_IF_FAILED(rv, what) \
26 PR_BEGIN_MACRO \
27 if (NS_FAILED(rv)) { \
28 printf(what ": failed - %08x\n", static_cast<uint32_t>(rv)); \
29 return -1; \
30 } \
31 PR_END_MACRO
33 int
34 main(int argc, char **argv)
36 if (test_common_init(&argc, &argv) != 0)
37 return -1;
39 nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
40 if (NS_FAILED(rv)) return -1;
42 char buf[256];
44 if (argc != 3) {
45 printf("Usage: TestOpen url filename\nLoads a URL using ::Open, writing it to a file\n");
46 return -1;
49 nsCOMPtr<nsIURI> uri;
50 nsCOMPtr<nsIInputStream> stream;
52 rv = NS_NewURI(getter_AddRefs(uri), argv[1]);
53 RETURN_IF_FAILED(rv, "NS_NewURI");
55 nsCOMPtr<nsIScriptSecurityManager> secman =
56 do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
57 RETURN_IF_FAILED(rv, "Couldn't get script security manager!");
58 nsCOMPtr<nsIPrincipal> systemPrincipal;
59 rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
60 RETURN_IF_FAILED(rv, "Couldn't get system principal!");
62 nsCOMPtr<nsIChannel> channel;
63 rv = NS_NewChannel(getter_AddRefs(channel),
64 uri,
65 systemPrincipal,
66 nsILoadInfo::SEC_NORMAL,
67 nsIContentPolicy::TYPE_OTHER);
68 RETURN_IF_FAILED(rv, "NS_NewChannel");
70 rv = channel->Open(getter_AddRefs(stream));
71 RETURN_IF_FAILED(rv, "channel->Open()");
73 FILE* outfile = fopen(argv[2], "wb");
74 if (!outfile) {
75 printf("error opening %s\n", argv[2]);
76 return 1;
79 uint32_t read;
80 while (NS_SUCCEEDED(stream->Read(buf, sizeof(buf), &read)) && read) {
81 unused << fwrite(buf, 1, read, outfile);
83 printf("Done\n");
85 fclose(outfile);
87 NS_ShutdownXPCOM(nullptr);
88 return 0;