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 <stdio.h>
16 #include <string.h>
17
18 #include <openssl/pool.h>
19
20 #include "../internal.h"
21
22
TestUnpooled()23 static bool TestUnpooled() {
24 static const uint8_t kData[4] = {1, 2, 3, 4};
25 bssl::UniquePtr<CRYPTO_BUFFER> buf(
26 CRYPTO_BUFFER_new(kData, sizeof(kData), nullptr));
27 if (!buf) {
28 return false;
29 }
30
31 if (CRYPTO_BUFFER_len(buf.get()) != sizeof(kData) ||
32 OPENSSL_memcmp(kData, CRYPTO_BUFFER_data(buf.get()), sizeof(kData)) !=
33 0) {
34 fprintf(stderr, "CRYPTO_BUFFER corrupted data.\n");
35 return false;
36 }
37
38 CRYPTO_BUFFER_up_ref(buf.get());
39 bssl::UniquePtr<CRYPTO_BUFFER> buf2(buf.get());
40
41 return true;
42 }
43
TestEmpty()44 static bool TestEmpty() {
45 bssl::UniquePtr<CRYPTO_BUFFER> buf(CRYPTO_BUFFER_new(nullptr, 0, nullptr));
46 if (!buf) {
47 return false;
48 }
49
50 return true;
51 }
52
TestPool()53 static bool TestPool() {
54 bssl::UniquePtr<CRYPTO_BUFFER_POOL> pool(CRYPTO_BUFFER_POOL_new());
55 if (!pool) {
56 return false;
57 }
58
59 static const uint8_t kData[4] = {1, 2, 3, 4};
60 bssl::UniquePtr<CRYPTO_BUFFER> buf(
61 CRYPTO_BUFFER_new(kData, sizeof(kData), pool.get()));
62 if (!buf) {
63 return false;
64 }
65
66 bssl::UniquePtr<CRYPTO_BUFFER> buf2(
67 CRYPTO_BUFFER_new(kData, sizeof(kData), pool.get()));
68 if (!buf2) {
69 return false;
70 }
71
72 if (buf.get() != buf2.get()) {
73 fprintf(stderr, "CRYPTO_BUFFER_POOL did not dedup data.\n");
74 return false;
75 }
76
77 return true;
78 }
79
main(int argc,char ** argv)80 int main(int argc, char **argv) {
81 if (!TestUnpooled() ||
82 !TestEmpty() ||
83 !TestPool()) {
84 return 1;
85 }
86
87 printf("PASS\n");
88 return 0;
89 }
90