Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 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_deflater.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/memory/ref_counted.h" | |
| 10 #include "net/base/io_buffer.h" | |
| 11 #include "testing/gtest/include/gtest/gtest.h" | |
| 12 | |
| 13 namespace net { | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 std::string ToString(IOBufferWithSize* buffer) { | |
| 18 return std::string(buffer->data(), buffer->size()); | |
| 19 } | |
| 20 | |
| 21 TEST(WebSocketDeflaterTest, Construct) { | |
| 22 WebSocketDeflater deflater(8, WebSocketDeflater::TAKE_OVER_CONTEXT); | |
| 23 ASSERT_EQ(0u, deflater.CurrentOutputSize()); | |
| 24 ASSERT_TRUE(deflater.Finish()); | |
| 25 scoped_refptr<IOBufferWithSize> actual = | |
| 26 deflater.GetOutput(deflater.CurrentOutputSize()); | |
| 27 EXPECT_EQ(std::string("\x02\00", 2), ToString(actual.get())); | |
| 28 ASSERT_EQ(0u, deflater.CurrentOutputSize()); | |
|
tyoshino (SeeGerritForStatus)
2013/09/09 06:44:13
please add a test that does
- add some non-empty d
yhirano
2013/09/09 08:02:34
Done.
| |
| 29 } | |
| 30 | |
| 31 TEST(WebSocketDeflaterTest, DeflateHelloTakeOverContext) { | |
| 32 WebSocketDeflater deflater(15, WebSocketDeflater::TAKE_OVER_CONTEXT); | |
| 33 scoped_refptr<IOBufferWithSize> actual1, actual2; | |
| 34 | |
| 35 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); | |
| 36 ASSERT_TRUE(deflater.Finish()); | |
| 37 actual1 = deflater.GetOutput(deflater.CurrentOutputSize()); | |
| 38 EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), | |
| 39 ToString(actual1.get())); | |
| 40 | |
| 41 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); | |
| 42 ASSERT_TRUE(deflater.Finish()); | |
| 43 actual2 = deflater.GetOutput(deflater.CurrentOutputSize()); | |
| 44 EXPECT_EQ(std::string("\xf2\x00\x11\x00\x00", 5), ToString(actual2.get())); | |
| 45 } | |
| 46 | |
| 47 TEST(WebSocketDeflaterTest, DeflateHelloDoNotTakeOverContext) { | |
| 48 WebSocketDeflater deflater(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); | |
| 49 scoped_refptr<IOBufferWithSize> actual1, actual2; | |
| 50 | |
| 51 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); | |
| 52 ASSERT_TRUE(deflater.Finish()); | |
| 53 actual1 = deflater.GetOutput(deflater.CurrentOutputSize()); | |
| 54 EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), | |
| 55 ToString(actual1.get())); | |
| 56 | |
| 57 ASSERT_TRUE(deflater.AddBytes("Hello", strlen("Hello"))); | |
| 58 ASSERT_TRUE(deflater.Finish()); | |
| 59 actual2 = deflater.GetOutput(deflater.CurrentOutputSize()); | |
| 60 EXPECT_EQ(std::string("\xf2\x48\xcd\xc9\xc9\x07\x00", 7), | |
| 61 ToString(actual2.get())); | |
| 62 } | |
| 63 | |
| 64 TEST(WebSocketDeflaterTest, MultipleAddBytesCalls) { | |
| 65 WebSocketDeflater deflater(15, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); | |
| 66 std::string input(32, 'a'); | |
| 67 scoped_refptr<IOBufferWithSize> actual; | |
| 68 | |
| 69 for (size_t i = 0; i < input.size(); ++i) { | |
| 70 ASSERT_TRUE(deflater.AddBytes(&input[i], 1)); | |
| 71 } | |
| 72 ASSERT_TRUE(deflater.Finish()); | |
| 73 actual = deflater.GetOutput(deflater.CurrentOutputSize()); | |
| 74 EXPECT_EQ(std::string("\x4a\x4c\xc4\x0f\x00\x00", 6), ToString(actual.get())); | |
| 75 } | |
| 76 | |
| 77 TEST(WebSocketDeflaterTest, WindowBits8) { | |
| 78 WebSocketDeflater deflater(8, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); | |
| 79 // Set the head and tail of |input| so that back-reference | |
| 80 // can be used if the window size is sufficiently-large. | |
| 81 const std::string word = "Chromium"; | |
| 82 std::string input = word + std::string(256, 'a') + word; | |
| 83 scoped_refptr<IOBufferWithSize> actual; | |
| 84 | |
| 85 ASSERT_TRUE(deflater.AddBytes(input.data(), input.size())); | |
| 86 ASSERT_TRUE(deflater.Finish()); | |
| 87 actual = deflater.GetOutput(deflater.CurrentOutputSize()); | |
| 88 EXPECT_EQ(std::string("r\xce(\xca\xcf\xcd,\xcdM\x1c\xe1\xc0\x39\xa3" | |
| 89 "(?7\xb3\x34\x17\x00", 21), | |
| 90 ToString(actual.get())); | |
| 91 } | |
| 92 | |
| 93 TEST(WebSocketDeflaterTest, WindowBits10) { | |
| 94 WebSocketDeflater deflater(10, WebSocketDeflater::DO_NOT_TAKE_OVER_CONTEXT); | |
| 95 // Set the head and tail of |input| so that back-reference | |
| 96 // can be used if the window size is sufficiently-large. | |
| 97 const std::string word = "Chromium"; | |
| 98 std::string input = word + std::string(256, 'a') + word; | |
| 99 scoped_refptr<IOBufferWithSize> actual; | |
| 100 | |
| 101 ASSERT_TRUE(deflater.AddBytes(input.data(), input.size())); | |
| 102 ASSERT_TRUE(deflater.Finish()); | |
| 103 actual = deflater.GetOutput(deflater.CurrentOutputSize()); | |
| 104 EXPECT_EQ( | |
| 105 std::string("r\xce(\xca\xcf\xcd,\xcdM\x1c\xe1\xc0\x19\x1a\x0e\0\0", 17), | |
| 106 ToString(actual.get())); | |
| 107 } | |
| 108 | |
| 109 } // namespace | |
| 110 | |
| 111 } // namespace net | |
| OLD | NEW |