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