diff --git a/netwerk/test/makefile.win b/netwerk/test/makefile.win index 4121c6974bc..a5ffee0ee52 100644 --- a/netwerk/test/makefile.win +++ b/netwerk/test/makefile.win @@ -23,7 +23,8 @@ MAKE_OBJ_TYPE = EXE PROG1 = .\$(OBJDIR)\TestFileInput.exe PROG2 = .\$(OBJDIR)\TestSocketInput.exe PROG3 = .\$(OBJDIR)\TestSocketIO.exe -PROGRAMS = $(PROG1) $(PROG2) $(PROG3) +PROG4 = .\$(OBJDIR)\nettest.exe +PROGRAMS = $(PROG1) $(PROG2) $(PROG3) $(PROG4) LCFLAGS=-DUSE_NSREG -GX @@ -38,6 +39,7 @@ LINCS = \ LLIBS= \ $(DIST)\lib\xpcom32.lib \ $(DIST)\lib\raptorbase.lib \ + $(DIST)\lib\netwerkbase_s.lib \ $(LIBNSPR) \ $(DIST)\lib\plc3.lib !if "$(MOZ_BITS)"=="32" && defined(MOZ_DEBUG) && defined(GLOWCODE) @@ -73,3 +75,5 @@ $(PROG1): $(OBJDIR) TestFileInput.cpp $(PROG2): $(OBJDIR) TestSocketInput.cpp $(PROG3): $(OBJDIR) TestSocketIO.cpp + +$(PROG4): $(OBJDIR) nettest.cpp diff --git a/netwerk/test/nettest.cpp b/netwerk/test/nettest.cpp new file mode 100644 index 00000000000..d4a1c80f582 --- /dev/null +++ b/netwerk/test/nettest.cpp @@ -0,0 +1,314 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- + * + * The contents of this file are subject to the Netscape Public License + * Version 1.0 (the "NPL"); you may not use this file except in + * compliance with the NPL. You may obtain a copy of the NPL at + * http://www.mozilla.org/NPL/ + * + * Software distributed under the NPL is distributed on an "AS IS" basis, + * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL + * for the specific language governing rights and limitations under the + * NPL. + * + * The Initial Developer of this code under the NPL is Netscape + * Communications Corporation. Portions created by Netscape are + * Copyright (C) 1998 Netscape Communications Corporation. All Rights + * Reserved. + */ + +#include +#include + +#ifdef XP_PC +#include +#endif + +#include "plstr.h" +#include "plevent.h" + +#include "nsIStreamListener.h" +#include "nsIInputStream.h" +#include "nsIUrl.h" +#include "nsINetService.h" +#include "nsIComponentManager.h" +#include "nsIServiceManager.h" +#include "nsIEventQueueService.h" +#include "nsXPComCIID.h" +#include "nsString2.h" + + +int urlLoaded; +PRBool bTraceEnabled; +PRBool bLoadAsync; + +//#include "nsIPostToServer.h" +#include "nsINetService.h" + +#ifdef XP_PC +#define NETLIB_DLL "netlib.dll" +#define XPCOM_DLL "xpcom32.dll" +#else +#ifdef XP_MAC +#include "nsMacRepository.h" +#else +#define NETLIB_DLL "libnetlib.so" +#define XPCOM_DLL "libxpcom.so" +#endif +#endif + +// Define CIDs... +static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID); +static NS_DEFINE_IID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID); + +// Define IIDs... +static NS_DEFINE_IID(kIEventQueueServiceIID, NS_IEVENTQUEUESERVICE_IID); +//NS_DEFINE_IID(kIPostToServerIID, NS_IPOSTTOSERVER_IID); + + +class TestConsumer : public nsIStreamListener +{ + +public: + NS_DECL_ISUPPORTS + + TestConsumer(); + + // stream listener methods + NS_IMETHOD OnDataAvailable(nsISupports* context, + nsIInputStream *aIStream, + PRUint32 aSourceOffset, + PRUint32 aLength); + + // stream observer methods + NS_IMETHOD OnStartBinding(nsISupports* context); + + NS_IMETHOD OnStopBinding(nsISupports* context, + nsresult aStatus, + nsIString* aMsg); + +protected: + ~TestConsumer(); + nsIUrl* mUrl; +}; + + +TestConsumer::TestConsumer() +{ + NS_INIT_REFCNT(); +} + + +NS_DEFINE_IID(kIStreamListenerIID, NS_ISTREAMLISTENER_IID); +NS_IMPL_ISUPPORTS(TestConsumer,kIStreamListenerIID); + + +TestConsumer::~TestConsumer() +{ + if (bTraceEnabled) { + printf("\n+++ TestConsumer is being deleted...\n"); + } +} + +NS_IMETHODIMP TestConsumer::OnDataAvailable( + nsISupports *context, + nsIInputStream* aIStream, + PRUint32 aSourceOffset, + PRUint32 aLength) { + PRUint32 len; + + if (bTraceEnabled) { + printf("\n+++ TestConsumer::OnDataAvailable: URL: %p, %d bytes available...\n", mUrl, aLength); + } + + do { + nsresult err; + char buffer[80]; + PRUint32 i; + + err = aIStream->Read(buffer, 80, &len); + if (err == NS_OK) { + for (i=0; i 0); + + return 0; +} + +NS_IMETHODIMP TestConsumer::OnStartBinding(nsISupports *context) { + if (bTraceEnabled) { + printf("\n+++ TestConsumer::OnStartBinding: URL: %p, Content type: %s\n", mUrl, "XXX some context"); + } + + return 0; +} + +NS_IMETHODIMP TestConsumer::OnStopBinding(nsISupports *context, nsresult aStatus, nsIString *aMsg) { + if (bTraceEnabled) { + printf("\n+++ TestConsumer::OnStopBinding... URL: %p status: %d\n", mUrl, aStatus); + } + + if (NS_FAILED(aStatus)) { + char* url; + mUrl->ToNewCString(&url); + + printf("Unable to load URL %s\n", url); + delete [] url; + } + + urlLoaded = 1; + return 0; +} + +nsresult ReadStreamSynchronously(nsIInputStream* aIn) +{ + nsresult rv; + char buffer[1024]; + + if (nsnull != aIn) { + PRUint32 len; + + do { + PRUint32 i; + + rv = aIn->Read(buffer, sizeof(buffer), &len); + for (i=0; i 0); + } + return NS_OK; +} + + + +int main(int argc, char **argv) +{ + nsAutoString2 url_address; + char buf[256]; + nsIStreamListener *pConsumer; + nsIEventQueueService* pEventQService; + nsIUrl *pURL; + nsresult result; + int i; + + if (argc < 2) { + printf("test: [-trace] [-sync] \n"); + return 0; + } + + nsComponentManager::RegisterComponent(kEventQueueServiceCID, NULL, NULL, XPCOM_DLL, PR_FALSE, PR_FALSE); + nsComponentManager::RegisterComponent(kNetServiceCID, NULL, NULL, NETLIB_DLL, PR_FALSE, PR_FALSE); + + // Create the Event Queue for this thread... + pEventQService = nsnull; + result = nsServiceManager::GetService(kEventQueueServiceCID, + kIEventQueueServiceIID, + (nsISupports **)&pEventQService); + if (NS_SUCCEEDED(result)) { + // XXX: What if this fails? + result = pEventQService->CreateThreadEventQueue(); + } + + bTraceEnabled = PR_FALSE; + bLoadAsync = PR_TRUE; + + for (i=1; i < argc; i++) { + // Turn on netlib tracing... + if (PL_strcasecmp(argv[i], "-trace") == 0) { + //NET_ToggleTrace(); + bTraceEnabled = PR_TRUE; + continue; + } + // Turn on synchronous URL loading... + if (PL_strcasecmp(argv[i], "-sync") == 0) { + bLoadAsync = PR_FALSE; + continue; + } + + urlLoaded = 0; + + url_address = argv[i]; + if (bTraceEnabled) { + url_address.ToCString(buf, 256); + printf("+++ loading URL: %s...\n", buf); + } + + pConsumer = new TestConsumer; + pConsumer->AddRef(); + + // Create the URL object... + pURL = NULL; +#if 1 + result = NS_NewURL(&pURL, argv[i], nsnull); + if (NS_OK != result) { + if (bTraceEnabled) { + printf("NS_NewURL() failed...\n"); + } + return 1; + } +#else + return 1; +#endif // 0 + +#if 0 + nsIPostToServer *pPoster; + result = pURL->QueryInterface(kIPostToServerIID, (void**)&pPoster); + if (result == NS_OK) { + pPoster->SendFile("foo.txt"); + } + NS_IF_RELEASE(pPoster); +#endif + + +#if 0 + // Start the URL load... + if (PR_TRUE == bLoadAsync) { + result = NS_OpenURL(pURL, pConsumer); + + /* If the open failed, then do not drop into the message loop... */ + if (NS_OK != result) { + char* url; + pURL->ToNewCString(&url); + + printf("Unable to load URL %s\n", url); + delete [] url; + urlLoaded = 1; + } + } + // Load the URL synchronously... + else { + nsIInputStream *in; + + result = NS_OpenURL(pURL, &in); + ReadStreamSynchronously(in); + NS_IF_RELEASE(in); + urlLoaded = 1; + } +#else + return 1; +#endif // 0 + + + // Enter the message pump to allow the URL load to proceed. + while ( !urlLoaded ) { +#ifdef XP_PC + MSG msg; + + if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { + TranslateMessage(&msg); + DispatchMessage(&msg); + } +#endif + } + + pURL->Release(); + } + + if (nsnull != pEventQService) { + pEventQService->DestroyThreadEventQueue(); + nsServiceManager::ReleaseService(kEventQueueServiceCID, pEventQService); + } + return 0; +}