1 // Copyright 2021 The Chromium Authors 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_TEST_TEST_DOH_SERVER_H_ 6 #define NET_TEST_TEST_DOH_SERVER_H_ 7 8 #include <cstdint> 9 #include <map> 10 #include <memory> 11 #include <string> 12 #include <utility> 13 14 #include "base/containers/span.h" 15 #include "base/strings/string_piece.h" 16 #include "base/synchronization/lock.h" 17 #include "base/thread_annotations.h" 18 #include "base/time/time.h" 19 #include "net/base/ip_address.h" 20 #include "net/dns/dns_response.h" 21 #include "net/test/embedded_test_server/embedded_test_server.h" 22 #include "net/test/embedded_test_server/http_response.h" 23 24 namespace net { 25 26 // TestDohServer is a test DoH server. It allows tests to specify DNS behavior 27 // at the level of individual DNS records. 28 class TestDohServer { 29 public: 30 TestDohServer(); 31 ~TestDohServer(); 32 33 // Configures the hostname the DoH server serves from. If not specified, the 34 // server is accessed over 127.0.0.1. This determines the TLS certificate 35 // used, and the hostname in `GetTemplate`. 36 void SetHostname(base::StringPiece name); 37 38 // Configures whether the server should fail all requests with an HTTP error. 39 void SetFailRequests(bool fail_requests); 40 41 // Adds `address` to the set of A (or AAAA, if IPv6) responses when querying 42 // `name`. This is a convenience wrapper over `AddRecord`. 43 void AddAddressRecord(base::StringPiece name, 44 const IPAddress& address, 45 base::TimeDelta ttl = base::Days(1)); 46 47 // Adds `record` to the set of records served by this server. 48 void AddRecord(const DnsResourceRecord& record); 49 50 // Starts the test server and returns true on success or false on failure. 51 // 52 // Note this method starts a background thread. In some tests, such as 53 // browser_tests, the process is required to be single-threaded in the early 54 // stages of test setup. Tests that call `GetTemplate` at that point should 55 // call `InitializeAndListen` before `GetTemplate`, followed by 56 // `StartAcceptingConnections` when threads are allowed. See 57 // `EmbeddedTestServer` for an example. 58 [[nodiscard]] bool Start(); 59 60 // Initializes the listening socket for the test server, allocating a 61 // listening port, and returns true on success or false on failure. Call 62 // `StartAcceptingConnections` to finish initialization. 63 [[nodiscard]] bool InitializeAndListen(); 64 65 // Spawns a background thread and begins accepting connections. This method 66 // must be called after `InitializeAndListen`. 67 void StartAcceptingConnections(); 68 69 // Shuts down the server and waits until the shutdown is complete. 70 [[nodiscard]] bool ShutdownAndWaitUntilComplete(); 71 72 // Returns the number of queries served so far. 73 int QueriesServed(); 74 75 // Returns the URI template to connect to this server. The server's listening 76 // port must have been allocated with `Start` or `InitializeAndListen` before 77 // calling this function. 78 std::string GetTemplate(); 79 80 // Behaves like `GetTemplate`, but returns a template without the "dns" URL 81 // and thus can only be used with POST. 82 std::string GetPostOnlyTemplate(); 83 84 private: 85 std::unique_ptr<test_server::HttpResponse> HandleRequest( 86 const test_server::HttpRequest& request); 87 88 absl::optional<std::string> hostname_; 89 base::Lock lock_; 90 // The following fields are accessed from a background thread and protected by 91 // `lock_`. 92 bool fail_requests_ GUARDED_BY(lock_) = false; 93 // Maps from query name and query type to a record set. 94 std::multimap<std::pair<std::string, uint16_t>, DnsResourceRecord> records_ 95 GUARDED_BY(lock_); 96 int queries_served_ GUARDED_BY(lock_) = 0; 97 EmbeddedTestServer server_{EmbeddedTestServer::TYPE_HTTPS}; 98 }; 99 100 } // namespace net 101 102 #endif // NET_TEST_TEST_DOH_SERVER_H_ 103