2014-06-26 18:26:42 +01:00
|
|
|
/* Copyright (c) 2014, Google Inc.
|
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
|
|
|
|
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
|
|
|
|
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-10-02 23:35:35 +01:00
|
|
|
#if !defined(_POSIX_C_SOURCE)
|
2014-10-02 21:58:19 +01:00
|
|
|
#define _POSIX_C_SOURCE 201410L
|
2014-10-02 23:35:35 +01:00
|
|
|
#endif
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-10-01 01:51:57 +01:00
|
|
|
#include <openssl/base.h>
|
|
|
|
|
|
|
|
#if !defined(OPENSSL_WINDOWS)
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <netinet/in.h>
|
2014-08-04 20:06:28 +01:00
|
|
|
#include <string.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <unistd.h>
|
2014-10-01 01:51:57 +01:00
|
|
|
#else
|
|
|
|
#include <io.h>
|
2016-06-09 21:48:33 +01:00
|
|
|
OPENSSL_MSVC_PRAGMA(warning(push, 3))
|
2015-03-20 23:32:23 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
2016-06-09 21:48:33 +01:00
|
|
|
OPENSSL_MSVC_PRAGMA(warning(pop))
|
2014-10-01 01:51:57 +01:00
|
|
|
#endif
|
2014-06-20 20:00:00 +01:00
|
|
|
|
|
|
|
#include <openssl/bio.h>
|
2014-09-12 00:11:15 +01:00
|
|
|
#include <openssl/crypto.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
#include <openssl/err.h>
|
2015-03-28 07:12:01 +00:00
|
|
|
#include <openssl/mem.h>
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
#include "../test/scoped_types.h"
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-10-01 01:51:57 +01:00
|
|
|
#if !defined(OPENSSL_WINDOWS)
|
|
|
|
static int closesocket(int sock) {
|
|
|
|
return close(sock);
|
|
|
|
}
|
2014-10-01 17:57:25 +01:00
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
static void PrintSocketError(const char *func) {
|
2014-10-01 17:57:25 +01:00
|
|
|
perror(func);
|
|
|
|
}
|
|
|
|
#else
|
2015-03-26 05:38:04 +00:00
|
|
|
static void PrintSocketError(const char *func) {
|
2014-10-01 17:57:25 +01:00
|
|
|
fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
|
|
|
|
}
|
2014-10-01 01:51:57 +01:00
|
|
|
#endif
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
class ScopedSocket {
|
|
|
|
public:
|
|
|
|
ScopedSocket(int sock) : sock_(sock) {}
|
|
|
|
~ScopedSocket() {
|
|
|
|
closesocket(sock_);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const int sock_;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool TestSocketConnect() {
|
2014-06-20 20:00:00 +01:00
|
|
|
static const char kTestMessage[] = "test";
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
int listening_sock = socket(AF_INET, SOCK_STREAM, 0);
|
|
|
|
if (listening_sock == -1) {
|
|
|
|
PrintSocketError("socket");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ScopedSocket listening_sock_closer(listening_sock);
|
|
|
|
|
|
|
|
struct sockaddr_in sin;
|
2014-06-20 20:00:00 +01:00
|
|
|
memset(&sin, 0, sizeof(sin));
|
|
|
|
sin.sin_family = AF_INET;
|
2014-10-01 01:51:57 +01:00
|
|
|
if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
|
2015-03-26 05:38:04 +00:00
|
|
|
PrintSocketError("inet_pton");
|
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
if (bind(listening_sock, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
|
2015-03-26 05:38:04 +00:00
|
|
|
PrintSocketError("bind");
|
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
if (listen(listening_sock, 1)) {
|
2015-03-26 05:38:04 +00:00
|
|
|
PrintSocketError("listen");
|
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-03-26 05:38:04 +00:00
|
|
|
socklen_t sockaddr_len = sizeof(sin);
|
2014-06-20 20:00:00 +01:00
|
|
|
if (getsockname(listening_sock, (struct sockaddr *)&sin, &sockaddr_len) ||
|
|
|
|
sockaddr_len != sizeof(sin)) {
|
2015-03-26 05:38:04 +00:00
|
|
|
PrintSocketError("getsockname");
|
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
char hostname[80];
|
2014-10-01 01:51:57 +01:00
|
|
|
BIO_snprintf(hostname, sizeof(hostname), "%s:%d", "127.0.0.1",
|
|
|
|
ntohs(sin.sin_port));
|
2015-03-26 05:38:04 +00:00
|
|
|
ScopedBIO bio(BIO_new_connect(hostname));
|
2014-06-20 20:00:00 +01:00
|
|
|
if (!bio) {
|
|
|
|
fprintf(stderr, "BIO_new_connect failed.\n");
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
if (BIO_write(bio.get(), kTestMessage, sizeof(kTestMessage)) !=
|
2014-06-20 20:00:00 +01:00
|
|
|
sizeof(kTestMessage)) {
|
|
|
|
fprintf(stderr, "BIO_write failed.\n");
|
2015-04-10 03:21:10 +01:00
|
|
|
ERR_print_errors_fp(stderr);
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
int sock = accept(listening_sock, (struct sockaddr *) &sin, &sockaddr_len);
|
|
|
|
if (sock == -1) {
|
|
|
|
PrintSocketError("accept");
|
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
2015-03-26 05:38:04 +00:00
|
|
|
ScopedSocket sock_closer(sock);
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
char buf[5];
|
2014-10-01 17:57:25 +01:00
|
|
|
if (recv(sock, buf, sizeof(buf), 0) != sizeof(kTestMessage)) {
|
2015-03-26 05:38:04 +00:00
|
|
|
PrintSocketError("read");
|
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
if (memcmp(buf, kTestMessage, sizeof(kTestMessage))) {
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
return true;
|
2014-06-20 20:00:00 +01:00
|
|
|
}
|
|
|
|
|
2014-10-14 11:03:05 +01:00
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
// BioReadZeroCopyWrapper is a wrapper around the zero-copy APIs to make
|
|
|
|
// testing easier.
|
|
|
|
static size_t BioReadZeroCopyWrapper(BIO *bio, uint8_t *data, size_t len) {
|
2014-12-01 18:22:47 +00:00
|
|
|
uint8_t *read_buf;
|
2014-10-14 11:03:05 +01:00
|
|
|
size_t read_buf_offset;
|
|
|
|
size_t available_bytes;
|
|
|
|
size_t len_read = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!BIO_zero_copy_get_read_buf(bio, &read_buf, &read_buf_offset,
|
|
|
|
&available_bytes)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
available_bytes = std::min(available_bytes, len - len_read);
|
2014-10-14 11:03:05 +01:00
|
|
|
memmove(data + len_read, read_buf + read_buf_offset, available_bytes);
|
|
|
|
|
|
|
|
BIO_zero_copy_get_read_buf_done(bio, available_bytes);
|
|
|
|
|
|
|
|
len_read += available_bytes;
|
|
|
|
} while (len - len_read > 0 && available_bytes > 0);
|
|
|
|
|
|
|
|
return len_read;
|
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
// BioWriteZeroCopyWrapper is a wrapper around the zero-copy APIs to make
|
|
|
|
// testing easier.
|
|
|
|
static size_t BioWriteZeroCopyWrapper(BIO *bio, const uint8_t *data,
|
|
|
|
size_t len) {
|
2014-12-01 18:22:47 +00:00
|
|
|
uint8_t *write_buf;
|
2014-10-14 11:03:05 +01:00
|
|
|
size_t write_buf_offset;
|
|
|
|
size_t available_bytes;
|
|
|
|
size_t len_written = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
if (!BIO_zero_copy_get_write_buf(bio, &write_buf, &write_buf_offset,
|
|
|
|
&available_bytes)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
available_bytes = std::min(available_bytes, len - len_written);
|
2014-10-14 11:03:05 +01:00
|
|
|
memmove(write_buf + write_buf_offset, data + len_written, available_bytes);
|
|
|
|
|
|
|
|
BIO_zero_copy_get_write_buf_done(bio, available_bytes);
|
|
|
|
|
|
|
|
len_written += available_bytes;
|
|
|
|
} while (len - len_written > 0 && available_bytes > 0);
|
|
|
|
|
|
|
|
return len_written;
|
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
static bool TestZeroCopyBioPairs() {
|
|
|
|
// Test read and write, especially triggering the ring buffer wrap-around.
|
2014-10-14 11:03:05 +01:00
|
|
|
uint8_t bio1_application_send_buffer[1024];
|
|
|
|
uint8_t bio2_application_recv_buffer[1024];
|
|
|
|
|
|
|
|
const size_t kLengths[] = {254, 255, 256, 257, 510, 511, 512, 513};
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
// These trigger ring buffer wrap around.
|
2014-10-14 11:03:05 +01:00
|
|
|
const size_t kPartialLengths[] = {0, 1, 2, 3, 128, 255, 256, 257, 511, 512};
|
|
|
|
|
|
|
|
static const size_t kBufferSize = 512;
|
|
|
|
|
|
|
|
srand(1);
|
2015-03-26 05:38:04 +00:00
|
|
|
for (size_t i = 0; i < sizeof(bio1_application_send_buffer); i++) {
|
2014-10-14 11:03:05 +01:00
|
|
|
bio1_application_send_buffer[i] = rand() & 255;
|
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
// Transfer bytes from bio1_application_send_buffer to
|
|
|
|
// bio2_application_recv_buffer in various ways.
|
|
|
|
for (size_t i = 0; i < sizeof(kLengths) / sizeof(kLengths[0]); i++) {
|
|
|
|
for (size_t j = 0; j < sizeof(kPartialLengths) / sizeof(kPartialLengths[0]);
|
|
|
|
j++) {
|
|
|
|
size_t total_write = 0;
|
|
|
|
size_t total_read = 0;
|
|
|
|
|
|
|
|
BIO *bio1, *bio2;
|
|
|
|
if (!BIO_new_bio_pair(&bio1, kBufferSize, &bio2, kBufferSize)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ScopedBIO bio1_scoper(bio1);
|
|
|
|
ScopedBIO bio2_scoper(bio2);
|
2014-10-14 11:03:05 +01:00
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
total_write += BioWriteZeroCopyWrapper(
|
2014-10-14 11:03:05 +01:00
|
|
|
bio1, bio1_application_send_buffer, kLengths[i]);
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
// This tests interleaved read/write calls. Do a read between zero copy
|
|
|
|
// write calls.
|
|
|
|
uint8_t *write_buf;
|
|
|
|
size_t write_buf_offset;
|
|
|
|
size_t available_bytes;
|
2014-10-14 11:03:05 +01:00
|
|
|
if (!BIO_zero_copy_get_write_buf(bio1, &write_buf, &write_buf_offset,
|
|
|
|
&available_bytes)) {
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-10-14 11:03:05 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
// Free kPartialLengths[j] bytes in the beginning of bio1 write buffer.
|
|
|
|
// This enables ring buffer wrap around for the next write.
|
2014-10-14 11:03:05 +01:00
|
|
|
total_read += BIO_read(bio2, bio2_application_recv_buffer + total_read,
|
|
|
|
kPartialLengths[j]);
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
size_t interleaved_write_len = std::min(kPartialLengths[j],
|
|
|
|
available_bytes);
|
2014-10-14 11:03:05 +01:00
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
// Write the data for the interleaved write call. If the buffer becomes
|
|
|
|
// empty after a read, the write offset is normally set to 0. Check that
|
|
|
|
// this does not happen for interleaved read/write and that
|
|
|
|
// |write_buf_offset| is still valid.
|
2014-10-14 11:03:05 +01:00
|
|
|
memcpy(write_buf + write_buf_offset,
|
|
|
|
bio1_application_send_buffer + total_write, interleaved_write_len);
|
|
|
|
if (BIO_zero_copy_get_write_buf_done(bio1, interleaved_write_len)) {
|
|
|
|
total_write += interleaved_write_len;
|
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
// Do another write in case |write_buf_offset| was wrapped.
|
|
|
|
total_write += BioWriteZeroCopyWrapper(
|
2014-10-14 11:03:05 +01:00
|
|
|
bio1, bio1_application_send_buffer + total_write,
|
|
|
|
kPartialLengths[j] - interleaved_write_len);
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
// Drain the rest.
|
|
|
|
size_t bytes_left = BIO_pending(bio2);
|
|
|
|
total_read += BioReadZeroCopyWrapper(
|
2014-10-14 11:03:05 +01:00
|
|
|
bio2, bio2_application_recv_buffer + total_read, bytes_left);
|
|
|
|
|
|
|
|
if (total_read != total_write) {
|
|
|
|
fprintf(stderr, "Lengths not equal in round (%u, %u)\n", (unsigned)i,
|
|
|
|
(unsigned)j);
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-10-14 11:03:05 +01:00
|
|
|
}
|
|
|
|
if (total_read > kLengths[i] + kPartialLengths[j]) {
|
|
|
|
fprintf(stderr, "Bad lengths in round (%u, %u)\n", (unsigned)i,
|
|
|
|
(unsigned)j);
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-10-14 11:03:05 +01:00
|
|
|
}
|
|
|
|
if (memcmp(bio1_application_send_buffer, bio2_application_recv_buffer,
|
|
|
|
total_read) != 0) {
|
|
|
|
fprintf(stderr, "Buffers not equal in round (%u, %u)\n", (unsigned)i,
|
|
|
|
(unsigned)j);
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-10-14 11:03:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
return true;
|
2014-10-14 11:03:05 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
static bool TestPrintf() {
|
|
|
|
// Test a short output, a very long one, and various sizes around
|
|
|
|
// 256 (the size of the buffer) to ensure edge cases are correct.
|
2014-08-04 20:06:28 +01:00
|
|
|
static const size_t kLengths[] = { 5, 250, 251, 252, 253, 254, 1023 };
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
ScopedBIO bio(BIO_new(BIO_s_mem()));
|
2014-08-04 20:06:28 +01:00
|
|
|
if (!bio) {
|
|
|
|
fprintf(stderr, "BIO_new failed\n");
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-08-04 20:06:28 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
for (size_t i = 0; i < sizeof(kLengths) / sizeof(kLengths[0]); i++) {
|
|
|
|
char string[1024];
|
2014-08-04 20:06:28 +01:00
|
|
|
if (kLengths[i] >= sizeof(string)) {
|
|
|
|
fprintf(stderr, "Bad test string length\n");
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-08-04 20:06:28 +01:00
|
|
|
}
|
|
|
|
memset(string, 'a', sizeof(string));
|
|
|
|
string[kLengths[i]] = '\0';
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
int ret = BIO_printf(bio.get(), "test %s", string);
|
|
|
|
if (ret < 0 || static_cast<size_t>(ret) != 5 + kLengths[i]) {
|
2014-10-01 17:57:25 +01:00
|
|
|
fprintf(stderr, "BIO_printf failed: %d\n", ret);
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-08-04 20:06:28 +01:00
|
|
|
}
|
2015-03-26 05:38:04 +00:00
|
|
|
const uint8_t *contents;
|
|
|
|
size_t len;
|
|
|
|
if (!BIO_mem_contents(bio.get(), &contents, &len)) {
|
2014-08-04 20:06:28 +01:00
|
|
|
fprintf(stderr, "BIO_mem_contents failed\n");
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-08-04 20:06:28 +01:00
|
|
|
}
|
|
|
|
if (len != 5 + kLengths[i] ||
|
|
|
|
strncmp((const char *)contents, "test ", 5) != 0 ||
|
|
|
|
strncmp((const char *)contents + 5, string, kLengths[i]) != 0) {
|
|
|
|
fprintf(stderr, "Contents did not match: %.*s\n", (int)len, contents);
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-08-04 20:06:28 +01:00
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
if (!BIO_reset(bio.get())) {
|
2014-08-04 20:06:28 +01:00
|
|
|
fprintf(stderr, "BIO_reset failed\n");
|
2015-03-26 05:38:04 +00:00
|
|
|
return false;
|
2014-08-04 20:06:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
return true;
|
2014-08-04 20:06:28 +01:00
|
|
|
}
|
|
|
|
|
2015-05-19 01:25:20 +01:00
|
|
|
static bool ReadASN1(bool should_succeed, const uint8_t *data, size_t data_len,
|
|
|
|
size_t expected_len, size_t max_len) {
|
2016-02-24 19:00:22 +00:00
|
|
|
ScopedBIO bio(BIO_new_mem_buf(data, data_len));
|
2015-05-19 01:25:20 +01:00
|
|
|
|
|
|
|
uint8_t *out;
|
|
|
|
size_t out_len;
|
|
|
|
int ok = BIO_read_asn1(bio.get(), &out, &out_len, max_len);
|
|
|
|
if (!ok) {
|
|
|
|
out = nullptr;
|
|
|
|
}
|
|
|
|
ScopedOpenSSLBytes out_storage(out);
|
|
|
|
|
|
|
|
if (should_succeed != (ok == 1)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (should_succeed &&
|
|
|
|
(out_len != expected_len || memcmp(data, out, expected_len) != 0)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool TestASN1() {
|
|
|
|
static const uint8_t kData1[] = {0x30, 2, 1, 2, 0, 0};
|
|
|
|
static const uint8_t kData2[] = {0x30, 3, 1, 2}; /* truncated */
|
|
|
|
static const uint8_t kData3[] = {0x30, 0x81, 1, 1}; /* should be short len */
|
|
|
|
static const uint8_t kData4[] = {0x30, 0x82, 0, 1, 1}; /* zero padded. */
|
|
|
|
|
|
|
|
if (!ReadASN1(true, kData1, sizeof(kData1), 4, 100) ||
|
|
|
|
!ReadASN1(false, kData2, sizeof(kData2), 0, 100) ||
|
|
|
|
!ReadASN1(false, kData3, sizeof(kData3), 0, 100) ||
|
|
|
|
!ReadASN1(false, kData4, sizeof(kData4), 0, 100)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const size_t kLargePayloadLen = 8000;
|
|
|
|
static const uint8_t kLargePrefix[] = {0x30, 0x82, kLargePayloadLen >> 8,
|
|
|
|
kLargePayloadLen & 0xff};
|
|
|
|
ScopedOpenSSLBytes large(reinterpret_cast<uint8_t *>(
|
|
|
|
OPENSSL_malloc(sizeof(kLargePrefix) + kLargePayloadLen)));
|
|
|
|
if (!large) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
memset(large.get() + sizeof(kLargePrefix), 0, kLargePayloadLen);
|
|
|
|
memcpy(large.get(), kLargePrefix, sizeof(kLargePrefix));
|
|
|
|
|
|
|
|
if (!ReadASN1(true, large.get(), sizeof(kLargePrefix) + kLargePayloadLen,
|
|
|
|
sizeof(kLargePrefix) + kLargePayloadLen,
|
|
|
|
kLargePayloadLen * 2)) {
|
|
|
|
fprintf(stderr, "Large payload test failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReadASN1(false, large.get(), sizeof(kLargePrefix) + kLargePayloadLen,
|
|
|
|
sizeof(kLargePrefix) + kLargePayloadLen,
|
|
|
|
kLargePayloadLen - 1)) {
|
|
|
|
fprintf(stderr, "max_len test failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const uint8_t kIndefPrefix[] = {0x30, 0x80};
|
|
|
|
memcpy(large.get(), kIndefPrefix, sizeof(kIndefPrefix));
|
|
|
|
if (!ReadASN1(true, large.get(), sizeof(kLargePrefix) + kLargePayloadLen,
|
|
|
|
sizeof(kLargePrefix) + kLargePayloadLen,
|
|
|
|
kLargePayloadLen*2)) {
|
|
|
|
fprintf(stderr, "indefinite length test failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ReadASN1(false, large.get(), sizeof(kLargePrefix) + kLargePayloadLen,
|
|
|
|
sizeof(kLargePrefix) + kLargePayloadLen,
|
|
|
|
kLargePayloadLen-1)) {
|
|
|
|
fprintf(stderr, "indefinite length, max_len test failed.\n");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-20 21:24:00 +01:00
|
|
|
int main(void) {
|
2014-09-12 00:11:15 +01:00
|
|
|
CRYPTO_library_init();
|
2014-06-20 20:00:00 +01:00
|
|
|
|
2014-10-01 17:57:25 +01:00
|
|
|
#if defined(OPENSSL_WINDOWS)
|
2015-03-26 05:38:04 +00:00
|
|
|
// Initialize Winsock.
|
|
|
|
WORD wsa_version = MAKEWORD(2, 2);
|
|
|
|
WSADATA wsa_data;
|
|
|
|
int wsa_err = WSAStartup(wsa_version, &wsa_data);
|
2014-10-01 17:57:25 +01:00
|
|
|
if (wsa_err != 0) {
|
|
|
|
fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (wsa_data.wVersion != wsa_version) {
|
|
|
|
fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-03-26 05:38:04 +00:00
|
|
|
if (!TestSocketConnect() ||
|
|
|
|
!TestPrintf() ||
|
2015-05-19 01:25:20 +01:00
|
|
|
!TestZeroCopyBioPairs() ||
|
|
|
|
!TestASN1()) {
|
2014-10-14 11:03:05 +01:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-06-20 20:00:00 +01:00
|
|
|
printf("PASS\n");
|
|
|
|
return 0;
|
|
|
|
}
|