xref: /aosp_15_r20/external/tink/cc/internal/parser_index_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/parser_index.h"
18 
19 #include <string>
20 
21 #include "gmock/gmock.h"
22 #include "gtest/gtest.h"
23 #include "absl/strings/string_view.h"
24 #include "tink/internal/serialization.h"
25 
26 namespace crypto {
27 namespace tink {
28 namespace internal {
29 
30 using ::testing::Eq;
31 using ::testing::Not;
32 
33 class ExampleSerialization : public Serialization {
34  public:
ExampleSerialization(absl::string_view object_identifier)35   explicit ExampleSerialization(absl::string_view object_identifier)
36       : object_identifier_(object_identifier) {}
37 
ObjectIdentifier() const38   absl::string_view ObjectIdentifier() const override {
39     return object_identifier_;
40   }
41 
42  protected:
43   std::string object_identifier_;
44 };
45 
46 class DifferentSerialization : public ExampleSerialization {
47  public:
DifferentSerialization(absl::string_view object_identifier)48   explicit DifferentSerialization(absl::string_view object_identifier)
49       : ExampleSerialization(object_identifier) {}
50 };
51 
TEST(ParserIndex,CreateEquivalent)52 TEST(ParserIndex, CreateEquivalent) {
53   ASSERT_THAT(ParserIndex::Create<ExampleSerialization>("id"),
54               Eq(ParserIndex::Create<ExampleSerialization>("id")));
55   ASSERT_THAT(ParserIndex::Create<ExampleSerialization>("id"),
56               Eq(ParserIndex::Create(ExampleSerialization("id"))));
57   ASSERT_THAT(ParserIndex::Create(ExampleSerialization("id")),
58               Eq(ParserIndex::Create(ExampleSerialization("id"))));
59 }
60 
TEST(ParserIndex,CreateWithDifferentObjectIdentifier)61 TEST(ParserIndex, CreateWithDifferentObjectIdentifier) {
62   ASSERT_THAT(
63       ParserIndex::Create<ExampleSerialization>("id"),
64       Not(Eq(ParserIndex::Create<ExampleSerialization>("different id"))));
65   ASSERT_THAT(
66       ParserIndex::Create(ExampleSerialization("id")),
67       Not(Eq(ParserIndex::Create(ExampleSerialization("different id")))));
68 }
69 
TEST(ParserIndex,CreateWithDifferentSerializationType)70 TEST(ParserIndex, CreateWithDifferentSerializationType) {
71   ASSERT_THAT(ParserIndex::Create<ExampleSerialization>("id"),
72               Not(Eq(ParserIndex::Create<DifferentSerialization>("id"))));
73   ASSERT_THAT(ParserIndex::Create(ExampleSerialization("id")),
74               Not(Eq(ParserIndex::Create(DifferentSerialization("id")))));
75 }
76 
77 }  // namespace internal
78 }  // namespace tink
79 }  // namespace crypto
80