1 // Copyright 2017 The BoringSSL Authors
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 <stdio.h>
16 #include <vector>
17 
18 #include <gtest/gtest.h>
19 
20 #include <openssl/ssl.h>
21 
22 BSSL_NAMESPACE_BEGIN
23 namespace {
24 
TestCtor(Span<int> s,const int * ptr,size_t size)25 static void TestCtor(Span<int> s, const int *ptr, size_t size) {
26   EXPECT_EQ(s.data(), ptr);
27   EXPECT_EQ(s.size(), size);
28 }
29 
TestConstCtor(Span<const int> s,const int * ptr,size_t size)30 static void TestConstCtor(Span<const int> s, const int *ptr, size_t size) {
31   EXPECT_EQ(s.data(), ptr);
32   EXPECT_EQ(s.size(), size);
33 }
34 
TEST(SpanTest,CtorEmpty)35 TEST(SpanTest, CtorEmpty) {
36   Span<int> s;
37   TestCtor(s, nullptr, 0);
38 }
39 
TEST(SpanTest,CtorFromPtrAndSize)40 TEST(SpanTest, CtorFromPtrAndSize) {
41   std::vector<int> v = {7, 8, 9, 10};
42   Span<int> s(v.data(), v.size());
43   TestCtor(s, v.data(), v.size());
44 }
45 
TEST(SpanTest,CtorFromVector)46 TEST(SpanTest, CtorFromVector) {
47   std::vector<int> v = {1, 2};
48   // Const ctor is implicit.
49   TestConstCtor(v, v.data(), v.size());
50   // Mutable is explicit.
51   Span<int> s(v);
52   TestCtor(s, v.data(), v.size());
53 }
54 
TEST(SpanTest,CtorConstFromArray)55 TEST(SpanTest, CtorConstFromArray) {
56   int v[] = {10, 11};
57   // Array ctor is implicit for const and mutable T.
58   TestConstCtor(v, v, 2);
59   TestCtor(v, v, 2);
60 }
61 
TEST(SpanTest,MakeSpan)62 TEST(SpanTest, MakeSpan) {
63   std::vector<int> v = {100, 200, 300};
64   TestCtor(MakeSpan(v), v.data(), v.size());
65   TestCtor(MakeSpan(v.data(), v.size()), v.data(), v.size());
66   TestConstCtor(MakeSpan(v.data(), v.size()), v.data(), v.size());
67   TestConstCtor(MakeSpan(v), v.data(), v.size());
68 }
69 
TEST(SpanTest,MakeConstSpan)70 TEST(SpanTest, MakeConstSpan) {
71   std::vector<int> v = {100, 200, 300};
72   TestConstCtor(MakeConstSpan(v), v.data(), v.size());
73   TestConstCtor(MakeConstSpan(v.data(), v.size()), v.data(), v.size());
74   // But not:
75   // TestConstCtor(MakeSpan(v), v.data(), v.size());
76 }
77 
TEST(SpanTest,Accessor)78 TEST(SpanTest, Accessor) {
79   std::vector<int> v({42, 23, 5, 101, 80});
80   Span<int> s(v);
81   for (size_t i = 0; i < s.size(); ++i) {
82     EXPECT_EQ(s[i], v[i]);
83     EXPECT_EQ(s.at(i), v.at(i));
84   }
85   EXPECT_EQ(s.begin(), v.data());
86   EXPECT_EQ(s.end(), v.data() + v.size());
87 }
88 
TEST(SpanTest,ConstExpr)89 TEST(SpanTest, ConstExpr) {
90   static constexpr int v[] = {1, 2, 3, 4};
91   constexpr bssl::Span<const int> span1(v);
92   static_assert(span1.size() == 4u, "wrong size");
93   constexpr bssl::Span<const int> span2 = MakeConstSpan(v);
94   static_assert(span2.size() == 4u, "wrong size");
95   static_assert(span2.subspan(1).size() == 3u, "wrong size");
96   static_assert(span2.first(1).size() == 1u, "wrong size");
97   static_assert(span2.last(1).size() == 1u, "wrong size");
98   static_assert(span2[0] == 1, "wrong value");
99 }
100 
TEST(SpanDeathTest,BoundsChecks)101 TEST(SpanDeathTest, BoundsChecks) {
102   // Make an array that's larger than we need, so that a failure to bounds check
103   // won't crash.
104   const int v[] = {1, 2, 3, 4};
105   Span<const int> span(v, 3);
106   // Out of bounds access.
107   EXPECT_DEATH_IF_SUPPORTED(span[3], "");
108   EXPECT_DEATH_IF_SUPPORTED(span.subspan(4), "");
109   EXPECT_DEATH_IF_SUPPORTED(span.first(4), "");
110   EXPECT_DEATH_IF_SUPPORTED(span.last(4), "");
111   // Accessing an empty span.
112   Span<const int> empty(v, 0);
113   EXPECT_DEATH_IF_SUPPORTED(empty[0], "");
114   EXPECT_DEATH_IF_SUPPORTED(empty.front(), "");
115   EXPECT_DEATH_IF_SUPPORTED(empty.back(), "");
116 }
117 
118 }  // namespace
119 BSSL_NAMESPACE_END
120