Home | History | Annotate | Download | only in websockets
      1 // Copyright (c) 2011 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 "base/memory/scoped_ptr.h"
      6 #include "net/base/io_buffer.h"
      7 #include "net/websockets/websocket_frame_handler.h"
      8 
      9 #include "testing/gmock/include/gmock/gmock.h"
     10 #include "testing/gtest/include/gtest/gtest.h"
     11 #include "testing/platform_test.h"
     12 
     13 namespace net {
     14 
     15 TEST(WebSocketFrameHandlerTest, Basic) {
     16   const char kInputData[] = "\0hello, world\xff";
     17   const int kInputDataLen = sizeof(kInputData) - 1;  // no terminating NUL.
     18 
     19   scoped_ptr<WebSocketFrameHandler> handler(new WebSocketFrameHandler);
     20 
     21   // No data.
     22   EXPECT_EQ(0, handler->UpdateCurrentBuffer(true));
     23   EXPECT_TRUE(handler->GetCurrentBuffer() == NULL);
     24   EXPECT_EQ(0, handler->GetCurrentBufferSize());
     25 
     26   // WebKit sends data (WebSocketJob::SendData),
     27   // or data is received from network (WebSocketJob::OnReceivedData)
     28   handler->AppendData(kInputData, kInputDataLen);
     29   EXPECT_TRUE(handler->GetCurrentBuffer() == NULL);
     30   EXPECT_GT(handler->UpdateCurrentBuffer(true), 0);
     31   // Get data to send to the socket (send),
     32   // or to send to WebKit (receive).
     33   IOBuffer* buf = handler->GetCurrentBuffer();
     34   ASSERT_TRUE(buf != NULL);
     35   EXPECT_TRUE(memcmp(buf->data(), kInputData, kInputDataLen) == 0);
     36   EXPECT_EQ(kInputDataLen, handler->GetCurrentBufferSize());
     37   EXPECT_EQ(kInputDataLen, handler->GetOriginalBufferSize());
     38   // Data was sent. (WebSocketJob::OnSentData)
     39   buf = NULL;
     40   handler->ReleaseCurrentBuffer();
     41   EXPECT_TRUE(handler->GetCurrentBuffer() == NULL);
     42   EXPECT_EQ(0, handler->GetCurrentBufferSize());
     43   EXPECT_EQ(0, handler->UpdateCurrentBuffer(true));
     44 }
     45 
     46 TEST(WebSocketFrameHandlerTest, ParseFrame) {
     47   std::vector<WebSocketFrameHandler::FrameInfo> frames;
     48   const char kInputData[] = "\0hello, world\xff\xff\0";
     49   const int kInputDataLen = sizeof(kInputData) - 1;
     50   const int kHelloWorldFrameLen = 14;
     51 
     52   EXPECT_EQ(kInputDataLen,
     53             WebSocketFrameHandler::ParseWebSocketFrame(
     54                 kInputData, kInputDataLen, &frames));
     55   EXPECT_EQ(2UL, frames.size());
     56 
     57   EXPECT_EQ(kInputData, frames[0].frame_start);
     58   EXPECT_EQ(kHelloWorldFrameLen, frames[0].frame_length);
     59   EXPECT_EQ(kInputData + 1, frames[0].message_start);
     60   EXPECT_EQ(kHelloWorldFrameLen - 2, frames[0].message_length);
     61 
     62   EXPECT_EQ(kInputData + kHelloWorldFrameLen, frames[1].frame_start);
     63   EXPECT_EQ(2, frames[1].frame_length);
     64   EXPECT_EQ(0, frames[1].message_length);
     65 }
     66 
     67 TEST(WebSocketFrameHandlerTest, ParseFrameLength) {
     68   std::vector<WebSocketFrameHandler::FrameInfo> frames;
     69   const char kHelloWorldFrame[] = "\0hello, world\xff";
     70   const int kHelloWorldFrameLen = sizeof(kHelloWorldFrame) - 1;
     71   const char kLengthFrame[3 + 129] = "\x80\x81\x01\x01\0should be skipped\xff";
     72   const int kLengthFrameLen = sizeof(kLengthFrame);
     73   const int kInputDataLen = kHelloWorldFrameLen +
     74       kLengthFrameLen +
     75       kHelloWorldFrameLen;
     76   char inputData[kInputDataLen];
     77   memcpy(inputData, kHelloWorldFrame, kHelloWorldFrameLen);
     78   memcpy(inputData + kHelloWorldFrameLen, kLengthFrame, kLengthFrameLen);
     79   memcpy(inputData + kHelloWorldFrameLen + kLengthFrameLen,
     80          kHelloWorldFrame, kHelloWorldFrameLen);
     81 
     82   EXPECT_EQ(kInputDataLen,
     83             WebSocketFrameHandler::ParseWebSocketFrame(
     84                 inputData, kInputDataLen, &frames));
     85   ASSERT_EQ(3UL, frames.size());
     86 
     87   EXPECT_EQ(inputData, frames[0].frame_start);
     88   EXPECT_EQ(kHelloWorldFrameLen, frames[0].frame_length);
     89   EXPECT_EQ(inputData + 1, frames[0].message_start);
     90   EXPECT_EQ(kHelloWorldFrameLen - 2, frames[0].message_length);
     91 
     92   EXPECT_EQ(inputData + kHelloWorldFrameLen, frames[1].frame_start);
     93   EXPECT_EQ(kLengthFrameLen, frames[1].frame_length);
     94   EXPECT_EQ(inputData + kHelloWorldFrameLen + 3, frames[1].message_start);
     95   EXPECT_EQ(kLengthFrameLen - 3, frames[1].message_length);
     96 
     97   EXPECT_EQ(inputData + kHelloWorldFrameLen + kLengthFrameLen,
     98             frames[2].frame_start);
     99   EXPECT_EQ(kHelloWorldFrameLen, frames[2].frame_length);
    100   EXPECT_EQ(inputData + kHelloWorldFrameLen + kLengthFrameLen + 1,
    101             frames[2].message_start);
    102   EXPECT_EQ(kHelloWorldFrameLen - 2, frames[2].message_length);
    103 }
    104 
    105 TEST(WebSocketFrameHandlerTest, ParsePartialFrame) {
    106   std::vector<WebSocketFrameHandler::FrameInfo> frames;
    107   const char kInputData[] = "\0hello, world\xff"
    108       "\x80\x81\x01"  // skip 1*128+1 bytes.
    109       "\x01\xff"
    110       "\0should be skipped\xff";
    111   const int kInputDataLen = sizeof(kInputData) - 1;
    112   const int kHelloWorldFrameLen = 14;
    113 
    114   EXPECT_EQ(kHelloWorldFrameLen,
    115             WebSocketFrameHandler::ParseWebSocketFrame(
    116                 kInputData, kInputDataLen, &frames));
    117   ASSERT_EQ(1UL, frames.size());
    118 
    119   EXPECT_EQ(kInputData, frames[0].frame_start);
    120   EXPECT_EQ(kHelloWorldFrameLen, frames[0].frame_length);
    121   EXPECT_EQ(kInputData + 1, frames[0].message_start);
    122   EXPECT_EQ(kHelloWorldFrameLen - 2, frames[0].message_length);
    123 }
    124 
    125 }  // namespace net
    126