about summary refs log tree commit diff
path: root/absl/status/status_test.cc
blob: 7cc65e45cb849ffcb6c073529fefa3582d283ba3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// Copyright 2019 The Abseil Authors.
//
// 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
//
//      https://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 "absl/status/status.h"

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/strings/str_cat.h"

namespace {

using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::Optional;
using ::testing::UnorderedElementsAreArray;

TEST(StatusCode, InsertionOperator) {
  const absl::StatusCode code = absl::StatusCode::kUnknown;
  std::ostringstream oss;
  oss << code;
  EXPECT_EQ(oss.str(), absl::StatusCodeToString(code));
}

// This structure holds the details for testing a single error code,
// its creator, and its classifier.
struct ErrorTest {
  absl::StatusCode code;
  using Creator = absl::Status (*)(absl::string_view);
  using Classifier = bool (*)(const absl::Status&);
  Creator creator;
  Classifier classifier;
};

constexpr ErrorTest kErrorTests[]{
    {absl::StatusCode::kCancelled, absl::CancelledError, absl::IsCancelled},
    {absl::StatusCode::kUnknown, absl::UnknownError, absl::IsUnknown},
    {absl::StatusCode::kInvalidArgument, absl::InvalidArgumentError,
     absl::IsInvalidArgument},
    {absl::StatusCode::kDeadlineExceeded, absl::DeadlineExceededError,
     absl::IsDeadlineExceeded},
    {absl::StatusCode::kNotFound, absl::NotFoundError, absl::IsNotFound},
    {absl::StatusCode::kAlreadyExists, absl::AlreadyExistsError,
     absl::IsAlreadyExists},
    {absl::StatusCode::kPermissionDenied, absl::PermissionDeniedError,
     absl::IsPermissionDenied},
    {absl::StatusCode::kResourceExhausted, absl::ResourceExhaustedError,
     absl::IsResourceExhausted},
    {absl::StatusCode::kFailedPrecondition, absl::FailedPreconditionError,
     absl::IsFailedPrecondition},
    {absl::StatusCode::kAborted, absl::AbortedError, absl::IsAborted},
    {absl::StatusCode::kOutOfRange, absl::OutOfRangeError, absl::IsOutOfRange},
    {absl::StatusCode::kUnimplemented, absl::UnimplementedError,
     absl::IsUnimplemented},
    {absl::StatusCode::kInternal, absl::InternalError, absl::IsInternal},
    {absl::StatusCode::kUnavailable, absl::UnavailableError,
     absl::IsUnavailable},
    {absl::StatusCode::kDataLoss, absl::DataLossError, absl::IsDataLoss},
    {absl::StatusCode::kUnauthenticated, absl::UnauthenticatedError,
     absl::IsUnauthenticated},
};

TEST(Status, CreateAndClassify) {
  for (const auto& test : kErrorTests) {
    SCOPED_TRACE(absl::StatusCodeToString(test.code));

    // Ensure that the creator does, in fact, create status objects with the
    // expected error code and message.
    std::string message =
        absl::StrCat("error code ", test.code, " test message");
    absl::Status status = test.creator(message);
    EXPECT_EQ(test.code, status.code());
    EXPECT_EQ(message, status.message());

    // Ensure that the classifier returns true for a status produced by the
    // creator.
    EXPECT_TRUE(test.classifier(status));

    // Ensure that the classifier returns false for status with a different
    // code.
    for (const auto& other : kErrorTests) {
      if (other.code != test.code) {
        EXPECT_FALSE(test.classifier(absl::Status(other.code, "")))
            << " other.code = " << other.code;
      }
    }
  }
}

TEST(Status, DefaultConstructor) {
  absl::Status status;
  EXPECT_TRUE(status.ok());
  EXPECT_EQ(absl::StatusCode::kOk, status.code());
  EXPECT_EQ("", status.message());
}

TEST(Status, OkStatus) {
  absl::Status status = absl::OkStatus();
  EXPECT_TRUE(status.ok());
  EXPECT_EQ(absl::StatusCode::kOk, status.code());
  EXPECT_EQ("", status.message());
}

TEST(Status, ConstructorWithCodeMessage) {
  {
    absl::Status status(absl::StatusCode::kCancelled, "");
    EXPECT_FALSE(status.ok());
    EXPECT_EQ(absl::StatusCode::kCancelled, status.code());
    EXPECT_EQ("", status.message());
  }
  {
    absl::Status status(absl::StatusCode::kInternal, "message");
    EXPECT_FALSE(status.ok());
    EXPECT_EQ(absl::StatusCode::kInternal, status.code());
    EXPECT_EQ("message", status.message());
  }
}

TEST(Status, ConstructOutOfRangeCode) {
  const int kRawCode = 9999;
  absl::Status status(static_cast<absl::StatusCode>(kRawCode), "");
  EXPECT_EQ(absl::StatusCode::kUnknown, status.code());
  EXPECT_EQ(kRawCode, status.raw_code());
}

constexpr char kUrl1[] = "url.payload.1";
constexpr char kUrl2[] = "url.payload.2";
constexpr char kUrl3[] = "url.payload.3";
constexpr char kUrl4[] = "url.payload.xx";

constexpr char kPayload1[] = "aaaaa";
constexpr char kPayload2[] = "bbbbb";
constexpr char kPayload3[] = "ccccc";

using PayloadsVec = std::vector<std::pair<std::string, absl::Cord>>;

TEST(Status, TestGetSetPayload) {
  absl::Status ok_status = absl::OkStatus();
  ok_status.SetPayload(kUrl1, absl::Cord(kPayload1));
  ok_status.SetPayload(kUrl2, absl::Cord(kPayload2));

  EXPECT_FALSE(ok_status.GetPayload(kUrl1));
  EXPECT_FALSE(ok_status.GetPayload(kUrl2));

  absl::Status bad_status(absl::StatusCode::kInternal, "fail");
  bad_status.SetPayload(kUrl1, absl::Cord(kPayload1));
  bad_status.SetPayload(kUrl2, absl::Cord(kPayload2));

  EXPECT_THAT(bad_status.GetPayload(kUrl1), Optional(Eq(kPayload1)));
  EXPECT_THAT(bad_status.GetPayload(kUrl2), Optional(Eq(kPayload2)));

  EXPECT_FALSE(bad_status.GetPayload(kUrl3));

  bad_status.SetPayload(kUrl1, absl::Cord(kPayload3));
  EXPECT_THAT(bad_status.GetPayload(kUrl1), Optional(Eq(kPayload3)));

  // Testing dynamically generated type_url
  bad_status.SetPayload(absl::StrCat(kUrl1, ".1"), absl::Cord(kPayload1));
  EXPECT_THAT(bad_status.GetPayload(absl::StrCat(kUrl1, ".1")),
              Optional(Eq(kPayload1)));
}

TEST(Status, TestErasePayload) {
  absl::Status bad_status(absl::StatusCode::kInternal, "fail");
  bad_status.SetPayload(kUrl1, absl::Cord(kPayload1));
  bad_status.SetPayload(kUrl2, absl::Cord(kPayload2));
  bad_status.SetPayload(kUrl3, absl::Cord(kPayload3));

  EXPECT_FALSE(bad_status.ErasePayload(kUrl4));

  EXPECT_TRUE(bad_status.GetPayload(kUrl2));
  EXPECT_TRUE(bad_status.ErasePayload(kUrl2));
  EXPECT_FALSE(bad_status.GetPayload(kUrl2));
  EXPECT_FALSE(bad_status.ErasePayload(kUrl2));

  EXPECT_TRUE(bad_status.ErasePayload(kUrl1));
  EXPECT_TRUE(bad_status.ErasePayload(kUrl3));

  bad_status.SetPayload(kUrl1, absl::Cord(kPayload1));
  EXPECT_TRUE(bad_status.ErasePayload(kUrl1));
}

TEST(Status, TestComparePayloads) {
  absl::Status bad_status1(absl::StatusCode::kInternal, "fail");
  bad_status1.SetPayload(kUrl1, absl::Cord(kPayload1));
  bad_status1.SetPayload(kUrl2, absl::Cord(kPayload2));
  bad_status1.SetPayload(kUrl3, absl::Cord(kPayload3));

  absl::Status bad_status2(absl::StatusCode::kInternal, "fail");
  bad_status2.SetPayload(kUrl2, absl::Cord(kPayload2));
  bad_status2.SetPayload(kUrl3, absl::Cord(kPayload3));
  bad_status2.SetPayload(kUrl1, absl::Cord(kPayload1));

  EXPECT_EQ(bad_status1, bad_status2);
}

PayloadsVec AllVisitedPayloads(const absl::Status& s) {
  PayloadsVec result;

  s.ForEachPayload([&](absl::string_view type_url, const absl::Cord& payload) {
    result.push_back(std::make_pair(std::string(type_url), payload));
  });

  return result;
}

TEST(Status, TestForEachPayload) {
  absl::Status bad_status(absl::StatusCode::kInternal, "fail");
  bad_status.SetPayload(kUrl1, absl::Cord(kPayload1));
  bad_status.SetPayload(kUrl2, absl::Cord(kPayload2));
  bad_status.SetPayload(kUrl3, absl::Cord(kPayload3));

  int count = 0;

  bad_status.ForEachPayload(
      [&count](absl::string_view, const absl::Cord&) { ++count; });

  EXPECT_EQ(count, 3);

  PayloadsVec expected_payloads = {{kUrl1, absl::Cord(kPayload1)},
                                   {kUrl2, absl::Cord(kPayload2)},
                                   {kUrl3, absl::Cord(kPayload3)}};

  // Test that we visit all the payloads in the status.
  PayloadsVec visited_payloads = AllVisitedPayloads(bad_status);
  EXPECT_THAT(visited_payloads, UnorderedElementsAreArray(expected_payloads));

  // Test that visitation order is not consistent between run.
  std::vector<absl::Status> scratch;
  while (true) {
    scratch.emplace_back(absl::StatusCode::kInternal, "fail");

    scratch.back().SetPayload(kUrl1, absl::Cord(kPayload1));
    scratch.back().SetPayload(kUrl2, absl::Cord(kPayload2));
    scratch.back().SetPayload(kUrl3, absl::Cord(kPayload3));

    if (AllVisitedPayloads(scratch.back()) != visited_payloads) {
      break;
    }
  }
}

TEST(Status, ToString) {
  absl::Status s(absl::StatusCode::kInternal, "fail");
  EXPECT_EQ("INTERNAL: fail", s.ToString());
  s.SetPayload("foo", absl::Cord("bar"));
  EXPECT_EQ("INTERNAL: fail [foo='bar']", s.ToString());
  s.SetPayload("bar", absl::Cord("\377"));
  EXPECT_THAT(s.ToString(),
              AllOf(HasSubstr("INTERNAL: fail"), HasSubstr("[foo='bar']"),
                    HasSubstr("[bar='\\xff']")));
}

TEST(Status, CopyConstructor) {
  {
    absl::Status status;
    absl::Status copy(status);
    EXPECT_EQ(copy, status);
  }
  {
    absl::Status status(absl::StatusCode::kInvalidArgument, "message");
    absl::Status copy(status);
    EXPECT_EQ(copy, status);
  }
  {
    absl::Status status(absl::StatusCode::kInvalidArgument, "message");
    status.SetPayload(kUrl1, absl::Cord(kPayload1));
    absl::Status copy(status);
    EXPECT_EQ(copy, status);
  }
}

TEST(Status, CopyAssignment) {
  absl::Status assignee;
  {
    absl::Status status;
    assignee = status;
    EXPECT_EQ(assignee, status);
  }
  {
    absl::Status status(absl::StatusCode::kInvalidArgument, "message");
    assignee = status;
    EXPECT_EQ(assignee, status);
  }
  {
    absl::Status status(absl::StatusCode::kInvalidArgument, "message");
    status.SetPayload(kUrl1, absl::Cord(kPayload1));
    assignee = status;
    EXPECT_EQ(assignee, status);
  }
}

TEST(Status, MoveConstructor) {
  {
    absl::Status status;
    absl::Status copy(absl::Status{});
    EXPECT_EQ(copy, status);
  }
  {
    absl::Status status(absl::StatusCode::kInvalidArgument, "message");
    absl::Status copy(
        absl::Status(absl::StatusCode::kInvalidArgument, "message"));
    EXPECT_EQ(copy, status);
  }
  {
    absl::Status status(absl::StatusCode::kInvalidArgument, "message");
    status.SetPayload(kUrl1, absl::Cord(kPayload1));
    absl::Status copy1(status);
    absl::Status copy2(std::move(status));
    EXPECT_EQ(copy1, copy2);
  }
}

TEST(Status, MoveAssignment) {
  absl::Status assignee;
  {
    absl::Status status;
    assignee = absl::Status();
    EXPECT_EQ(assignee, status);
  }
  {
    absl::Status status(absl::StatusCode::kInvalidArgument, "message");
    assignee = absl::Status(absl::StatusCode::kInvalidArgument, "message");
    EXPECT_EQ(assignee, status);
  }
  {
    absl::Status status(absl::StatusCode::kInvalidArgument, "message");
    status.SetPayload(kUrl1, absl::Cord(kPayload1));
    absl::Status copy(status);
    assignee = std::move(status);
    EXPECT_EQ(assignee, copy);
  }
}

TEST(Status, Update) {
  absl::Status s;
  s.Update(absl::OkStatus());
  EXPECT_TRUE(s.ok());
  const absl::Status a(absl::StatusCode::kCancelled, "message");
  s.Update(a);
  EXPECT_EQ(s, a);
  const absl::Status b(absl::StatusCode::kInternal, "other message");
  s.Update(b);
  EXPECT_EQ(s, a);
  s.Update(absl::OkStatus());
  EXPECT_EQ(s, a);
  EXPECT_FALSE(s.ok());
}

TEST(Status, Equality) {
  absl::Status ok;
  absl::Status no_payload = absl::CancelledError("no payload");
  absl::Status one_payload = absl::InvalidArgumentError("one payload");
  one_payload.SetPayload(kUrl1, absl::Cord(kPayload1));
  absl::Status two_payloads = one_payload;
  two_payloads.SetPayload(kUrl2, absl::Cord(kPayload2));
  const std::array<absl::Status, 4> status_arr = {ok, no_payload, one_payload,
                                                  two_payloads};
  for (int i = 0; i < status_arr.size(); i++) {
    for (int j = 0; j < status_arr.size(); j++) {
      if (i == j) {
        EXPECT_TRUE(status_arr[i] == status_arr[j]);
        EXPECT_FALSE(status_arr[i] != status_arr[j]);
      } else {
        EXPECT_TRUE(status_arr[i] != status_arr[j]);
        EXPECT_FALSE(status_arr[i] == status_arr[j]);
      }
    }
  }
}

TEST(Status, Swap) {
  auto test_swap = [](const absl::Status& s1, const absl::Status& s2) {
    absl::Status copy1 = s1, copy2 = s2;
    swap(copy1, copy2);
    EXPECT_EQ(copy1, s2);
    EXPECT_EQ(copy2, s1);
  };
  const absl::Status ok;
  const absl::Status no_payload(absl::StatusCode::kAlreadyExists, "no payload");
  absl::Status with_payload(absl::StatusCode::kInternal, "with payload");
  with_payload.SetPayload(kUrl1, absl::Cord(kPayload1));
  test_swap(ok, no_payload);
  test_swap(no_payload, ok);
  test_swap(ok, with_payload);
  test_swap(with_payload, ok);
  test_swap(no_payload, with_payload);
  test_swap(with_payload, no_payload);
}

}  // namespace