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 
20 #include <gtest/gtest.h>
21 
22 #include <HadamardUtils.h>
23 
24 using namespace aidl::android::hardware::rebootescrow::hadamard;
25 
26 class HadamardTest : public testing::Test {};
27 
AddError(std::vector<uint8_t> * data)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             }
34         }
35     }
36 }
37 
TEST_F(HadamardTest,Decode_error_correction)38 TEST_F(HadamardTest, Decode_error_correction) {
39     constexpr auto iteration = 10;
40     for (int i = 0; i < iteration; i++) {
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()));
50     }
51 }
52