1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/test/TestOpen.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,73 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "TestCommon.h" 1.10 +#include "nsCOMPtr.h" 1.11 +#include "nsStringAPI.h" 1.12 +#include "nsIURI.h" 1.13 +#include "nsIChannel.h" 1.14 +#include "nsIHttpChannel.h" 1.15 +#include "nsIInputStream.h" 1.16 +#include "nsNetUtil.h" 1.17 +#include "mozilla/unused.h" 1.18 + 1.19 +#include <stdio.h> 1.20 + 1.21 +using namespace mozilla; 1.22 + 1.23 +/* 1.24 + * Test synchronous Open. 1.25 + */ 1.26 + 1.27 +#define RETURN_IF_FAILED(rv, what) \ 1.28 + PR_BEGIN_MACRO \ 1.29 + if (NS_FAILED(rv)) { \ 1.30 + printf(what ": failed - %08x\n", static_cast<uint32_t>(rv)); \ 1.31 + return -1; \ 1.32 + } \ 1.33 + PR_END_MACRO 1.34 + 1.35 +int 1.36 +main(int argc, char **argv) 1.37 +{ 1.38 + if (test_common_init(&argc, &argv) != 0) 1.39 + return -1; 1.40 + 1.41 + nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.42 + if (NS_FAILED(rv)) return -1; 1.43 + 1.44 + char buf[256]; 1.45 + 1.46 + if (argc != 3) { 1.47 + printf("Usage: TestOpen url filename\nLoads a URL using ::Open, writing it to a file\n"); 1.48 + return -1; 1.49 + } 1.50 + 1.51 + nsCOMPtr<nsIURI> uri; 1.52 + nsCOMPtr<nsIInputStream> stream; 1.53 + 1.54 + rv = NS_NewURI(getter_AddRefs(uri), argv[1]); 1.55 + RETURN_IF_FAILED(rv, "NS_NewURI"); 1.56 + 1.57 + rv = NS_OpenURI(getter_AddRefs(stream), uri); 1.58 + RETURN_IF_FAILED(rv, "NS_OpenURI"); 1.59 + 1.60 + FILE* outfile = fopen(argv[2], "wb"); 1.61 + if (!outfile) { 1.62 + printf("error opening %s\n", argv[2]); 1.63 + return 1; 1.64 + } 1.65 + 1.66 + uint32_t read; 1.67 + while (NS_SUCCEEDED(stream->Read(buf, sizeof(buf), &read)) && read) { 1.68 + unused << fwrite(buf, 1, read, outfile); 1.69 + } 1.70 + printf("Done\n"); 1.71 + 1.72 + fclose(outfile); 1.73 + 1.74 + NS_ShutdownXPCOM(nullptr); 1.75 + return 0; 1.76 +}