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/signature/failing_signature.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(AlwaysFailPublicKeySign,SignFails)33 TEST(AlwaysFailPublicKeySign, SignFails) {
34 std::unique_ptr<PublicKeySign> failing_sign =
35 CreateAlwaysFailingPublicKeySign();
36
37 EXPECT_THAT(failing_sign->Sign("message").status(),
38 StatusIs(absl::StatusCode::kInternal));
39 }
40
TEST(AlwaysFailPublicKeySign,SignFailsContainsMessage)41 TEST(AlwaysFailPublicKeySign, SignFailsContainsMessage) {
42 const std::string expected_message = "expected_message";
43 std::unique_ptr<PublicKeySign> failing_sign =
44 CreateAlwaysFailingPublicKeySign(expected_message);
45
46 EXPECT_THAT(
47 failing_sign->Sign("message").status(),
48 StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
49 }
50
TEST(AlwaysFailPublicKeyVerify,VerifyFails)51 TEST(AlwaysFailPublicKeyVerify, VerifyFails) {
52 std::unique_ptr<PublicKeyVerify> failing_verify =
53 CreateAlwaysFailingPublicKeyVerify();
54
55 EXPECT_THAT(failing_verify->Verify("signature", "message"),
56 StatusIs(absl::StatusCode::kInternal));
57 }
58
TEST(AlwaysFailPublicKeyVerify,VerifyFailsContainsMessage)59 TEST(AlwaysFailPublicKeyVerify, VerifyFailsContainsMessage) {
60 const std::string expected_message = "expected_message";
61 std::unique_ptr<PublicKeyVerify> failing_verify =
62 CreateAlwaysFailingPublicKeyVerify(expected_message);
63
64 EXPECT_THAT(
65 failing_verify->Verify("signature", "message"),
66 StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
67 }
68
69 } // namespace
70 } // namespace tink
71 } // namespace crypto
72