1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16
17 #include "tink/internal/key_info.h"
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "proto/tink.pb.h"
22
23 namespace crypto {
24 namespace tink {
25
26 namespace {
27
28 using ::testing::Eq;
29 using ::google::crypto::tink::Keyset;
30
TEST(KeyInfoFromKeyTest,Basic)31 TEST(KeyInfoFromKeyTest, Basic) {
32 Keyset::Key key;
33 key.set_key_id(1234);
34 key.set_output_prefix_type(google::crypto::tink::OutputPrefixType::TINK);
35 key.set_status(google::crypto::tink::ENABLED);
36 key.mutable_key_data()->set_type_url("MyTypeUrl");
37
38 EXPECT_THAT(KeyInfoFromKey(key).key_id(), Eq(1234));
39 EXPECT_THAT(KeyInfoFromKey(key).output_prefix_type(),
40 Eq(google::crypto::tink::OutputPrefixType::TINK));
41 EXPECT_THAT(KeyInfoFromKey(key).status(), Eq(google::crypto::tink::ENABLED));
42 EXPECT_THAT(KeyInfoFromKey(key).type_url(), Eq("MyTypeUrl"));
43 }
44
TEST(KeyInfoFromKeyTest,Status)45 TEST(KeyInfoFromKeyTest, Status) {
46 google::crypto::tink::Keyset::Key key;
47 key.set_status(google::crypto::tink::ENABLED);
48 EXPECT_THAT(KeyInfoFromKey(key).status(), Eq(google::crypto::tink::ENABLED));
49 key.set_status(google::crypto::tink::DISABLED);
50 EXPECT_THAT(KeyInfoFromKey(key).status(), Eq(google::crypto::tink::DISABLED));
51 }
52
TEST(KeyInfoFromKeyTest,OutputPrefixType)53 TEST(KeyInfoFromKeyTest, OutputPrefixType) {
54 google::crypto::tink::Keyset::Key key;
55 key.set_output_prefix_type(google::crypto::tink::OutputPrefixType::TINK);
56 EXPECT_THAT(KeyInfoFromKey(key).output_prefix_type(),
57 Eq(google::crypto::tink::OutputPrefixType::TINK));
58 key.set_output_prefix_type(google::crypto::tink::OutputPrefixType::CRUNCHY);
59 EXPECT_THAT(KeyInfoFromKey(key).output_prefix_type(),
60 Eq(google::crypto::tink::OutputPrefixType::CRUNCHY));
61 }
62
TEST(KeySetInfoForKeySetTest,Basic)63 TEST(KeySetInfoForKeySetTest, Basic) {
64 Keyset keyset;
65 keyset.set_primary_key_id(1234);
66 keyset.add_key()->set_key_id(1233);
67 keyset.add_key()->set_key_id(1234);
68 keyset.add_key()->set_key_id(1235);
69 EXPECT_THAT(KeysetInfoFromKeyset(keyset).primary_key_id(), Eq(1234));
70 EXPECT_THAT(KeysetInfoFromKeyset(keyset).key_info().size(), Eq(3));
71 EXPECT_THAT(KeysetInfoFromKeyset(keyset).key_info(0).key_id(), Eq(1233));
72 EXPECT_THAT(KeysetInfoFromKeyset(keyset).key_info(1).key_id(), Eq(1234));
73 EXPECT_THAT(KeysetInfoFromKeyset(keyset).key_info(2).key_id(), Eq(1235));
74 }
75
76 } // namespace
77 } // namespace tink
78 } // namespace crypto
79