Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / bio / bio_test.c
1 /* Copyright (c) 2014, 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 #if !defined(_POSIX_C_SOURCE)
16 #define _POSIX_C_SOURCE 201410L
17 #endif
18
19 #include <openssl/base.h>
20
21 #if !defined(OPENSSL_WINDOWS)
22 #include <arpa/inet.h>
23 #include <fcntl.h>
24 #include <netinet/in.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <unistd.h>
28 #else
29 #include <io.h>
30 #include <WinSock2.h>
31 #include <WS2tcpip.h>
32 #endif
33
34 #include <openssl/bio.h>
35 #include <openssl/crypto.h>
36 #include <openssl/err.h>
37
38
39 #if !defined(OPENSSL_WINDOWS)
40 static int closesocket(int sock) {
41   return close(sock);
42 }
43
44 static void print_socket_error(const char *func) {
45   perror(func);
46 }
47 #else
48 static void print_socket_error(const char *func) {
49   fprintf(stderr, "%s: %d\n", func, WSAGetLastError());
50 }
51 #endif
52
53 static int test_socket_connect(void) {
54   int listening_sock = socket(AF_INET, SOCK_STREAM, 0);
55   int sock;
56   struct sockaddr_in sin;
57   socklen_t sockaddr_len = sizeof(sin);
58   static const char kTestMessage[] = "test";
59   char hostname[80], buf[5];
60   BIO *bio;
61
62   memset(&sin, 0, sizeof(sin));
63   sin.sin_family = AF_INET;
64   if (!inet_pton(AF_INET, "127.0.0.1", &sin.sin_addr)) {
65     print_socket_error("inet_pton");
66     return 0;
67   }
68
69   if (bind(listening_sock, (struct sockaddr *)&sin, sizeof(sin)) != 0) {
70     print_socket_error("bind");
71     return 0;
72   }
73
74   if (listen(listening_sock, 1)) {
75     print_socket_error("listen");
76     return 0;
77   }
78
79   if (getsockname(listening_sock, (struct sockaddr *)&sin, &sockaddr_len) ||
80       sockaddr_len != sizeof(sin)) {
81     print_socket_error("getsockname");
82     return 0;
83   }
84
85   BIO_snprintf(hostname, sizeof(hostname), "%s:%d", "127.0.0.1",
86                ntohs(sin.sin_port));
87   bio = BIO_new_connect(hostname);
88   if (!bio) {
89     fprintf(stderr, "BIO_new_connect failed.\n");
90     return 0;
91   }
92
93   if (BIO_write(bio, kTestMessage, sizeof(kTestMessage)) !=
94       sizeof(kTestMessage)) {
95     fprintf(stderr, "BIO_write failed.\n");
96     BIO_print_errors_fp(stderr);
97     return 0;
98   }
99
100   sock = accept(listening_sock, (struct sockaddr *) &sin, &sockaddr_len);
101   if (sock < 0) {
102     print_socket_error("accept");
103     return 0;
104   }
105
106   if (recv(sock, buf, sizeof(buf), 0) != sizeof(kTestMessage)) {
107     print_socket_error("read");
108     return 0;
109   }
110
111   if (memcmp(buf, kTestMessage, sizeof(kTestMessage))) {
112     return 0;
113   }
114
115   closesocket(sock);
116   closesocket(listening_sock);
117   BIO_free(bio);
118
119   return 1;
120 }
121
122 static int test_printf(void) {
123   /* Test a short output, a very long one, and various sizes around
124    * 256 (the size of the buffer) to ensure edge cases are correct. */
125   static const size_t kLengths[] = { 5, 250, 251, 252, 253, 254, 1023 };
126   BIO *bio;
127   char string[1024];
128   int ret;
129   const uint8_t *contents;
130   size_t i, len;
131
132   bio = BIO_new(BIO_s_mem());
133   if (!bio) {
134     fprintf(stderr, "BIO_new failed\n");
135     return 0;
136   }
137
138   for (i = 0; i < sizeof(kLengths) / sizeof(kLengths[0]); i++) {
139     if (kLengths[i] >= sizeof(string)) {
140       fprintf(stderr, "Bad test string length\n");
141       return 0;
142     }
143     memset(string, 'a', sizeof(string));
144     string[kLengths[i]] = '\0';
145
146     ret = BIO_printf(bio, "test %s", string);
147     if (ret != 5 + kLengths[i]) {
148       fprintf(stderr, "BIO_printf failed: %d\n", ret);
149       return 0;
150     }
151     if (!BIO_mem_contents(bio, &contents, &len)) {
152       fprintf(stderr, "BIO_mem_contents failed\n");
153       return 0;
154     }
155     if (len != 5 + kLengths[i] ||
156         strncmp((const char *)contents, "test ", 5) != 0 ||
157         strncmp((const char *)contents + 5, string, kLengths[i]) != 0) {
158       fprintf(stderr, "Contents did not match: %.*s\n", (int)len, contents);
159       return 0;
160     }
161
162     if (!BIO_reset(bio)) {
163       fprintf(stderr, "BIO_reset failed\n");
164       return 0;
165     }
166   }
167
168   BIO_free(bio);
169   return 1;
170 }
171
172 int main(void) {
173 #if defined(OPENSSL_WINDOWS)
174   WSADATA wsa_data;
175   WORD wsa_version;
176   int wsa_err;
177 #endif
178
179   CRYPTO_library_init();
180   ERR_load_crypto_strings();
181
182 #if defined(OPENSSL_WINDOWS)
183   /* Initialize Winsock. */
184   wsa_version = MAKEWORD(2, 2);
185   wsa_err = WSAStartup(wsa_version, &wsa_data);
186   if (wsa_err != 0) {
187     fprintf(stderr, "WSAStartup failed: %d\n", wsa_err);
188     return 1;
189   }
190   if (wsa_data.wVersion != wsa_version) {
191     fprintf(stderr, "Didn't get expected version: %x\n", wsa_data.wVersion);
192     return 1;
193   }
194 #endif
195
196   if (!test_socket_connect()) {
197     return 1;
198   }
199
200   if (!test_printf()) {
201     return 1;
202   }
203
204   printf("PASS\n");
205   return 0;
206 }