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 "nsISocketTransportService.h"
41 #include "nsISocketTransport.h"
42 #include "nsIServiceManager.h"
43 #include "nsIComponentManager.h"
45 #include "nsStringAPI.h"
46 #include "nsILocalFile.h"
47 #include "nsNetUtil.h"
53 ////////////////////////////////////////////////////////////////////////////////
55 #if defined(PR_LOGGING)
57 // set NSPR_LOG_MODULES=Test:5
59 static PRLogModuleInfo
*gTestLog
= nsnull
;
61 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
63 ////////////////////////////////////////////////////////////////////////////////
65 static NS_DEFINE_CID(kSocketTransportServiceCID
, NS_SOCKETTRANSPORTSERVICE_CID
);
67 ////////////////////////////////////////////////////////////////////////////////
70 RunBlockingTest(const nsACString
&host
, PRInt32 port
, nsIFile
*file
)
74 LOG(("RunBlockingTest\n"));
76 nsCOMPtr
<nsISocketTransportService
> sts
=
77 do_GetService(kSocketTransportServiceCID
, &rv
);
78 if (NS_FAILED(rv
)) return rv
;
80 nsCOMPtr
<nsIInputStream
> input
;
81 rv
= NS_NewLocalFileInputStream(getter_AddRefs(input
), file
);
82 if (NS_FAILED(rv
)) return rv
;
84 nsCOMPtr
<nsISocketTransport
> trans
;
85 rv
= sts
->CreateTransport(nsnull
, 0, host
, port
, nsnull
, getter_AddRefs(trans
));
86 if (NS_FAILED(rv
)) return rv
;
88 nsCOMPtr
<nsIOutputStream
> output
;
89 rv
= trans
->OpenOutputStream(nsITransport::OPEN_BLOCKING
, 100, 10, getter_AddRefs(output
));
90 if (NS_FAILED(rv
)) return rv
;
95 rv
= input
->Read(buf
, sizeof(buf
), &nr
);
96 if (NS_FAILED(rv
) || (nr
== 0)) return rv
;
101 rv = output->Write(p, nr, &nw);
102 if (NS_FAILED(rv)) return rv;
109 rv
= output
->Write(buf
, nr
, &nw
);
110 if (NS_FAILED(rv
)) return rv
;
112 NS_ASSERTION(nr
== nw
, "not all written");
115 LOG((" done copying data.\n"));
119 ////////////////////////////////////////////////////////////////////////////////
122 main(int argc
, char* argv
[])
124 if (test_common_init(&argc
, &argv
) != 0)
130 printf("usage: %s <host> <port> <file-to-read>\n", argv
[0]);
133 char* hostName
= argv
[1];
134 PRInt32 port
= atoi(argv
[2]);
135 char* fileName
= argv
[3];
137 nsCOMPtr
<nsIServiceManager
> servMan
;
138 NS_InitXPCOM2(getter_AddRefs(servMan
), nsnull
, nsnull
);
139 nsCOMPtr
<nsIComponentRegistrar
> registrar
= do_QueryInterface(servMan
);
140 NS_ASSERTION(registrar
, "Null nsIComponentRegistrar");
142 registrar
->AutoRegister(nsnull
);
144 #if defined(PR_LOGGING)
145 gTestLog
= PR_NewLogModule("Test");
148 nsCOMPtr
<nsILocalFile
> file
;
149 rv
= NS_NewNativeLocalFile(nsDependentCString(fileName
), PR_FALSE
, getter_AddRefs(file
));
150 if (NS_FAILED(rv
)) return rv
;
152 rv
= RunBlockingTest(nsDependentCString(hostName
), port
, file
);
153 #if defined(PR_LOGGING)
155 LOG(("RunBlockingTest failed [rv=%x]\n", rv
));
158 // give background threads a chance to finish whatever work they may
160 LOG(("sleeping for 5 seconds...\n"));
161 PR_Sleep(PR_SecondsToInterval(5));
162 } // this scopes the nsCOMPtrs
163 // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
164 rv
= NS_ShutdownXPCOM(nsnull
);
165 NS_ASSERTION(NS_SUCCEEDED(rv
), "NS_ShutdownXPCOM failed");