Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / boringssl / src / crypto / err / err_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 #include <stdio.h>
16
17 #include <openssl/crypto.h>
18 #include <openssl/err.h>
19 #include <openssl/mem.h>
20
21
22 static int test_overflow(void) {
23   unsigned i;
24
25   for (i = 0; i < ERR_NUM_ERRORS*2; i++) {
26     ERR_put_error(1, 2, 3, "test", 1);
27   }
28
29   for (i = 0; i < ERR_NUM_ERRORS - 1; i++) {
30     if (ERR_get_error() == 0) {
31       fprintf(stderr, "ERR_get_error failed at %u\n", i);
32       return 0;
33     }
34   }
35
36   if (ERR_get_error() != 0) {
37     fprintf(stderr, "ERR_get_error more than the expected number of values.\n");
38     return 0;
39   }
40
41   return 1;
42 }
43
44 static int test_put_error(void) {
45   uint32_t packed_error;
46   int line, flags;
47   const char *file, *data;
48
49   if (ERR_get_error() != 0) {
50     fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
51     return 0;
52   }
53
54   ERR_put_error(1, 2, 3, "test", 4);
55   ERR_add_error_data(1, "testing");
56
57   packed_error = ERR_get_error_line_data(&file, &line, &data, &flags);
58   if (strcmp(file, "test") != 0 ||
59       line != 4 ||
60       (flags & ERR_FLAG_STRING) == 0 ||
61       ERR_GET_LIB(packed_error) != 1 ||
62       ERR_GET_FUNC(packed_error) != 2 ||
63       ERR_GET_REASON(packed_error) != 3 ||
64       strcmp(data, "testing") != 0) {
65     fprintf(stderr, "Bad error data returned.\n");
66     return 0;
67   }
68
69   return 1;
70 }
71
72 static int test_clear_error(void) {
73   if (ERR_get_error() != 0) {
74     fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
75     return 0;
76   }
77
78   ERR_put_error(1, 2, 3, "test", 4);
79   ERR_clear_error();
80
81   if (ERR_get_error() != 0) {
82     fprintf(stderr, "Error remained after clearing.\n");
83     return 0;
84   }
85
86   return 1;
87 }
88
89 static int test_print(void) {
90   size_t i;
91   char buf[256];
92   uint32_t packed_error;
93
94   ERR_put_error(1, 2, 3, "test", 4);
95   ERR_add_error_data(1, "testing");
96   packed_error = ERR_get_error();
97
98   for (i = 0; i <= sizeof(buf); i++) {
99     ERR_error_string_n(packed_error, buf, i);
100   }
101
102   return 1;
103 }
104
105 static int test_release(void) {
106   ERR_put_error(1, 2, 3, "test", 4);
107   ERR_remove_thread_state(NULL);
108   return 1;
109 }
110
111 int main(void) {
112   CRYPTO_library_init();
113
114   if (!test_overflow() ||
115       !test_put_error() ||
116       !test_clear_error() ||
117       !test_print() ||
118       !test_release()) {
119     return 1;
120   }
121
122   printf("PASS\n");
123   return 0;
124 }