1 /* Copyright (c) 2016, 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 <gtest/gtest.h>
16 
17 #include <openssl/pool.h>
18 
19 #include "../test/test_util.h"
20 
21 #if defined(OPENSSL_THREADS)
22 #include <chrono>
23 #include <thread>
24 #endif
25 
26 
TEST(PoolTest,Unpooled)27 TEST(PoolTest, Unpooled) {
28   static const uint8_t kData[4] = {1, 2, 3, 4};
29   bssl::UniquePtr<CRYPTO_BUFFER> buf(
30       CRYPTO_BUFFER_new(kData, sizeof(kData), nullptr));
31   ASSERT_TRUE(buf);
32 
33   EXPECT_EQ(Bytes(kData),
34             Bytes(CRYPTO_BUFFER_data(buf.get()), CRYPTO_BUFFER_len(buf.get())));
35 
36   // Test that reference-counting works properly.
37   bssl::UniquePtr<CRYPTO_BUFFER> buf2 = bssl::UpRef(buf);
38 }
39 
TEST(PoolTest,Empty)40 TEST(PoolTest, Empty) {
41   bssl::UniquePtr<CRYPTO_BUFFER> buf(CRYPTO_BUFFER_new(nullptr, 0, nullptr));
42   ASSERT_TRUE(buf);
43 
44   EXPECT_EQ(Bytes(""),
45             Bytes(CRYPTO_BUFFER_data(buf.get()), CRYPTO_BUFFER_len(buf.get())));
46 }
47 
TEST(PoolTest,Pooled)48 TEST(PoolTest, Pooled) {
49   bssl::UniquePtr<CRYPTO_BUFFER_POOL> pool(CRYPTO_BUFFER_POOL_new());
50   ASSERT_TRUE(pool);
51 
52   static const uint8_t kData[4] = {1, 2, 3, 4};
53   bssl::UniquePtr<CRYPTO_BUFFER> buf(
54       CRYPTO_BUFFER_new(kData, sizeof(kData), pool.get()));
55   ASSERT_TRUE(buf);
56 
57   bssl::UniquePtr<CRYPTO_BUFFER> buf2(
58       CRYPTO_BUFFER_new(kData, sizeof(kData), pool.get()));
59   ASSERT_TRUE(buf2);
60 
61   EXPECT_EQ(buf.get(), buf2.get()) << "CRYPTO_BUFFER_POOL did not dedup data.";
62 }
63 
64 #if defined(OPENSSL_THREADS)
TEST(PoolTest,Threads)65 TEST(PoolTest, Threads) {
66   bssl::UniquePtr<CRYPTO_BUFFER_POOL> pool(CRYPTO_BUFFER_POOL_new());
67   ASSERT_TRUE(pool);
68 
69   // Race threads making pooled |CRYPTO_BUFFER|s.
70   static const uint8_t kData[4] = {1, 2, 3, 4};
71   static const uint8_t kData2[3] = {4, 5, 6};
72   bssl::UniquePtr<CRYPTO_BUFFER> buf, buf2, buf3;
73   {
74     std::thread thread([&] {
75       buf.reset(CRYPTO_BUFFER_new(kData, sizeof(kData), pool.get()));
76     });
77     std::thread thread2([&] {
78       buf2.reset(CRYPTO_BUFFER_new(kData, sizeof(kData), pool.get()));
79     });
80     buf3.reset(CRYPTO_BUFFER_new(kData2, sizeof(kData2), pool.get()));
81     thread.join();
82     thread2.join();
83   }
84 
85   ASSERT_TRUE(buf);
86   ASSERT_TRUE(buf2);
87   ASSERT_TRUE(buf3);
88   EXPECT_EQ(buf.get(), buf2.get()) << "CRYPTO_BUFFER_POOL did not dedup data.";
89   EXPECT_NE(buf.get(), buf3.get())
90       << "CRYPTO_BUFFER_POOL incorrectly deduped data.";
91   EXPECT_EQ(Bytes(kData),
92             Bytes(CRYPTO_BUFFER_data(buf.get()), CRYPTO_BUFFER_len(buf.get())));
93   EXPECT_EQ(Bytes(kData2), Bytes(CRYPTO_BUFFER_data(buf3.get()),
94                                  CRYPTO_BUFFER_len(buf3.get())));
95 
96   // Reference-counting of |CRYPTO_BUFFER| interacts with pooling. Race an
97   // increment and free.
98   {
99     bssl::UniquePtr<CRYPTO_BUFFER> buf_ref;
100     std::thread thread([&] { buf_ref = bssl::UpRef(buf); });
101     buf2.reset();
102     thread.join();
103   }
104 
105   // |buf|'s data is still valid.
106   EXPECT_EQ(Bytes(kData), Bytes(CRYPTO_BUFFER_data(buf.get()),
107                                 CRYPTO_BUFFER_len(buf.get())));
108 
109   // Race a thread re-creating the |CRYPTO_BUFFER| with another thread freeing
110   // it. Do this twice with sleeps so ThreadSanitizer can observe two different
111   // interleavings. Ideally we would run this test under a tool that could
112   // search all interleavings.
113   {
114     std::thread thread([&] {
115       std::this_thread::sleep_for(std::chrono::milliseconds(1));
116       buf.reset();
117     });
118     buf2.reset(CRYPTO_BUFFER_new(kData, sizeof(kData), pool.get()));
119     thread.join();
120 
121     ASSERT_TRUE(buf2);
122     EXPECT_EQ(Bytes(kData), Bytes(CRYPTO_BUFFER_data(buf2.get()),
123                                   CRYPTO_BUFFER_len(buf2.get())));
124     buf = std::move(buf2);
125   }
126 
127   {
128     std::thread thread([&] { buf.reset(); });
129     std::this_thread::sleep_for(std::chrono::milliseconds(1));
130     buf2.reset(CRYPTO_BUFFER_new(kData, sizeof(kData), pool.get()));
131     thread.join();
132 
133     ASSERT_TRUE(buf2);
134     EXPECT_EQ(Bytes(kData), Bytes(CRYPTO_BUFFER_data(buf2.get()),
135                                   CRYPTO_BUFFER_len(buf2.get())));
136     buf = std::move(buf2);
137   }
138 
139   // Finally, race the frees.
140   {
141     buf2 = bssl::UpRef(buf);
142     std::thread thread([&] { buf.reset(); });
143     std::thread thread2([&] { buf3.reset(); });
144     buf2.reset();
145     thread.join();
146     thread2.join();
147   }
148 }
149 #endif
150