xref: /aosp_15_r20/external/tink/testing/cc/create_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 "create.h"
18 
19 #include <memory>
20 #include <ostream>
21 #include <sstream>
22 #include <string>
23 
24 #include "gmock/gmock.h"
25 #include "gtest/gtest.h"
26 #include "tink/aead/aead_config.h"
27 #include "tink/aead/aead_key_templates.h"
28 #include "tink/binary_keyset_writer.h"
29 #include "tink/keyset_handle.h"
30 #include "tink/mac.h"
31 
32 namespace tink_testing_api {
33 
34 namespace {
35 
36 using ::google::crypto::tink::KeyTemplate;
37 using ::testing::IsEmpty;
38 using ::testing::Not;
39 using ::testing::NotNull;
40 
ValidAeadKeyset()41 std::string ValidAeadKeyset() {
42   const KeyTemplate& key_template = crypto::tink::AeadKeyTemplates::Aes128Eax();
43   auto handle_result = crypto::tink::KeysetHandle::GenerateNew(key_template);
44   EXPECT_TRUE(handle_result.ok());
45   std::stringbuf keyset;
46   auto writer_result = crypto::tink::BinaryKeysetWriter::New(
47       absl::make_unique<std::ostream>(&keyset));
48   EXPECT_TRUE(writer_result.ok());
49 
50   auto status = crypto::tink::CleartextKeysetHandle::Write(
51       writer_result.value().get(), *handle_result.value());
52   EXPECT_TRUE(status.ok());
53   return keyset.str();
54 }
55 
56 class CreateTest : public ::testing::Test {
57  protected:
SetUpTestSuite()58   static void SetUpTestSuite() {
59     ASSERT_TRUE(crypto::tink::AeadConfig::Register().ok());
60   }
61 };
62 
TEST_F(CreateTest,RpcHelperSuccess)63 TEST_F(CreateTest, RpcHelperSuccess) {
64   std::string keyset = ValidAeadKeyset();
65   CreationRequest request;
66   request.mutable_annotated_keyset()->set_serialized_keyset(keyset);
67   CreationResponse response;
68 
69   EXPECT_TRUE(
70       CreatePrimitiveForRpc<crypto::tink::Aead>(&request, &response)
71           .ok());
72   EXPECT_THAT(response.err(), IsEmpty());
73 }
74 
TEST_F(CreateTest,RpcHelperWrongPrimitiveFails)75 TEST_F(CreateTest, RpcHelperWrongPrimitiveFails) {
76   std::string keyset = ValidAeadKeyset();
77   CreationRequest request;
78   request.mutable_annotated_keyset()->set_serialized_keyset(keyset);
79   CreationResponse response;
80   EXPECT_TRUE(
81       CreatePrimitiveForRpc<crypto::tink::Mac>(&request, &response).ok());
82   EXPECT_THAT(response.err(), Not(IsEmpty()));
83 }
84 
TEST_F(CreateTest,PrimitiveCreationWorks)85 TEST_F(CreateTest, PrimitiveCreationWorks) {
86   AnnotatedKeyset annotated_keyset;
87   annotated_keyset.set_serialized_keyset(ValidAeadKeyset());
88   crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::Aead>> aead =
89       PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::Aead>(
90           annotated_keyset);
91   ASSERT_TRUE(aead.status().ok()) << aead.status();
92   EXPECT_THAT(*aead, NotNull());
93 }
94 
TEST_F(CreateTest,PrimitiveCreationWrongPrimitiveFails)95 TEST_F(CreateTest, PrimitiveCreationWrongPrimitiveFails) {
96   AnnotatedKeyset annotated_keyset;
97   annotated_keyset.set_serialized_keyset(ValidAeadKeyset());
98   crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::Mac>> aead =
99       PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::Mac>(
100           annotated_keyset);
101   ASSERT_FALSE(aead.status().ok());
102 }
103 
TEST_F(CreateTest,PrimitiveWithAnnotationsWorks)104 TEST_F(CreateTest, PrimitiveWithAnnotationsWorks) {
105   AnnotatedKeyset annotated_keyset;
106   annotated_keyset.set_serialized_keyset(ValidAeadKeyset());
107   annotated_keyset.mutable_annotations()->insert({"key1", "value1"});
108   crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::Aead>> aead =
109       PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::Aead>(
110           annotated_keyset);
111   ASSERT_TRUE(aead.status().ok());
112   EXPECT_THAT(*aead, NotNull());
113 }
114 
115 }  // namespace
116 
117 }  // namespace tink_testing_api
118