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/prf/failing_prfset.h"
17
18 #include <map>
19 #include <memory>
20 #include <string>
21
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "absl/status/status.h"
25 #include "tink/util/test_matchers.h"
26
27 namespace crypto {
28 namespace tink {
29 namespace {
30
31 using ::crypto::tink::test::StatusIs;
32 using ::testing::HasSubstr;
33
TEST(AlwaysFailPrf,ComputePrimaryFails)34 TEST(AlwaysFailPrf, ComputePrimaryFails) {
35 std::unique_ptr<Prf> failing_prf = CreateAlwaysFailingPrf();
36
37 EXPECT_THAT(failing_prf->Compute("someinput", 16).status(),
38 StatusIs(absl::StatusCode::kInternal));
39 }
40
TEST(AlwaysFailPrf,ComputePrimaryFailsContainsMessage)41 TEST(AlwaysFailPrf, ComputePrimaryFailsContainsMessage) {
42 const std::string expected_message = "expected_message";
43 std::unique_ptr<Prf> failing_prf = CreateAlwaysFailingPrf(expected_message);
44
45 EXPECT_THAT(
46 failing_prf->Compute("someinput", 16).status(),
47 StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
48 }
49
TEST(AlwaysFailPrfSet,ComputePrimaryFails)50 TEST(AlwaysFailPrfSet, ComputePrimaryFails) {
51 std::unique_ptr<PrfSet> failing_prf_set = CreateAlwaysFailingPrfSet();
52
53 EXPECT_THAT(failing_prf_set->ComputePrimary("someinput", 16).status(),
54 StatusIs(absl::StatusCode::kInternal));
55 }
56
TEST(AlwaysFailPrfSet,ComputePrimaryFailsContainsMessage)57 TEST(AlwaysFailPrfSet, ComputePrimaryFailsContainsMessage) {
58 const std::string expected_message = "expected_message";
59 std::unique_ptr<PrfSet> failing_prf_set =
60 CreateAlwaysFailingPrfSet(expected_message);
61
62 EXPECT_THAT(
63 failing_prf_set->ComputePrimary("someinput", 16).status(),
64 StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
65 }
66
TEST(AlwaysFailPrfSet,GetPrfsReturnsFailingPrfs)67 TEST(AlwaysFailPrfSet, GetPrfsReturnsFailingPrfs) {
68 const std::string expected_message = "expected_message";
69 std::unique_ptr<PrfSet> failing_prf_set =
70 CreateAlwaysFailingPrfSet(expected_message);
71 const std::map<uint32_t, Prf*>& prfs_map = failing_prf_set->GetPrfs();
72
73 for (auto const& entry : prfs_map) {
74 EXPECT_THAT(
75 entry.second->Compute("someinput", 16).status(),
76 StatusIs(absl::StatusCode::kInternal, HasSubstr(expected_message)));
77 }
78 }
79
80 } // namespace
81 } // namespace tink
82 } // namespace crypto
83