• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright (c) 2017, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
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