1 // Copyright 2022 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_parser.h"
18
19 #include <memory>
20 #include <optional>
21 #include <string_view>
22
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include "absl/memory/memory.h"
26 #include "absl/status/status.h"
27 #include "absl/types/optional.h"
28 #include "tink/insecure_secret_key_access.h"
29 #include "tink/internal/parser_index.h"
30 #include "tink/internal/serialization.h"
31 #include "tink/internal/serialization_test_util.h"
32 #include "tink/key.h"
33 #include "tink/parameters.h"
34 #include "tink/util/statusor.h"
35 #include "tink/util/test_matchers.h"
36
37 namespace crypto {
38 namespace tink {
39 namespace internal {
40 namespace {
41
42 using ::crypto::tink::test::IsOk;
43 using ::crypto::tink::test::StatusIs;
44 using ::testing::Eq;
45
TEST(KeyParserTest,Create)46 TEST(KeyParserTest, Create) {
47 std::unique_ptr<KeyParser> parser =
48 absl::make_unique<KeyParserImpl<NoIdSerialization, NoIdKey>>(
49 kNoIdTypeUrl, ParseNoIdKey);
50
51 EXPECT_THAT(parser->ObjectIdentifier(), Eq(kNoIdTypeUrl));
52 EXPECT_THAT(
53 parser->Index(),
54 Eq(ParserIndex::Create<NoIdSerialization>(kNoIdTypeUrl)));
55 }
56
TEST(KeyParserTest,ParseKey)57 TEST(KeyParserTest, ParseKey) {
58 std::unique_ptr<KeyParser> parser =
59 absl::make_unique<KeyParserImpl<NoIdSerialization, NoIdKey>>(
60 kNoIdTypeUrl, ParseNoIdKey);
61
62 NoIdSerialization serialization;
63 util::StatusOr<std::unique_ptr<Key>> key =
64 parser->ParseKey(serialization, InsecureSecretKeyAccess::Get());
65 ASSERT_THAT(key, IsOk());
66 EXPECT_THAT((*key)->GetIdRequirement(), Eq(absl::nullopt));
67 EXPECT_THAT((*key)->GetParameters(), Eq(NoIdParams()));
68 EXPECT_THAT(**key, Eq(NoIdKey()));
69 }
70
TEST(KeyParserTest,ParsePublicKeyNoAccessToken)71 TEST(KeyParserTest, ParsePublicKeyNoAccessToken) {
72 std::unique_ptr<KeyParser> parser =
73 absl::make_unique<KeyParserImpl<NoIdSerialization, NoIdKey>>(
74 kNoIdTypeUrl, ParseNoIdKey);
75
76 NoIdSerialization serialization;
77 util::StatusOr<std::unique_ptr<Key>> public_key =
78 parser->ParseKey(serialization, absl::nullopt);
79 ASSERT_THAT(public_key, IsOk());
80 EXPECT_THAT((*public_key)->GetIdRequirement(), Eq(absl::nullopt));
81 EXPECT_THAT((*public_key)->GetParameters(), Eq(NoIdParams()));
82 EXPECT_THAT(**public_key, Eq(NoIdKey()));
83 }
84
TEST(KeyParserTest,ParseKeyWithInvalidSerializationType)85 TEST(KeyParserTest, ParseKeyWithInvalidSerializationType) {
86 std::unique_ptr<KeyParser> parser =
87 absl::make_unique<KeyParserImpl<NoIdSerialization, NoIdKey>>(
88 "example_type_url", ParseNoIdKey);
89
90 IdKeySerialization serialization(/*id=*/123);
91 util::StatusOr<std::unique_ptr<Key>> key =
92 parser->ParseKey(serialization, InsecureSecretKeyAccess::Get());
93 ASSERT_THAT(key.status(), StatusIs(absl::StatusCode::kInvalidArgument));
94 }
95
TEST(KeyParserTest,ParseKeyWithInvalidObjectIdentifier)96 TEST(KeyParserTest, ParseKeyWithInvalidObjectIdentifier) {
97 std::unique_ptr<KeyParser> parser =
98 absl::make_unique<KeyParserImpl<NoIdSerialization, NoIdKey>>(
99 "mismatched_type_url", ParseNoIdKey);
100
101 NoIdSerialization serialization;
102 util::StatusOr<std::unique_ptr<Key>> key =
103 parser->ParseKey(serialization, InsecureSecretKeyAccess::Get());
104 ASSERT_THAT(key.status(), StatusIs(absl::StatusCode::kInvalidArgument));
105 }
106
107 } // namespace
108 } // namespace internal
109 } // namespace tink
110 } // namespace crypto
111