xref: /aosp_15_r20/external/tink/cc/util/secret_data_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2020 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/util/secret_data.h"
18 
19 #include <string>
20 #include <utility>
21 
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "absl/strings/string_view.h"
25 
26 namespace crypto {
27 namespace tink {
28 namespace util {
29 namespace {
30 
31 using ::testing::AnyOf;
32 using ::testing::ElementsAreArray;
33 using ::testing::Eq;
34 
35 
36 constexpr int kEightKb = 8192;
37 struct alignas(kEightKb) TwoMbAlignedStruct {
38   int data;
39 };
40 
41 // If we don't have __cpp_aligned_new we currently do not support types
42 // whose alginment requirement is greater than the default.
43 #ifdef __cpp_aligned_new
44 
TEST(SecretUniqueptrTest,Alignment)45 TEST(SecretUniqueptrTest, Alignment) {
46   SecretUniquePtr<TwoMbAlignedStruct> s =
47       MakeSecretUniquePtr<TwoMbAlignedStruct>();
48   EXPECT_THAT(reinterpret_cast<size_t>(s.get()) % kEightKb, Eq(0));
49 }
50 
51 #endif
52 
TEST(SecretDataTest,OneByOneInsertion)53 TEST(SecretDataTest, OneByOneInsertion) {
54   constexpr unsigned char kContents[] = {41, 42, 64, 12, 41, 0,
55                                          52, 56, 6,  12, 127, 13};
56   SecretData data;
57   for (unsigned char c : kContents) {
58     data.push_back(c);
59   }
60   EXPECT_THAT(data, ElementsAreArray(kContents));
61 }
62 
TEST(SecretDataTest,SecretDataFromStringViewConstructor)63 TEST(SecretDataTest, SecretDataFromStringViewConstructor) {
64   constexpr unsigned char kContents[] = {41, 42, 64, 12, 41,  0,
65                                          52, 56, 6,  12, 124, 16};
66   std::string s;
67   for (unsigned char c : kContents) {
68     s.push_back(c);
69   }
70   SecretData data = SecretDataFromStringView(s);
71   EXPECT_THAT(data, ElementsAreArray(kContents));
72 }
73 
TEST(SecretDataTest,StringViewFromSecretData)74 TEST(SecretDataTest, StringViewFromSecretData) {
75   constexpr unsigned char kContents[] = {41, 42, 64, 12, 41,  0,
76                                          52, 56, 6,  12, 124, 16};
77   std::string s;
78   for (unsigned char c : kContents) {
79     s.push_back(c);
80   }
81   SecretData data = SecretDataFromStringView(s);
82   absl::string_view data_view = SecretDataAsStringView(data);
83   EXPECT_THAT(data_view, Eq(s));
84 }
85 
TEST(SecretDataTest,SecretDataCopy)86 TEST(SecretDataTest, SecretDataCopy) {
87   constexpr unsigned char kContents[] = {41, 42, 64, 12, 41, 0,
88                                          52, 56, 6,  12, 127, 13};
89   SecretData data;
90   for (unsigned char c : kContents) {
91     data.push_back(c);
92   }
93   SecretData data_copy = data;
94   EXPECT_THAT(data_copy, ElementsAreArray(kContents));
95 }
96 
TEST(SecretValueTest,DefaultConstructor)97 TEST(SecretValueTest, DefaultConstructor) {
98   SecretValue<int> s;
99   EXPECT_THAT(s.value(), Eq(0));
100 }
101 
TEST(SecretValueTest,Constructor)102 TEST(SecretValueTest, Constructor) {
103   SecretValue<int> s(102);
104   EXPECT_THAT(s.value(), Eq(102));
105 }
106 
TEST(SecretValueTest,CopyConstructor)107 TEST(SecretValueTest, CopyConstructor) {
108   SecretValue<int> s(102);
109   SecretValue<int> t(s);
110   EXPECT_THAT(t.value(), Eq(102));
111 }
112 
TEST(SecretValueTest,AssignmentOperator)113 TEST(SecretValueTest, AssignmentOperator) {
114   SecretValue<int> s(102);
115   SecretValue<int> t(101);
116   t = s;
117   EXPECT_THAT(t.value(), Eq(102));
118 }
119 
TEST(SecretValueTest,MoveConstructor)120 TEST(SecretValueTest, MoveConstructor) {
121   SecretValue<int> s(102);
122   SecretValue<int> t(std::move(s));
123   EXPECT_THAT(t.value(), Eq(102));
124   // NOLINTNEXTLINE(bugprone-use-after-move)
125   EXPECT_THAT(s.value(), AnyOf(Eq(0), Eq(102)));
126 }
127 
TEST(SecretValueTest,MoveAssignment)128 TEST(SecretValueTest, MoveAssignment) {
129   SecretValue<int> s(102);
130   SecretValue<int> t;
131   t = std::move(s);
132   EXPECT_THAT(t.value(), Eq(102));
133   // NOLINTNEXTLINE(bugprone-use-after-move)
134   EXPECT_THAT(s.value(), AnyOf(Eq(0), Eq(102)));
135 }
136 
137 }  // namespace
138 }  // namespace util
139 }  // namespace tink
140 }  // namespace crypto
141