xref: /aosp_15_r20/external/tink/cc/examples/walkthrough/test_util.h (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 #ifndef TINK_EXAMPLES_WALKTHROUGH_TEST_UTIL_H_
17 #define TINK_EXAMPLES_WALKTHROUGH_TEST_UTIL_H_
18 
19 #include <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/strings/string_view.h"
24 #include "tink/aead.h"
25 #include "tink/kms_client.h"
26 #include "tink/util/statusor.h"
27 
28 namespace tink_walkthrough {
29 
30 // A fake KmsClient that for every key URI always returns an aead from
31 // kSerializedMasterKeyKeyset.
32 class FakeKmsClient : public crypto::tink::KmsClient {
33  public:
FakeKmsClient(absl::string_view serialized_master_key_keyset)34   explicit FakeKmsClient(absl::string_view serialized_master_key_keyset)
35       : serialized_master_key_keyset_(serialized_master_key_keyset) {}
36 
37   bool DoesSupport(absl::string_view key_uri) const override;
38 
39   crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::Aead>> GetAead(
40       absl::string_view key_uri) const override;
41 
42  private:
43   std::string serialized_master_key_keyset_;
44 };
45 
46 // A fake KmsClient that always fails to return an AEAD.
47 class AlwaysFailingFakeKmsClient : public crypto::tink::KmsClient {
48  public:
49   bool DoesSupport(absl::string_view key_uri) const override;
50 
51   crypto::tink::util::StatusOr<std::unique_ptr<crypto::tink::Aead>> GetAead(
52       absl::string_view key_uri) const override;
53 };
54 
55 }  // namespace tink_walkthrough
56 
57 #endif  // TINK_EXAMPLES_WALKTHROUGH_TEST_UTIL_H_
58