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/parameters_parser.h"
18
19 #include <memory>
20 #include <string_view>
21
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "absl/memory/memory.h"
25 #include "absl/status/status.h"
26 #include "tink/internal/parser_index.h"
27 #include "tink/internal/serialization.h"
28 #include "tink/internal/serialization_test_util.h"
29 #include "tink/parameters.h"
30 #include "tink/util/statusor.h"
31 #include "tink/util/test_matchers.h"
32
33 namespace crypto {
34 namespace tink {
35 namespace internal {
36 namespace {
37
38 using ::crypto::tink::test::IsOk;
39 using ::crypto::tink::test::StatusIs;
40 using ::testing::Eq;
41 using ::testing::IsFalse;
42
TEST(ParametersParserTest,Create)43 TEST(ParametersParserTest, Create) {
44 std::unique_ptr<ParametersParser> parser =
45 absl::make_unique<ParametersParserImpl<NoIdSerialization, NoIdParams>>(
46 kNoIdTypeUrl, ParseNoIdParams);
47
48 EXPECT_THAT(parser->ObjectIdentifier(), Eq(kNoIdTypeUrl));
49 EXPECT_THAT(parser->Index(),
50 Eq(ParserIndex::Create<NoIdSerialization>(kNoIdTypeUrl)));
51 }
52
TEST(ParametersParserTest,ParseParameters)53 TEST(ParametersParserTest, ParseParameters) {
54 std::unique_ptr<ParametersParser> parser =
55 absl::make_unique<ParametersParserImpl<NoIdSerialization, NoIdParams>>(
56 kNoIdTypeUrl, ParseNoIdParams);
57
58 NoIdSerialization serialization;
59 util::StatusOr<std::unique_ptr<Parameters>> params =
60 parser->ParseParameters(serialization);
61 ASSERT_THAT(params, IsOk());
62 EXPECT_THAT((*params)->HasIdRequirement(), IsFalse());
63 }
64
TEST(ParametersParserTest,ParseParametersWithInvalidSerializationType)65 TEST(ParametersParserTest, ParseParametersWithInvalidSerializationType) {
66 std::unique_ptr<ParametersParser> parser =
67 absl::make_unique<ParametersParserImpl<NoIdSerialization, NoIdParams>>(
68 kNoIdTypeUrl, ParseNoIdParams);
69
70 IdParamsSerialization serialization;
71 util::StatusOr<std::unique_ptr<Parameters>> params =
72 parser->ParseParameters(serialization);
73 ASSERT_THAT(params.status(), StatusIs(absl::StatusCode::kInvalidArgument));
74 }
75
TEST(ParametersParserTest,ParseParametersWithInvalidObjectIdentifier)76 TEST(ParametersParserTest, ParseParametersWithInvalidObjectIdentifier) {
77 std::unique_ptr<ParametersParser> parser =
78 absl::make_unique<ParametersParserImpl<NoIdSerialization, NoIdParams>>(
79 "mismatched_type_url", ParseNoIdParams);
80
81 IdParamsSerialization serialization;
82 util::StatusOr<std::unique_ptr<Parameters>> params =
83 parser->ParseParameters(serialization);
84 ASSERT_THAT(params.status(), StatusIs(absl::StatusCode::kInvalidArgument));
85 }
86
87 } // namespace
88 } // namespace internal
89 } // namespace tink
90 } // namespace crypto
91