xref: /aosp_15_r20/external/tink/cc/mac/failing_mac_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/mac/failing_mac.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(AlwaysFailMac,ComputeMacFails)33 TEST(AlwaysFailMac, ComputeMacFails) {
34   std::unique_ptr<Mac> failing_Mac = CreateAlwaysFailingMac();
35 
36   EXPECT_THAT(failing_Mac->ComputeMac("message").status(),
37               StatusIs(absl::StatusCode::kInternal));
38 }
39 
TEST(AlwaysFailMac,ComputeMacFailsContainsMessage)40 TEST(AlwaysFailMac, ComputeMacFailsContainsMessage) {
41   const std::string expected_message = "expected_message";
42   std::unique_ptr<Mac> failing_Mac =
43       CreateAlwaysFailingMac(expected_message);
44 
45   EXPECT_THAT(
46       failing_Mac->ComputeMac("message").status(),
47       StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
48 }
49 
TEST(AlwaysFailMac,VerifyMacFails)50 TEST(AlwaysFailMac, VerifyMacFails) {
51   std::unique_ptr<Mac> failing_Mac = CreateAlwaysFailingMac();
52 
53   EXPECT_THAT(failing_Mac->VerifyMac("tag", "message"),
54               StatusIs(absl::StatusCode::kInternal));
55 }
56 
TEST(AlwaysFailMac,VerifyMacFailsContainsMessage)57 TEST(AlwaysFailMac, VerifyMacFailsContainsMessage) {
58   const std::string expected_message = "expected_message";
59   std::unique_ptr<Mac> failing_Mac =
60       CreateAlwaysFailingMac(expected_message);
61 
62   EXPECT_THAT(
63       failing_Mac->VerifyMac("tag", "message"),
64       StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
65 }
66 
67 }  // namespace
68 }  // namespace tink
69 }  // namespace crypto
70