1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/websockets/websocket_extension_parser.h"
9 #include "net/websockets/websocket_extension.h"
10 #include "testing/gtest/include/gtest/gtest.h"
16 TEST(WebSocketExtensionParserTest
, ParseEmpty
) {
17 WebSocketExtensionParser parser
;
20 EXPECT_TRUE(parser
.has_error());
21 EXPECT_EQ(0U, parser
.extensions().size());
24 TEST(WebSocketExtensionParserTest
, ParseSimple
) {
25 WebSocketExtensionParser parser
;
26 WebSocketExtension
expected("foo");
30 ASSERT_FALSE(parser
.has_error());
31 ASSERT_EQ(1U, parser
.extensions().size());
32 EXPECT_TRUE(expected
.Equals(parser
.extensions()[0]));
35 TEST(WebSocketExtensionParserTest
, ParseMoreThanOnce
) {
36 WebSocketExtensionParser parser
;
37 WebSocketExtension
expected("foo");
40 ASSERT_FALSE(parser
.has_error());
41 ASSERT_EQ(1U, parser
.extensions().size());
42 EXPECT_TRUE(expected
.Equals(parser
.extensions()[0]));
45 EXPECT_TRUE(parser
.has_error());
46 EXPECT_EQ(0U, parser
.extensions().size());
49 ASSERT_FALSE(parser
.has_error());
50 ASSERT_EQ(1U, parser
.extensions().size());
51 EXPECT_TRUE(expected
.Equals(parser
.extensions()[0]));
54 TEST(WebSocketExtensionParserTest
, ParseOneExtensionWithOneParamWithoutValue
) {
55 WebSocketExtensionParser parser
;
56 WebSocketExtension
expected("foo");
57 expected
.Add(WebSocketExtension::Parameter("bar"));
59 parser
.Parse("\tfoo ; bar");
61 ASSERT_FALSE(parser
.has_error());
62 ASSERT_EQ(1U, parser
.extensions().size());
63 EXPECT_TRUE(expected
.Equals(parser
.extensions()[0]));
66 TEST(WebSocketExtensionParserTest
, ParseOneExtensionWithOneParamWithValue
) {
67 WebSocketExtensionParser parser
;
68 WebSocketExtension
expected("foo");
69 expected
.Add(WebSocketExtension::Parameter("bar", "baz"));
71 parser
.Parse("foo ; bar= baz\t");
73 ASSERT_FALSE(parser
.has_error());
74 ASSERT_EQ(1U, parser
.extensions().size());
75 EXPECT_TRUE(expected
.Equals(parser
.extensions()[0]));
78 TEST(WebSocketExtensionParserTest
, ParseOneExtensionWithParams
) {
79 WebSocketExtensionParser parser
;
80 WebSocketExtension
expected("foo");
81 expected
.Add(WebSocketExtension::Parameter("bar", "baz"));
82 expected
.Add(WebSocketExtension::Parameter("hoge", "fuga"));
84 parser
.Parse("foo ; bar= baz;\t \thoge\t\t=fuga");
86 ASSERT_FALSE(parser
.has_error());
87 ASSERT_EQ(1U, parser
.extensions().size());
88 EXPECT_TRUE(expected
.Equals(parser
.extensions()[0]));
91 TEST(WebSocketExtensionParserTest
, ParseTwoExtensions
) {
92 WebSocketExtensionParser parser
;
94 WebSocketExtension
expected0("foo");
95 expected0
.Add(WebSocketExtension::Parameter("alpha", "x"));
97 WebSocketExtension
expected1("bar");
98 expected1
.Add(WebSocketExtension::Parameter("beta", "y"));
100 parser
.Parse(" foo ; alpha = x , bar ; beta = y ");
102 ASSERT_FALSE(parser
.has_error());
103 ASSERT_EQ(2U, parser
.extensions().size());
105 EXPECT_TRUE(expected0
.Equals(parser
.extensions()[0]));
106 EXPECT_TRUE(expected1
.Equals(parser
.extensions()[1]));
109 TEST(WebSocketExtensionParserTest
, InvalidPatterns
) {
110 const char* const patterns
[] = {
112 " , ", // just a comma with surrounding spaces
113 "foo,", // second extension is incomplete (empty)
114 "foo , ", // second extension is incomplete (space)
115 "foo,;", // second extension is incomplete (semicolon)
116 "fo\ao", // control in extension name
117 "fo\x01o", // control in extension name
118 "fo<o", // separator in extension name
119 "foo/", // separator in extension name
120 ";bar", // empty extension name
121 "foo bar", // missing ';'
122 "foo;", // extension parameter without name and value
123 "foo; b\ar", // control in parameter name
124 "foo; b\x7fr", // control in parameter name
125 "foo; b[r", // separator in parameter name
126 "foo; ba:", // separator in parameter name
127 "foo; =baz", // empty parameter name
128 "foo; bar=", // empty parameter value
129 "foo; =", // empty parameter name and value
130 "foo; bar=b\x02z", // control in parameter value
131 "foo; bar=b@z", // separator in parameter value
132 "foo; bar=b\\z", // separator in parameter value
133 "foo; bar=b?z", // separator in parameter value
134 "\"foo\"", // quoted extension name
135 "foo; \"bar\"", // quoted parameter name
136 "foo; bar=\"\a2\"", // control in quoted parameter value
137 "foo; bar=\"b@z\"", // separator in quoted parameter value
138 "foo; bar=\"b\\\\z\"", // separator in quoted parameter value
139 "foo; bar=\"\"", // quoted empty parameter value
140 "foo; bar=\"baz", // unterminated quoted string
141 "foo; bar=\"baz \"", // space in quoted string
142 "foo; bar baz", // mising '='
143 "foo; bar - baz", // '-' instead of '=' (note: "foo; bar-baz" is valid).
144 "foo; bar=\r\nbaz", // CRNL not followed by a space
145 "foo; bar=\r\n baz", // CRNL followed by a space
148 for (size_t i
= 0; i
< arraysize(patterns
); ++i
) {
149 WebSocketExtensionParser parser
;
150 parser
.Parse(patterns
[i
]);
151 EXPECT_TRUE(parser
.has_error());
152 EXPECT_EQ(0U, parser
.extensions().size());
156 TEST(WebSocketExtensionParserTest
, QuotedParameterValue
) {
157 WebSocketExtensionParser parser
;
158 WebSocketExtension
expected("foo");
159 expected
.Add(WebSocketExtension::Parameter("bar", "baz"));
161 parser
.Parse("foo; bar = \"ba\\z\" ");
163 ASSERT_FALSE(parser
.has_error());
164 ASSERT_EQ(1U, parser
.extensions().size());
165 EXPECT_TRUE(expected
.Equals(parser
.extensions()[0]));