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"
8 #include "nsStringAPI.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"
19 using namespace mozilla
;
22 * Test synchronous Open.
25 #define RETURN_IF_FAILED(rv, what) \
27 if (NS_FAILED(rv)) { \
28 printf(what ": failed - %08x\n", static_cast<uint32_t>(rv)); \
34 main(int argc
, char **argv
)
36 if (test_common_init(&argc
, &argv
) != 0)
39 nsresult rv
= NS_InitXPCOM2(nullptr, nullptr, nullptr);
40 if (NS_FAILED(rv
)) return -1;
45 printf("Usage: TestOpen url filename\nLoads a URL using ::Open, writing it to a file\n");
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
),
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");
75 printf("error opening %s\n", argv
[2]);
80 while (NS_SUCCEEDED(stream
->Read(buf
, sizeof(buf
), &read
)) && read
) {
81 unused
<< fwrite(buf
, 1, read
, outfile
);
87 NS_ShutdownXPCOM(nullptr);