1 /*
2 * Copyright 2016 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 <trusty_unittest.h>
18
19 #include "secure_storage_fake.h"
20
21 namespace avb {
22
TEST(AvbTest,BasicReadTest)23 TEST(AvbTest, BasicReadTest) {
24 uint32_t slot = 0;
25 uint64_t value = 0;
26 RollbackIndexRequest request(slot, value);
27 RollbackIndexResponse response;
28 AvbManager avb_manager(new SecureStorageFake);
29 avb_manager.ReadRollbackIndex(request, &response);
30 EXPECT_EQ(AvbError::kNone, response.get_error(), "Read failed");
31 EXPECT_EQ(1, response.get_value(), "Did not read expected value");
32 }
33
TEST(AvbTest,ValidWriteTest)34 TEST(AvbTest, ValidWriteTest) {
35 uint32_t slot = 0;
36 uint64_t value = 2; // Read always returns 1
37 RollbackIndexRequest request(slot, value);
38 RollbackIndexResponse response;
39 AvbManager avb_manager(new SecureStorageFake);
40 avb_manager.WriteRollbackIndex(request, &response);
41 EXPECT_EQ(AvbError::kNone, response.get_error(), "Write failed");
42 EXPECT_EQ(2, response.get_value(), "Did not write expected value");
43 }
44
TEST(AvbTest,InvalidWriteTest)45 TEST(AvbTest, InvalidWriteTest) {
46 uint32_t slot = 0;
47 uint64_t value = 0; // Read always returns 1
48 RollbackIndexRequest request(slot, value);
49 RollbackIndexResponse response;
50 AvbManager avb_manager(new SecureStorageFake);
51 avb_manager.WriteRollbackIndex(request, &response);
52 EXPECT_EQ(AvbError::kInvalid, response.get_error(),
53 "Allowed writing index value less than existing value");
54 EXPECT_EQ(1, response.get_value(),
55 "Did not read expected value after failed write");
56 }
57
TEST(AvbTest,SlotUpperBitsSetTest)58 TEST(AvbTest, SlotUpperBitsSetTest) {
59 uint32_t slot = 0x00010000;
60 uint64_t value = 0;
61 RollbackIndexRequest request(slot, value);
62 RollbackIndexResponse response;
63 AvbManager avb_manager(new SecureStorageFake);
64 avb_manager.ReadRollbackIndex(request, &response);
65 EXPECT_EQ(AvbError::kInvalid, response.get_error(),
66 "Slot was not rejected");
67 }
68
TEST(AvbTest,SlotMaxValueTest)69 TEST(AvbTest, SlotMaxValueTest) {
70 uint32_t slot = kRollbackSlotMax + 1;
71 uint64_t value = 0;
72 RollbackIndexRequest request(slot, value);
73 RollbackIndexResponse response;
74 AvbManager avb_manager(new SecureStorageFake);
75 avb_manager.ReadRollbackIndex(request, &response);
76 EXPECT_EQ(AvbError::kInvalid, response.get_error(),
77 "Failed to reject write to slot > max slot");
78 }
79
TEST(AvbTest,SlotFlagTest)80 TEST(AvbTest, SlotFlagTest) {
81 uint32_t slot = 0x0000f000;
82 uint64_t value = 0;
83 RollbackIndexRequest request(slot, value);
84 RollbackIndexResponse response;
85 AvbManager avb_manager(new SecureStorageFake);
86 avb_manager.ReadRollbackIndex(request, &response);
87 EXPECT_EQ(AvbError::kNone, response.get_error(),
88 "Could not validate 0xf flag");
89 }
90
91 } // namespace avb
92
main()93 int main() {
94 return RUN_ALL_TESTS() ? 0 : 1;
95 }
96