xref: /aosp_15_r20/external/tink/cc/core/cleartext_keyset_handle_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2017 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/cleartext_keyset_handle.h"
18 
19 #include <istream>
20 #include <memory>
21 #include <ostream>
22 #include <sstream>
23 #include <utility>
24 
25 #include "gtest/gtest.h"
26 #include "tink/binary_keyset_reader.h"
27 #include "tink/keyset_handle.h"
28 #include "tink/util/test_keyset_handle.h"
29 #include "tink/util/test_util.h"
30 #include "proto/tink.pb.h"
31 
32 using crypto::tink::test::AddRawKey;
33 using crypto::tink::test::AddTinkKey;
34 
35 using google::crypto::tink::KeyData;
36 using google::crypto::tink::Keyset;
37 using google::crypto::tink::KeyStatusType;
38 
39 
40 namespace crypto {
41 namespace tink {
42 namespace {
43 
44 class CleartextKeysetHandleTest : public ::testing::Test {
45  protected:
46 };
47 
TEST_F(CleartextKeysetHandleTest,testRead)48 TEST_F(CleartextKeysetHandleTest, testRead) {
49   Keyset keyset;
50   Keyset::Key key;
51   AddTinkKey("some_key_type", 42, key, KeyStatusType::ENABLED,
52              KeyData::SYMMETRIC, &keyset);
53   AddRawKey("some_other_key_type", 711, key, KeyStatusType::ENABLED,
54             KeyData::SYMMETRIC, &keyset);
55   keyset.set_primary_key_id(42);
56   {  // Reader that reads a valid keyset.
57     auto reader =
58         std::move(BinaryKeysetReader::New(keyset.SerializeAsString()).value());
59     auto result = CleartextKeysetHandle::Read(std::move(reader));
60     EXPECT_TRUE(result.ok()) << result.status();
61     auto handle = std::move(result.value());
62     EXPECT_EQ(keyset.SerializeAsString(),
63               TestKeysetHandle::GetKeyset(*handle).SerializeAsString());
64   }
65 
66   {  // Reader that fails upon read.
67     auto reader =
68         std::move(BinaryKeysetReader::New("invalid serialized keyset").value());
69     auto result = CleartextKeysetHandle::Read(std::move(reader));
70     EXPECT_FALSE(result.ok());
71     EXPECT_EQ(absl::StatusCode::kInvalidArgument, result.status().code());
72   }
73 }
74 
TEST_F(CleartextKeysetHandleTest,testWrite)75 TEST_F(CleartextKeysetHandleTest, testWrite) {
76   Keyset keyset;
77   Keyset::Key key;
78   AddTinkKey("some_key_type", 42, key, KeyStatusType::ENABLED,
79              KeyData::SYMMETRIC, &keyset);
80   AddRawKey("some_other_key_type", 711, key, KeyStatusType::ENABLED,
81             KeyData::SYMMETRIC, &keyset);
82   keyset.set_primary_key_id(42);
83 
84   auto handle = TestKeysetHandle::GetKeysetHandle(keyset);
85 
86   std::stringbuf buffer;
87   std::unique_ptr<std::ostream> destination_stream(new std::ostream(&buffer));
88   auto writer =
89       test::DummyKeysetWriter::New(std::move(destination_stream)).value();
90 
91   // Write a valid keyset.
92   EXPECT_EQ(CleartextKeysetHandle::Write(writer.get(), *(handle.get())),
93             util::OkStatus());
94 
95   // Null writer.
96   EXPECT_NE(CleartextKeysetHandle::Write(nullptr, *(handle.get())),
97             util::OkStatus());
98 }
99 
100 }  // namespace
101 }  // namespace tink
102 }  // namespace crypto
103