summaryrefslogtreecommitdiff
path: root/chromium/net/http2/http2_constants_test.cc
blob: 5616d42e9175c102ad0b41fe651d9fe70cc8d9bc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/http2/http2_constants.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {
namespace test {
namespace {

class Http2ConstantsTest : public testing::Test {};

TEST(Http2ConstantsTest, Http2FrameType) {
  EXPECT_EQ(Http2FrameType::DATA, static_cast<Http2FrameType>(0));
  EXPECT_EQ(Http2FrameType::HEADERS, static_cast<Http2FrameType>(1));
  EXPECT_EQ(Http2FrameType::PRIORITY, static_cast<Http2FrameType>(2));
  EXPECT_EQ(Http2FrameType::RST_STREAM, static_cast<Http2FrameType>(3));
  EXPECT_EQ(Http2FrameType::SETTINGS, static_cast<Http2FrameType>(4));
  EXPECT_EQ(Http2FrameType::PUSH_PROMISE, static_cast<Http2FrameType>(5));
  EXPECT_EQ(Http2FrameType::PING, static_cast<Http2FrameType>(6));
  EXPECT_EQ(Http2FrameType::GOAWAY, static_cast<Http2FrameType>(7));
  EXPECT_EQ(Http2FrameType::WINDOW_UPDATE, static_cast<Http2FrameType>(8));
  EXPECT_EQ(Http2FrameType::CONTINUATION, static_cast<Http2FrameType>(9));
  EXPECT_EQ(Http2FrameType::ALTSVC, static_cast<Http2FrameType>(10));
}

TEST(Http2ConstantsTest, Http2FrameTypeToString) {
  EXPECT_EQ("DATA", Http2FrameTypeToString(Http2FrameType::DATA));
  EXPECT_EQ("HEADERS", Http2FrameTypeToString(Http2FrameType::HEADERS));
  EXPECT_EQ("PRIORITY", Http2FrameTypeToString(Http2FrameType::PRIORITY));
  EXPECT_EQ("RST_STREAM", Http2FrameTypeToString(Http2FrameType::RST_STREAM));
  EXPECT_EQ("SETTINGS", Http2FrameTypeToString(Http2FrameType::SETTINGS));
  EXPECT_EQ("PUSH_PROMISE",
            Http2FrameTypeToString(Http2FrameType::PUSH_PROMISE));
  EXPECT_EQ("PING", Http2FrameTypeToString(Http2FrameType::PING));
  EXPECT_EQ("GOAWAY", Http2FrameTypeToString(Http2FrameType::GOAWAY));
  EXPECT_EQ("WINDOW_UPDATE",
            Http2FrameTypeToString(Http2FrameType::WINDOW_UPDATE));
  EXPECT_EQ("CONTINUATION",
            Http2FrameTypeToString(Http2FrameType::CONTINUATION));
  EXPECT_EQ("ALTSVC", Http2FrameTypeToString(Http2FrameType::ALTSVC));

  EXPECT_EQ("DATA", Http2FrameTypeToString(0));
  EXPECT_EQ("HEADERS", Http2FrameTypeToString(1));
  EXPECT_EQ("PRIORITY", Http2FrameTypeToString(2));
  EXPECT_EQ("RST_STREAM", Http2FrameTypeToString(3));
  EXPECT_EQ("SETTINGS", Http2FrameTypeToString(4));
  EXPECT_EQ("PUSH_PROMISE", Http2FrameTypeToString(5));
  EXPECT_EQ("PING", Http2FrameTypeToString(6));
  EXPECT_EQ("GOAWAY", Http2FrameTypeToString(7));
  EXPECT_EQ("WINDOW_UPDATE", Http2FrameTypeToString(8));
  EXPECT_EQ("CONTINUATION", Http2FrameTypeToString(9));
  EXPECT_EQ("ALTSVC", Http2FrameTypeToString(10));

  EXPECT_EQ("UnknownFrameType(99)", Http2FrameTypeToString(99));
}

TEST(Http2ConstantsTest, Http2FrameFlag) {
  EXPECT_EQ(Http2FrameFlag::END_STREAM, static_cast<Http2FrameFlag>(0x01));
  EXPECT_EQ(Http2FrameFlag::ACK, static_cast<Http2FrameFlag>(0x01));
  EXPECT_EQ(Http2FrameFlag::END_HEADERS, static_cast<Http2FrameFlag>(0x04));
  EXPECT_EQ(Http2FrameFlag::PADDED, static_cast<Http2FrameFlag>(0x08));
  EXPECT_EQ(Http2FrameFlag::PRIORITY, static_cast<Http2FrameFlag>(0x20));

  EXPECT_EQ(Http2FrameFlag::END_STREAM, 0x01);
  EXPECT_EQ(Http2FrameFlag::ACK, 0x01);
  EXPECT_EQ(Http2FrameFlag::END_HEADERS, 0x04);
  EXPECT_EQ(Http2FrameFlag::PADDED, 0x08);
  EXPECT_EQ(Http2FrameFlag::PRIORITY, 0x20);
}

TEST(Http2ConstantsTest, Http2FrameFlagsToString) {
  // Single flags...

  // 0b00000001
  EXPECT_EQ("END_STREAM", Http2FrameFlagsToString(Http2FrameType::DATA,
                                                  Http2FrameFlag::END_STREAM));
  EXPECT_EQ("END_STREAM",
            Http2FrameFlagsToString(Http2FrameType::HEADERS, 0x01));
  EXPECT_EQ("ACK", Http2FrameFlagsToString(Http2FrameType::SETTINGS,
                                           Http2FrameFlag::ACK));
  EXPECT_EQ("ACK", Http2FrameFlagsToString(Http2FrameType::PING, 0x01));

  // 0b00000010
  EXPECT_EQ("0x02", Http2FrameFlagsToString(0xff, 0x02));

  // 0b00000100
  EXPECT_EQ("END_HEADERS",
            Http2FrameFlagsToString(Http2FrameType::HEADERS,
                                    Http2FrameFlag::END_HEADERS));
  EXPECT_EQ("END_HEADERS",
            Http2FrameFlagsToString(Http2FrameType::PUSH_PROMISE, 0x04));
  EXPECT_EQ("END_HEADERS", Http2FrameFlagsToString(0x09, 0x04));
  EXPECT_EQ("0x04", Http2FrameFlagsToString(0xff, 0x04));

  // 0b00001000
  EXPECT_EQ("PADDED", Http2FrameFlagsToString(Http2FrameType::DATA,
                                              Http2FrameFlag::PADDED));
  EXPECT_EQ("PADDED", Http2FrameFlagsToString(Http2FrameType::HEADERS, 0x08));
  EXPECT_EQ("PADDED", Http2FrameFlagsToString(0x05, 0x08));
  EXPECT_EQ("0x08", Http2FrameFlagsToString(0xff, Http2FrameFlag::PADDED));

  // 0b00010000
  EXPECT_EQ("0x10", Http2FrameFlagsToString(Http2FrameType::SETTINGS, 0x10));

  // 0b00100000
  EXPECT_EQ("PRIORITY", Http2FrameFlagsToString(Http2FrameType::HEADERS, 0x20));
  EXPECT_EQ("0x20",
            Http2FrameFlagsToString(Http2FrameType::PUSH_PROMISE, 0x20));

  // 0b01000000
  EXPECT_EQ("0x40", Http2FrameFlagsToString(0xff, 0x40));

  // 0b10000000
  EXPECT_EQ("0x80", Http2FrameFlagsToString(0xff, 0x80));

  // Combined flags...

  EXPECT_EQ("END_STREAM|PADDED|0xf6",
            Http2FrameFlagsToString(Http2FrameType::DATA, 0xff));
  EXPECT_EQ("END_STREAM|END_HEADERS|PADDED|PRIORITY|0xd2",
            Http2FrameFlagsToString(Http2FrameType::HEADERS, 0xff));
  EXPECT_EQ("0xff", Http2FrameFlagsToString(Http2FrameType::PRIORITY, 0xff));
  EXPECT_EQ("0xff", Http2FrameFlagsToString(Http2FrameType::RST_STREAM, 0xff));
  EXPECT_EQ("ACK|0xfe",
            Http2FrameFlagsToString(Http2FrameType::SETTINGS, 0xff));
  EXPECT_EQ("END_HEADERS|PADDED|0xf3",
            Http2FrameFlagsToString(Http2FrameType::PUSH_PROMISE, 0xff));
  EXPECT_EQ("ACK|0xfe", Http2FrameFlagsToString(Http2FrameType::PING, 0xff));
  EXPECT_EQ("0xff", Http2FrameFlagsToString(Http2FrameType::GOAWAY, 0xff));
  EXPECT_EQ("0xff",
            Http2FrameFlagsToString(Http2FrameType::WINDOW_UPDATE, 0xff));
  EXPECT_EQ("END_HEADERS|0xfb",
            Http2FrameFlagsToString(Http2FrameType::CONTINUATION, 0xff));
  EXPECT_EQ("0xff", Http2FrameFlagsToString(Http2FrameType::ALTSVC, 0xff));
  EXPECT_EQ("0xff", Http2FrameFlagsToString(0xff, 0xff));
}

TEST(Http2ConstantsTest, Http2ErrorCode) {
  EXPECT_EQ(Http2ErrorCode::HTTP2_NO_ERROR, static_cast<Http2ErrorCode>(0x0));
  EXPECT_EQ(Http2ErrorCode::PROTOCOL_ERROR, static_cast<Http2ErrorCode>(0x1));
  EXPECT_EQ(Http2ErrorCode::INTERNAL_ERROR, static_cast<Http2ErrorCode>(0x2));
  EXPECT_EQ(Http2ErrorCode::FLOW_CONTROL_ERROR,
            static_cast<Http2ErrorCode>(0x3));
  EXPECT_EQ(Http2ErrorCode::SETTINGS_TIMEOUT, static_cast<Http2ErrorCode>(0x4));
  EXPECT_EQ(Http2ErrorCode::STREAM_CLOSED, static_cast<Http2ErrorCode>(0x5));
  EXPECT_EQ(Http2ErrorCode::FRAME_SIZE_ERROR, static_cast<Http2ErrorCode>(0x6));
  EXPECT_EQ(Http2ErrorCode::REFUSED_STREAM, static_cast<Http2ErrorCode>(0x7));
  EXPECT_EQ(Http2ErrorCode::CANCEL, static_cast<Http2ErrorCode>(0x8));
  EXPECT_EQ(Http2ErrorCode::COMPRESSION_ERROR,
            static_cast<Http2ErrorCode>(0x9));
  EXPECT_EQ(Http2ErrorCode::CONNECT_ERROR, static_cast<Http2ErrorCode>(0xa));
  EXPECT_EQ(Http2ErrorCode::ENHANCE_YOUR_CALM,
            static_cast<Http2ErrorCode>(0xb));
  EXPECT_EQ(Http2ErrorCode::INADEQUATE_SECURITY,
            static_cast<Http2ErrorCode>(0xc));
  EXPECT_EQ(Http2ErrorCode::HTTP_1_1_REQUIRED,
            static_cast<Http2ErrorCode>(0xd));
}

TEST(Http2ConstantsTest, Http2ErrorCodeToString) {
  EXPECT_EQ("NO_ERROR", Http2ErrorCodeToString(Http2ErrorCode::HTTP2_NO_ERROR));
  EXPECT_EQ("NO_ERROR", Http2ErrorCodeToString(0x0));
  EXPECT_EQ("PROTOCOL_ERROR",
            Http2ErrorCodeToString(Http2ErrorCode::PROTOCOL_ERROR));
  EXPECT_EQ("PROTOCOL_ERROR", Http2ErrorCodeToString(0x1));
  EXPECT_EQ("INTERNAL_ERROR",
            Http2ErrorCodeToString(Http2ErrorCode::INTERNAL_ERROR));
  EXPECT_EQ("INTERNAL_ERROR", Http2ErrorCodeToString(0x2));
  EXPECT_EQ("FLOW_CONTROL_ERROR",
            Http2ErrorCodeToString(Http2ErrorCode::FLOW_CONTROL_ERROR));
  EXPECT_EQ("FLOW_CONTROL_ERROR", Http2ErrorCodeToString(0x3));
  EXPECT_EQ("SETTINGS_TIMEOUT",
            Http2ErrorCodeToString(Http2ErrorCode::SETTINGS_TIMEOUT));
  EXPECT_EQ("SETTINGS_TIMEOUT", Http2ErrorCodeToString(0x4));
  EXPECT_EQ("STREAM_CLOSED",
            Http2ErrorCodeToString(Http2ErrorCode::STREAM_CLOSED));
  EXPECT_EQ("STREAM_CLOSED", Http2ErrorCodeToString(0x5));
  EXPECT_EQ("FRAME_SIZE_ERROR",
            Http2ErrorCodeToString(Http2ErrorCode::FRAME_SIZE_ERROR));
  EXPECT_EQ("FRAME_SIZE_ERROR", Http2ErrorCodeToString(0x6));
  EXPECT_EQ("REFUSED_STREAM",
            Http2ErrorCodeToString(Http2ErrorCode::REFUSED_STREAM));
  EXPECT_EQ("REFUSED_STREAM", Http2ErrorCodeToString(0x7));
  EXPECT_EQ("CANCEL", Http2ErrorCodeToString(Http2ErrorCode::CANCEL));
  EXPECT_EQ("CANCEL", Http2ErrorCodeToString(0x8));
  EXPECT_EQ("COMPRESSION_ERROR",
            Http2ErrorCodeToString(Http2ErrorCode::COMPRESSION_ERROR));
  EXPECT_EQ("COMPRESSION_ERROR", Http2ErrorCodeToString(0x9));
  EXPECT_EQ("CONNECT_ERROR",
            Http2ErrorCodeToString(Http2ErrorCode::CONNECT_ERROR));
  EXPECT_EQ("CONNECT_ERROR", Http2ErrorCodeToString(0xa));
  EXPECT_EQ("ENHANCE_YOUR_CALM",
            Http2ErrorCodeToString(Http2ErrorCode::ENHANCE_YOUR_CALM));
  EXPECT_EQ("ENHANCE_YOUR_CALM", Http2ErrorCodeToString(0xb));
  EXPECT_EQ("INADEQUATE_SECURITY",
            Http2ErrorCodeToString(Http2ErrorCode::INADEQUATE_SECURITY));
  EXPECT_EQ("INADEQUATE_SECURITY", Http2ErrorCodeToString(0xc));
  EXPECT_EQ("HTTP_1_1_REQUIRED",
            Http2ErrorCodeToString(Http2ErrorCode::HTTP_1_1_REQUIRED));
  EXPECT_EQ("HTTP_1_1_REQUIRED", Http2ErrorCodeToString(0xd));

  EXPECT_EQ("UnknownErrorCode(0x123)", Http2ErrorCodeToString(0x123));
}

TEST(Http2ConstantsTest, Http2SettingsParameter) {
  EXPECT_EQ(Http2SettingsParameter::HEADER_TABLE_SIZE,
            static_cast<Http2SettingsParameter>(0x1));
  EXPECT_EQ(Http2SettingsParameter::ENABLE_PUSH,
            static_cast<Http2SettingsParameter>(0x2));
  EXPECT_EQ(Http2SettingsParameter::MAX_CONCURRENT_STREAMS,
            static_cast<Http2SettingsParameter>(0x3));
  EXPECT_EQ(Http2SettingsParameter::INITIAL_WINDOW_SIZE,
            static_cast<Http2SettingsParameter>(0x4));
  EXPECT_EQ(Http2SettingsParameter::MAX_FRAME_SIZE,
            static_cast<Http2SettingsParameter>(0x5));
  EXPECT_EQ(Http2SettingsParameter::MAX_HEADER_LIST_SIZE,
            static_cast<Http2SettingsParameter>(0x6));

  EXPECT_TRUE(IsSupportedHttp2SettingsParameter(
      Http2SettingsParameter::HEADER_TABLE_SIZE));
  EXPECT_TRUE(
      IsSupportedHttp2SettingsParameter(Http2SettingsParameter::ENABLE_PUSH));
  EXPECT_TRUE(IsSupportedHttp2SettingsParameter(
      Http2SettingsParameter::MAX_CONCURRENT_STREAMS));
  EXPECT_TRUE(IsSupportedHttp2SettingsParameter(
      Http2SettingsParameter::INITIAL_WINDOW_SIZE));
  EXPECT_TRUE(IsSupportedHttp2SettingsParameter(
      Http2SettingsParameter::MAX_FRAME_SIZE));
  EXPECT_TRUE(IsSupportedHttp2SettingsParameter(
      Http2SettingsParameter::MAX_HEADER_LIST_SIZE));

  EXPECT_FALSE(IsSupportedHttp2SettingsParameter(
      static_cast<Http2SettingsParameter>(0)));
  EXPECT_FALSE(IsSupportedHttp2SettingsParameter(
      static_cast<Http2SettingsParameter>(7)));
}

TEST(Http2ConstantsTest, Http2SettingsParameterToString) {
  EXPECT_EQ("HEADER_TABLE_SIZE",
            Http2SettingsParameterToString(
                Http2SettingsParameter::HEADER_TABLE_SIZE));
  EXPECT_EQ("HEADER_TABLE_SIZE", Http2SettingsParameterToString(0x1));
  EXPECT_EQ("ENABLE_PUSH", Http2SettingsParameterToString(
                               Http2SettingsParameter::ENABLE_PUSH));
  EXPECT_EQ("ENABLE_PUSH", Http2SettingsParameterToString(0x2));
  EXPECT_EQ("MAX_CONCURRENT_STREAMS",
            Http2SettingsParameterToString(
                Http2SettingsParameter::MAX_CONCURRENT_STREAMS));
  EXPECT_EQ("MAX_CONCURRENT_STREAMS", Http2SettingsParameterToString(0x3));
  EXPECT_EQ("INITIAL_WINDOW_SIZE",
            Http2SettingsParameterToString(
                Http2SettingsParameter::INITIAL_WINDOW_SIZE));
  EXPECT_EQ("INITIAL_WINDOW_SIZE", Http2SettingsParameterToString(0x4));
  EXPECT_EQ("MAX_FRAME_SIZE", Http2SettingsParameterToString(
                                  Http2SettingsParameter::MAX_FRAME_SIZE));
  EXPECT_EQ("MAX_FRAME_SIZE", Http2SettingsParameterToString(0x5));
  EXPECT_EQ("MAX_HEADER_LIST_SIZE",
            Http2SettingsParameterToString(
                Http2SettingsParameter::MAX_HEADER_LIST_SIZE));
  EXPECT_EQ("MAX_HEADER_LIST_SIZE", Http2SettingsParameterToString(0x6));

  EXPECT_EQ("UnknownSettingsParameter(0x123)",
            Http2SettingsParameterToString(0x123));
}

}  // namespace
}  // namespace test
}  // namespace net