• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 <keymasterV4_0/keymaster_utils.h>
20 
21 namespace keystore {
22 namespace test {
23 
24 using android::hardware::keymaster::V4_0::SecurityLevel;
25 using android::hardware::keymaster::V4_0::VerificationToken;
26 using android::hardware::keymaster::V4_0::support::deserializeVerificationToken;
27 using android::hardware::keymaster::V4_0::support::serializeVerificationToken;
28 using std::optional;
29 using std::vector;
30 
TEST(VerificationTokenSeralizationTest,SerializationTest)31 TEST(VerificationTokenSeralizationTest, SerializationTest) {
32     VerificationToken token;
33     token.challenge = 12345;
34     token.timestamp = 67890;
35     token.securityLevel = SecurityLevel::TRUSTED_ENVIRONMENT;
36     token.mac.resize(32);
37     for (size_t n = 0; n < 32; n++) {
38         token.mac[n] = n;
39     }
40     optional<vector<uint8_t>> serialized = serializeVerificationToken(token);
41     ASSERT_TRUE(serialized.has_value());
42     optional<VerificationToken> deserialized = deserializeVerificationToken(serialized.value());
43     ASSERT_TRUE(deserialized.has_value());
44     ASSERT_EQ(token.challenge, deserialized.value().challenge);
45     ASSERT_EQ(token.timestamp, deserialized.value().timestamp);
46     ASSERT_EQ(token.securityLevel, deserialized.value().securityLevel);
47     ASSERT_EQ(0u, deserialized.value().parametersVerified.size());
48     ASSERT_EQ(token.mac, deserialized.value().mac);
49 }
50 
TEST(VerificationTokenSeralizationTest,SerializationTestNoMac)51 TEST(VerificationTokenSeralizationTest, SerializationTestNoMac) {
52     VerificationToken token;
53     token.challenge = 12345;
54     token.timestamp = 67890;
55     token.securityLevel = SecurityLevel::TRUSTED_ENVIRONMENT;
56     token.mac.resize(0);
57     optional<vector<uint8_t>> serialized = serializeVerificationToken(token);
58     ASSERT_TRUE(serialized.has_value());
59     optional<VerificationToken> deserialized = deserializeVerificationToken(serialized.value());
60     ASSERT_TRUE(deserialized.has_value());
61     ASSERT_EQ(token.challenge, deserialized.value().challenge);
62     ASSERT_EQ(token.timestamp, deserialized.value().timestamp);
63     ASSERT_EQ(token.securityLevel, deserialized.value().securityLevel);
64     ASSERT_EQ(0u, deserialized.value().parametersVerified.size());
65     ASSERT_EQ(token.mac, deserialized.value().mac);
66 }
67 
68 }  // namespace test
69 }  // namespace keystore
70