• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
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  *     http://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 
16 #include <string_view>
17 
18 #include "gtest/gtest.h"
19 #include "http.h"
20 
21 using namespace OHOS::ArkCompiler::Toolchain;
22 
23 namespace panda::test {
24 class HttpDecoderTest : public testing::Test {
25 public:
26     static constexpr std::string_view REQUEST_ORIGIN_HEADERS = "GET / HTTP/1.1\r\n"
27         "Host: 127.0.0.1:19015\r\n"
28         "Connection: Upgrade\r\n"
29         "Pragma: no-cache\r\n"
30         "Cache-Control: no-cache\r\n"
31         "User-Agent: Mozilla/5.0 (X11; Linux x86_64) Chrome/117.0.0.0 Safari/537.36\r\n"
32         "Upgrade: websocket\r\n"
33         "Origin: dvtls://dvtls\r\n"
34         "Sec-WebSocket-Version: 13\r\n"
35         "Accept-Encoding: gzip, deflate, br\r\n"
36         "Accept-Language: en-US,en;q=0.9\r\n"
37         "Sec-WebSocket-Key: AyuTxzyBTJJdViDskomT0Q==\r\n"
38         "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n\r\n";
39     std::string originRequestHeaders = std::string(REQUEST_ORIGIN_HEADERS);
40 
41     static constexpr std::string_view REQUEST_HEADERS = "GET / HTTP/1.1\r\n"
42         "Host: 127.0.0.1:19015\r\n"
43         "Connection: Upgrade\r\n"
44         "Pragma: no-cache\r\n"
45         "Cache-Control: no-cache\r\n"
46         "User-Agent: Mozilla/5.0 (X11; Linux x86_64) Chrome/117.0.0.0 Safari/537.36\r\n"
47         "Upgrade: websocket\r\n"
48         "Sec-WebSocket-Version: 13\r\n"
49         "Accept-Encoding: gzip, deflate, br\r\n"
50         "Accept-Language: en-US,en;q=0.9\r\n"
51         "Sec-WebSocket-Key: AyuTxzyBTJJdViDskomT0Q==\r\n"
52         "Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits\r\n\r\n";
53     std::string requestHeaders = std::string(REQUEST_HEADERS);
54 
55     static constexpr std::string_view ERR_REQUEST_HEADERS = "GEY\r\n";
56     std::string errRequestHeaders = std::string(ERR_REQUEST_HEADERS);
57 
58     static constexpr std::string_view NO_INFO_REQUEST_HEADERS = "GET\r\n";
59     std::string noInfoRequestHeaders = std::string(NO_INFO_REQUEST_HEADERS);
60 
61     static constexpr std::string_view RESPONSE_HEADERS = "HTTP/1.1 101 Switching Protocols\r\n"
62         "Connection: Upgrade\r\n"
63         "Upgrade: websocket\r\n"
64         "Sec-WebSocket-Accept: AyuTxzyBTJJdViDskomT0Q==\r\n\r\n";
65     std::string responseHeaders = std::string(RESPONSE_HEADERS);
66 
67     static constexpr std::string_view ERR_RESPONSE_HEADERS = "HTTB\r\n";
68     std::string errResponseHeaders = std::string(ERR_RESPONSE_HEADERS);
69 
70     static constexpr std::string_view NO_INFO_RESPONSE_HEADERS = "HTTP\r\n";
71     std::string noInfoResponseHeaders = std::string(NO_INFO_RESPONSE_HEADERS);
72 
73     static constexpr std::string_view EXPECTED_VERSION              = "HTTP/1.1";
74     static constexpr std::string_view EXPECTED_STATUS               = "101";
75     static constexpr std::string_view EXPECTED_CONNECTION           = "Upgrade";
76     static constexpr std::string_view EXPECTED_UPGRADE              = "websocket";
77     static constexpr std::string_view EXPECTED_SEC_WEBSOCKET_KEY    = "AyuTxzyBTJJdViDskomT0Q==";
78 };
79 
80 HWTEST_F(HttpDecoderTest, TestRequestDecode_1, testing::ext::TestSize.Level0)
81 {
82     HttpRequest parsed;
83     auto succeeded = HttpRequest::Decode(requestHeaders, parsed);
84 
85     ASSERT_TRUE(succeeded);
86     ASSERT_EQ(parsed.version, EXPECTED_VERSION);
87     ASSERT_EQ(parsed.connection, EXPECTED_CONNECTION);
88     ASSERT_EQ(parsed.upgrade, EXPECTED_UPGRADE);
89     ASSERT_EQ(parsed.secWebSocketKey, EXPECTED_SEC_WEBSOCKET_KEY);
90 }
91 
92 HWTEST_F(HttpDecoderTest, TestRequestDecode_2, testing::ext::TestSize.Level0)
93 {
94     HttpRequest parsed;
95     auto succeeded = HttpRequest::Decode(originRequestHeaders, parsed);
96 
97     ASSERT_FALSE(succeeded);
98 }
99 
100 HWTEST_F(HttpDecoderTest, TestAbnormalRequestDecode, testing::ext::TestSize.Level0)
101 {
102     HttpRequest parsed;
103     ASSERT_FALSE(HttpRequest::Decode(errRequestHeaders, parsed));
104     ASSERT_TRUE(HttpRequest::Decode(noInfoRequestHeaders, parsed));
105 
106     ASSERT_EQ(parsed.version, "");
107     ASSERT_EQ(parsed.connection, "");
108     ASSERT_EQ(parsed.upgrade, "");
109     ASSERT_EQ(parsed.secWebSocketKey, "");
110 }
111 
112 HWTEST_F(HttpDecoderTest, TestResponseDecode, testing::ext::TestSize.Level0)
113 {
114     HttpResponse parsed;
115     auto succeeded = HttpResponse::Decode(responseHeaders, parsed);
116 
117     ASSERT_TRUE(succeeded);
118     ASSERT_EQ(parsed.version, EXPECTED_VERSION);
119     ASSERT_EQ(parsed.status, EXPECTED_STATUS);
120     ASSERT_EQ(parsed.connection, EXPECTED_CONNECTION);
121     ASSERT_EQ(parsed.upgrade, EXPECTED_UPGRADE);
122     ASSERT_EQ(parsed.secWebSocketAccept, EXPECTED_SEC_WEBSOCKET_KEY);
123 }
124 
125 HWTEST_F(HttpDecoderTest, TestAbnormalResponseDecode, testing::ext::TestSize.Level0)
126 {
127     HttpResponse parsed;
128     ASSERT_FALSE(HttpResponse::Decode(errResponseHeaders, parsed));
129     ASSERT_TRUE(HttpResponse::Decode(noInfoResponseHeaders, parsed));
130 
131     ASSERT_EQ(parsed.version, "");
132     ASSERT_EQ(parsed.status, "");
133     ASSERT_EQ(parsed.connection, "");
134     ASSERT_EQ(parsed.upgrade, "");
135     ASSERT_EQ(parsed.secWebSocketAccept, "");
136 }
137 
138 }  // namespace panda::test
139