1 /* ***** BEGIN LICENSE BLOCK *****
2 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 * The contents of this file are subject to the Mozilla Public License Version
5 * 1.1 (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 * http://www.mozilla.org/MPL/
9 * Software distributed under the License is distributed on an "AS IS" basis,
10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11 * for the specific language governing rights and limitations under the
14 * The Original Code is Mozilla.
16 * The Initial Developer of the Original Code is
17 * Netscape Communications Corporation.
18 * Portions created by the Initial Developer are Copyright (C) 2002
19 * the Initial Developer. All Rights Reserved.
22 * Darin Fisher <darin@netscape.com>
24 * Alternatively, the contents of this file may be used under the terms of
25 * either the GNU General Public License Version 2 or later (the "GPL"), or
26 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "TestCommon.h"
39 #include "nsIComponentRegistrar.h"
40 #include "nsIStreamTransportService.h"
41 #include "nsIAsyncInputStream.h"
42 #include "nsIProgressEventSink.h"
43 #include "nsIInterfaceRequestor.h"
44 #include "nsIInterfaceRequestorUtils.h"
45 #include "nsIProxyObjectManager.h"
46 #include "nsIRequest.h"
47 #include "nsIServiceManager.h"
48 #include "nsIComponentManager.h"
49 #include "nsISeekableStream.h"
52 #include "nsStringAPI.h"
53 #include "nsIFileStreams.h"
54 #include "nsIStreamListener.h"
55 #include "nsILocalFile.h"
56 #include "nsNetUtil.h"
57 #include "nsAutoLock.h"
61 ////////////////////////////////////////////////////////////////////////////////
63 #if defined(PR_LOGGING)
65 // set NSPR_LOG_MODULES=Test:5
67 static PRLogModuleInfo
*gTestLog
= nsnull
;
68 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
73 ////////////////////////////////////////////////////////////////////////////////
75 class MyListener
: public nsIStreamListener
81 virtual ~MyListener() {}
83 NS_IMETHOD
OnStartRequest(nsIRequest
*req
, nsISupports
*ctx
)
85 LOG(("MyListener::OnStartRequest\n"));
89 NS_IMETHOD
OnDataAvailable(nsIRequest
*req
, nsISupports
*ctx
,
90 nsIInputStream
*stream
,
91 PRUint32 offset
, PRUint32 count
)
93 LOG(("MyListener::OnDataAvailable [offset=%u count=%u]\n", offset
, count
));
99 PRUint32 n
, amt
= PR_MIN(count
, sizeof(buf
));
101 rv
= stream
->Read(buf
, amt
, &n
);
103 LOG((" read returned 0x%08x\n", rv
));
107 fwrite(buf
, n
, 1, stdout
);
110 LOG((" read %u bytes\n", n
));
117 NS_IMETHOD
OnStopRequest(nsIRequest
*req
, nsISupports
*ctx
, nsresult status
)
119 LOG(("MyListener::OnStopRequest [status=%x]\n", status
));
125 NS_IMPL_ISUPPORTS2(MyListener
,
129 ////////////////////////////////////////////////////////////////////////////////
132 * asynchronously copy file.
135 RunTest(nsIFile
*file
, PRInt64 offset
, PRInt64 length
)
141 nsCOMPtr
<nsIInputStream
> stream
;
142 rv
= NS_NewLocalFileInputStream(getter_AddRefs(stream
), file
);
143 if (NS_FAILED(rv
)) return rv
;
145 nsCOMPtr
<nsIInputStreamPump
> pump
;
146 rv
= NS_NewInputStreamPump(getter_AddRefs(pump
), stream
, offset
, length
);
147 if (NS_FAILED(rv
)) return rv
;
149 rv
= pump
->AsyncRead(new MyListener(), nsnull
);
150 if (NS_FAILED(rv
)) return rv
;
156 ////////////////////////////////////////////////////////////////////////////////
159 main(int argc
, char* argv
[])
161 if (test_common_init(&argc
, &argv
) != 0)
167 printf("usage: %s <file-to-read> <start-offset> <read-length>\n", argv
[0]);
170 char* fileName
= argv
[1];
171 PRInt64 offset
, length
;
172 int err
= PR_sscanf(argv
[2], "%lld", &offset
);
174 printf("Start offset must be an integer!\n");
177 err
= PR_sscanf(argv
[3], "%lld", &length
);
179 printf("Length must be an integer!\n");
184 nsCOMPtr
<nsIServiceManager
> servMan
;
185 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
186 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
187 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
189 registrar
->AutoRegister(nsnull
);
191 #if defined(PR_LOGGING)
192 gTestLog
= PR_NewLogModule("Test");
195 nsCOMPtr
<nsILocalFile
> file
;
196 rv
= NS_NewNativeLocalFile(nsDependentCString(fileName
), PR_FALSE
, getter_AddRefs(file
));
197 if (NS_FAILED(rv
)) return rv
;
199 rv
= RunTest(file
, offset
, length
);
200 NS_ASSERTION(NS_SUCCEEDED(rv
), "RunTest failed");
202 // give background threads a chance to finish whatever work they may
204 PR_Sleep(PR_SecondsToInterval(1));
205 } // this scopes the nsCOMPtrs
206 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
207 rv
= NS_ShutdownXPCOM(nsnull
);
208 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");