~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/netwerk/test/TestMCTransport.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Netscape Public License
 
6
 * Version 1.1 (the "License"); you may not use this file except in
 
7
 * compliance with the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/NPL/
 
9
 *
 
10
 * Software distributed under the License is distributed on an "AS IS" basis,
 
11
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 
12
 * for the specific language governing rights and limitations under the
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
17
 * The Initial Developer of the Original Code is 
 
18
 * Netscape Communications Corporation.
 
19
 * Portions created by the Initial Developer are Copyright (C) 2001
 
20
 * the Initial Developer. All Rights Reserved.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   Darin Fisher <darin@netscape.com> (original author)
 
24
 *
 
25
 * Alternatively, the contents of this file may be used under the terms of
 
26
 * either the GNU General Public License Version 2 or later (the "GPL"), or 
 
27
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
28
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
29
 * of those above. If you wish to allow use of your version of this file only
 
30
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
31
 * use your version of this file under the terms of the NPL, indicate your
 
32
 * decision by deleting the provisions above and replace them with the notice
 
33
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
34
 * the provisions above, a recipient may use your version of this file under
 
35
 * the terms of any one of the NPL, the GPL or the LGPL.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include <stdio.h>
 
40
 
 
41
#include "nsIServiceManager.h"
 
42
#include "nsIEventQueueService.h"
 
43
#include "nsIOutputStream.h"
 
44
#include "nsIStreamListener.h"
 
45
#include "nsITransport.h"
 
46
#include "nsIInputStream.h"
 
47
#include "nsIOutputStream.h"
 
48
#include "nsCOMPtr.h"
 
49
#include "plstr.h"
 
50
#include "prprf.h"
 
51
 
 
52
#ifndef USE_CREATE_INSTANCE
 
53
#include "nsICacheService.h"
 
54
#include "nsICacheSession.h"
 
55
#include "nsICacheEntryDescriptor.h"
 
56
#include "nsNetCID.h"
 
57
static NS_DEFINE_CID(kCacheServiceCID, NS_CACHESERVICE_CID);
 
58
static nsICacheSession *session = nsnull;
 
59
static nsICacheEntryDescriptor *desc = nsnull;
 
60
#endif
 
61
 
 
62
/**
 
63
 * This test program exercises the memory cache's nsITransport implementation.
 
64
 *
 
65
 * This test program loads a file into the memory cache (using OpenOutputStream),
 
66
 * and then reads the file back out (using AsyncRead).  The data read from the
 
67
 * memory cache is written to a new file (with .out added as a suffix to the file
 
68
 * name).
 
69
 */
 
70
 
 
71
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
 
72
static nsIEventQueue *gEventQ = nsnull;
 
73
 
 
74
class TestListener : public nsIStreamListener
 
75
{
 
76
public:
 
77
    NS_DECL_ISUPPORTS
 
78
    NS_DECL_NSISTREAMLISTENER
 
79
    NS_DECL_NSIREQUESTOBSERVER
 
80
 
 
81
    TestListener(char *);
 
82
    virtual ~TestListener();
 
83
 
 
84
private:
 
85
    char *mFilename;
 
86
    FILE *mFile;
 
87
};
 
88
 
 
89
NS_IMPL_ISUPPORTS2(TestListener,
 
90
                   nsIStreamListener,
 
91
                   nsIRequestObserver)
 
92
 
 
93
TestListener::TestListener(char *filename)
 
94
    : mFilename(filename)
 
95
    , mFile(nsnull)
 
96
{
 
97
}
 
98
 
 
99
TestListener::~TestListener()
 
100
{
 
101
}
 
102
 
 
103
NS_IMETHODIMP
 
104
TestListener::OnStartRequest(nsIRequest *req, nsISupports *ctx)
 
105
{
 
106
    printf("OnStartRequest\n");
 
107
 
 
108
    mFile = fopen(mFilename, "w");
 
109
    if (!mFile)
 
110
        return NS_ERROR_FAILURE;
 
111
 
 
112
    return NS_OK;
 
113
}
 
114
 
 
115
NS_IMETHODIMP
 
116
TestListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
 
117
{
 
118
    printf("OnStopRequest: status=%x\n", status);
 
119
 
 
120
    if (mFile)
 
121
        fclose(mFile);
 
122
 
 
123
    return NS_OK;
 
124
}
 
125
 
 
126
NS_IMETHODIMP
 
127
TestListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx,
 
128
                              nsIInputStream *is,
 
129
                              PRUint32 offset, PRUint32 count)
 
130
{
 
131
    printf("OnDataAvailable: offset=%u count=%u\n", offset, count);
 
132
 
 
133
    if (!mFile) return NS_ERROR_FAILURE;
 
134
 
 
135
    char buf[128];
 
136
    nsresult rv;
 
137
    PRUint32 nread = 0;
 
138
 
 
139
    while (count) {
 
140
        PRUint32 amount = PR_MIN(count, sizeof(buf));
 
141
 
 
142
        rv = is->Read(buf, amount, &nread);
 
143
        if (NS_FAILED(rv)) return rv;
 
144
 
 
145
        fwrite(buf, nread, 1, mFile);
 
146
        count -= nread;
 
147
    }
 
148
    return NS_OK;
 
149
}
 
