Home | History | Annotate | Download | only in websockets
      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.
      4 
      5 #include "net/websockets/websocket_extension_parser.h"
      6 
      7 #include <string>
      8 
      9 #include "net/websockets/websocket_extension.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 
     12 namespace net {
     13 
     14 namespace {
     15 
     16 TEST(WebSocketExtensionParserTest, ParseEmpty) {
     17   WebSocketExtensionParser parser;
     18   parser.Parse("", 0);
     19 
     20   EXPECT_TRUE(parser.has_error());
     21 }
     22 
     23 TEST(WebSocketExtensionParserTest, ParseSimple) {
     24   WebSocketExtensionParser parser;
     25   WebSocketExtension expected("foo");
     26 
     27   parser.Parse("foo");
     28 
     29   ASSERT_FALSE(parser.has_error());
     30   EXPECT_TRUE(expected.Equals(parser.extension()));
     31 }
     32 
     33 TEST(WebSocketExtensionParserTest, ParseOneExtensionWithOneParamWithoutValue) {
     34   WebSocketExtensionParser parser;
     35   WebSocketExtension expected("foo");
     36   expected.Add(WebSocketExtension::Parameter("bar"));
     37 
     38   parser.Parse("\tfoo ; bar");
     39 
     40   ASSERT_FALSE(parser.has_error());
     41   EXPECT_TRUE(expected.Equals(parser.extension()));
     42 }
     43 
     44 TEST(WebSocketExtensionParserTest, ParseOneExtensionWithOneParamWithValue) {
     45   WebSocketExtensionParser parser;
     46   WebSocketExtension expected("foo");
     47   expected.Add(WebSocketExtension::Parameter("bar", "baz"));
     48 
     49   parser.Parse("foo ; bar= baz\t");
     50 
     51   ASSERT_FALSE(parser.has_error());
     52   EXPECT_TRUE(expected.Equals(parser.extension()));
     53 }
     54 
     55 TEST(WebSocketExtensionParserTest, ParseOneExtensionWithParams) {
     56   WebSocketExtensionParser parser;
     57   WebSocketExtension expected("foo");
     58   expected.Add(WebSocketExtension::Parameter("bar", "baz"));
     59   expected.Add(WebSocketExtension::Parameter("hoge", "fuga"));
     60 
     61   parser.Parse("foo ; bar= baz;\t \thoge\t\t=fuga");
     62 
     63   ASSERT_FALSE(parser.has_error());
     64   EXPECT_TRUE(expected.Equals(parser.extension()));
     65 }
     66 
     67 TEST(WebSocketExtensionParserTest, InvalidPatterns) {
     68   const char* patterns[] = {
     69     "fo\ao",  // control in extension name
     70     "fo\x01o",  // control in extension name
     71     "fo<o",  // separator in extension name
     72     "foo/",  // separator in extension name
     73     ";bar",  // empty extension name
     74     "foo bar",  // missing ';'
     75     "foo;",  // extension parameter without name and value
     76     "foo; b\ar",  // control in parameter name
     77     "foo; b\x7fr",  // control in parameter name
     78     "foo; b[r",  // separator in parameter name
     79     "foo; ba:",  // separator in parameter name
     80     "foo; =baz",  // empty parameter name
     81     "foo; bar=",  // empty parameter value
     82     "foo; =",  // empty parameter name and value
     83     "foo; bar=b\x02z",  // control in parameter value
     84     "foo; bar=b@z",  // separator in parameter value
     85     "foo; bar=b\\z",  // separator in parameter value
     86     "foo; bar=b?z",  // separator in parameter value
     87     "\"foo\"",  // quoted extension name
     88     "foo; \"bar\"",  // quoted parameter name
     89     "foo; bar=\"\a2\"",  // control in quoted parameter value
     90     "foo; bar=\"b@z\"",  // separator in quoted parameter value
     91     "foo; bar=\"b\\\\z\"",  // separator in quoted parameter value
     92     "foo; bar=\"\"",  // quoted empty parameter value
     93     "foo; bar=\"baz",  // unterminated quoted string
     94     "foo; bar=\"baz \"",  // space in quoted string
     95     "foo; bar baz",  // mising '='
     96     "foo; bar - baz",  // '-' instead of '=' (note: "foo; bar-baz" is valid).
     97     "foo; bar=\r\nbaz",  // CRNL not followed by a space
     98     "foo; bar=\r\n baz",  // CRNL followed by a space
     99     "foo, bar"  // multiple extensions
    100   };
    101 
    102   for (size_t i = 0; i < arraysize(patterns); ++i) {
    103     WebSocketExtensionParser parser;
    104     parser.Parse(patterns[i]);
    105     EXPECT_TRUE(parser.has_error());
    106   }
    107 }
    108 
    109 TEST(WebSocketExtensionParserTest, QuotedParameterValue) {
    110   WebSocketExtensionParser parser;
    111   WebSocketExtension expected("foo");
    112   expected.Add(WebSocketExtension::Parameter("bar", "baz"));
    113 
    114   parser.Parse("foo; bar = \"ba\\z\" ");
    115 
    116   ASSERT_FALSE(parser.has_error());
    117   EXPECT_TRUE(expected.Equals(parser.extension()));
    118 }
    119 
    120 }  // namespace
    121 
    122 }  // namespace net
    123