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