150
 
 
151
nsresult TestMCTransport(const char *filename)
 
152
{
 
153
    nsresult rv = NS_OK;
 
154
    nsCOMPtr<nsITransport> transport;
 
155
 
 
156
#ifdef USE_CREATE_INSTANCE
 
157
    rv = nsComponentManager::CreateInstance(
 
158
                    "@mozilla.org/network/memory-cache-transport;1",
 
159
                    nsnull,
 
160
                    NS_GET_IID(nsITransport),
 
161
                    getter_AddRefs(transport));
 
162
    if (NS_FAILED(rv))
 
163
        return rv;
 
164
#else
 
165
    nsCOMPtr<nsICacheService> serv(do_GetService(kCacheServiceCID, &rv));
 
166
    if (NS_FAILED(rv)) return rv;
 
167
 
 
168
    rv = serv->CreateSession("TestMCTransport",
 
169
                             nsICache::STORE_IN_MEMORY, PR_TRUE,
 
170
                             &session);
 
171
    if (NS_FAILED(rv)) return rv;
 
172
 
 
173
    rv = session->OpenCacheEntry(filename,
 
174
                                 nsICache::ACCESS_READ_WRITE,
 
175
                                 nsICache::BLOCKING,
 
176
                                 &desc);
 
177
    if (NS_FAILED(rv)) return rv;
 
178
 
 
179
    rv = desc->MarkValid();
 
180
    if (NS_FAILED(rv)) return rv;
 
181
 
 
182
    rv = desc->GetTransport(getter_AddRefs(transport));
 
183
    if (NS_FAILED(rv)) return rv;
 
184
#endif
 
185
 
 
186
    nsCOMPtr<nsIOutputStream> os;
 
187
    rv = transport->OpenOutputStream(0, (PRUint32) -1, 0, getter_AddRefs(os));
 
188
    if (NS_FAILED(rv)) return rv;
 
189
 
 
190
    char *out = PR_smprintf("%s.out", filename);
 
191
    nsCOMPtr<nsIStreamListener> listener = new TestListener(out);
 
192
    if (!listener)
 
193
        return NS_ERROR_OUT_OF_MEMORY;
 
194
 
 
195
    nsCOMPtr<nsIRequest> req;
 
196
    rv = transport->AsyncRead(listener, nsnull, 0, (PRUint32) -1, 0, getter_AddRefs(req));
 
197
    if (NS_FAILED(rv)) return rv;
 
198
 
 
199
    FILE *file = fopen(filename, "r");
 
200
    if (!file)
 
201
        return NS_ERROR_FILE_NOT_FOUND;
 
202
 
 
203
    char buf[256];
 
204
    PRUint32 count, total=0;
 
205
 
 
206
    while ((count = fread(buf, 1, sizeof(buf), file)) > 0) {
 
207
        printf("writing %u bytes\n", count);
 
208
        total += count;
 
209
        rv = os->Write(buf, count, &count);
 
210
        if (NS_FAILED(rv)) return rv;
 
211
 
 
212
        // process an event
 
213
        PLEvent *event = nsnull;
 
214
        gEventQ->GetEvent(&event);
 
215
        if (event) gEventQ->HandleEvent(event);
 
216
    }
 
217
 
 
218
    printf("wrote %u bytes\n", total);
 
219
 
 
220
    return rv;
 
221
}
 
222
 
 
223
int main(int argc, char **argv)
 
224
{
 
225
    nsresult rv;
 
226
 
 
227
    if (argc < 2) {
 
228
        printf("usage: %s filename\n", argv[0]);
 
229
        return -1;
 
230
    }
 
231
 
 
232
    nsCOMPtr<nsIEventQueueService> eqs = 
 
233
             do_GetService(kEventQueueServiceCID, &rv);
 
234
    if (NS_FAILED(rv)) {
 
235
        printf("failed to create event queue service: rv=%x\n", rv);
 
236
        return -1;
 
237
    }
 
238
 
 
239
    rv = eqs->CreateMonitoredThreadEventQueue();
 
240
    if (NS_FAILED(rv)) {
 
241
        printf("failed to create monitored event queue: rv=%x\n", rv);
 
242
        return -1;
 
243
    }
 
244
 
 
245
    rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
 
246
    if (NS_FAILED(rv)) {
 
247
        printf("failed to get thread event queue: %x\n", rv);
 
248
        return -1;
 
249
    }
 
250
 
 
251
    rv = TestMCTransport(argv[1]);
 
252
    printf("TestMCTransport returned %x\n", rv);
 
253
 
 
254
    gEventQ->ProcessPendingEvents();
 
255
 
 
256
#ifndef USE_CREATE_INSTANCE
 
257
    NS_IF_RELEASE(desc);
 
258
    NS_IF_RELEASE(session);
 
259
#endif
 
260
    return 0;
 
261
}