• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2021 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "varint.h"
17 
18 #include <cstdint>
19 
20 #include <gtest/gtest.h>
21 
22 // A straightforward implementation of Length64 for testing
Varint_Length64Old(uint64_t v)23 inline int Varint_Length64Old(uint64_t v) {
24     // Each byte of output stores 7 bits of "v" until "v" becomes zero
25     int nbytes = 0;
26     do {
27         nbytes++;
28         v >>= 7;
29     } while (v != 0);
30     return nbytes;
31 }
32 
TEST(VarintTest,Length64)33 TEST(VarintTest, Length64) {
34     ASSERT_EQ(Varint::Length64(0), 1);
35     ASSERT_EQ(Varint::Length64(1), 1);
36     ASSERT_EQ(Varint::Length64(127), 1);
37     ASSERT_EQ(Varint::Length64(128), 2);
38     ASSERT_EQ(Varint::Length64(16383), 2);
39     ASSERT_EQ(Varint::Length64(16384), 3);
40     // Check around each power of two
41     for (int i = 0; i < 63; i++) {
42         uint64_t v = (1ull << i);
43         ASSERT_EQ(Varint::Length64(v - 1), Varint_Length64Old(v - 1));
44         ASSERT_EQ(Varint::Length64(v), Varint_Length64Old(v));
45         ASSERT_EQ(Varint::Length64(v + 1), Varint_Length64Old(v + 1));
46     }
47     ASSERT_EQ(Varint::Length64(((uint64_t)1 << 21) - 1), 3);
48     ASSERT_EQ(Varint::Length64((uint64_t)1 << 21), 4);
49     ASSERT_EQ(Varint::Length64((uint64_t)1 << 63), Varint::kMax64);
50     ASSERT_EQ(Varint::Length64(~0ull), Varint::kMax64);
51 }
52 
TEST(VarintTest,Encode32)53 TEST(VarintTest, Encode32) {
54     // Encode32 the 28-bit number 1110 0100 1001 1001 1000 0110 0111
55     // aka hex E 49 98 67
56     // which can be split in groups of 7 bytes as 1110010 0100110 0110000 1100111
57     // which should encode to (reverse the bytes, set the MSB to 1):
58     // 11100111 10110000 10100110 01110010 aka hex E7 B0 A6 72
59     char s[20];
60     uint32_t n = 0xe499867;
61     unsigned char n_encrypt[5] = {0xe7, 0xb0, 0xa6, 0x72, '\0'};
62     char* end_s = Varint::Encode32(s, n);
63     // now end_s - s represents the encryption length
64     ASSERT_EQ(end_s - s, Varint::Length64(n));
65     *end_s = '\0';  // terminate the string
66     ASSERT_EQ(std::string(s), std::string(reinterpret_cast<char*>(n_encrypt)));
67 }
68 
TEST(VarintTest,Encode64)69 TEST(VarintTest, Encode64) {
70     // Encode64 the 60-bit number
71     // 1110 0100 1001 1001 1000 0110 0111 1001 0100 0111 0000 1101 1001 1000 1101
72     // aka hex E 49 98 67 94 70 D9 8D
73     // which can be split in groups of 7 bytes as
74     // 1110 0100100 1100110 0001100 1111001 0100011 1000011 0110011 0001101
75     // which should encode to (reverse the bytes, set the MSB to 1):
76     // 10001101 10110011 11000011 10100011 11111001 10001100 11100110 10100100
77     // 00001110 aka hex 8D B3 C3 A3 F9 8C E6 A4 0E
78     char s[10];
79     uint64_t n = 0xe4998679470d98dull;
80     unsigned char n_encrypt[10] = {0x8d, 0xb3, 0xc3, 0xa3, 0xf9, 0x8c, 0xe6, 0xa4, 0x0e, '\0'};
81     char* end_s = Varint::Encode64(s, n);
82     // now end_s - s represents the encryption length
83     ASSERT_EQ(end_s - s, Varint::Length64(n));
84     *end_s = '\0';  // terminate the string
85     ASSERT_EQ(std::string(s), std::string(reinterpret_cast<char*>(n_encrypt)));
86 }
87