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 <openssl/buf.h>
16 
17 #include <string.h>
18 
19 #include <string>
20 
21 #include <gtest/gtest.h>
22 
23 
TEST(BufTest,Basic)24 TEST(BufTest, Basic) {
25   bssl::UniquePtr<BUF_MEM> buf(BUF_MEM_new());
26   ASSERT_TRUE(buf);
27   EXPECT_EQ(0u, buf->length);
28 
29   // Use BUF_MEM_reserve to increase buf->max.
30   ASSERT_TRUE(BUF_MEM_reserve(buf.get(), 200));
31   EXPECT_GE(buf->max, 200u);
32   EXPECT_EQ(0u, buf->length);
33 
34   // BUF_MEM_reserve with a smaller cap is a no-op.
35   size_t old_max = buf->max;
36   ASSERT_TRUE(BUF_MEM_reserve(buf.get(), 100));
37   EXPECT_EQ(old_max, buf->max);
38   EXPECT_EQ(0u, buf->length);
39 
40   // BUF_MEM_grow can increase the length without reallocating.
41   ASSERT_EQ(100u, BUF_MEM_grow(buf.get(), 100));
42   EXPECT_EQ(100u, buf->length);
43   EXPECT_EQ(old_max, buf->max);
44   memset(buf->data, 'A', buf->length);
45 
46   // If BUF_MEM_reserve reallocates, it preserves the contents.
47   ASSERT_TRUE(BUF_MEM_reserve(buf.get(), old_max + 1));
48   ASSERT_GE(buf->max, old_max + 1);
49   EXPECT_EQ(100u, buf->length);
50   for (size_t i = 0; i < 100; i++) {
51     EXPECT_EQ('A', buf->data[i]);
52   }
53 
54   // BUF_MEM_grow should zero everything beyond buf->length.
55   memset(buf->data, 'B', buf->max);
56   ASSERT_EQ(150u, BUF_MEM_grow(buf.get(), 150));
57   EXPECT_EQ(150u, buf->length);
58   for (size_t i = 0; i < 100; i++) {
59     EXPECT_EQ('B', buf->data[i]);
60   }
61   for (size_t i = 100; i < 150; i++) {
62     EXPECT_EQ(0, buf->data[i]);
63   }
64 
65   // BUF_MEM_grow can rellocate if necessary.
66   size_t new_len = buf->max + 1;
67   ASSERT_EQ(new_len, BUF_MEM_grow(buf.get(), new_len));
68   EXPECT_GE(buf->max, new_len);
69   EXPECT_EQ(new_len, buf->length);
70   for (size_t i = 0; i < 100; i++) {
71     EXPECT_EQ('B', buf->data[i]);
72   }
73   for (size_t i = 100; i < new_len; i++) {
74     EXPECT_EQ(0, buf->data[i]);
75   }
76 
77   // BUF_MEM_grow can shink.
78   ASSERT_EQ(50u, BUF_MEM_grow(buf.get(), 50));
79   EXPECT_EQ(50u, buf->length);
80   for (size_t i = 0; i < 50; i++) {
81     EXPECT_EQ('B', buf->data[i]);
82   }
83 }
84 
TEST(BufTest,Append)85 TEST(BufTest, Append) {
86   bssl::UniquePtr<BUF_MEM> buf(BUF_MEM_new());
87   ASSERT_TRUE(buf);
88 
89   ASSERT_TRUE(BUF_MEM_append(buf.get(), nullptr, 0));
90   ASSERT_TRUE(BUF_MEM_append(buf.get(), "hello ", 6));
91   ASSERT_TRUE(BUF_MEM_append(buf.get(), nullptr, 0));
92   ASSERT_TRUE(BUF_MEM_append(buf.get(), "world", 5));
93   std::string str(128, 'A');
94   ASSERT_TRUE(BUF_MEM_append(buf.get(), str.data(), str.size()));
95 
96   EXPECT_EQ("hello world" + str, std::string(buf->data, buf->length));
97 }
98