summaryrefslogtreecommitdiffstats
path: root/remoting/base/decompressor_zlib_unittest.cc
blob: d04a923f58ea5fc1807e8d7aa41e3eb4d8816da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <stdlib.h>

#include "remoting/base/compressor_zlib.h"
#include "remoting/base/decompressor_zlib.h"
#include "testing/gtest/include/gtest/gtest.h"

static void GenerateTestData(uint8* data, int size, int seed) {
  srand(seed);
  for (int i = 0; i < size; ++i)
    data[i] = rand() % 256;
}

// Keep compressing |input_data| into |output_data| until the last
// bytes is consumed.
//
// The compressed size is written to |compressed_size|.
static void Compress(remoting::Compressor* compressor,
                     const uint8* input_data, int input_size,
                     uint8* output_data, int output_size,
                     int* compressed_size) {
  *compressed_size = 0;
  while (true) {
    int consumed, written;
    bool ret = compressor->Process(input_data, input_size,
                                   output_data, output_size,
                                   &consumed, &written);
    input_data += consumed;
    input_size -= consumed;
    output_data += written;
    output_size -= written;
    *compressed_size += written;

    if (!ret)
      break;
  }
}

// The decompressed size is written to |decompressed_size|.
static void Decompress(remoting::Decompressor* decompressor,
                       const uint8* input_data, int input_size,
                       uint8* output_data, int output_size,
                       int* decompressed_size) {
  *decompressed_size = 0;
  while (true) {
    int consumed, written;
    bool ret = decompressor->Process(input_data, input_size,
                                     output_data, output_size,
                                     &consumed, &written);
    input_data += consumed;
    input_size -= consumed;
    output_data += written;
    output_size -= written;
    *decompressed_size += written;

    if (!ret)
      break;
  }
}

TEST(DecompressorZlibTest, CompressAndDecompress) {
  static const int kRawDataSize = 1024 * 128;
  static const int kCompressedDataSize = 2 * kRawDataSize;
  static const int kDecompressedDataSize = kRawDataSize;

  uint8 raw_data[kRawDataSize];
  uint8 compressed_data[kCompressedDataSize];
  uint8 decompressed_data[kDecompressedDataSize];

  // Generate the test data.g
  GenerateTestData(raw_data, kRawDataSize, 99);

  // Then use the compressor.
  remoting::CompressorZlib compressor;
  int compressed_size = 0;
  Compress(&compressor, raw_data, kRawDataSize,
           compressed_data, kCompressedDataSize,
           &compressed_size);

  // Then use the decompressor.
  remoting::DecompressorZlib decompressor;
  int decompressed_size = 0;
  Decompress(&decompressor, compressed_data, compressed_size,
             decompressed_data, kDecompressedDataSize,
             &decompressed_size);

  EXPECT_EQ(kRawDataSize, decompressed_size);
  EXPECT_EQ(0, memcmp(raw_data, decompressed_data, decompressed_size));
}

// Checks that zlib can work with a small output buffer by limiting
// number of bytes it gets from the input.
TEST(DecompressorZlibTest, SmallOutputBuffer) {
  static const int kRawDataSize = 1024 * 128;
  static const int kCompressedDataSize = 2 * kRawDataSize;

  uint8 raw_data[kRawDataSize];
  uint8 compressed_data[kCompressedDataSize];

  // Generate the test data.
  GenerateTestData(raw_data, kRawDataSize, 99);

  // Then use the compressor to compress.
  remoting::CompressorZlib compressor;
  int compressed_size = 0;
  Compress(&compressor, raw_data, kRawDataSize,
           compressed_data, kCompressedDataSize,
           &compressed_size);

  // Then use the decompressor. We decompress into a 1 byte buffer
  // every time.
  remoting::DecompressorZlib decompressor;
  uint8* input_data = compressed_data;
  int input_size = compressed_size;
  int decompressed_size = 0;
  while (true) {
    int consumed, written;
    uint8 output_data;
    bool ret = decompressor.Process(input_data, input_size,
                                    &output_data, 1,
                                    &consumed, &written);
    input_data += consumed;
    input_size -= consumed;

    // Expect that there's only one byte written.
    EXPECT_EQ(1, written);
    decompressed_size += written;

    if (!ret)
      break;
  }
  EXPECT_EQ(kRawDataSize, decompressed_size);
}