aboutsummaryrefslogtreecommitdiff
path: root/src/varint_bigendian_test.cc
blob: 3debf512a8d9e6b039629579b25de4ce1735f462 (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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
// Copyright 2008 Google Inc.
// Author: Lincoln Smith
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <config.h>
#include "varint_bigendian.h"
#include <stdlib.h>  // rand, srand
#include <string.h>  // strlen
#include <string>
#include <vector>
#include "testing.h"

namespace open_vcdiff {
namespace {

class VarintBETestCommon : public testing::Test {
 protected:
  typedef std::string string;

  VarintBETestCommon()
      : varint_buf_(VarintBE<int64_t>::kMaxBytes),
        verify_encoded_byte_index_(0),
        verify_expected_length_(0),
        parse_data_ptr_(parse_data_all_FFs) {
  }

  virtual ~VarintBETestCommon() { }

  void ExpectEncodedByte(char expected_byte) {
    EXPECT_EQ(expected_byte, varint_buf_[verify_encoded_byte_index_]);
    EXPECT_EQ(expected_byte, s_[verify_encoded_byte_index_]);
    ++verify_encoded_byte_index_;
  }

  static const char parse_data_all_FFs[];
  static const char parse_data_CADA1[];

  std::vector<char> varint_buf_;
  string s_;
  int verify_encoded_byte_index_;
  int verify_expected_length_;
  const char* parse_data_ptr_;
};

template <typename SignedIntegerType>
class VarintBETestTemplate : public VarintBETestCommon {
 protected:
  VarintBETestTemplate() { }

  virtual ~VarintBETestTemplate() { }

  typedef SignedIntegerType SignedIntType;
  typedef VarintBE<SignedIntegerType> VarintType;

  void StartEncodingTest(SignedIntegerType v, int expected_length) {
    verify_expected_length_ = expected_length;
    EXPECT_EQ(expected_length, VarintType::Length(v));
    EXPECT_EQ(expected_length, VarintType::Encode(v, &varint_buf_[0]));
    VarintType::AppendToString(v, &s_);
    EXPECT_EQ(static_cast<size_t>(expected_length), s_.length());
  }

  void TestEncodeInvalid(SignedIntegerType v) {
    EXPECT_DEATH(VarintType::Length(v), "v >= 0");
    EXPECT_DEATH(VarintType::Encode(v, &varint_buf_[0]), "v >= 0");
    EXPECT_DEATH(VarintType::AppendToString(v, &s_), ">= 0");
  }

  // Need one function for each test type that will be applied to
  // multiple classes
  void TemplateTestDISABLED_EncodeNegative();
  void TemplateTestEncodeZero();
  void TemplateTestEncodeEightBits();
  void TemplateTestEncodeCADAD1A();
  void TemplateTestEncode32BitMaxInt();
  void TemplateTestEncodeDoesNotOverwriteExistingString();
  void TemplateTestParseNullPointer();
  void TemplateTestEndPointerPrecedesBeginning();
  void TemplateTestParseVarintTooLong();
  void TemplateTestParseZero();
  void TemplateTestParseCADA1();
  void TemplateTestParseEmpty();
  void TemplateTestParse123456789();
  void TemplateTestDecode31Bits();
  void TemplateTestEncodeDecodeRandom();
  void TemplateTestContinuationBytesPastEndOfInput();
};

typedef VarintBETestTemplate<int32_t> VarintBEInt32Test;
typedef VarintBETestTemplate<int64_t> VarintBEInt64Test;

#ifdef GTEST_HAS_DEATH_TEST
// These synonyms are needed for the tests that use ASSERT_DEATH
typedef VarintBEInt32Test VarintBEInt32DeathTest;
typedef VarintBEInt64Test VarintBEInt64DeathTest;
#endif  // GTEST_HAS_DEATH_TEST

const char VarintBETestCommon::parse_data_all_FFs[] =
    { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };

const char VarintBETestCommon::parse_data_CADA1[] =
    { 0xCA, 0xDA, 0x01 };

// A macro to allow defining tests once and having them run against
// both VarintBE<int32_t> and VarintBE<int64_t>.
//
#define TEMPLATE_TEST_F(TEST_TYPE, TEST_NAME) \
    TEST_F(VarintBEInt32##TEST_TYPE, TEST_NAME) { \
      TemplateTest##TEST_NAME(); \
    } \
    TEST_F(VarintBEInt64##TEST_TYPE, TEST_NAME) { \
      TemplateTest##TEST_NAME(); \
    } \
    template <class CacheType> \
    void VarintBETestTemplate<CacheType>::TemplateTest##TEST_NAME()

// Encoding tests: Length(), Encode(), AppendToString(), AppendToBuffer()

#ifdef GTEST_HAS_DEATH_TEST
// This test hangs for non-debug build (DeathTest threading problem)
TEMPLATE_TEST_F(DeathTest, DISABLED_EncodeNegative) {
  TestEncodeInvalid(-1);
}
#endif  // GTEST_HAS_DEATH_TEST

TEMPLATE_TEST_F(Test, EncodeZero) {
  StartEncodingTest(/* value */ 0x00, /* expected length */ 1);
  ExpectEncodedByte(0x00);
  EXPECT_EQ(verify_expected_length_, verify_encoded_byte_index_);
}

TEMPLATE_TEST_F(Test, EncodeEightBits) {
  StartEncodingTest(/* value */ 0xFF, /* expected length */ 2);
  ExpectEncodedByte(0x81);
  ExpectEncodedByte(0x7F);
  EXPECT_EQ(verify_expected_length_, verify_encoded_byte_index_);
}

TEMPLATE_TEST_F(Test, EncodeCADAD1A) {
  StartEncodingTest(/* value */ 0x0CADAD1A, /* expected length */ 4);
  ExpectEncodedByte(0xE5);
  ExpectEncodedByte(0xB6);
  ExpectEncodedByte(0xDA);
  ExpectEncodedByte(0x1A);
  EXPECT_EQ(verify_expected_length_, verify_encoded_byte_index_);
}

TEMPLATE_TEST_F(Test, Encode32BitMaxInt) {
  StartEncodingTest(/* value */ 0x7FFFFFFF, /* expected length */ 5);
  ExpectEncodedByte(0x87);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0x7F);
  EXPECT_EQ(verify_expected_length_, verify_encoded_byte_index_);
}

#ifdef GTEST_HAS_DEATH_TEST
// This test hangs for non-debug build (DeathTest threading problem)
TEST_F(VarintBEInt32DeathTest, DISABLED_Encode32BitsTooBig) {
  TestEncodeInvalid(0x80000000);
}
#endif  // GTEST_HAS_DEATH_TEST

TEST_F(VarintBEInt64Test, Encode32Bits) {
  StartEncodingTest(/* value */ 0x80000000, /* expected length */ 5);
  ExpectEncodedByte(0x88);
  ExpectEncodedByte(0x80);
  ExpectEncodedByte(0x80);
  ExpectEncodedByte(0x80);
  ExpectEncodedByte(0x00);
  EXPECT_EQ(verify_expected_length_, verify_encoded_byte_index_);
}

TEST_F(VarintBEInt64Test, Encode63Bits) {
  StartEncodingTest(/* value */ 0x7FFFFFFFFFFFFFFFULL, /* expected length */ 9);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0xFF);
  ExpectEncodedByte(0x7F);
  EXPECT_EQ(verify_expected_length_, verify_encoded_byte_index_);
}

#ifdef GTEST_HAS_DEATH_TEST
// This test hangs for non-debug build (DeathTest threading problem)
TEST_F(VarintBEInt64DeathTest, DISABLED_Encode64BitsTooBig) {
  TestEncodeInvalid(0x8000000000000000ULL);
}
#endif  // GTEST_HAS_DEATH_TEST

TEMPLATE_TEST_F(Test, EncodeDoesNotOverwriteExistingString) {
  s_.append("Test");
  VarintType::AppendToString('1', &s_);
  EXPECT_EQ(strlen("Test1"), s_.length());
  EXPECT_EQ("Test1", s_);
}

// Decoding tests: Parse(), ParseFromBuffer()

TEMPLATE_TEST_F(Test, ParseVarintTooLong) {
  EXPECT_EQ(RESULT_ERROR,
            VarintType::Parse(parse_data_ptr_ + VarintType::kMaxBytes,
                              &parse_data_ptr_));
}

TEST_F(VarintBEInt32Test, ParseFourFFs) {
  // For a 31-bit non-negative VarintBE, the sequence FF FF FF FF is invalid.
  // Even though the largest allowable 31-bit value occupies 5 bytes as a
  // Varint, it shouldn't have the highest bits set and so can't begin with FF.
  EXPECT_EQ(RESULT_ERROR, VarintType::Parse(parse_data_ptr_ + 4,
                                            &parse_data_ptr_));
}

TEST_F(VarintBEInt32Test, ParseThreeFFs) {
  EXPECT_EQ(RESULT_END_OF_DATA, VarintType::Parse(parse_data_ptr_ + 3,
                                                  &parse_data_ptr_));
}

TEST_F(VarintBEInt64Test, ParseEightFFs) {
  // For a 63-bit non-negative VarintBE, a series of eight FFs is valid, because
  // the largest allowable 63-bit value is expressed as eight FF bytes followed
  // by a 7F byte.  This is in contrast to the 32-bit case (see ParseFourFFs,
  // above.)
  EXPECT_EQ(RESULT_END_OF_DATA, VarintType::Parse(parse_data_ptr_ + 8,
                                                  &parse_data_ptr_));
}

TEMPLATE_TEST_F(Test, ParseZero) {
  const char zero_data[] = { 0x00 };
  parse_data_ptr_ = zero_data;
  EXPECT_EQ(0x00, VarintType::Parse(parse_data_ptr_ + 1, &parse_data_ptr_));
  EXPECT_EQ(zero_data + 1, parse_data_ptr_);
}

TEMPLATE_TEST_F(Test, ParseCADA1) {
  parse_data_ptr_ = parse_data_CADA1;
  EXPECT_EQ(0x12AD01,
            VarintType::Parse(parse_data_CADA1 + sizeof(parse_data_CADA1),
                              &parse_data_ptr_));
  EXPECT_EQ(parse_data_CADA1 + 3, parse_data_ptr_);
}

TEMPLATE_TEST_F(Test, ParseNullPointer) {
  parse_data_ptr_ = parse_data_CADA1;
  EXPECT_EQ(RESULT_ERROR,
            VarintType::Parse((const char*) NULL, &parse_data_ptr_));
}

TEMPLATE_TEST_F(Test, EndPointerPrecedesBeginning) {
  // This is not an error.
  parse_data_ptr_ = parse_data_CADA1;
  EXPECT_EQ(RESULT_END_OF_DATA,
            VarintType::Parse(parse_data_ptr_ - 1, &parse_data_ptr_));
}

TEMPLATE_TEST_F(Test, ParseEmpty) {
  EXPECT_EQ(RESULT_END_OF_DATA,
            VarintType::Parse(parse_data_ptr_, &parse_data_ptr_));
}

// This example is taken from the Varint description in RFC 3284, section 2.
TEMPLATE_TEST_F(Test, Parse123456789) {
  const char parse_data_123456789[] = { 0x80 + 58, 0x80 + 111, 0x80 + 26, 21 };
  parse_data_ptr_ = parse_data_123456789;
  EXPECT_EQ(123456789, VarintType::Parse(parse_data_123456789
                                             + sizeof(parse_data_123456789),
                                         &parse_data_ptr_));
}

TEMPLATE_TEST_F(Test, Decode31Bits) {
  const char parse_data_31_bits[] = { 0x87, 0xFF, 0xFF, 0xFF, 0x7F };
  parse_data_ptr_ = parse_data_31_bits;
  EXPECT_EQ(0x7FFFFFFF,
            VarintType::Parse(parse_data_31_bits + sizeof(parse_data_31_bits),
                              &parse_data_ptr_));
}

TEST_F(VarintBEInt32Test, Decode32Bits) {
  const char parse_data_32_bits[] = { 0x88, 0x80, 0x80, 0x80, 0x00 };
  parse_data_ptr_ = parse_data_32_bits;
  EXPECT_EQ(RESULT_ERROR,
            VarintType::Parse(parse_data_32_bits + sizeof(parse_data_32_bits),
                              &parse_data_ptr_));
}

TEST_F(VarintBEInt64Test, Decode32Bits) {
  const char parse_data_32_bits[] = { 0x88, 0x80, 0x80, 0x80, 0x00 };
  parse_data_ptr_ = parse_data_32_bits;
  EXPECT_EQ(0x80000000,
            VarintType::Parse(parse_data_32_bits + sizeof(parse_data_32_bits),
                              &parse_data_ptr_));
}

TEMPLATE_TEST_F(Test, EncodeDecodeRandom) {
  const int test_size = 1024;  // 1K random encode/decode operations
  char encode_buffer[VarintType::kMaxBytes];
  srand(1);
  for (int i = 0; i < test_size; ++i) {
    SignedIntType value = PortableRandomInRange(VarintType::kMaxVal);
    int length = VarintType::Encode(value, encode_buffer);
    EXPECT_EQ(length, VarintType::Length(value));
    const char* parse_pointer = encode_buffer;
    EXPECT_EQ(value, VarintType::Parse(encode_buffer + sizeof(encode_buffer),
                                       &parse_pointer));
    EXPECT_EQ(encode_buffer + length, parse_pointer);
  }
  for (int i = 0; i < test_size; ++i) {
    s_.clear();
    SignedIntType value = PortableRandomInRange(VarintType::kMaxVal);
    VarintType::AppendToString(value, &s_);
    const int varint_length = static_cast<int>(s_.length());
    EXPECT_EQ(VarintType::Length(value), varint_length);
    const char* parse_pointer = s_.c_str();
    const char* const buffer_end_pointer = s_.c_str() + s_.length();
    EXPECT_EQ(value, VarintType::Parse(buffer_end_pointer, &parse_pointer));
    EXPECT_EQ(buffer_end_pointer, parse_pointer);
  }
}

// If only 10 bytes of data are available, but there are 20 continuation
// bytes, Parse() should not read to the end of the continuation bytes.  It is
// legal (according to the RFC3284 spec) to use any number of continuation
// bytes, but they should not cause us to read past the end of available input.
TEMPLATE_TEST_F(Test, ContinuationBytesPastEndOfInput) {
  const char parse_data_20_continuations[] =
    { 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
      0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80,
      0x00 };
  parse_data_ptr_ = parse_data_20_continuations;
  EXPECT_EQ(RESULT_END_OF_DATA,
            VarintType::Parse(parse_data_20_continuations + 10,
                              &parse_data_ptr_));
}

}  // anonymous namespace
}  // namespace open_vcdiff