1 // Copyright 2019 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 // https://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 #include "sandboxed_api/util/status.h"
16
17 #include <cstdint>
18 #include <string>
19
20 #include "gmock/gmock.h"
21 #include "gtest/gtest.h"
22 #include "absl/status/status.h"
23 #include "absl/strings/string_view.h"
24
25 using ::testing::Eq;
26 using ::testing::StrEq;
27
28 namespace sapi {
29 namespace {
30
OkStatusProto()31 StatusProto OkStatusProto() {
32 StatusProto proto;
33 proto.set_code(static_cast<int>(absl::StatusCode::kOk));
34 return proto;
35 }
36
InvalidArgumentStatusProto(absl::string_view msg)37 StatusProto InvalidArgumentStatusProto(absl::string_view msg) {
38 StatusProto proto;
39 proto.set_code(static_cast<int>(absl::StatusCode::kInvalidArgument));
40 proto.set_message(std::string(msg));
41 return proto;
42 }
43
TEST(StatusTest,SaveOkStatusProto)44 TEST(StatusTest, SaveOkStatusProto) {
45 StatusProto proto;
46 SaveStatusToProto(absl::OkStatus(), &proto);
47 const auto ok_proto = OkStatusProto();
48 EXPECT_THAT(proto.code(), Eq(ok_proto.code()));
49 EXPECT_THAT(proto.message(), StrEq(ok_proto.message()));
50 }
51
TEST(StatusTest,SaveStatusWithMessage)52 TEST(StatusTest, SaveStatusWithMessage) {
53 constexpr char kErrorMessage[] = "Bad foo argument";
54 absl::Status status(absl::StatusCode::kInvalidArgument, kErrorMessage);
55 StatusProto proto;
56 SaveStatusToProto(status, &proto);
57 const auto invalid_proto = InvalidArgumentStatusProto(kErrorMessage);
58 EXPECT_THAT(proto.code(), Eq(invalid_proto.code()));
59 EXPECT_THAT(proto.message(), StrEq(invalid_proto.message()));
60 }
61
62 } // namespace
63 } // namespace sapi
64