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/aead/failing_aead.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(AlwaysFailAead,EncryptFails)33 TEST(AlwaysFailAead, EncryptFails) {
34 std::unique_ptr<Aead> failing_aead = CreateAlwaysFailingAead();
35
36 EXPECT_THAT(failing_aead->Encrypt("plaintext", "associated_data").status(),
37 StatusIs(absl::StatusCode::kInternal));
38 }
39
TEST(AlwaysFailAead,EncryptFailsContainsMessage)40 TEST(AlwaysFailAead, EncryptFailsContainsMessage) {
41 const std::string expected_message = "expected_message";
42 std::unique_ptr<Aead> failing_aead =
43 CreateAlwaysFailingAead(expected_message);
44
45 EXPECT_THAT(
46 failing_aead->Encrypt("plaintext", "associated_data").status(),
47 StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
48 }
49
TEST(AlwaysFailAead,DecryptFails)50 TEST(AlwaysFailAead, DecryptFails) {
51 std::unique_ptr<Aead> failing_aead = CreateAlwaysFailingAead();
52
53 EXPECT_THAT(failing_aead->Decrypt("ciphertext", "associated_data").status(),
54 StatusIs(absl::StatusCode::kInternal));
55 }
56
TEST(AlwaysFailAead,DecryptFailsContainsMessage)57 TEST(AlwaysFailAead, DecryptFailsContainsMessage) {
58 const std::string expected_message = "expected_message";
59 std::unique_ptr<Aead> failing_aead =
60 CreateAlwaysFailingAead(expected_message);
61
62 EXPECT_THAT(
63 failing_aead->Decrypt("ciphertext", "associated_data").status(),
64 StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
65 }
66
67 } // namespace
68 } // namespace tink
69 } // namespace crypto
70