xref: /aosp_15_r20/external/tink/cc/internal/serialization_test_util_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2023 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/serialization_test_util.h"
18 
19 #include <string_view>
20 
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "absl/types/optional.h"
24 #include "tink/insecure_secret_key_access.h"
25 #include "tink/parameters.h"
26 #include "tink/util/statusor.h"
27 #include "tink/util/test_matchers.h"
28 
29 namespace crypto {
30 namespace tink {
31 namespace internal {
32 namespace {
33 
34 using ::crypto::tink::test::IsOkAndHolds;
35 using ::testing::Eq;
36 using ::testing::IsFalse;
37 using ::testing::IsTrue;
38 using ::testing::Not;
39 
TEST(SerializationTest,Create)40 TEST(SerializationTest, Create) {
41   EXPECT_THAT(BaseSerialization("base_type_url").ObjectIdentifier(),
42               Eq("base_type_url"));
43   EXPECT_THAT(NoIdSerialization().ObjectIdentifier(), Eq(kNoIdTypeUrl));
44   EXPECT_THAT(IdParamsSerialization().ObjectIdentifier(), Eq(kIdTypeUrl));
45 
46   IdKeySerialization id_key(123);
47   EXPECT_THAT(id_key.ObjectIdentifier(), Eq(kIdTypeUrl));
48   EXPECT_THAT(id_key.GetKeyId(), Eq(123));
49 }
50 
TEST(NoIdParamsTest,Create)51 TEST(NoIdParamsTest, Create) {
52   NoIdParams params;
53 
54   EXPECT_THAT(params.HasIdRequirement(), IsFalse());
55   EXPECT_THAT(params, Eq(NoIdParams()));
56   EXPECT_THAT(params, Not(Eq(IdParams())));
57 }
58 
TEST(NoIdParamsTest,ParseAndSerialize)59 TEST(NoIdParamsTest, ParseAndSerialize) {
60   EXPECT_THAT(ParseNoIdParams(NoIdSerialization()), IsOkAndHolds(NoIdParams()));
61   EXPECT_THAT(SerializeNoIdParams(NoIdParams()),
62               IsOkAndHolds(NoIdSerialization()));
63 }
64 
TEST(IdParamsTest,Create)65 TEST(IdParamsTest, Create) {
66   IdParams params;
67 
68   EXPECT_THAT(params.HasIdRequirement(), IsTrue());
69   EXPECT_THAT(params, Eq(IdParams()));
70   EXPECT_THAT(params, Not(Eq(NoIdParams())));
71 }
72 
TEST(IdParamsTest,ParseAndSerialize)73 TEST(IdParamsTest, ParseAndSerialize) {
74   EXPECT_THAT(ParseIdParams(IdParamsSerialization()), IsOkAndHolds(IdParams()));
75   EXPECT_THAT(SerializeIdParams(IdParams()),
76               IsOkAndHolds(IdParamsSerialization()));
77 }
78 
TEST(NoIdKeyTest,Create)79 TEST(NoIdKeyTest, Create) {
80   NoIdKey key;
81 
82   EXPECT_THAT(key.GetIdRequirement(), Eq(absl::nullopt));
83   EXPECT_THAT(key.GetParameters(), Eq(NoIdParams()));
84   EXPECT_THAT(key, Eq(NoIdKey()));
85   EXPECT_THAT(key, Not(Eq(IdKey(123))));
86 }
87 
TEST(NoIdKeyTest,ParseAndSerialize)88 TEST(NoIdKeyTest, ParseAndSerialize) {
89   EXPECT_THAT(ParseNoIdKey(NoIdSerialization(), InsecureSecretKeyAccess::Get()),
90               IsOkAndHolds(NoIdKey()));
91   EXPECT_THAT(SerializeNoIdKey(NoIdKey(), InsecureSecretKeyAccess::Get()),
92               IsOkAndHolds(NoIdSerialization()));
93 }
94 
TEST(IdKeyTest,Create)95 TEST(IdKeyTest, Create) {
96   IdKey key(123);
97 
98   EXPECT_THAT(key.GetIdRequirement(), Eq(123));
99   EXPECT_THAT(key.GetParameters(), Eq(IdParams()));
100   EXPECT_THAT(key, Eq(IdKey(123)));
101   EXPECT_THAT(key, Not(Eq(IdKey(456))));
102   EXPECT_THAT(key, Not(Eq(NoIdKey())));
103 }
104 
TEST(IdKeyTest,ParseAndSerialize)105 TEST(IdKeyTest, ParseAndSerialize) {
106   EXPECT_THAT(ParseIdKey(IdKeySerialization(123),
107                          InsecureSecretKeyAccess::Get()),
108               IsOkAndHolds(IdKey(123)));
109   EXPECT_THAT(SerializeIdKey(IdKey(123), InsecureSecretKeyAccess::Get()),
110               IsOkAndHolds(IdKeySerialization(123)));
111 }
112 
113 }  // namespace
114 }  // namespace internal
115 }  // namespace tink
116 }  // namespace crypto
117