1// Copyright (c) 2006-2008 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#if defined(USE_SYSTEM_LIBBZ2)
6#include <bzlib.h>
7#else
8#include "third_party/bzip2/bzlib.h"
9#endif
10
11#include "base/basictypes.h"
12#include "testing/gtest/include/gtest/gtest.h"
13
14namespace {
15  class Bzip2Test : public testing::Test {
16  };
17};
18
19// This test does a simple round trip to test that the bzip2 library is
20// present and working.
21TEST(Bzip2Test, Roundtrip) {
22  char input[] = "Test Data, More Test Data, Even More Data of Test";
23  char output[256];
24
25  memset(output, 0, arraysize(output));
26
27  bz_stream stream;
28  stream.bzalloc = NULL;
29  stream.bzfree = NULL;
30  stream.opaque = NULL;
31  int result = BZ2_bzCompressInit(&stream,
32                                  9,   // 900k block size
33                                  0,   // quiet
34                                  0);  // default work factor
35  ASSERT_EQ(BZ_OK, result);
36
37  stream.next_in = input;
38  stream.avail_in = arraysize(input) - 1;
39  stream.next_out = output;
40  stream.avail_out = arraysize(output);
41  do {
42    result = BZ2_bzCompress(&stream, BZ_FINISH);
43  } while (result == BZ_FINISH_OK);
44  ASSERT_EQ(BZ_STREAM_END, result);
45  result = BZ2_bzCompressEnd(&stream);
46  ASSERT_EQ(BZ_OK, result);
47  int written = stream.total_out_lo32;
48
49  // Make sure we wrote something; otherwise not sure what to expect
50  ASSERT_GT(written, 0);
51
52  // Now decompress and check that we got the same thing.
53  result = BZ2_bzDecompressInit(&stream, 0, 0);
54  ASSERT_EQ(BZ_OK, result);
55  char output2[256];
56  memset(output2, 0, arraysize(output2));
57
58  stream.next_in = output;
59  stream.avail_in = written;
60  stream.next_out = output2;
61  stream.avail_out = arraysize(output2);
62
63  do {
64    result = BZ2_bzDecompress(&stream);
65  } while (result == BZ_OK);
66  ASSERT_EQ(result, BZ_STREAM_END);
67  result = BZ2_bzDecompressEnd(&stream);
68  ASSERT_EQ(result, BZ_OK);
69
70  EXPECT_EQ(arraysize(input) - 1, stream.total_out_lo32);
71  EXPECT_STREQ(input, output2);
72}
73