Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/xpcom/tests/gtest/TestSnappyStreams.cpp
Line
Count
Source (jump to first uncovered line)
1
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
3
/* This Source Code Form is subject to the terms of the Mozilla Public
4
 * License, v. 2.0. If a copy of the MPL was not distributed with this
5
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7
#include <algorithm>
8
#include "gtest/gtest.h"
9
#include "Helpers.h"
10
#include "mozilla/SnappyCompressOutputStream.h"
11
#include "mozilla/SnappyUncompressInputStream.h"
12
#include "nsIPipe.h"
13
#include "nsStreamUtils.h"
14
#include "nsString.h"
15
#include "nsStringStream.h"
16
#include "nsTArray.h"
17
18
namespace {
19
20
using mozilla::SnappyCompressOutputStream;
21
using mozilla::SnappyUncompressInputStream;
22
23
static already_AddRefed<nsIOutputStream>
24
CompressPipe(nsIInputStream** aReaderOut)
25
0
{
26
0
  nsCOMPtr<nsIOutputStream> pipeWriter;
27
0
28
0
  nsresult rv = NS_NewPipe(aReaderOut, getter_AddRefs(pipeWriter));
29
0
  if (NS_FAILED(rv)) { return nullptr; }
30
0
31
0
  nsCOMPtr<nsIOutputStream> compress =
32
0
    new SnappyCompressOutputStream(pipeWriter);
33
0
  return compress.forget();
34
0
}
35
36
// Verify the given number of bytes compresses to a smaller number of bytes.
37
static void TestCompress(uint32_t aNumBytes)
38
0
{
39
0
  // Don't permit this test on small data sizes as snappy can slightly
40
0
  // bloat very small content.
41
0
  ASSERT_GT(aNumBytes, 1024u);
42
0
43
0
  nsCOMPtr<nsIInputStream> pipeReader;
44
0
  nsCOMPtr<nsIOutputStream> compress = CompressPipe(getter_AddRefs(pipeReader));
45
0
  ASSERT_TRUE(compress);
46
0
47
0
  nsTArray<char> inputData;
48
0
  testing::CreateData(aNumBytes, inputData);
49
0
50
0
  testing::WriteAllAndClose(compress, inputData);
51
0
52
0
  nsAutoCString outputData;
53
0
  nsresult rv = NS_ConsumeStream(pipeReader, UINT32_MAX, outputData);
54
0
  ASSERT_TRUE(NS_SUCCEEDED(rv));
55
0
56
0
  ASSERT_LT(outputData.Length(), inputData.Length());
57
0
}
58
59
// Verify that the given number of bytes can be compressed and uncompressed
60
// successfully.
61
static void TestCompressUncompress(uint32_t aNumBytes)
62
0
{
63
0
  nsCOMPtr<nsIInputStream> pipeReader;
64
0
  nsCOMPtr<nsIOutputStream> compress = CompressPipe(getter_AddRefs(pipeReader));
65
0
  ASSERT_TRUE(compress);
66
0
67
0
  nsCOMPtr<nsIInputStream> uncompress =
68
0
    new SnappyUncompressInputStream(pipeReader);
69
0
70
0
  nsTArray<char> inputData;
71
0
  testing::CreateData(aNumBytes, inputData);
72
0
73
0
  testing::WriteAllAndClose(compress, inputData);
74
0
75
0
  nsAutoCString outputData;
76
0
  nsresult rv = NS_ConsumeStream(uncompress, UINT32_MAX, outputData);
77
0
  ASSERT_TRUE(NS_SUCCEEDED(rv));
78
0
79
0
  ASSERT_EQ(inputData.Length(), outputData.Length());
80
0
  for (uint32_t i = 0; i < inputData.Length(); ++i) {
81
0
    EXPECT_EQ(inputData[i], outputData.get()[i]) << "Byte " << i;
82
0
  }
83
0
}
84
85
static void TestUncompressCorrupt(const char* aCorruptData,
86
                                  uint32_t aCorruptLength)
87
0
{
88
0
  nsCOMPtr<nsIInputStream> source;
89
0
  nsresult rv = NS_NewByteInputStream(getter_AddRefs(source), aCorruptData,
90
0
                                      aCorruptLength);
91
0
  ASSERT_TRUE(NS_SUCCEEDED(rv));
92
0
93
0
  nsCOMPtr<nsIInputStream> uncompress =
94
0
    new SnappyUncompressInputStream(source);
95
0
96
0
  nsAutoCString outputData;
97
0
  rv = NS_ConsumeStream(uncompress, UINT32_MAX, outputData);
98
0
  ASSERT_EQ(NS_ERROR_CORRUPTED_CONTENT, rv);
99
0
}
100
101
} // namespace
102
103
TEST(SnappyStream, Compress_32k)
104
0
{
105
0
  TestCompress(32 * 1024);
106
0
}
107
108
TEST(SnappyStream, Compress_64k)
109
0
{
110
0
  TestCompress(64 * 1024);
111
0
}
112
113
TEST(SnappyStream, Compress_128k)
114
0
{
115
0
  TestCompress(128 * 1024);
116
0
}
117
118
TEST(SnappyStream, CompressUncompress_0)
119
0
{
120
0
  TestCompressUncompress(0);
121
0
}
122
123
TEST(SnappyStream, CompressUncompress_1)
124
0
{
125
0
  TestCompressUncompress(1);
126
0
}
127
128
TEST(SnappyStream, CompressUncompress_32)
129
0
{
130
0
  TestCompressUncompress(32);
131
0
}
132
133
TEST(SnappyStream, CompressUncompress_1k)
134
0
{
135
0
  TestCompressUncompress(1024);
136
0
}
137
138
TEST(SnappyStream, CompressUncompress_32k)
139
0
{
140
0
  TestCompressUncompress(32 * 1024);
141
0
}
142
143
TEST(SnappyStream, CompressUncompress_64k)
144
0
{
145
0
  TestCompressUncompress(64 * 1024);
146
0
}
147
148
TEST(SnappyStream, CompressUncompress_128k)
149
0
{
150
0
  TestCompressUncompress(128 * 1024);
151
0
}
152
153
// Test buffers that are not exactly power-of-2 in length to try to
154
// exercise more edge cases.  The number 13 is arbitrary.
155
156
TEST(SnappyStream, CompressUncompress_256k_less_13)
157
0
{
158
0
  TestCompressUncompress((256 * 1024) - 13);
159
0
}
160
161
TEST(SnappyStream, CompressUncompress_256k)
162
0
{
163
0
  TestCompressUncompress(256 * 1024);
164
0
}
165
166
TEST(SnappyStream, CompressUncompress_256k_plus_13)
167
0
{
168
0
  TestCompressUncompress((256 * 1024) + 13);
169
0
}
170
171
TEST(SnappyStream, UncompressCorruptStreamIdentifier)
172
0
{
173
0
  static const char data[] = "This is not a valid compressed stream";
174
0
  TestUncompressCorrupt(data, strlen(data));
175
0
}
176
177
TEST(SnappyStream, UncompressCorruptCompressedDataLength)
178
0
{
179
0
  static const char data[] = "\xff\x06\x00\x00sNaPpY" // stream identifier
180
0
                             "\x00\x99\x00\x00This is not a valid compressed stream";
181
0
  static const uint32_t dataLength = (sizeof(data) / sizeof(const char)) - 1;
182
0
  TestUncompressCorrupt(data, dataLength);
183
0
}
184
185
TEST(SnappyStream, UncompressCorruptCompressedDataContent)
186
0
{
187
0
  static const char data[] = "\xff\x06\x00\x00sNaPpY" // stream identifier
188
0
                             "\x00\x25\x00\x00This is not a valid compressed stream";
189
0
  static const uint32_t dataLength = (sizeof(data) / sizeof(const char)) - 1;
190
0
  TestUncompressCorrupt(data, dataLength);
191
0
}