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.
7 #include "remoting/base/compressor_zlib.h"
8 #include "remoting/base/decompressor_zlib.h"
9 #include "testing/gtest/include/gtest/gtest.h"
13 static void GenerateTestData(uint8
* data
, int size
, int seed
) {
15 for (int i
= 0; i
< size
; ++i
)
16 data
[i
] = rand() % 256;
19 // Keep compressing |input_data| into |output_data| until the last
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
) {
29 int consumed
, written
;
30 bool ret
= compressor
->Process(
31 input_data
, input_size
, output_data
, output_size
,
33 Compressor::CompressorFinish
: Compressor::CompressorNoFlush
,
35 input_data
+= consumed
;
36 input_size
-= consumed
;
37 output_data
+= written
;
38 output_size
-= written
;
39 *compressed_size
+= written
;
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;
53 int consumed
, written
;
54 bool ret
= decompressor
->Process(input_data
, input_size
,
55 output_data
, output_size
,
57 input_data
+= consumed
;
58 input_size
-= consumed
;
59 output_data
+= written
;
60 output_size
-= written
;
61 *decompressed_size
+= written
;
68 TEST(DecompressorZlibTest
, CompressAndDecompress
) {
69 static const int kRawDataSize
= 1024 * 128;
70 static const int kCompressedDataSize
= 2 * kRawDataSize
;
71 static const int kDecompressedDataSize
= kRawDataSize
;
73 uint8 raw_data
[kRawDataSize
];
74 uint8 compressed_data
[kCompressedDataSize
];
75 uint8 decompressed_data
[kDecompressedDataSize
];
77 // Generate the test data.g
78 GenerateTestData(raw_data
, kRawDataSize
, 99);
80 // Then use the compressor.
81 remoting::CompressorZlib compressor
;
82 int compressed_size
= 0;
83 Compress(&compressor
, raw_data
, kRawDataSize
,
84 compressed_data
, kCompressedDataSize
,
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
,
94 EXPECT_EQ(kRawDataSize
, decompressed_size
);
95 EXPECT_EQ(0, memcmp(raw_data
, decompressed_data
, decompressed_size
));
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
;
104 uint8 raw_data
[kRawDataSize
];
105 uint8 compressed_data
[kCompressedDataSize
];
107 // Generate the test data.
108 GenerateTestData(raw_data
, kRawDataSize
, 99);
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
,
117 // Then use the decompressor. We decompress into a 1 byte buffer
119 remoting::DecompressorZlib decompressor
;
120 uint8
* input_data
= compressed_data
;
121 int input_size
= compressed_size
;
122 int decompressed_size
= 0;
124 int consumed
, written
;
126 bool ret
= decompressor
.Process(input_data
, input_size
,
128 &consumed
, &written
);
129 input_data
+= consumed
;
130 input_size
-= consumed
;
132 // Expect that there's only one byte written.
133 EXPECT_EQ(1, written
);
134 decompressed_size
+= written
;
139 EXPECT_EQ(kRawDataSize
, decompressed_size
);
142 } // namespace remoting