Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 <stdint.h> |
| 18 | #include <random> |
| 19 | |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | |
| 22 | #include <HadamardUtils.h> |
| 23 | |
| 24 | using namespace aidl::android::hardware::rebootescrow::hadamard; |
| 25 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 26 | class HadamardTest : public testing::Test {}; |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 27 | |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 28 | static void AddError(std::vector<uint8_t>* data) { |
| 29 | for (size_t i = 0; i < data->size(); i++) { |
| 30 | for (size_t j = 0; j < BYTE_LENGTH; j++) { |
| 31 | if (random() % 100 < 47) { |
| 32 | (*data)[i] ^= (1 << j); |
| 33 | } |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 34 | } |
| 35 | } |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 36 | } |
| 37 | |
| 38 | TEST_F(HadamardTest, Decode_error_correction) { |
| 39 | constexpr auto iteration = 10; |
| 40 | for (int i = 0; i < iteration; i++) { |
Paul Crowley | c675b18 | 2019-12-18 16:09:24 -0800 | [diff] [blame] | 41 | std::vector<uint8_t> key; |
| 42 | for (int j = 0; j < KEY_SIZE_IN_BYTES; j++) { |
| 43 | key.emplace_back(random() & 0xff); |
| 44 | } |
| 45 | auto encoded = EncodeKey(key); |
| 46 | ASSERT_EQ(64 * 1024, encoded.size()); |
| 47 | AddError(&encoded); |
| 48 | auto decoded = DecodeKey(encoded); |
| 49 | ASSERT_EQ(key, std::vector<uint8_t>(decoded.begin(), decoded.begin() + key.size())); |
Tianjie Xu | a0a12cf | 2019-12-05 21:50:22 -0800 | [diff] [blame] | 50 | } |
| 51 | } |