xref: /aosp_15_r20/external/tink/cc/daead/failing_daead_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 #include "tink/daead/failing_daead.h"
17 #include <memory>
18 #include <string>
19 
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "absl/status/status.h"
23 #include "tink/util/test_matchers.h"
24 
25 namespace crypto {
26 namespace tink {
27 namespace {
28 
29 using ::crypto::tink::test::StatusIs;
30 using ::testing::HasSubstr;
31 
TEST(AlwaysFailDeterministicAead,EncryptFails)32 TEST(AlwaysFailDeterministicAead, EncryptFails) {
33   std::unique_ptr<DeterministicAead> failing_daead =
34       CreateAlwaysFailingDeterministicAead();
35 
36   EXPECT_THAT(
37       failing_daead->EncryptDeterministically("plaintext", "associated_data")
38           .status(),
39       StatusIs(absl::StatusCode::kInternal));
40 }
41 
TEST(AlwaysFailDeterministicAead,EncryptFailsContainsMessage)42 TEST(AlwaysFailDeterministicAead, EncryptFailsContainsMessage) {
43   const std::string expected_message = "expected_message";
44   std::unique_ptr<DeterministicAead> failing_aead =
45       CreateAlwaysFailingDeterministicAead(expected_message);
46 
47   EXPECT_THAT(
48       failing_aead->EncryptDeterministically("plaintext", "associated_data")
49           .status(),
50       StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
51 }
52 
TEST(AlwaysFailDeterministicAead,DecryptFails)53 TEST(AlwaysFailDeterministicAead, DecryptFails) {
54   std::unique_ptr<DeterministicAead> failing_daead =
55       CreateAlwaysFailingDeterministicAead();
56 
57   EXPECT_THAT(
58       failing_daead->DecryptDeterministically("ciphertext", "associated_data")
59           .status(),
60       StatusIs(absl::StatusCode::kInternal));
61 }
62 
TEST(AlwaysFailDeterministicAead,DecryptFailsContainsMessage)63 TEST(AlwaysFailDeterministicAead, DecryptFailsContainsMessage) {
64   const std::string expected_message = "expected_message";
65   std::unique_ptr<DeterministicAead> failing_aead =
66       CreateAlwaysFailingDeterministicAead(expected_message);
67 
68   EXPECT_THAT(
69       failing_aead->EncryptDeterministically("plaintext", "associated_data")
70           .status(),
71       StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
72 }
73 
74 }  // namespace
75 }  // namespace tink
76 }  // namespace crypto
77 
78