1 /*
2 * Copyright (C) 2017 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 <gtest/gtest.h>
18
19 #include <include/ese/app/weaver.h>
20
21 #include <esecpp/NxpPn80tNqNci.h>
22 using EseInterfaceImpl = android::NxpPn80tNqNci;
23
24 const uint8_t KEY[kEseWeaverKeySize] = {
25 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16};
26 const uint8_t WRONG_KEY[kEseWeaverKeySize] = {0};
27 const uint8_t VALUE[kEseWeaverValueSize] = {
28 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1};
29
30 struct WeaverTest : public ::testing::Test {
31 EseInterfaceImpl mEse;
32 EseWeaverSession mSession;
33
SetUpWeaverTest34 virtual void SetUp() override {
35 mEse.init();
36 if (mEse.open() < 0) {
37 std::string errMsg = "Failed to open connection to eSE";
38 if (mEse.error()) {
39 errMsg += " (" + std::to_string(mEse.error_code()) + "): " + mEse.error_message();
40 } else {
41 errMsg += ": reason unknown";
42 }
43 FAIL() << errMsg;
44 }
45
46 ese_weaver_session_init(&mSession);
47 ASSERT_EQ(ESE_APP_RESULT_OK, ese_weaver_session_open(mEse.ese_interface(), &mSession));
48 }
49
TearDownWeaverTest50 virtual void TearDown() override {
51 ASSERT_EQ(ESE_APP_RESULT_OK, ese_weaver_session_close(&mSession));
52 mEse.close();
53 }
54 };
55
TEST_F(WeaverTest,getNumSlots)56 TEST_F(WeaverTest, getNumSlots) {
57 uint32_t numSlots;
58 ASSERT_EQ(ESE_APP_RESULT_OK, ese_weaver_get_num_slots(&mSession, &numSlots));
59 ASSERT_EQ(uint32_t{64}, numSlots);
60 }
61
TEST_F(WeaverTest,writeAndReadWithCorrectKey)62 TEST_F(WeaverTest, writeAndReadWithCorrectKey) {
63 const uint32_t slotId = 3;
64 ASSERT_EQ(ese_weaver_write(&mSession, slotId, KEY, VALUE), ESE_APP_RESULT_OK);
65
66 uint8_t readValue[kEseWeaverValueSize];
67 uint32_t timeout;
68 ASSERT_EQ(ESE_APP_RESULT_OK, ese_weaver_read(&mSession, slotId, KEY, readValue, &timeout));
69 ASSERT_EQ(0, memcmp(VALUE, readValue, kEseWeaverValueSize));
70 }
71
TEST_F(WeaverTest,writeAndReadWithIncorrectKey)72 TEST_F(WeaverTest, writeAndReadWithIncorrectKey) {
73 const uint32_t slotId = 7;
74 ASSERT_EQ(ese_weaver_write(&mSession, slotId, KEY, VALUE), ESE_APP_RESULT_OK);
75
76 uint8_t readValue[kEseWeaverValueSize];
77 uint32_t timeout;
78 ASSERT_EQ(ESE_WEAVER_READ_WRONG_KEY,
79 ese_weaver_read(&mSession, slotId, WRONG_KEY, readValue, &timeout));
80 ASSERT_NE(0, memcmp(VALUE, readValue, kEseWeaverValueSize));
81 ASSERT_EQ(uint32_t{0}, timeout); // First timeout is 0
82 }
83
TEST_F(WeaverTest,writeAndEraseValue)84 TEST_F(WeaverTest, writeAndEraseValue) {
85 const uint32_t slotId = 0;
86 ASSERT_EQ(ese_weaver_write(&mSession, slotId, KEY, VALUE), ESE_APP_RESULT_OK);
87 ASSERT_EQ(ese_weaver_erase_value(&mSession, slotId), ESE_APP_RESULT_OK);
88
89 // The read should be successful as the key is unchanged but the value should
90 // be all zeros
91 uint8_t readValue[kEseWeaverValueSize];
92 uint32_t timeout;
93 ASSERT_EQ(ESE_APP_RESULT_OK, ese_weaver_read(&mSession, slotId, KEY, readValue, &timeout));
94
95 const uint8_t expectedValue[kEseWeaverValueSize] = {0};
96 ASSERT_EQ(0, memcmp(readValue, expectedValue, kEseWeaverValueSize));
97 }
98