blob: 1c9a2fb295f65d3966a381819e100fb89a3d5734 [file] [log] [blame]
Tianjie Xua0a12cf2019-12-05 21:50:22 -08001/*
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 Xua0a12cf2019-12-05 21:50:22 -080020#include <gtest/gtest.h>
21
22#include <HadamardUtils.h>
23
24using namespace aidl::android::hardware::rebootescrow::hadamard;
25
Paul Crowleyc675b182019-12-18 16:09:24 -080026class HadamardTest : public testing::Test {};
Tianjie Xua0a12cf2019-12-05 21:50:22 -080027
Paul Crowleyc675b182019-12-18 16:09:24 -080028static 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 Xua0a12cf2019-12-05 21:50:22 -080034 }
35 }
Tianjie Xua0a12cf2019-12-05 21:50:22 -080036}
37
38TEST_F(HadamardTest, Decode_error_correction) {
39 constexpr auto iteration = 10;
40 for (int i = 0; i < iteration; i++) {
Paul Crowleyc675b182019-12-18 16:09:24 -080041 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 Xua0a12cf2019-12-05 21:50:22 -080050 }
51}