1 // Copyright 2020 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <limits>
6 #include <vector>
7
8 #include "gtest/gtest.h"
9 #include "third_party/abseil-cpp/absl/base/options.h"
10 #include "third_party/abseil-cpp/absl/container/fixed_array.h"
11 #include "third_party/abseil-cpp/absl/container/inlined_vector.h"
12 #include "third_party/abseil-cpp/absl/strings/string_view.h"
13 #include "third_party/abseil-cpp/absl/types/optional.h"
14 #include "third_party/abseil-cpp/absl/types/span.h"
15 #include "third_party/abseil-cpp/absl/types/variant.h"
16
17 namespace {
18
19 #if !ABSL_OPTION_HARDENED
20 # error "Define ABSL_OPTION_HARDENED to 1 in absl/base/options.h"
21 #endif
22
TEST(AbslHardeningTest,Optional)23 TEST(AbslHardeningTest, Optional) {
24 absl::optional<int> optional;
25 EXPECT_DEATH_IF_SUPPORTED(*optional, "");
26 }
27
TEST(AbslHardeningTest,StringView)28 TEST(AbslHardeningTest, StringView) {
29 absl::string_view view("foo");
30 EXPECT_DEATH_IF_SUPPORTED(view[4], "");
31 EXPECT_DEATH_IF_SUPPORTED(view.remove_prefix(10), "");
32 EXPECT_DEATH_IF_SUPPORTED(view.remove_suffix(10), "");
33
34 absl::string_view empty("");
35 EXPECT_DEATH_IF_SUPPORTED(empty.front(), "");
36 EXPECT_DEATH_IF_SUPPORTED(empty.back(), "");
37 }
38
TEST(AbslHardeningTest,FixedArray)39 TEST(AbslHardeningTest, FixedArray) {
40 absl::FixedArray<int, 4> fixed_array(0);
41 EXPECT_DEATH_IF_SUPPORTED(fixed_array[1], "");
42 EXPECT_DEATH_IF_SUPPORTED(fixed_array.front(), "");
43 EXPECT_DEATH_IF_SUPPORTED(fixed_array.back(), "");
44 }
45
TEST(AbslHardeningTest,InlinedVector)46 TEST(AbslHardeningTest, InlinedVector) {
47 absl::InlinedVector<int, 10> inlined_vector;
48 EXPECT_DEATH_IF_SUPPORTED(inlined_vector[1], "");
49 EXPECT_DEATH_IF_SUPPORTED(inlined_vector.front(), "");
50 EXPECT_DEATH_IF_SUPPORTED(inlined_vector.back(), "");
51 EXPECT_DEATH_IF_SUPPORTED(
52 inlined_vector.resize(inlined_vector.max_size() + 1), "");
53 EXPECT_DEATH_IF_SUPPORTED(inlined_vector.pop_back(), "");
54
55 auto it = inlined_vector.end();
56 EXPECT_DEATH_IF_SUPPORTED(inlined_vector.erase(it), "");
57 }
58
TEST(AbslHardeningTest,Span)59 TEST(AbslHardeningTest, Span) {
60 std::vector<int> v;
61 auto span = absl::Span<const int>(v);
62
63 EXPECT_DEATH_IF_SUPPORTED(span.front(), "");
64 EXPECT_DEATH_IF_SUPPORTED(span.back(), "");
65 EXPECT_DEATH_IF_SUPPORTED(span.remove_prefix(10), "");
66 EXPECT_DEATH_IF_SUPPORTED(span.remove_suffix(10), "");
67 EXPECT_DEATH_IF_SUPPORTED(span[10], "");
68
69 std::vector<int> v1 = {1, 2, 3, 4};
70 EXPECT_DEATH_IF_SUPPORTED(absl::MakeSpan(&v1[2], &v[0]), "");
71 EXPECT_DEATH_IF_SUPPORTED(absl::MakeConstSpan(&v1[2], &v[0]), "");
72 }
73
TEST(AbslHardeningTest,Variant)74 TEST(AbslHardeningTest, Variant) {
75 absl::variant<int, std::string> variant = 5;
76 EXPECT_DEATH_IF_SUPPORTED(absl::get<std::string>(variant), "");
77 EXPECT_DEATH_IF_SUPPORTED(absl::get<1>(variant), "");
78 }
79
80 } // namespace
81