xref: /aosp_15_r20/external/cronet/net/test/test_doh_server.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 <string_view>
13 #include <utility>
14 
15 #include "base/containers/span.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(std::string_view 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(std::string_view 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 number of queries so far with qnames that are subdomains of
76   // `domain`. Domains are considered subdomains of themselves. The given domain
77   // must be a valid DNS name in dotted form.
78   int QueriesServedForSubdomains(std::string_view domain);
79 
80   // Returns the URI template to connect to this server. The server's listening
81   // port must have been allocated with `Start` or `InitializeAndListen` before
82   // calling this function.
83   std::string GetTemplate();
84 
85   // Behaves like `GetTemplate`, but returns a template without the "dns" URL
86   // and thus can only be used with POST.
87   std::string GetPostOnlyTemplate();
88 
89  private:
90   std::unique_ptr<test_server::HttpResponse> HandleRequest(
91       const test_server::HttpRequest& request);
92 
93   std::optional<std::string> hostname_;
94   base::Lock lock_;
95   // The following fields are accessed from a background thread and protected by
96   // `lock_`.
97   bool fail_requests_ GUARDED_BY(lock_) = false;
98   // Maps from query name and query type to a record set.
99   std::multimap<std::pair<std::string, uint16_t>, DnsResourceRecord> records_
100       GUARDED_BY(lock_);
101   int queries_served_ GUARDED_BY(lock_) = 0;
102   // Contains qnames parsed from queries.
103   std::vector<std::string> query_qnames_ GUARDED_BY(lock_);
104   EmbeddedTestServer server_{EmbeddedTestServer::TYPE_HTTPS};
105 };
106 
107 }  // namespace net
108 
109 #endif  // NET_TEST_TEST_DOH_SERVER_H_
110