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
17 #include "tink/internal/key_status_util.h"
18
19 #include "gmock/gmock.h"
20 #include "gtest/gtest.h"
21 #include "absl/status/status.h"
22 #include "tink/key_status.h"
23 #include "tink/util/statusor.h"
24 #include "tink/util/test_matchers.h"
25 #include "proto/tink.pb.h"
26
27 namespace crypto {
28 namespace tink {
29 namespace internal {
30 namespace {
31
32 using ::crypto::tink::test::IsOkAndHolds;
33 using ::crypto::tink::test::StatusIs;
34 using ::google::crypto::tink::KeyStatusType;
35
TEST(KeyStatusUtilTest,FromKeyStatusType)36 TEST(KeyStatusUtilTest, FromKeyStatusType) {
37 util::StatusOr<KeyStatus> enabled = FromKeyStatusType(KeyStatusType::ENABLED);
38 EXPECT_THAT(enabled, IsOkAndHolds(KeyStatus::kEnabled));
39
40 util::StatusOr<KeyStatus> disabled =
41 FromKeyStatusType(KeyStatusType::DISABLED);
42 EXPECT_THAT(disabled, IsOkAndHolds(KeyStatus::kDisabled));
43
44 util::StatusOr<KeyStatus> destroyed =
45 FromKeyStatusType(KeyStatusType::DESTROYED);
46 EXPECT_THAT(destroyed, IsOkAndHolds(KeyStatus::kDestroyed));
47
48 util::StatusOr<KeyStatus> unknown =
49 FromKeyStatusType(KeyStatusType::UNKNOWN_STATUS);
50 EXPECT_THAT(unknown.status(), StatusIs(absl::StatusCode::kInvalidArgument));
51 }
52
TEST(KeyStatusUtilTest,ToKeyStatusType)53 TEST(KeyStatusUtilTest, ToKeyStatusType) {
54 util::StatusOr<KeyStatusType> enabled = ToKeyStatusType(KeyStatus::kEnabled);
55 EXPECT_THAT(enabled, IsOkAndHolds(KeyStatusType::ENABLED));
56
57 util::StatusOr<KeyStatusType> disabled =
58 ToKeyStatusType(KeyStatus::kDisabled);
59 EXPECT_THAT(disabled, IsOkAndHolds(KeyStatusType::DISABLED));
60
61 util::StatusOr<KeyStatusType> destroyed =
62 ToKeyStatusType(KeyStatus::kDestroyed);
63 EXPECT_THAT(destroyed, IsOkAndHolds(KeyStatusType::DESTROYED));
64
65 util::StatusOr<KeyStatusType> unknown = ToKeyStatusType(
66 KeyStatus::kDoNotUseInsteadUseDefaultWhenWritingSwitchStatements);
67 EXPECT_THAT(unknown.status(), StatusIs(absl::StatusCode::kInvalidArgument));
68 }
69
TEST(KeyStatusUtilTest,ToKeyStatusName)70 TEST(KeyStatusUtilTest, ToKeyStatusName) {
71 EXPECT_EQ(ToKeyStatusName(KeyStatus::kEnabled), "ENABLED");
72 EXPECT_EQ(ToKeyStatusName(KeyStatus::kDisabled), "DISABLED");
73 EXPECT_EQ(ToKeyStatusName(KeyStatus::kDestroyed), "DESTROYED");
74 }
75
76 } // namespace
77 } // namespace internal
78 } // namespace tink
79 } // namespace crypto
80