aboutsummaryrefslogtreecommitdiff
path: root/cc/mac/failing_mac.cc
diff options
context:
space:
mode:
authorkste <kste@google.com>2022-06-15 08:54:15 -0700
committerCopybara-Service <copybara-worker@google.com>2022-06-15 08:55:19 -0700
commitdcf495fd7d5cecfda42298feca128f25f687bc07 (patch)
tree4e2b23e2e63ac1eb77e70824f88d450dd5419a52 /cc/mac/failing_mac.cc
parentfce15973f9fc3a8d504ce9e9ffcf8f9aafe241b7 (diff)
downloadtink-dcf495fd7d5cecfda42298feca128f25f687bc07.tar.gz
Add a method to create an always failing Mac.
This allows to create Mac objects which will return an error on any API call, which can for instance be used for testing correct failure behavior. PiperOrigin-RevId: 455141793
Diffstat (limited to 'cc/mac/failing_mac.cc')
-rw-r--r--cc/mac/failing_mac.cc59
1 files changed, 59 insertions, 0 deletions
diff --git a/cc/mac/failing_mac.cc b/cc/mac/failing_mac.cc
new file mode 100644
index 000000000..999bb83bc
--- /dev/null
+++ b/cc/mac/failing_mac.cc
@@ -0,0 +1,59 @@
+// Copyright 2022 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+//
+///////////////////////////////////////////////////////////////////////////////
+#include "tink/mac/failing_mac.h"
+
+#include <string>
+#include <utility>
+
+#include "absl/strings/string_view.h"
+
+namespace crypto {
+namespace tink {
+namespace {
+
+// A MAC that always returns a kInternal status on API calls.
+class AlwaysFailMac : public Mac {
+ public:
+ explicit AlwaysFailMac(std::string message) : message_(std::move(message)) {}
+
+ util::StatusOr<std::string> ComputeMac(
+ absl::string_view /*data*/) const override {
+ return util::Status(
+ absl::StatusCode::kInternal,
+ absl::StrCat("AlwaysFailMac will always fail on ComputeMac (msg=",
+ message_, ")"));
+ }
+
+ util::Status VerifyMac(absl::string_view /*mac_value*/,
+ absl::string_view /*data*/) const override {
+ return util::Status(
+ absl::StatusCode::kInternal,
+ absl::StrCat("AlwaysFailMac will always fail on VerifyMac (msg=",
+ message_, ")"));
+ }
+
+ private:
+ const std::string message_;
+};
+
+} // namespace
+
+std::unique_ptr<Mac> CreateAlwaysFailingMac(std::string message) {
+ return absl::make_unique<AlwaysFailMac>(std::move(message));
+}
+
+} // namespace tink
+} // namespace crypto