1 /*
2 * Copyright (C) 2014 The Android Open Source Project
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
17 #include "dictionary/utils/format_utils.h"
18
19 #include <gtest/gtest.h>
20
21 #include <vector>
22
23 #include "utils/byte_array_view.h"
24
25 namespace latinime {
26 namespace {
27
TEST(FormatUtilsTest,TestMagicNumber)28 TEST(FormatUtilsTest, TestMagicNumber) {
29 EXPECT_EQ(0x9BC13AFE, FormatUtils::MAGIC_NUMBER) << "Magic number must not be changed.";
30 }
31
getBuffer(const int magicNumber,const int version,const uint16_t flags,const size_t headerSize)32 const std::vector<uint8_t> getBuffer(const int magicNumber, const int version, const uint16_t flags,
33 const size_t headerSize) {
34 std::vector<uint8_t> buffer;
35 buffer.push_back(magicNumber >> 24);
36 buffer.push_back(magicNumber >> 16);
37 buffer.push_back(magicNumber >> 8);
38 buffer.push_back(magicNumber);
39
40 buffer.push_back(version >> 8);
41 buffer.push_back(version);
42
43 buffer.push_back(flags >> 8);
44 buffer.push_back(flags);
45
46 buffer.push_back(headerSize >> 24);
47 buffer.push_back(headerSize >> 16);
48 buffer.push_back(headerSize >> 8);
49 buffer.push_back(headerSize);
50 return buffer;
51 }
52
TEST(FormatUtilsTest,TestDetectFormatVersion)53 TEST(FormatUtilsTest, TestDetectFormatVersion) {
54 EXPECT_EQ(FormatUtils::UNKNOWN_VERSION,
55 FormatUtils::detectFormatVersion(ReadOnlyByteArrayView()));
56
57 {
58 const std::vector<uint8_t> buffer =
59 getBuffer(FormatUtils::MAGIC_NUMBER, FormatUtils::VERSION_2, 0, 0);
60 EXPECT_EQ(FormatUtils::UNKNOWN_VERSION, FormatUtils::detectFormatVersion(
61 ReadOnlyByteArrayView(buffer.data(), buffer.size())));
62 }
63 {
64 const std::vector<uint8_t> buffer =
65 getBuffer(FormatUtils::MAGIC_NUMBER, FormatUtils::VERSION_202, 0, 0);
66 EXPECT_EQ(FormatUtils::VERSION_202, FormatUtils::detectFormatVersion(
67 ReadOnlyByteArrayView(buffer.data(), buffer.size())));
68 }
69 {
70 const std::vector<uint8_t> buffer =
71 getBuffer(FormatUtils::MAGIC_NUMBER, FormatUtils::VERSION_402, 0, 0);
72 EXPECT_EQ(FormatUtils::VERSION_402, FormatUtils::detectFormatVersion(
73 ReadOnlyByteArrayView(buffer.data(), buffer.size())));
74 }
75 {
76 const std::vector<uint8_t> buffer =
77 getBuffer(FormatUtils::MAGIC_NUMBER, FormatUtils::VERSION_403, 0, 0);
78 EXPECT_EQ(FormatUtils::VERSION_403, FormatUtils::detectFormatVersion(
79 ReadOnlyByteArrayView(buffer.data(), buffer.size())));
80 }
81
82 {
83 const std::vector<uint8_t> buffer =
84 getBuffer(FormatUtils::MAGIC_NUMBER - 1, FormatUtils::VERSION_402, 0, 0);
85 EXPECT_EQ(FormatUtils::UNKNOWN_VERSION, FormatUtils::detectFormatVersion(
86 ReadOnlyByteArrayView(buffer.data(), buffer.size())));
87 }
88 {
89 const std::vector<uint8_t> buffer =
90 getBuffer(FormatUtils::MAGIC_NUMBER, 100, 0, 0);
91 EXPECT_EQ(FormatUtils::UNKNOWN_VERSION, FormatUtils::detectFormatVersion(
92 ReadOnlyByteArrayView(buffer.data(), buffer.size())));
93 }
94 {
95 const std::vector<uint8_t> buffer =
96 getBuffer(FormatUtils::MAGIC_NUMBER, FormatUtils::VERSION_402, 0, 0);
97 EXPECT_EQ(FormatUtils::UNKNOWN_VERSION, FormatUtils::detectFormatVersion(
98 ReadOnlyByteArrayView(buffer.data(), buffer.size() - 1)));
99 }
100 }
101
102 } // namespace
103 } // namespace latinime
104