| 1 | /* |
| 2 | * Open Chinese Convert |
| 3 | * |
| 4 | * Copyright 2020-2026 Carbo Kuo and contributors |
| 5 | * |
| 6 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | * you may not use this file except in compliance with the License. |
| 8 | * You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, software |
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | * See the License for the specific language governing permissions and |
| 16 | * limitations under the License. |
| 17 | */ |
| 18 | |
| 19 | #include <cstdio> |
| 20 | #include <cstring> |
| 21 | |
| 22 | #include "SerializedValues.hpp" |
| 23 | #include "TextDictTestBase.hpp" |
| 24 | |
| 25 | namespace opencc { |
| 26 | |
| 27 | class SerializedValuesTest : public TextDictTestBase { |
| 28 | protected: |
| 29 | SerializedValuesTest() |
| 30 | : binDict(new SerializedValues(textDict->GetLexicon())), |
| 31 | fileName("dict.bin"){}; |
| 32 | |
| 33 | // Write a crafted binary file for testing malformed input handling. |
| 34 | // Format: uint32 numItems, uint32 valueTotalLength, char[] valueBuffer, |
| 35 | // then per item: uint16 numValues, per value: uint16 numValueBytes |
| 36 | static std::string WriteMalformedFile(uint32_t numItems, |
| 37 | uint32_t valueTotalLength, |
| 38 | const std::string& valueBuffer, |
| 39 | const std::vector<std::vector<uint16_t>>& |
| 40 | itemValueBytes) { |
| 41 | const std::string path = "malformed_test.bin"; |
| 42 | FILE* fp = fopen(path.c_str(), "wb"); |
| 43 | fwrite(&numItems, sizeof(uint32_t), 1, fp); |
| 44 | fwrite(&valueTotalLength, sizeof(uint32_t), 1, fp); |
| 45 | fwrite(valueBuffer.data(), sizeof(char), valueBuffer.size(), fp); |
| 46 | for (const auto& item : itemValueBytes) { |
| 47 | uint16_t numValues = static_cast<uint16_t>(item.size()); |
| 48 | fwrite(&numValues, sizeof(uint16_t), 1, fp); |
| 49 | for (uint16_t nb : item) { |
| 50 | fwrite(&nb, sizeof(uint16_t), 1, fp); |
| 51 | } |
| 52 | } |
| 53 | fclose(fp); |
| 54 | return path; |
| 55 | } |
| 56 | |
| 57 | const std::shared_ptr<SerializedValues> binDict; |
| 58 | const std::string fileName; |
| 59 | }; |
| 60 | |
| 61 | TEST_F(SerializedValuesTest, Serialization) { |
| 62 | binDict->opencc::SerializableDict::SerializeToFile(fileName); |
| 63 | } |
| 64 | |
| 65 | TEST_F(SerializedValuesTest, Deserialization) { |
| 66 | const std::shared_ptr<SerializedValues>& deserialized = |
| 67 | SerializableDict::NewFromFile<SerializedValues>(fileName); |
| 68 | const LexiconPtr& lex1 = binDict->GetLexicon(); |
| 69 | const LexiconPtr& lex2 = deserialized->GetLexicon(); |
| 70 | |
| 71 | // Compare every entry |
| 72 | EXPECT_EQ(lex1->Length(), lex2->Length()); |
| 73 | for (size_t i = 0; i < lex1->Length(); i++) { |
| 74 | EXPECT_EQ(lex1->At(i)->NumValues(), lex2->At(i)->NumValues()); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Test that numValueBytes exceeding the value buffer triggers InvalidFormat. |
| 79 | TEST_F(SerializedValuesTest, RejectsValueOffsetOutOfBounds) { |
| 80 | // Value buffer has 4 bytes: "ab\0\0", but we claim the single value is 10 |
| 81 | // bytes, which would read past the buffer end. |
| 82 | std::string valueBuf = {'a', 'b', '\0', '\0'}; |
| 83 | std::string path = |
| 84 | WriteMalformedFile(1, 4, valueBuf, {{10}}); |
| 85 | EXPECT_THROW(SerializableDict::NewFromFile<SerializedValues>(path), |
| 86 | InvalidFormat); |
| 87 | std::remove(path.c_str()); |
| 88 | } |
| 89 | |
| 90 | // Test that a value whose region is not null-terminated triggers InvalidFormat. |
| 91 | TEST_F(SerializedValuesTest, RejectsValueNotNullTerminated) { |
| 92 | // Value buffer has 4 bytes with no null terminator in the claimed range. |
| 93 | std::string valueBuf = {'a', 'b', 'c', '\0'}; |
| 94 | // Claim numValueBytes=3, so bytes [0..2] = "abc" with no null at index 2. |
| 95 | std::string path = |
| 96 | WriteMalformedFile(1, 4, valueBuf, {{3}}); |
| 97 | EXPECT_THROW(SerializableDict::NewFromFile<SerializedValues>(path), |
| 98 | InvalidFormat); |
| 99 | std::remove(path.c_str()); |
| 100 | } |
| 101 | |
| 102 | // Test that numValueBytes of zero triggers InvalidFormat. |
| 103 | TEST_F(SerializedValuesTest, RejectsZeroValueBytes) { |
| 104 | std::string valueBuf = {'a', '\0'}; |
| 105 | std::string path = |
| 106 | WriteMalformedFile(1, 2, valueBuf, {{0}}); |
| 107 | EXPECT_THROW(SerializableDict::NewFromFile<SerializedValues>(path), |
| 108 | InvalidFormat); |
| 109 | std::remove(path.c_str()); |
| 110 | } |
| 111 | |
| 112 | // Test that cumulative offsets exceeding buffer length are caught. |
| 113 | TEST_F(SerializedValuesTest, RejectsCumulativeOffsetOverflow) { |
| 114 | // Buffer has 6 bytes: two null-terminated strings "ab\0" and "cd\0". |
| 115 | // First value correctly uses 3 bytes, second claims 5 which overflows. |
| 116 | std::string valueBuf = {'a', 'b', '\0', 'c', 'd', '\0'}; |
| 117 | std::string path = |
| 118 | WriteMalformedFile(1, 6, valueBuf, {{3, 5}}); |
| 119 | EXPECT_THROW(SerializableDict::NewFromFile<SerializedValues>(path), |
| 120 | InvalidFormat); |
| 121 | std::remove(path.c_str()); |
| 122 | } |
| 123 | |
| 124 | // Sanity check: a well-formed crafted file deserializes successfully. |
| 125 | TEST_F(SerializedValuesTest, AcceptsWellFormedFile) { |
| 126 | // "ab\0" (3 bytes) + "cd\0" (3 bytes) = 6 byte buffer, two items each with |
| 127 | // one value. |
| 128 | std::string valueBuf = {'a', 'b', '\0', 'c', 'd', '\0'}; |
| 129 | std::string path = |
| 130 | WriteMalformedFile(2, 6, valueBuf, {{3}, {3}}); |
| 131 | const auto deserialized = |
| 132 | SerializableDict::NewFromFile<SerializedValues>(path); |
| 133 | EXPECT_EQ(deserialized->GetLexicon()->Length(), 2); |
| 134 | std::remove(path.c_str()); |
| 135 | } |
| 136 | |
| 137 | // Test that valueTotalLength exceeding file size triggers InvalidFormat (#812). |
| 138 | TEST_F(SerializedValuesTest, RejectsHugeValueTotalLength) { |
| 139 | // File only has a few bytes, but claims valueTotalLength = 0xFFFFFFFF. |
| 140 | std::string path = |
| 141 | WriteMalformedFile(1, 0xFFFFFFFF, "", {}); |
| 142 | EXPECT_THROW(SerializableDict::NewFromFile<SerializedValues>(path), |
| 143 | InvalidFormat); |
| 144 | std::remove(path.c_str()); |
| 145 | } |
| 146 | |
| 147 | } // namespace opencc |