netwerk/test/TestServ.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* vim:set ts=4 sw=4 et cindent: */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include "TestCommon.h"
     7 #include <stdlib.h>
     8 #include "nsIServiceManager.h"
     9 #include "nsIServerSocket.h"
    10 #include "nsISocketTransport.h"
    11 #include "nsNetUtil.h"
    12 #include "nsStringAPI.h"
    13 #include "nsCOMPtr.h"
    14 #include "prlog.h"
    16 #if defined(PR_LOGGING)
    17 //
    18 // set NSPR_LOG_MODULES=Test:5
    19 //
    20 static PRLogModuleInfo *gTestLog = nullptr;
    21 #endif
    22 #define LOG(args) PR_LOG(gTestLog, PR_LOG_DEBUG, args)
    24 class MySocketListener : public nsIServerSocketListener
    25 {
    26 public:
    27     NS_DECL_THREADSAFE_ISUPPORTS
    28     NS_DECL_NSISERVERSOCKETLISTENER
    30     MySocketListener() {}
    31     virtual ~MySocketListener() {}
    32 };
    34 NS_IMPL_ISUPPORTS(MySocketListener, nsIServerSocketListener)
    36 NS_IMETHODIMP
    37 MySocketListener::OnSocketAccepted(nsIServerSocket *serv,
    38                                    nsISocketTransport *trans)
    39 {
    40     LOG(("MySocketListener::OnSocketAccepted [serv=%p trans=%p]\n", serv, trans));
    42     nsAutoCString host;
    43     int32_t port;
    45     trans->GetHost(host);
    46     trans->GetPort(&port);
    48     LOG(("  -> %s:%d\n", host.get(), port));
    50     nsCOMPtr<nsIInputStream> input;
    51     nsCOMPtr<nsIOutputStream> output;
    52     nsresult rv;
    54     rv = trans->OpenInputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(input));
    55     if (NS_FAILED(rv))
    56         return rv;
    58     rv = trans->OpenOutputStream(nsITransport::OPEN_BLOCKING, 0, 0, getter_AddRefs(output));
    59     if (NS_FAILED(rv))
    60         return rv;
    62     char buf[256];
    63     uint32_t n;
    65     rv = input->Read(buf, sizeof(buf), &n);
    66     if (NS_FAILED(rv))
    67         return rv;
    69     const char response[] = "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nFooooopy!!\r\n";
    70     rv = output->Write(response, sizeof(response) - 1, &n);
    71     if (NS_FAILED(rv))
    72         return rv;
    74     input->Close();
    75     output->Close();
    76     return NS_OK;
    77 }
    79 NS_IMETHODIMP
    80 MySocketListener::OnStopListening(nsIServerSocket *serv, nsresult status)
    81 {
    82     LOG(("MySocketListener::OnStopListening [serv=%p status=%x]\n", serv, status));
    83     QuitPumpingEvents();
    84     return NS_OK;
    85 }
    87 static nsresult
    88 MakeServer(int32_t port)
    89 {
    90     nsresult rv;
    91     nsCOMPtr<nsIServerSocket> serv = do_CreateInstance(NS_SERVERSOCKET_CONTRACTID, &rv);
    92     if (NS_FAILED(rv))
    93         return rv;
    95     rv = serv->Init(port, true, 5);
    96     if (NS_FAILED(rv))
    97         return rv;
    99     rv = serv->GetPort(&port);
   100     if (NS_FAILED(rv))
   101         return rv;
   102     LOG(("  listening on port %d\n", port));
   104     rv = serv->AsyncListen(new MySocketListener());
   105     return rv;
   106 }
   108 int
   109 main(int argc, char* argv[])
   110 {
   111     if (test_common_init(&argc, &argv) != 0)
   112         return -1;
   114     nsresult rv= (nsresult)-1;
   115     if (argc < 2) {
   116         printf("usage: %s <port>\n", argv[0]);
   117         return -1;
   118     }
   120 #if defined(PR_LOGGING)
   121     gTestLog = PR_NewLogModule("Test");
   122 #endif
   124     /* 
   125      * The following code only deals with XPCOM registration stuff. and setting
   126      * up the event queues. Copied from TestSocketIO.cpp
   127      */
   129     rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
   130     if (NS_FAILED(rv)) return -1;
   132     {
   133         rv = MakeServer(atoi(argv[1]));
   134         if (NS_FAILED(rv)) {
   135             LOG(("MakeServer failed [rv=%x]\n", rv));
   136             return -1;
   137         }
   139         // Enter the message pump to allow the URL load to proceed.
   140         PumpEvents();
   141     } // this scopes the nsCOMPtrs
   142     // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
   143     NS_ShutdownXPCOM(nullptr);
   144     return 0;
   145 }

mercurial