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"
51 #include "nsStringAPI.h"
52 #include "nsIFileStreams.h"
53 #include "nsIStreamListener.h"
54 #include "nsILocalFile.h"
55 #include "nsNetUtil.h"
56 #include "nsAutoLock.h"
60 ////////////////////////////////////////////////////////////////////////////////
62 #if defined(PR_LOGGING)
64 // set NSPR_LOG_MODULES=Test:5
66 static PRLogModuleInfo
*gTestLog
= nsnull
;
68 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
70 ////////////////////////////////////////////////////////////////////////////////
72 static NS_DEFINE_CID(kStreamTransportServiceCID
, NS_STREAMTRANSPORTSERVICE_CID
);
74 ////////////////////////////////////////////////////////////////////////////////
76 #define CHUNK_SIZE 500
78 class MyCopier
: public nsIInputStreamCallback
79 , public nsIOutputStreamCallback
86 , mInputCondition(NS_OK
)
93 PR_DestroyLock(mLock
);
100 // called on any thread
101 NS_IMETHOD
OnInputStreamReady(nsIAsyncInputStream
*inStr
)
103 LOG(("OnInputStreamReady\n"));
104 nsAutoLock
lock(mLock
);
105 NS_ASSERTION(inStr
== mInput
, "unexpected stream");
110 // called on any thread
111 NS_IMETHOD
OnOutputStreamReady(nsIAsyncOutputStream
*outStr
)
113 LOG(("OnOutputStreamReady\n"));
114 nsAutoLock
lock(mLock
);
115 NS_ASSERTION(outStr
== mOutput
, "unexpected stream");
122 LOG(("Close_Locked\n"));
129 // post done copying event
133 void Process_Locked()
136 mInputCondition
= NS_OK
; // reset
139 nsresult rv
= mOutput
->WriteSegments(FillOutputBuffer
, this, CHUNK_SIZE
, &n
);
140 if (NS_FAILED(rv
) || (n
== 0)) {
141 if (rv
== NS_BASE_STREAM_WOULD_BLOCK
)
142 mOutput
->AsyncWait(this, 0, 0, nsnull
);
143 else if (mInputCondition
== NS_BASE_STREAM_WOULD_BLOCK
)
144 mInput
->AsyncWait(this, 0, 0, nsnull
);
152 nsresult
AsyncCopy(nsITransport
*srcTrans
, nsITransport
*destTrans
)
154 mLock
= PR_NewLock();
156 return NS_ERROR_OUT_OF_MEMORY
;
160 nsCOMPtr
<nsIInputStream
> inStr
;
161 rv
= srcTrans
->OpenInputStream(0, 0, 0, getter_AddRefs(inStr
));
162 if (NS_FAILED(rv
)) return rv
;
164 nsCOMPtr
<nsIOutputStream
> outStr
;
165 rv
= destTrans
->OpenOutputStream(0, 0, 0, getter_AddRefs(outStr
));
166 if (NS_FAILED(rv
)) return rv
;
168 mInput
= do_QueryInterface(inStr
);
169 mOutput
= do_QueryInterface(outStr
);
171 return mInput
->AsyncWait(this, 0, 0, nsnull
);
174 static NS_METHOD
FillOutputBuffer(nsIOutputStream
*outStr
,
181 MyCopier
*self
= (MyCopier
*) closure
;
183 nsresult rv
= self
->mInput
->Read(buffer
, count
, countRead
);
185 self
->mInputCondition
= rv
;
186 else if (*countRead
== 0)
187 self
->mInputCondition
= NS_BASE_STREAM_CLOSED
;
189 return self
->mInputCondition
;
194 nsCOMPtr
<nsIAsyncInputStream
> mInput
;
195 nsCOMPtr
<nsIAsyncOutputStream
> mOutput
;
196 nsresult mInputCondition
;
199 NS_IMPL_THREADSAFE_ISUPPORTS2(MyCopier
,
200 nsIInputStreamCallback
,
201 nsIOutputStreamCallback
)
203 ////////////////////////////////////////////////////////////////////////////////
206 * asynchronously copy file.
209 RunTest(nsIFile
*srcFile
, nsIFile
*destFile
)
215 nsCOMPtr
<nsIStreamTransportService
> sts
=
216 do_GetService(kStreamTransportServiceCID
, &rv
);
217 if (NS_FAILED(rv
)) return rv
;
219 nsCOMPtr
<nsIInputStream
> srcStr
;
220 rv
= NS_NewLocalFileInputStream(getter_AddRefs(srcStr
), srcFile
);
221 if (NS_FAILED(rv
)) return rv
;
223 nsCOMPtr
<nsIOutputStream
> destStr
;
224 rv
= NS_NewLocalFileOutputStream(getter_AddRefs(destStr
), destFile
);
225 if (NS_FAILED(rv
)) return rv
;
227 nsCOMPtr
<nsITransport
> srcTransport
;
228 rv
= sts
->CreateInputTransport(srcStr
, nsInt64(-1), nsInt64(-1), PR_TRUE
,
229 getter_AddRefs(srcTransport
));
230 if (NS_FAILED(rv
)) return rv
;
232 nsCOMPtr
<nsITransport
> destTransport
;
233 rv
= sts
->CreateOutputTransport(destStr
, nsInt64(-1), nsInt64(-1), PR_TRUE
,
234 getter_AddRefs(destTransport
));
235 if (NS_FAILED(rv
)) return rv
;
237 MyCopier
*copier
= new MyCopier();
238 if (copier
== nsnull
)
239 return NS_ERROR_OUT_OF_MEMORY
;
242 rv
= copier
->AsyncCopy(srcTransport
, destTransport
);
243 if (NS_FAILED(rv
)) return rv
;
251 ////////////////////////////////////////////////////////////////////////////////
254 RunBlockingTest(nsIFile
*srcFile
, nsIFile
*destFile
)
258 LOG(("RunBlockingTest\n"));
260 nsCOMPtr
<nsIStreamTransportService
> sts
=
261 do_GetService(kStreamTransportServiceCID
, &rv
);
262 if (NS_FAILED(rv
)) return rv
;
264 nsCOMPtr
<nsIInputStream
> srcIn
;
265 rv
= NS_NewLocalFileInputStream(getter_AddRefs(srcIn
), srcFile
);
266 if (NS_FAILED(rv
)) return rv
;
268 nsCOMPtr
<nsIOutputStream
> fileOut
;
269 rv
= NS_NewLocalFileOutputStream(getter_AddRefs(fileOut
), destFile
);
270 if (NS_FAILED(rv
)) return rv
;
272 nsCOMPtr
<nsITransport
> destTransport
;
273 rv
= sts
->CreateOutputTransport(fileOut
, nsInt64(-1), nsInt64(-1),
274 PR_TRUE
, getter_AddRefs(destTransport
));
275 if (NS_FAILED(rv
)) return rv
;
277 nsCOMPtr
<nsIOutputStream
> destOut
;
278 rv
= destTransport
->OpenOutputStream(nsITransport::OPEN_BLOCKING
, 100, 10, getter_AddRefs(destOut
));
279 if (NS_FAILED(rv
)) return rv
;
284 rv
= srcIn
->Read(buf
, sizeof(buf
), &n
);
285 if (NS_FAILED(rv
) || (n
== 0)) return rv
;
287 rv
= destOut
->Write(buf
, n
, &n
);
288 if (NS_FAILED(rv
)) return rv
;
294 ////////////////////////////////////////////////////////////////////////////////
297 main(int argc
, char* argv
[])
299 if (test_common_init(&argc
, &argv
) != 0)
305 printf("usage: %s <file-to-read>\n", argv
[0]);
308 char* fileName
= argv
[1];
310 nsCOMPtr
<nsIServiceManager
> servMan
;
311 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
312 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
313 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
315 registrar
->AutoRegister(nsnull
);
317 #if defined(PR_LOGGING)
318 gTestLog
= PR_NewLogModule("Test");
321 nsCOMPtr
<nsILocalFile
> srcFile
;
322 rv
= NS_NewNativeLocalFile(nsDependentCString(fileName
), PR_FALSE
, getter_AddRefs(srcFile
));
323 if (NS_FAILED(rv
)) return rv
;
325 nsCOMPtr
<nsIFile
> destFile
;
326 rv
= srcFile
->Clone(getter_AddRefs(destFile
));
327 if (NS_FAILED(rv
)) return rv
;
329 nsCAutoString leafName
;
330 rv
= destFile
->GetNativeLeafName(leafName
);
331 if (NS_FAILED(rv
)) return rv
;
333 nsCAutoString
newName(leafName
);
334 newName
.Append(NS_LITERAL_CSTRING(".1"));
335 rv
= destFile
->SetNativeLeafName(newName
);
336 if (NS_FAILED(rv
)) return rv
;
338 rv
= RunTest(srcFile
, destFile
);
339 NS_ASSERTION(NS_SUCCEEDED(rv
), "RunTest failed");
342 newName
.Append(NS_LITERAL_CSTRING(".2"));
343 rv
= destFile
->SetNativeLeafName(newName
);
344 if (NS_FAILED(rv
)) return rv
;
346 rv
= RunBlockingTest(srcFile
, destFile
);
347 NS_ASSERTION(NS_SUCCEEDED(rv
), "RunBlockingTest failed");
349 // give background threads a chance to finish whatever work they may
351 PR_Sleep(PR_SecondsToInterval(1));
352 } // this scopes the nsCOMPtrs
353 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
354 rv
= NS_ShutdownXPCOM(nsnull
);
355 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");