author Marco Bonardo <>
Fri, 15 Jan 2010 17:40:47 +0100
changeset 37235 7581e042fabd2c589544fe074cb7f1004a76cd6f
parent 1 9b2a99adc05e53cd4010de512f50118594756650
child 72496 fc776fa4afb78d940418997eab4a958c6a5a4d48
permissions -rw-r--r--
Bug 520165 - Part13: New expiration tests, r=mano

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * The Original Code is code.
 * The Initial Developer of the Original Code is
 * Netscape Communications Corporation.
 * Portions created by the Initial Developer are Copyright (C) 2001
 * the Initial Developer. All Rights Reserved.
 * Contributor(s):
 *   Darin Fisher <> (original author)
 * Alternatively, the contents of this file may be used under the terms of
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 * in which case the provisions of the GPL or the LGPL are applicable instead
 * of those above. If you wish to allow use of your version of this file only
 * under the terms of either the GPL or the LGPL, and not to allow others to
 * use your version of this file under the terms of the MPL, indicate your
 * decision by deleting the provisions above and replace them with the notice
 * and other provisions required by the GPL or the LGPL. If you do not delete
 * the provisions above, a recipient may use your version of this file under
 * the terms of any one of the MPL, the GPL or the LGPL.
 * ***** END LICENSE BLOCK ***** */

#include <stdio.h>

#include "nsIServiceManager.h"
#include "nsIEventQueueService.h"
#include "nsIOutputStream.h"
#include "nsIStreamListener.h"
#include "nsITransport.h"
#include "nsIInputStream.h"
#include "nsIOutputStream.h"
#include "nsCOMPtr.h"
#include "plstr.h"
#include "prprf.h"

#include "nsICacheService.h"
#include "nsICacheSession.h"
#include "nsICacheEntryDescriptor.h"
#include "nsNetCID.h"
static nsICacheSession *session = nsnull;
static nsICacheEntryDescriptor *desc = nsnull;

 * This test program exercises the memory cache's nsITransport implementation.
 * This test program loads a file into the memory cache (using OpenOutputStream),
 * and then reads the file back out (using AsyncRead).  The data read from the
 * memory cache is written to a new file (with .out added as a suffix to the file
 * name).

static nsIEventQueue *gEventQ = nsnull;

class TestListener : public nsIStreamListener

    TestListener(char *);
    virtual ~TestListener();

    char *mFilename;
    FILE *mFile;


TestListener::TestListener(char *filename)
    : mFilename(filename)
    , mFile(nsnull)


TestListener::OnStartRequest(nsIRequest *req, nsISupports *ctx)

    mFile = fopen(mFilename, "w");
    if (!mFile)
        return NS_ERROR_FAILURE;

    return NS_OK;

TestListener::OnStopRequest(nsIRequest *req, nsISupports *ctx, nsresult status)
    printf("OnStopRequest: status=%x\n", status);

    if (mFile)

    return NS_OK;

TestListener::OnDataAvailable(nsIRequest *req, nsISupports *ctx,
                              nsIInputStream *is,
                              PRUint32 offset, PRUint32 count)
    printf("OnDataAvailable: offset=%u count=%u\n", offset, count);

    if (!mFile) return NS_ERROR_FAILURE;

    char buf[128];
    nsresult rv;
    PRUint32 nread = 0;

    while (count) {
        PRUint32 amount = PR_MIN(count, sizeof(buf));

        rv = is->Read(buf, amount, &nread);
        if (NS_FAILED(rv)) return rv;

        fwrite(buf, nread, 1, mFile);
        count -= nread;
    return NS_OK;

nsresult TestMCTransport(const char *filename)
    nsresult rv = NS_OK;
    nsCOMPtr<nsITransport> transport;

    transport = do_CreateInstance(
            ";1", &rv);
    if (NS_FAILED(rv))
        return rv;
    nsCOMPtr<nsICacheService> serv(do_GetService(kCacheServiceCID, &rv));
    if (NS_FAILED(rv)) return rv;

    rv = serv->CreateSession("TestMCTransport",
                             nsICache::STORE_IN_MEMORY, PR_TRUE,
    if (NS_FAILED(rv)) return rv;

    rv = session->OpenCacheEntry(nsDependentCString(filename),
    if (NS_FAILED(rv)) return rv;

    rv = desc->MarkValid();
    if (NS_FAILED(rv)) return rv;

    rv = desc->GetTransport(getter_AddRefs(transport));
    if (NS_FAILED(rv)) return rv;

    nsCOMPtr<nsIOutputStream> os;
    rv = transport->OpenOutputStream(0, (PRUint32) -1, 0, getter_AddRefs(os));
    if (NS_FAILED(rv)) return rv;

    char *out = PR_smprintf("%s.out", filename);
    nsCOMPtr<nsIStreamListener> listener = new TestListener(out);
    if (!listener)
        return NS_ERROR_OUT_OF_MEMORY;

    nsCOMPtr<nsIRequest> req;
    rv = transport->AsyncRead(listener, nsnull, 0, (PRUint32) -1, 0, getter_AddRefs(req));
    if (NS_FAILED(rv)) return rv;

    FILE *file = fopen(filename, "r");
    if (!file)
        return NS_ERROR_FILE_NOT_FOUND;

    char buf[256];
    PRUint32 count, total=0;

    while ((count = fread(buf, 1, sizeof(buf), file)) > 0) {
        printf("writing %u bytes\n", count);
        total += count;
        rv = os->Write(buf, count, &count);
        if (NS_FAILED(rv)) return rv;

        // process an event
        PLEvent *event = nsnull;
        if (event) gEventQ->HandleEvent(event);

    printf("wrote %u bytes\n", total);

    return rv;

int main(int argc, char **argv)
    nsresult rv;

    if (argc < 2) {
        printf("usage: %s filename\n", argv[0]);
        return -1;

    nsCOMPtr<nsIEventQueueService> eqs = 
             do_GetService(kEventQueueServiceCID, &rv);
    if (NS_FAILED(rv)) {
        printf("failed to create event queue service: rv=%x\n", rv);
        return -1;

    rv = eqs->CreateMonitoredThreadEventQueue();
    if (NS_FAILED(rv)) {
        printf("failed to create monitored event queue: rv=%x\n", rv);
        return -1;

    rv = eqs->GetThreadEventQueue(NS_CURRENT_THREAD, &gEventQ);
    if (NS_FAILED(rv)) {
        printf("failed to get thread event queue: %x\n", rv);
        return -1;

    rv = TestMCTransport(argv[1]);
    printf("TestMCTransport returned %x\n", rv);


    return 0;