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/proto_parameters_serialization.h"
18
19 #include <string>
20
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "tink/util/statusor.h"
24 #include "tink/util/test_matchers.h"
25 #include "proto/test_proto.pb.h"
26 #include "proto/tink.pb.h"
27
28 namespace crypto {
29 namespace tink {
30 namespace internal {
31
32 using ::crypto::tink::test::IsOk;
33 using ::google::crypto::tink::KeyTemplate;
34 using ::google::crypto::tink::OutputPrefixType;
35 using ::google::crypto::tink::TestProto;
36 using ::testing::Eq;
37 using ::testing::IsFalse;
38 using ::testing::IsTrue;
39
40 class ProtoParametersSerializationTest : public ::testing::Test {
41 protected:
Equals(ProtoParametersSerialization serialization,ProtoParametersSerialization other)42 bool Equals(ProtoParametersSerialization serialization,
43 ProtoParametersSerialization other) {
44 return serialization.EqualsWithPotentialFalseNegatives(other);
45 }
46 };
47
TEST_F(ProtoParametersSerializationTest,CreateFromIndividualComponents)48 TEST_F(ProtoParametersSerializationTest, CreateFromIndividualComponents) {
49 TestProto test_proto;
50 test_proto.set_num(12345);
51 util::StatusOr<ProtoParametersSerialization> serialization =
52 ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
53 test_proto.SerializeAsString());
54 ASSERT_THAT(serialization.status(), IsOk());
55
56 EXPECT_THAT(serialization->ObjectIdentifier(), "type_url");
57 EXPECT_THAT(serialization->GetKeyTemplate().type_url(), "type_url");
58 EXPECT_THAT(serialization->GetKeyTemplate().output_prefix_type(),
59 OutputPrefixType::RAW);
60 EXPECT_THAT(serialization->GetKeyTemplate().value(),
61 test_proto.SerializeAsString());
62 TestProto parsed_proto;
63 parsed_proto.ParseFromString(serialization->GetKeyTemplate().value());
64 EXPECT_THAT(parsed_proto.num(), Eq(12345));
65 }
66
TEST_F(ProtoParametersSerializationTest,CreateFromKeyTemplate)67 TEST_F(ProtoParametersSerializationTest, CreateFromKeyTemplate) {
68 TestProto test_proto;
69 test_proto.set_num(12345);
70 KeyTemplate key_template;
71 key_template.set_value(test_proto.SerializeAsString());
72 key_template.set_output_prefix_type(OutputPrefixType::TINK);
73 key_template.set_type_url("type_url");
74 util::StatusOr<ProtoParametersSerialization> serialization =
75 ProtoParametersSerialization::Create(key_template);
76 ASSERT_THAT(serialization.status(), IsOk());
77
78 EXPECT_THAT(serialization->ObjectIdentifier(), "type_url");
79 EXPECT_THAT(serialization->GetKeyTemplate().type_url(), "type_url");
80 EXPECT_THAT(serialization->GetKeyTemplate().output_prefix_type(),
81 OutputPrefixType::TINK);
82 EXPECT_THAT(serialization->GetKeyTemplate().value(),
83 test_proto.SerializeAsString());
84 TestProto parsed_proto;
85 parsed_proto.ParseFromString(serialization->GetKeyTemplate().value());
86 EXPECT_THAT(parsed_proto.num(), Eq(12345));
87 }
88
TEST_F(ProtoParametersSerializationTest,Equals)89 TEST_F(ProtoParametersSerializationTest, Equals) {
90 TestProto test_proto;
91 test_proto.set_num(12345);
92
93 util::StatusOr<ProtoParametersSerialization> serialization =
94 ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
95 test_proto.SerializeAsString());
96 ASSERT_THAT(serialization.status(), IsOk());
97
98 util::StatusOr<ProtoParametersSerialization> other_serialization =
99 ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
100 test_proto.SerializeAsString());
101 ASSERT_THAT(other_serialization.status(), IsOk());
102
103 EXPECT_THAT(Equals(*serialization, *other_serialization), IsTrue());
104 }
105
TEST_F(ProtoParametersSerializationTest,TypeUrlNotEqual)106 TEST_F(ProtoParametersSerializationTest, TypeUrlNotEqual) {
107 TestProto test_proto;
108 test_proto.set_num(12345);
109
110 util::StatusOr<ProtoParametersSerialization> serialization =
111 ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
112 test_proto.SerializeAsString());
113 ASSERT_THAT(serialization.status(), IsOk());
114
115
116 util::StatusOr<ProtoParametersSerialization> other_serialization =
117 ProtoParametersSerialization::Create("other_url", OutputPrefixType::RAW,
118 test_proto.SerializeAsString());
119 ASSERT_THAT(other_serialization.status(), IsOk());
120
121 EXPECT_THAT(Equals(*serialization, *other_serialization), IsFalse());
122 }
123
TEST_F(ProtoParametersSerializationTest,OutputPrefixTypeNotEqual)124 TEST_F(ProtoParametersSerializationTest, OutputPrefixTypeNotEqual) {
125 TestProto test_proto;
126 test_proto.set_num(12345);
127
128 util::StatusOr<ProtoParametersSerialization> serialization =
129 ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
130 test_proto.SerializeAsString());
131 ASSERT_THAT(serialization.status(), IsOk());
132
133 util::StatusOr<ProtoParametersSerialization> other_serialization =
134 ProtoParametersSerialization::Create("type_url", OutputPrefixType::TINK,
135 test_proto.SerializeAsString());
136 ASSERT_THAT(other_serialization.status(), IsOk());
137
138 EXPECT_THAT(Equals(*serialization, *other_serialization), IsFalse());
139 }
140
TEST_F(ProtoParametersSerializationTest,DifferentValueNotEqual)141 TEST_F(ProtoParametersSerializationTest, DifferentValueNotEqual) {
142 TestProto test_proto;
143 test_proto.set_num(12345);
144 TestProto other_proto;
145 other_proto.set_num(67890);
146
147 util::StatusOr<ProtoParametersSerialization> serialization =
148 ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
149 test_proto.SerializeAsString());
150 ASSERT_THAT(serialization.status(), IsOk());
151
152 util::StatusOr<ProtoParametersSerialization> other_serialization =
153 ProtoParametersSerialization::Create("type_url", OutputPrefixType::RAW,
154 other_proto.SerializeAsString());
155 ASSERT_THAT(other_serialization.status(), IsOk());
156
157 EXPECT_THAT(Equals(*serialization, *other_serialization), IsFalse());
158 }
159
160 } // namespace internal
161 } // namespace tink
162 } // namespace crypto
163