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_serializer.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/serialization.h"
27 #include "tink/internal/serialization_test_util.h"
28 #include "tink/internal/serializer_index.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
TEST(ParametersSerializerTest,Create)42 TEST(ParametersSerializerTest, Create) {
43 std::unique_ptr<ParametersSerializer> serializer = absl::make_unique<
44 ParametersSerializerImpl<NoIdParams, NoIdSerialization>>(
45 kNoIdTypeUrl, SerializeNoIdParams);
46
47 EXPECT_THAT(serializer->ObjectIdentifier(), Eq(kNoIdTypeUrl));
48 EXPECT_THAT(serializer->Index(),
49 Eq(SerializerIndex::Create<NoIdParams, NoIdSerialization>()));
50 }
51
TEST(ParametersSerializerTest,SerializeParameters)52 TEST(ParametersSerializerTest, SerializeParameters) {
53 std::unique_ptr<ParametersSerializer> serializer = absl::make_unique<
54 ParametersSerializerImpl<NoIdParams, NoIdSerialization>>(
55 kNoIdTypeUrl, SerializeNoIdParams);
56
57 NoIdParams parameters;
58 util::StatusOr<std::unique_ptr<Serialization>> serialization =
59 serializer->SerializeParameters(parameters);
60 ASSERT_THAT(serialization, IsOk());
61 EXPECT_THAT((*serialization)->ObjectIdentifier(), Eq(kNoIdTypeUrl));
62 }
63
TEST(ParametersSerializerTest,SerializeParametersWithInvalidParametersType)64 TEST(ParametersSerializerTest, SerializeParametersWithInvalidParametersType) {
65 std::unique_ptr<ParametersSerializer> serializer = absl::make_unique<
66 ParametersSerializerImpl<NoIdParams, NoIdSerialization>>(
67 kNoIdTypeUrl, SerializeNoIdParams);
68
69 IdParams parameters;
70 util::StatusOr<std::unique_ptr<Serialization>> serialization =
71 serializer->SerializeParameters(parameters);
72 ASSERT_THAT(serialization.status(),
73 StatusIs(absl::StatusCode::kInvalidArgument));
74 }
75
76 } // namespace
77 } // namespace internal
78 } // namespace tink
79 } // namespace crypto
80