xref: /aosp_15_r20/external/sandboxed-api/oss-internship-2020/curl/tests/tests.cc (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 #include "test_utils.h"  // NOLINT(build/include)
16 #include "sandboxed_api/util/status_matchers.h"
17 
18 namespace curl::tests {
19 namespace {
20 
21 using ::sapi::IsOk;
22 using ::sapi::StatusIs;
23 using ::testing::Eq;
24 using ::testing::IsTrue;
25 
26 class CurlTest : public CurlTestUtils, public ::testing::Test {
27  protected:
SetUpTestSuite()28   static void SetUpTestSuite() {
29     // Start mock server, get port number and check for any error
30     StartMockServer();
31     ASSERT_THAT(server_thread_.joinable(), IsTrue());
32   }
33 
TearDownTestSuite()34   static void TearDownTestSuite() {
35     // Detach the server thread
36     server_thread_.detach();
37   }
38 
SetUp()39   void SetUp() override { ASSERT_THAT(CurlTestSetUp(), IsOk()); }
40 
TearDown()41   void TearDown() override { ASSERT_THAT(CurlTestTearDown(), IsOk()); }
42 };
43 
TEST_F(CurlTest,EffectiveUrl)44 TEST_F(CurlTest, EffectiveUrl) {
45   ASSERT_THAT(PerformRequest().status(), IsOk());
46 
47   // Get effective URL
48   sapi::v::RemotePtr effective_url_ptr(nullptr);
49   SAPI_ASSERT_OK_AND_ASSIGN(
50       int getinfo_code,
51       api_->curl_easy_getinfo_ptr(curl_.get(), curl::CURLINFO_EFFECTIVE_URL,
52                                   effective_url_ptr.PtrBoth()));
53   ASSERT_THAT(getinfo_code, Eq(curl::CURLE_OK));
54 
55   // Store effective URL in a string
56   SAPI_ASSERT_OK_AND_ASSIGN(std::string effective_url,
57                             sandbox_->GetCString(sapi::v::RemotePtr(
58                                 effective_url_ptr.GetPointedVar())));
59 
60   // Compare effective URL with original URL
61   ASSERT_THAT(effective_url, Eq(kUrl));
62 }
63 
TEST_F(CurlTest,EffectivePort)64 TEST_F(CurlTest, EffectivePort) {
65   ASSERT_THAT(PerformRequest().status(), IsOk());
66 
67   // Get effective port
68   sapi::v::Int effective_port;
69   SAPI_ASSERT_OK_AND_ASSIGN(
70       int getinfo_code,
71       api_->curl_easy_getinfo_ptr(curl_.get(), curl::CURLINFO_PRIMARY_PORT,
72                                   effective_port.PtrBoth()));
73   ASSERT_EQ(getinfo_code, curl::CURLE_OK);
74 
75   // Compare effective port with port set by the mock server
76   ASSERT_EQ(effective_port.GetValue(), port_);
77 }
78 
TEST_F(CurlTest,ResponseCode)79 TEST_F(CurlTest, ResponseCode) {
80   ASSERT_THAT(PerformRequest().status(), IsOk());
81 
82   // Get response code
83   sapi::v::Int response_code;
84   SAPI_ASSERT_OK_AND_ASSIGN(
85       int getinfo_code,
86       api_->curl_easy_getinfo_ptr(curl_.get(), curl::CURLINFO_RESPONSE_CODE,
87                                   response_code.PtrBoth()));
88   ASSERT_EQ(getinfo_code, curl::CURLE_OK);
89 
90   // Check response code
91   ASSERT_EQ(response_code.GetValue(), 200);
92 }
93 
TEST_F(CurlTest,ContentType)94 TEST_F(CurlTest, ContentType) {
95   sapi::v::RemotePtr content_type_ptr(nullptr);
96 
97   ASSERT_TRUE(PerformRequest().ok());
98 
99   // Get effective URL
100   SAPI_ASSERT_OK_AND_ASSIGN(
101       int getinfo_code,
102       api_->curl_easy_getinfo_ptr(curl_.get(), curl::CURLINFO_CONTENT_TYPE,
103                                   content_type_ptr.PtrBoth()));
104   ASSERT_EQ(getinfo_code, curl::CURLE_OK);
105 
106   // Store content type in a string
107   SAPI_ASSERT_OK_AND_ASSIGN(std::string content_type,
108                             sandbox_->GetCString(sapi::v::RemotePtr(
109                                 content_type_ptr.GetPointedVar())));
110 
111   // Compare content type with "text/plain"
112   ASSERT_EQ(content_type, "text/plain");
113 }
114 
TEST_F(CurlTest,GetResponse)115 TEST_F(CurlTest, GetResponse) {
116   SAPI_ASSERT_OK_AND_ASSIGN(std::string response, PerformRequest());
117 
118   // Compare response with expected response
119   ASSERT_EQ(response, "OK");
120 }
121 
TEST_F(CurlTest,PostResponse)122 TEST_F(CurlTest, PostResponse) {
123   sapi::v::ConstCStr post_fields("postfields");
124 
125   // Set request method to POST
126   SAPI_ASSERT_OK_AND_ASSIGN(
127       int setopt_post,
128       api_->curl_easy_setopt_long(curl_.get(), curl::CURLOPT_POST, 1l));
129   ASSERT_EQ(setopt_post, curl::CURLE_OK);
130 
131   // Set the size of the POST fields
132   SAPI_ASSERT_OK_AND_ASSIGN(
133       int setopt_post_fields_size,
134       api_->curl_easy_setopt_long(curl_.get(), curl::CURLOPT_POSTFIELDSIZE,
135                                   post_fields.GetSize()));
136   ASSERT_EQ(setopt_post_fields_size, curl::CURLE_OK);
137 
138   // Set the POST fields
139   SAPI_ASSERT_OK_AND_ASSIGN(
140       int setopt_post_fields,
141       api_->curl_easy_setopt_ptr(curl_.get(), curl::CURLOPT_POSTFIELDS,
142                                  post_fields.PtrBefore()));
143   ASSERT_EQ(setopt_post_fields, curl::CURLE_OK);
144 
145   SAPI_ASSERT_OK_AND_ASSIGN(std::string response, PerformRequest());
146 
147   // Compare response with expected response
148   ASSERT_EQ(std::string(post_fields.GetData()), response);
149 }
150 
151 }  // namespace
152 }  // namespace curl::tests
153