2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-01-18 04:27:53 +03:00
|
|
|
|
2004-10-22 00:57:17 +04:00
|
|
|
#include "TestCommon.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsIComponentRegistrar.h"
|
|
|
|
#include "nsIStreamTransportService.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIProgressEventSink.h"
|
|
|
|
#include "nsIInterfaceRequestor.h"
|
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsISeekableStream.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsMemory.h"
|
2005-11-08 22:23:00 +03:00
|
|
|
#include "nsStringAPI.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIStreamListener.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2003-01-18 04:27:53 +03:00
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include "prlog.h"
|
2005-01-09 23:43:35 +03:00
|
|
|
#include "prprf.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
//
|
|
|
|
// set NSPR_LOG_MODULES=Test:5
|
|
|
|
//
|
2012-07-30 18:20:58 +04:00
|
|
|
static PRLogModuleInfo *gTestLog = nullptr;
|
2003-01-18 04:27:53 +03:00
|
|
|
#define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
|
|
|
|
#else
|
|
|
|
#define LOG(args)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class MyListener : public nsIStreamListener
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
MyListener() {}
|
|
|
|
virtual ~MyListener() {}
|
|
|
|
|
|
|
|
NS_IMETHOD OnStartRequest(nsIRequest *req, nsISupports *ctx)
|
|
|
|
{
|
|
|
|
LOG(("MyListener::OnStartRequest\n"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnDataAvailable(nsIRequest *req, nsISupports *ctx,
|
|
|
|
nsIInputStream *stream,
|
2012-09-06 06:41:02 +04:00
|
|
|
uint64_t offset, uint32_t count)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
2012-09-06 06:41:02 +04:00
|
|
|
LOG(("MyListener::OnDataAvailable [offset=%llu count=%u]\n", offset, count));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
char buf[500];
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
while (count) {
|
2013-01-15 16:22:03 +04:00
|
|
|
uint32_t n, amt = std::min<uint32_t>(count, sizeof(buf));
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
rv = stream->Read(buf, amt, &n);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
LOG((" read returned 0x%08x\n", rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
fwrite(buf, n, 1, stdout);
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
LOG((" read %u bytes\n", n));
|
|
|
|
count -= n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
|
|
|
|
{
|
|
|
|
LOG(("MyListener::OnStopRequest [status=%x]\n", status));
|
2006-05-10 21:30:15 +04:00
|
|
|
QuitPumpingEvents();
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(MyListener,
|
|
|
|
nsIRequestObserver,
|
|
|
|
nsIStreamListener)
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
/**
|
|
|
|
* asynchronously copy file.
|
|
|
|
*/
|
|
|
|
static nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
RunTest(nsIFile *file, int64_t offset, int64_t length)
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
LOG(("RunTest\n"));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
|
|
|
rv = NS_NewLocalFileInputStream(getter_AddRefs(stream), file);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStreamPump> pump;
|
|
|
|
rv = NS_NewInputStreamPump(getter_AddRefs(pump), stream, offset, length);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = pump->AsyncRead(new MyListener(), nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
PumpEvents();
|
2003-01-18 04:27:53 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char* argv[])
|
|
|
|
{
|
2004-10-22 00:57:17 +04:00
|
|
|
if (test_common_init(&argc, &argv) != 0)
|
|
|
|
return -1;
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (argc < 4) {
|
|
|
|
printf("usage: %s <file-to-read> <start-offset> <read-length>\n", argv[0]);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
char* fileName = argv[1];
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t offset, length;
|
2005-01-09 23:43:35 +03:00
|
|
|
int err = PR_sscanf(argv[2], "%lld", &offset);
|
|
|
|
if (err == -1) {
|
|
|
|
printf("Start offset must be an integer!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
err = PR_sscanf(argv[3], "%lld", &length);
|
|
|
|
if (err == -1) {
|
|
|
|
printf("Length must be an integer!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-01-18 04:27:53 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
|
|
|
|
NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
|
|
|
|
if (registrar)
|
2012-07-30 18:20:58 +04:00
|
|
|
registrar->AutoRegister(nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
|
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
gTestLog = PR_NewLogModule("Test");
|
|
|
|
#endif
|
|
|
|
|
2012-06-06 06:08:30 +04:00
|
|
|
nsCOMPtr<nsIFile> file;
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = NS_NewNativeLocalFile(nsDependentCString(fileName), false, getter_AddRefs(file));
|
2003-01-18 04:27:53 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = RunTest(file, offset, length);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
|
|
|
|
|
|
|
|
// give background threads a chance to finish whatever work they may
|
|
|
|
// be doing.
|
|
|
|
PR_Sleep(PR_SecondsToInterval(1));
|
|
|
|
} // this scopes the nsCOMPtrs
|
|
|
|
// no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = NS_ShutdownXPCOM(nullptr);
|
2003-01-18 04:27:53 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
|
|
|
return NS_OK;
|
|
|
|
}
|