Home | History | Annotate | Download | only in socket
      1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #ifndef NET_SOCKET_SSL_TEST_UTIL_H_
      6 #define NET_SOCKET_SSL_TEST_UTIL_H_
      7 
      8 #include "build/build_config.h"
      9 
     10 #include <string>
     11 
     12 #include "base/file_path.h"
     13 #include "base/process_util.h"
     14 
     15 // TODO(dkegel): share this between net/base and
     16 // chrome/browser without putting it in net.lib
     17 
     18 namespace net {
     19 
     20 // This object bounds the lifetime of an external python-based HTTP/HTTPS/FTP
     21 // server that can provide various responses useful for testing.
     22 // A few basic convenience methods are provided, but no
     23 // URL handling methods (those belong at a higher layer, e.g. in
     24 // url_request_unittest.h).
     25 
     26 class TestServerLauncher {
     27  public:
     28   TestServerLauncher();
     29   TestServerLauncher(int connection_attempts, int connection_timeout);
     30 
     31   virtual ~TestServerLauncher();
     32 
     33   enum Protocol {
     34     ProtoHTTP, ProtoFTP
     35   };
     36 
     37   // Load the test root cert, if it hasn't been loaded yet.
     38   bool LoadTestRootCert();
     39 
     40   // Tells the server to enable/disable servicing each request
     41   // in a separate process. Takes effect only if called before Start.
     42   void set_forking(bool forking) { forking_ = forking; }
     43 
     44   // Start src/net/tools/testserver/testserver.py and
     45   // ask it to serve the given protocol.
     46   // If protocol is HTTP, and cert_path is not empty, serves HTTPS.
     47   // file_root_url specifies the root url on the server that documents will be
     48   // served out of. This is /files/ by default.
     49   // Returns true on success, false if files not found or root cert
     50   // not trusted.
     51   bool Start(Protocol protocol,
     52              const std::string& host_name, int port,
     53              const FilePath& document_root,
     54              const FilePath& cert_path,
     55              const std::wstring& file_root_url);
     56 
     57   // Stop the server started by Start().
     58   bool Stop();
     59 
     60   // If you access the server's Kill url, it will exit by itself
     61   // without a call to Stop().
     62   // WaitToFinish is handy in that case.
     63   // It returns true if the server exited cleanly.
     64   bool WaitToFinish(int milliseconds);
     65 
     66   // Paths to a good, an expired, and an invalid server certificate
     67   // (use as arguments to Start()).
     68   FilePath GetOKCertPath();
     69   FilePath GetExpiredCertPath();
     70 
     71   FilePath GetDocumentRootPath() { return document_root_dir_; }
     72 
     73   // Issuer name of the root cert that should be trusted for the test to work.
     74   static const wchar_t kCertIssuerName[];
     75 
     76   // Hostname to use for test server
     77   static const char kHostName[];
     78 
     79   // Different hostname to use for test server (that still resolves to same IP)
     80   static const char kMismatchedHostName[];
     81 
     82   // Port to use for test server
     83   static const int kOKHTTPSPort;
     84 
     85   // Port to use for bad test server
     86   static const int kBadHTTPSPort;
     87 
     88  private:
     89   // Wait a while for the server to start, return whether
     90   // we were able to make a connection to it.
     91   bool WaitToStart(const std::string& host_name, int port);
     92 
     93   // Append to PYTHONPATH so Python can find pyftpdlib and tlslite.
     94   void SetPythonPath();
     95 
     96   // Path to our test root certificate.
     97   FilePath GetRootCertPath();
     98 
     99   // Returns false if our test root certificate is not trusted.
    100   bool CheckCATrusted();
    101 
    102   // Initilize the certificate path.
    103   void InitCertPath();
    104 
    105   FilePath document_root_dir_;
    106 
    107   FilePath cert_dir_;
    108 
    109   FilePath python_runtime_;
    110 
    111   base::ProcessHandle process_handle_;
    112 
    113   // True if the server should handle each request in a separate process.
    114   bool forking_;
    115 
    116   // Number of tries and timeout for each try used for WaitToStart.
    117   int connection_attempts_;
    118   int connection_timeout_;
    119 
    120 #if defined(USE_NSS)
    121   struct PrivateCERTCertificate;
    122   PrivateCERTCertificate *cert_;
    123 #endif
    124 
    125   DISALLOW_COPY_AND_ASSIGN(TestServerLauncher);
    126 };
    127 
    128 }
    129 
    130 #endif  // NET_SOCKET_SSL_TEST_UTIL_H_
    131