xref: /aosp_15_r20/external/tink/cc/internal/legacy_proto_parameters_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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/legacy_proto_parameters.h"
18 
19 #include <string>
20 
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "tink/internal/proto_parameters_serialization.h"
24 #include "tink/parameters.h"
25 #include "tink/util/statusor.h"
26 #include "tink/util/test_matchers.h"
27 #include "proto/test_proto.pb.h"
28 #include "proto/tink.pb.h"
29 
30 namespace crypto {
31 namespace tink {
32 namespace internal {
33 
34 using ::crypto::tink::test::IsOk;
35 using ::google::crypto::tink::OutputPrefixType;
36 using ::google::crypto::tink::TestProto;
37 using ::testing::IsFalse;
38 using ::testing::IsTrue;
39 
40 class LegacyProtoParametersTest : public ::testing::Test {
41  protected:
42   // Although this is a friend class, this utility function is necessary to
43   // access `ProtoParametersSerialization::EqualsWithPotentialFalseNegatives()`
44   // since the test fixtures are subclasses that would not have direct access.
Equals(ProtoParametersSerialization serialization,ProtoParametersSerialization other)45   bool Equals(ProtoParametersSerialization serialization,
46               ProtoParametersSerialization other) {
47     return serialization.EqualsWithPotentialFalseNegatives(other);
48   }
49 };
50 
TEST_F(LegacyProtoParametersTest,CreateWithIdRequirement)51 TEST_F(LegacyProtoParametersTest, CreateWithIdRequirement) {
52   TestProto test_proto;
53   test_proto.set_num(12345);
54   util::StatusOr<ProtoParametersSerialization> serialization =
55       ProtoParametersSerialization::Create("type_url", OutputPrefixType::TINK,
56                                            test_proto.SerializeAsString());
57   ASSERT_THAT(serialization.status(), IsOk());
58 
59   LegacyProtoParameters parameters(*serialization);
60 
61   EXPECT_THAT(parameters.HasIdRequirement(), IsTrue());
62   EXPECT_THAT(Equals(*serialization, parameters.Serialization()), IsTrue());
63 }
64 
TEST_F(LegacyProtoParametersTest,CreateWithoutIdRequirement)65 TEST_F(LegacyProtoParametersTest, CreateWithoutIdRequirement) {
66   TestProto test_proto;
67   test_proto.set_num(12345);
68   util::StatusOr<ProtoParametersSerialization> serialization =
69       ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
70                                            test_proto.SerializeAsString());
71   ASSERT_THAT(serialization.status(), IsOk());
72 
73   LegacyProtoParameters parameters(*serialization);
74 
75   EXPECT_THAT(parameters.HasIdRequirement(), IsFalse());
76   EXPECT_THAT(Equals(*serialization, parameters.Serialization()), IsTrue());
77 }
78 
TEST_F(LegacyProtoParametersTest,Equals)79 TEST_F(LegacyProtoParametersTest, Equals) {
80   TestProto test_proto;
81   test_proto.set_num(12345);
82 
83   util::StatusOr<ProtoParametersSerialization> serialization =
84       ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
85                                            test_proto.SerializeAsString());
86   ASSERT_THAT(serialization.status(), IsOk());
87 
88   util::StatusOr<ProtoParametersSerialization> other_serialization =
89       ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
90                                            test_proto.SerializeAsString());
91   ASSERT_THAT(other_serialization.status(), IsOk());
92 
93   LegacyProtoParameters parameters(*serialization);
94   LegacyProtoParameters other_parameters(*other_serialization);
95 
96   EXPECT_TRUE(parameters == other_parameters);
97   EXPECT_TRUE(other_parameters == parameters);
98   EXPECT_FALSE(parameters != other_parameters);
99   EXPECT_FALSE(other_parameters != parameters);
100 }
101 
TEST_F(LegacyProtoParametersTest,TypeUrlNotEqual)102 TEST_F(LegacyProtoParametersTest, TypeUrlNotEqual) {
103   TestProto test_proto;
104   test_proto.set_num(12345);
105 
106   util::StatusOr<ProtoParametersSerialization> serialization =
107       ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
108                                            test_proto.SerializeAsString());
109   ASSERT_THAT(serialization.status(), IsOk());
110 
111   util::StatusOr<ProtoParametersSerialization> other_serialization =
112       ProtoParametersSerialization::Create("other_type_url",
113                                            OutputPrefixType::RAW,
114                                            test_proto.SerializeAsString());
115   ASSERT_THAT(other_serialization.status(), IsOk());
116 
117   LegacyProtoParameters parameters(*serialization);
118   LegacyProtoParameters other_parameters(*other_serialization);
119 
120   EXPECT_TRUE(parameters != other_parameters);
121   EXPECT_TRUE(other_parameters != parameters);
122   EXPECT_FALSE(parameters == other_parameters);
123   EXPECT_FALSE(other_parameters == parameters);
124 }
125 
TEST_F(LegacyProtoParametersTest,OutputPrefixTypeNotEqual)126 TEST_F(LegacyProtoParametersTest, OutputPrefixTypeNotEqual) {
127   TestProto test_proto;
128   test_proto.set_num(12345);
129 
130   util::StatusOr<ProtoParametersSerialization> serialization =
131       ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
132                                            test_proto.SerializeAsString());
133   ASSERT_THAT(serialization.status(), IsOk());
134 
135   util::StatusOr<ProtoParametersSerialization> other_serialization =
136       ProtoParametersSerialization::Create("type_url", OutputPrefixType::TINK,
137                                            test_proto.SerializeAsString());
138   ASSERT_THAT(other_serialization.status(), IsOk());
139 
140   LegacyProtoParameters parameters(*serialization);
141   LegacyProtoParameters other_parameters(*other_serialization);
142 
143   EXPECT_TRUE(parameters != other_parameters);
144   EXPECT_TRUE(other_parameters != parameters);
145   EXPECT_FALSE(parameters == other_parameters);
146   EXPECT_FALSE(other_parameters == parameters);
147 }
148 
TEST_F(LegacyProtoParametersTest,DifferentValueNotEqual)149 TEST_F(LegacyProtoParametersTest, DifferentValueNotEqual) {
150   TestProto test_proto;
151   test_proto.set_num(12345);
152   TestProto other_proto;
153   other_proto.set_num(67890);
154 
155   util::StatusOr<ProtoParametersSerialization> serialization =
156       ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
157                                            test_proto.SerializeAsString());
158   ASSERT_THAT(serialization.status(), IsOk());
159 
160   util::StatusOr<ProtoParametersSerialization> other_serialization =
161       ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
162                                            other_proto.SerializeAsString());
163   ASSERT_THAT(other_serialization.status(), IsOk());
164 
165   LegacyProtoParameters parameters(*serialization);
166   LegacyProtoParameters other_parameters(*other_serialization);
167 
168   EXPECT_TRUE(parameters != other_parameters);
169   EXPECT_TRUE(other_parameters != parameters);
170   EXPECT_FALSE(parameters == other_parameters);
171   EXPECT_FALSE(other_parameters == parameters);
172 }
173 
174 }  // namespace internal
175 }  // namespace tink
176 }  // namespace crypto
177