OLD | NEW |
| (Empty) |
1 // Copyright (c) 2010 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 <stdlib.h> | |
6 | |
7 #include "remoting/base/compressor_zlib.h" | |
8 #include "remoting/base/decompressor_zlib.h" | |
9 #include "testing/gtest/include/gtest/gtest.h" | |
10 | |
11 namespace remoting { | |
12 | |
13 static void GenerateTestData(uint8* data, int size, int seed) { | |
14 srand(seed); | |
15 for (int i = 0; i < size; ++i) | |
16 data[i] = rand() % 256; | |
17 } | |
18 | |
19 // Keep compressing |input_data| into |output_data| until the last | |
20 // bytes is consumed. | |
21 // | |
22 // The compressed size is written to |compressed_size|. | |
23 static void Compress(remoting::Compressor* compressor, | |
24 const uint8* input_data, int input_size, | |
25 uint8* output_data, int output_size, | |
26 int* compressed_size) { | |
27 *compressed_size = 0; | |
28 while (true) { | |
29 int consumed, written; | |
30 bool ret = compressor->Process( | |
31 input_data, input_size, output_data, output_size, | |
32 input_size == 0 ? | |
33 Compressor::CompressorFinish : Compressor::CompressorNoFlush, | |
34 &consumed, &written); | |
35 input_data += consumed; | |
36 input_size -= consumed; | |
37 output_data += written; | |
38 output_size -= written; | |
39 *compressed_size += written; | |
40 | |
41 if (!ret) | |
42 break; | |
43 } | |
44 } | |
45 | |
46 // The decompressed size is written to |decompressed_size|. | |
47 static void Decompress(remoting::Decompressor* decompressor, | |
48 const uint8* input_data, int input_size, | |
49 uint8* output_data, int output_size, | |
50 int* decompressed_size) { | |
51 *decompressed_size = 0; | |
52 while (true) { | |
53 int consumed, written; | |
54 bool ret = decompressor->Process(input_data, input_size, | |
55 output_data, output_size, | |
56 &consumed, &written); | |
57 input_data += consumed; | |
58 input_size -= consumed; | |
59 output_data += written; | |
60 output_size -= written; | |
61 *decompressed_size += written; | |
62 | |
63 if (!ret) | |
64 break; | |
65 } | |
66 } | |
67 | |
68 TEST(DecompressorZlibTest, CompressAndDecompress) { | |
69 static const int kRawDataSize = 1024 * 128; | |
70 static const int kCompressedDataSize = 2 * kRawDataSize; | |
71 static const int kDecompressedDataSize = kRawDataSize; | |
72 | |
73 uint8 raw_data[kRawDataSize]; | |
74 uint8 compressed_data[kCompressedDataSize]; | |
75 uint8 decompressed_data[kDecompressedDataSize]; | |
76 | |
77 // Generate the test data.g | |
78 GenerateTestData(raw_data, kRawDataSize, 99); | |
79 | |
80 // Then use the compressor. | |
81 remoting::CompressorZlib compressor; | |
82 int compressed_size = 0; | |
83 Compress(&compressor, raw_data, kRawDataSize, | |
84 compressed_data, kCompressedDataSize, | |
85 &compressed_size); | |
86 | |
87 // Then use the decompressor. | |
88 remoting::DecompressorZlib decompressor; | |
89 int decompressed_size = 0; | |
90 Decompress(&decompressor, compressed_data, compressed_size, | |
91 decompressed_data, kDecompressedDataSize, | |
92 &decompressed_size); | |
93 | |
94 EXPECT_EQ(kRawDataSize, decompressed_size); | |
95 EXPECT_EQ(0, memcmp(raw_data, decompressed_data, decompressed_size)); | |
96 } | |
97 | |
98 // Checks that zlib can work with a small output buffer by limiting | |
99 // number of bytes it gets from the input. | |
100 TEST(DecompressorZlibTest, SmallOutputBuffer) { | |
101 static const int kRawDataSize = 1024 * 128; | |
102 static const int kCompressedDataSize = 2 * kRawDataSize; | |
103 | |
104 uint8 raw_data[kRawDataSize]; | |
105 uint8 compressed_data[kCompressedDataSize]; | |
106 | |
107 // Generate the test data. | |
108 GenerateTestData(raw_data, kRawDataSize, 99); | |
109 | |
110 // Then use the compressor to compress. | |
111 remoting::CompressorZlib compressor; | |
112 int compressed_size = 0; | |
113 Compress(&compressor, raw_data, kRawDataSize, | |
114 compressed_data, kCompressedDataSize, | |
115 &compressed_size); | |
116 | |
117 // Then use the decompressor. We decompress into a 1 byte buffer | |
118 // every time. | |
119 remoting::DecompressorZlib decompressor; | |
120 uint8* input_data = compressed_data; | |
121 int input_size = compressed_size; | |
122 int decompressed_size = 0; | |
123 while (true) { | |
124 int consumed, written; | |
125 uint8 output_data; | |
126 bool ret = decompressor.Process(input_data, input_size, | |
127 &output_data, 1, | |
128 &consumed, &written); | |
129 input_data += consumed; | |
130 input_size -= consumed; | |
131 | |
132 // Expect that there's only one byte written. | |
133 EXPECT_EQ(1, written); | |
134 decompressed_size += written; | |
135 | |
136 if (!ret) | |
137 break; | |
138 } | |
139 EXPECT_EQ(kRawDataSize, decompressed_size); | |
140 } | |
141 | |
142 } // namespace remoting | |
OLD | NEW |