1 /*
2  * Copyright (C) 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 #pragma once
19 
20 #include <mutex>
21 #include <string>
22 
23 #include <android-base/thread_annotations.h>
24 
25 #include "doh/tests/doh_frontend/include/lib.rs.h"
26 
27 namespace test {
28 
29 /*
30  * The wrapper between tests and Rust DoH frontend.
31  * It is designed to be as close as possible to DnsTlsFrontend, so we can write one test for
32  * both DoT and DoH.
33  */
34 class DohFrontend {
35   public:
36     DohFrontend(const std::string& listen_address = kDefaultListenAddr,
37                 const std::string& listen_service = kDefaultListenService,
38                 const std::string& backend_address = kDefaultBackendAddr,
39                 const std::string& backend_service = kDefaultBackendService)
mAddress(listen_address)40         : mAddress(listen_address),
41           mService(listen_service),
42           mBackendAddress(backend_address),
43           mBackendService(backend_service) {}
44     ~DohFrontend();
listen_address()45     std::string listen_address() const { return mAddress; }
listen_service()46     std::string listen_service() const { return mService; }
47 
48     bool startServer();
49     bool stopServer();
50 
51     // Returns the number of received DoH queries.
52     int queries() const;
53 
54     // Returns the number of accepted DoH connections.
55     int connections() const;
56 
57     // Returns the number of alive DoH connections.
58     int aliveConnections() const;
59 
60     // Returns the number of connections using session resumption.
61     int resumedConnections() const;
62 
63     // Returns the number of connections that had early data.
64     int earlyDataConnections() const;
65 
66     void clearQueries();
67     bool block_sending(bool block);
68     bool setResetStreamId(uint64_t value);
69     bool waitForAllClientsDisconnected() const;
70 
71     // To make the configuration effective, callers need to restart the DoH server after calling
72     // these methods.
73     bool setMaxIdleTimeout(uint64_t value);
74     bool setMaxBufferSize(uint64_t value);
75     bool setMaxStreamsBidi(uint64_t value);
76 
initRustAndroidLogger()77     static void initRustAndroidLogger() { rust::init_android_logger(); }
78 
79     static constexpr char kDefaultListenAddr[] = "127.0.0.3";
80     static constexpr char kDefaultListenService[] = "443";
81     static constexpr char kDefaultBackendAddr[] = "127.0.0.3";
82     static constexpr char kDefaultBackendService[] = "53";
83 
84   private:
85     const std::string mAddress;
86     const std::string mService;
87     const std::string mBackendAddress;
88     const std::string mBackendService;
89 
90     mutable std::mutex mMutex;
91     rust::DohFrontend* mRustDoh GUARDED_BY(mMutex) = nullptr;
92 };
93 
94 }  // namespace test
95