xref: /aosp_15_r20/external/tink/cc/core/binary_keyset_writer_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2018 Google Inc.
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/binary_keyset_writer.h"
18 
19 #include <memory>
20 #include <ostream>
21 #include <sstream>
22 #include <string>
23 #include <utility>
24 
25 #include "gtest/gtest.h"
26 #include "tink/util/test_util.h"
27 #include "proto/tink.pb.h"
28 
29 using crypto::tink::test::AddRawKey;
30 using crypto::tink::test::AddTinkKey;
31 
32 using google::crypto::tink::EncryptedKeyset;
33 using google::crypto::tink::KeyData;
34 using google::crypto::tink::Keyset;
35 using google::crypto::tink::KeyStatusType;
36 
37 namespace crypto {
38 namespace tink {
39 namespace {
40 
41 class BinaryKeysetWriterTest : public ::testing::Test {
42  protected:
SetUp()43   void SetUp() override {
44     Keyset::Key key;
45     AddTinkKey("some key type", 42, key, KeyStatusType::ENABLED,
46                KeyData::SYMMETRIC, &keyset_);
47     AddRawKey("some other key type", 711, key, KeyStatusType::ENABLED,
48                KeyData::SYMMETRIC, &keyset_);
49     keyset_.set_primary_key_id(42);
50     binary_keyset_ = keyset_.SerializeAsString();
51 
52 
53     encrypted_keyset_.set_encrypted_keyset("some ciphertext with keyset");
54     auto keyset_info = encrypted_keyset_.mutable_keyset_info();
55     keyset_info->set_primary_key_id(42);
56     auto key_info = keyset_info->add_key_info();
57     key_info->set_type_url("some type_url");
58     key_info->set_key_id(42);
59     binary_encrypted_keyset_ = encrypted_keyset_.SerializeAsString();
60   }
61 
62   EncryptedKeyset encrypted_keyset_;
63   Keyset keyset_;
64   std::string binary_keyset_;
65   std::string binary_encrypted_keyset_;
66 };
67 
TEST_F(BinaryKeysetWriterTest,testWriterCreation)68 TEST_F(BinaryKeysetWriterTest, testWriterCreation) {
69   {  // Input stream is null.
70     std::unique_ptr<std::ostream> null_stream(nullptr);
71     auto writer_result = BinaryKeysetWriter::New(std::move(null_stream));
72     EXPECT_FALSE(writer_result.ok());
73     EXPECT_EQ(absl::StatusCode::kInvalidArgument,
74               writer_result.status().code());
75   }
76 
77   {  // Stream with good keyset.
78     std::unique_ptr<std::ostream> destination_stream(new std::stringstream());
79     auto writer_result = BinaryKeysetWriter::New(std::move(destination_stream));
80     EXPECT_TRUE(writer_result.ok()) << writer_result.status();
81   }
82 }
83 
TEST_F(BinaryKeysetWriterTest,testWriteKeyset)84 TEST_F(BinaryKeysetWriterTest, testWriteKeyset) {
85   std::stringbuf buffer;
86   std::unique_ptr<std::ostream> destination_stream(new std::ostream(&buffer));
87   auto writer_result = BinaryKeysetWriter::New(std::move(destination_stream));
88   ASSERT_TRUE(writer_result.ok()) << writer_result.status();
89   auto writer = std::move(writer_result.value());
90   auto status = writer->Write(keyset_);
91   EXPECT_TRUE(status.ok()) << status;
92   EXPECT_EQ(binary_keyset_, buffer.str());
93 }
94 
TEST_F(BinaryKeysetWriterTest,testWriteEncryptedKeyset)95 TEST_F(BinaryKeysetWriterTest, testWriteEncryptedKeyset) {
96   std::stringbuf buffer;
97   std::unique_ptr<std::ostream> destination_stream(new std::ostream(&buffer));
98   auto writer_result = BinaryKeysetWriter::New(std::move(destination_stream));
99   ASSERT_TRUE(writer_result.ok()) << writer_result.status();
100   auto writer = std::move(writer_result.value());
101   auto status = writer->Write(encrypted_keyset_);
102   EXPECT_TRUE(status.ok()) << status;
103   EXPECT_EQ(binary_encrypted_keyset_, buffer.str());
104 }
105 
TEST_F(BinaryKeysetWriterTest,testDestinationStreamErrors)106 TEST_F(BinaryKeysetWriterTest, testDestinationStreamErrors) {
107   std::stringbuf buffer;
108   std::unique_ptr<std::ostream> destination_stream(new std::ostream(&buffer));
109   destination_stream->setstate(std::ostream::badbit);
110   auto writer_result = BinaryKeysetWriter::New(std::move(destination_stream));
111   ASSERT_TRUE(writer_result.ok()) << writer_result.status();
112   auto writer = std::move(writer_result.value());
113   {  // Write keyset.
114     auto status = writer->Write(keyset_);
115     EXPECT_FALSE(status.ok()) << status;
116     EXPECT_EQ(absl::StatusCode::kUnknown, status.code());
117   }
118   {  // Write encrypted keyset.
119     auto status = writer->Write(encrypted_keyset_);
120     EXPECT_FALSE(status.ok()) << status;
121     EXPECT_EQ(absl::StatusCode::kUnknown, status.code());
122   }
123 }
124 
125 }  // namespace
126 }  // namespace tink
127 }  // namespace crypto
128