xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/curl/tests/test_utils.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef TESTS_H_
16 #define TESTS_H_
17 
18 #include "../sandbox.h"      // NOLINT(build/include)
19 #include "curl_sapi.sapi.h"  // NOLINT(build/include)
20 #include "gtest/gtest.h"
21 #include "absl/flags/flag.h"
22 #include "absl/status/statusor.h"
23 #include "sandboxed_api/util/status_matchers.h"
24 
25 namespace curl::tests {
26 
27 // Helper class that can be used to test Curl sandbox.
28 class CurlTestUtils {
29  protected:
30   static constexpr char kUrl[] = "http://127.0.0.1/";
31 
32   // Starts a mock server (only once) that will manage connections for the
33   // tests. The server listens on a port asynchronously by creating a thread.
34   // The port number is stored in port_. Responds with "OK" to a GET request,
35   // responds with the POST request fields to a POST request.
36   static void StartMockServer();
37 
38   // Initializes and sets up the curl handle.
39   absl::Status CurlTestSetUp();
40   // Cleans up the curl handle.
41   absl::Status CurlTestTearDown();
42 
43   // Performs a request to the mock server, returning the response.
44   absl::StatusOr<std::string> PerformRequest();
45 
46   static std::thread server_thread_;
47   static int port_;
48 
49   std::unique_ptr<curl::CurlSapiSandbox> sandbox_;
50   std::unique_ptr<curl::CurlApi> api_;
51   std::unique_ptr<sapi::v::RemotePtr> curl_;
52 
53  private:
54   std::unique_ptr<sapi::v::LenVal> chunk_;
55 };
56 
57 }  // namespace curl::tests
58 
59 #endif  // TESTS_H_
60