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/. */
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2004-10-22 00:57:17 +04:00
|
|
|
#include "TestCommon.h"
|
2002-01-30 00:22:13 +03:00
|
|
|
#include "nsIComponentRegistrar.h"
|
2005-04-15 03:18:34 +04:00
|
|
|
#include "nsPISocketTransportService.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsISocketTransport.h"
|
|
|
|
#include "nsIAsyncInputStream.h"
|
|
|
|
#include "nsIAsyncOutputStream.h"
|
|
|
|
#include "nsIProgressEventSink.h"
|
2000-03-06 00:26:01 +03:00
|
|
|
#include "nsIInterfaceRequestor.h"
|
2001-09-06 01:28:38 +04:00
|
|
|
#include "nsIInterfaceRequestorUtils.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsIRequest.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsMemory.h"
|
2005-11-08 22:23:00 +03:00
|
|
|
#include "nsStringAPI.h"
|
2005-08-30 04:30:38 +04:00
|
|
|
#include "nsIDNSService.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsIFileStreams.h"
|
|
|
|
#include "nsIStreamListener.h"
|
2012-06-06 06:08:30 +04:00
|
|
|
#include "nsIFile.h"
|
2001-01-24 01:10:34 +03:00
|
|
|
#include "nsNetUtil.h"
|
2003-01-18 05:15:14 +03:00
|
|
|
#include "nsAutoLock.h"
|
|
|
|
#include "prlog.h"
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
#if defined(PR_LOGGING)
|
1999-05-31 09:18:19 +04:00
|
|
|
//
|
2003-01-18 05:15:14 +03:00
|
|
|
// set NSPR_LOG_MODULES=Test:5
|
1999-05-31 09:18:19 +04:00
|
|
|
//
|
2012-07-30 18:20:58 +04:00
|
|
|
static PRLogModuleInfo *gTestLog = nullptr;
|
2003-01-18 05:15:14 +03:00
|
|
|
#endif
|
|
|
|
#define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
|
1999-10-06 12:26:01 +04:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
static NS_DEFINE_CID(kSocketTransportServiceCID, NS_SOCKETTRANSPORTSERVICE_CID);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-10-06 05:46:31 +04:00
|
|
|
class MyHandler : public nsIOutputStreamCallback
|
|
|
|
, public nsIInputStreamCallback
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
2003-01-18 05:15:14 +03:00
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
MyHandler(const char *path,
|
|
|
|
nsIAsyncInputStream *in,
|
|
|
|
nsIAsyncOutputStream *out)
|
|
|
|
: mInput(in)
|
|
|
|
, mOutput(out)
|
|
|
|
, mWriteOffset(0)
|
|
|
|
{
|
2005-11-08 22:23:00 +03:00
|
|
|
mBuf.Assign(NS_LITERAL_CSTRING("GET "));
|
|
|
|
mBuf.Append(path);
|
|
|
|
mBuf.Append(NS_LITERAL_CSTRING(" HTTP/1.0\r\n\r\n"));
|
2003-01-18 05:15:14 +03:00
|
|
|
}
|
|
|
|
virtual ~MyHandler() {}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
// called on any thread
|
|
|
|
NS_IMETHOD OnOutputStreamReady(nsIAsyncOutputStream *out)
|
|
|
|
{
|
|
|
|
LOG(("OnOutputStreamReady\n"));
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t n, count = mBuf.Length() - mWriteOffset;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
rv = out->Write(mBuf.get() + mWriteOffset, count, &n);
|
1999-07-03 08:14:28 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
LOG((" write returned [rv=%x count=%u]\n", rv, n));
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
if (NS_FAILED(rv) || (n == 0)) {
|
|
|
|
if (rv != NS_BASE_STREAM_WOULD_BLOCK) {
|
|
|
|
LOG((" done writing; starting to read\n"));
|
2012-07-30 18:20:58 +04:00
|
|
|
mInput->AsyncWait(this, 0, 0, nullptr);
|
2003-01-18 05:15:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
mWriteOffset += n;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return out->AsyncWait(this, 0, 0, nullptr);
|
2000-03-06 00:26:01 +03:00
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
// called on any thread
|
|
|
|
NS_IMETHOD OnInputStreamReady(nsIAsyncInputStream *in)
|
|
|
|
{
|
|
|
|
LOG(("OnInputStreamReady\n"));
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult rv;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t n;
|
2003-01-18 05:15:14 +03:00
|
|
|
char buf[500];
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
rv = in->Read(buf, sizeof(buf), &n);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
LOG((" read returned [rv=%x count=%u]\n", rv, n));
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
if (NS_FAILED(rv) || (n == 0)) {
|
|
|
|
if (rv != NS_BASE_STREAM_WOULD_BLOCK) {
|
2006-05-10 21:30:15 +04:00
|
|
|
QuitPumpingEvents();
|
2003-01-18 05:15:14 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-07-12 10:22:39 +04:00
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
return in->AsyncWait(this, 0, 0, nullptr);
|
1999-06-03 09:45:23 +04:00
|
|
|
}
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> mInput;
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> mOutput;
|
|
|
|
nsCString mBuf;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mWriteOffset;
|
2003-01-18 05:15:14 +03:00
|
|
|
};
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_IMPL_THREADSAFE_ISUPPORTS2(MyHandler,
|
2003-10-06 05:46:31 +04:00
|
|
|
nsIOutputStreamCallback,
|
|
|
|
nsIInputStreamCallback)
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
/**
|
|
|
|
* create transport, open streams, and close
|
|
|
|
*/
|
|
|
|
static nsresult
|
|
|
|
RunCloseTest(nsISocketTransportService *sts,
|
|
|
|
const char *host, int port,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t inFlags, uint32_t outFlags)
|
1999-05-31 09:18:19 +04:00
|
|
|
{
|
2003-01-18 05:15:14 +03:00
|
|
|
nsresult rv;
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
LOG(("RunCloseTest\n"));
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsISocketTransport> transport;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = sts->CreateTransport(nullptr, 0,
|
|
|
|
nsDependentCString(host), port, nullptr,
|
2003-01-18 05:15:14 +03:00
|
|
|
getter_AddRefs(transport));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsIInputStream> in;
|
|
|
|
rv = transport->OpenInputStream(inFlags, 0, 0, getter_AddRefs(in));
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncIn = do_QueryInterface(in, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsIOutputStream> out;
|
|
|
|
rv = transport->OpenOutputStream(outFlags, 0, 0, getter_AddRefs(out));
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> asyncOut = do_QueryInterface(out, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
LOG(("waiting 1 second before closing transport and streams...\n"));
|
|
|
|
PR_Sleep(PR_SecondsToInterval(1));
|
|
|
|
|
|
|
|
// let nsCOMPtr destructors close everything...
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-06-03 09:45:23 +04:00
|
|
|
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
/**
|
|
|
|
* asynchronously read socket stream
|
|
|
|
*/
|
|
|
|
static nsresult
|
|
|
|
RunTest(nsISocketTransportService *sts,
|
|
|
|
const char *host, int port, const char *path,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t inFlags, uint32_t outFlags)
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsresult rv;
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
LOG(("RunTest\n"));
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsISocketTransport> transport;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = sts->CreateTransport(nullptr, 0,
|
|
|
|
nsDependentCString(host), port, nullptr,
|
2003-01-18 05:15:14 +03:00
|
|
|
getter_AddRefs(transport));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsIInputStream> in;
|
|
|
|
rv = transport->OpenInputStream(inFlags, 0, 0, getter_AddRefs(in));
|
|
|
|
nsCOMPtr<nsIAsyncInputStream> asyncIn = do_QueryInterface(in, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
nsCOMPtr<nsIOutputStream> out;
|
|
|
|
rv = transport->OpenOutputStream(outFlags, 0, 0, getter_AddRefs(out));
|
|
|
|
nsCOMPtr<nsIAsyncOutputStream> asyncOut = do_QueryInterface(out, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
MyHandler *handler = new MyHandler(path, asyncIn, asyncOut);
|
2012-07-30 18:20:58 +04:00
|
|
|
if (handler == nullptr)
|
2003-01-18 05:15:14 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(handler);
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = asyncOut->AsyncWait(handler, 0, 0, nullptr);
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
PumpEvents();
|
1999-06-03 09:45:23 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
NS_RELEASE(handler);
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-06-03 09:45:23 +04:00
|
|
|
}
|
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
1999-06-03 09:45:23 +04:00
|
|
|
|
1999-05-31 09:18:19 +04:00
|
|
|
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 05:15:14 +03:00
|
|
|
nsresult rv;
|
1999-08-14 02:18:34 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
if (argc < 4) {
|
|
|
|
printf("usage: TestSocketTransport <host> <port> <path>\n");
|
|
|
|
return -1;
|
2002-07-04 18:29:25 +04:00
|
|
|
}
|
1999-08-14 02:18:34 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIServiceManager> servMan;
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
|
2003-01-18 05:15:14 +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);
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2003-01-18 05:15:14 +03:00
|
|
|
#if defined(PR_LOGGING)
|
|
|
|
gTestLog = PR_NewLogModule("Test");
|
|
|
|
#endif
|
1999-05-31 09:18:19 +04:00
|
|
|
|
2005-08-30 04:30:38 +04:00
|
|
|
// Make sure the DNS service is initialized on the main thread
|
|
|
|
nsCOMPtr<nsIDNSService> dns =
|
|
|
|
do_GetService(NS_DNSSERVICE_CONTRACTID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2005-04-15 03:18:34 +04:00
|
|
|
nsCOMPtr<nsPISocketTransportService> sts =
|
2003-01-18 05:15:14 +03:00
|
|
|
do_GetService(kSocketTransportServiceCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
LOG(("phase 1 tests...\n"));
|
|
|
|
|
|
|
|
LOG(("flags = { OPEN_UNBUFFERED, OPEN_UNBUFFERED }\n"));
|
|
|
|
rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
|
|
|
|
nsITransport::OPEN_UNBUFFERED,
|
|
|
|
nsITransport::OPEN_UNBUFFERED);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
|
|
|
|
|
|
|
|
LOG(("flags = { OPEN_BUFFERED, OPEN_UNBUFFERED }\n"));
|
|
|
|
rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
|
|
|
|
0 /* nsITransport::OPEN_BUFFERED */,
|
|
|
|
nsITransport::OPEN_UNBUFFERED);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
|
|
|
|
|
|
|
|
LOG(("flags = { OPEN_UNBUFFERED, OPEN_BUFFERED }\n"));
|
|
|
|
rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
|
|
|
|
nsITransport::OPEN_UNBUFFERED,
|
|
|
|
0 /*nsITransport::OPEN_BUFFERED */);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
|
|
|
|
|
|
|
|
LOG(("flags = { OPEN_BUFFERED, OPEN_BUFFERED }\n"));
|
|
|
|
rv = RunCloseTest(sts, argv[1], atoi(argv[2]),
|
|
|
|
0 /*nsITransport::OPEN_BUFFERED */,
|
|
|
|
0 /*nsITransport::OPEN_BUFFERED */);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunCloseTest failed");
|
|
|
|
|
|
|
|
LOG(("calling Shutdown on socket transport service:\n"));
|
|
|
|
sts->Shutdown();
|
|
|
|
|
|
|
|
LOG(("calling Init on socket transport service:\n"));
|
|
|
|
sts->Init();
|
|
|
|
|
|
|
|
LOG(("phase 2 tests...\n"));
|
|
|
|
|
|
|
|
LOG(("flags = { OPEN_UNBUFFERED, OPEN_UNBUFFERED }\n"));
|
|
|
|
rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
|
|
|
|
nsITransport::OPEN_UNBUFFERED,
|
|
|
|
nsITransport::OPEN_UNBUFFERED);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
|
|
|
|
|
|
|
|
LOG(("flags = { OPEN_BUFFERED, OPEN_UNBUFFERED }\n"));
|
|
|
|
rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
|
|
|
|
0 /* nsITransport::OPEN_BUFFERED */,
|
|
|
|
nsITransport::OPEN_UNBUFFERED);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
|
|
|
|
|
|
|
|
LOG(("flags = { OPEN_UNBUFFERED, OPEN_BUFFERED }\n"));
|
|
|
|
rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
|
|
|
|
nsITransport::OPEN_UNBUFFERED,
|
|
|
|
0 /*nsITransport::OPEN_BUFFERED */);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
|
|
|
|
|
|
|
|
LOG(("flags = { OPEN_BUFFERED, OPEN_BUFFERED }\n"));
|
|
|
|
rv = RunTest(sts, argv[1], atoi(argv[2]), argv[3],
|
|
|
|
0 /*nsITransport::OPEN_BUFFERED */,
|
|
|
|
0 /*nsITransport::OPEN_BUFFERED */);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "RunTest failed");
|
|
|
|
|
|
|
|
LOG(("waiting 1 second before calling Shutdown...\n"));
|
|
|
|
PR_Sleep(PR_SecondsToInterval(1));
|
|
|
|
|
|
|
|
LOG(("calling Shutdown on socket transport service:\n"));
|
|
|
|
sts->Shutdown();
|
|
|
|
|
|
|
|
// give background threads a chance to finish whatever work they may
|
|
|
|
// be doing.
|
|
|
|
LOG(("waiting 1 second before exiting...\n"));
|
|
|
|
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 05:15:14 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
|
2005-08-30 04:30:38 +04:00
|
|
|
return 0;
|
1999-05-31 09:18:19 +04:00
|
|
|
}
|