Upstream version 9.38.198.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/err.h>
18 #include <openssl/mem.h>
19
20
21 static int test_overflow() {
22   unsigned i;
23
24   for (i = 0; i < ERR_NUM_ERRORS*2; i++) {
25     ERR_put_error(1, 2, 3, "test", 1);
26   }
27
28   for (i = 0; i < ERR_NUM_ERRORS - 1; i++) {
29     if (ERR_get_error() == 0) {
30       fprintf(stderr, "ERR_get_error failed at %u\n", i);
31       return 0;
32     }
33   }
34
35   if (ERR_get_error() != 0) {
36     fprintf(stderr, "ERR_get_error more than the expected number of values.\n");
37     return 0;
38   }
39
40   return 1;
41 }
42
43 static int test_put_error() {
44   uint32_t packed_error;
45   int line, flags;
46   const char *file;
47   char *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       (flags & ERR_FLAG_MALLOCED) == 0 ||
62       ERR_GET_LIB(packed_error) != 1 ||
63       ERR_GET_FUNC(packed_error) != 2 ||
64       ERR_GET_REASON(packed_error) != 3 ||
65       strcmp(data, "testing") != 0) {
66     fprintf(stderr, "Bad error data returned.\n");
67     return 0;
68   }
69
70   OPENSSL_free(data);
71
72   return 1;
73 }
74
75 static int test_clear_error() {
76   if (ERR_get_error() != 0) {
77     fprintf(stderr, "ERR_get_error returned value before an error was added.\n");
78     return 0;
79   }
80
81   ERR_put_error(1, 2, 3, "test", 4);
82   ERR_clear_error();
83
84   if (ERR_get_error() != 0) {
85     fprintf(stderr, "Error remained after clearing.\n");
86     return 0;
87   }
88
89   return 1;
90 }
91
92 static int test_print() {
93   size_t i;
94   char buf[256];
95   uint32_t packed_error;
96
97   ERR_put_error(1, 2, 3, "test", 4);
98   ERR_add_error_data(1, "testing");
99   packed_error = ERR_get_error();
100
101   for (i = 0; i <= sizeof(buf); i++) {
102     ERR_error_string_n(packed_error, buf, i);
103   }
104
105   return 1;
106 }
107
108 int main() {
109   if (!test_overflow() ||
110       !test_put_error() ||
111       !test_clear_error() ||
112       !test_print()) {
113     return 1;
114   }
115
116   printf("PASS\n");
117   return 0;
118 }