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 #include <string.h> 17 18 #include <openssl/crypto.h> 19 #include <openssl/err.h> 20 #include <openssl/mem.h> 21 22 23 static bool TestOverflow() { 24 for (unsigned i = 0; i < ERR_NUM_ERRORS*2; i++) { 25 ERR_put_error(1, 2, i+1, "test", 1); 26 } 27 28 for (unsigned i = 0; i < ERR_NUM_ERRORS - 1; i++) { 29 uint32_t err = ERR_get_error(); 30 /* Errors are returned in order they were pushed, with the least recent ones 31 * removed, up to |ERR_NUM_ERRORS - 1| errors. So the errors returned are 32 * |ERR_NUM_ERRORS + 2| through |ERR_NUM_ERRORS * 2|, inclusive. */ 33 if (err == 0 || ERR_GET_REASON(err) != i + ERR_NUM_ERRORS + 2) { 34 fprintf(stderr, "ERR_get_error failed at %u\n", i); 35 return false; 36 } 37 } 38 39 if (ERR_get_error() != 0) { 40 fprintf(stderr, "ERR_get_error more than the expected number of values.\n"); 41 return false; 42 } 43 44 return true; 45 } 46 47 static bool TestPutError() { 48 if (ERR_get_error() != 0) { 49 fprintf(stderr, "ERR_get_error returned value before an error was added.\n"); 50 return false; 51 } 52 53 ERR_put_error(1, 2, 3, "test", 4); 54 ERR_add_error_data(1, "testing"); 55 56 int peeked_line, line, peeked_flags, flags; 57 const char *peeked_file, *file, *peeked_data, *data; 58 uint32_t peeked_packed_error = 59 ERR_peek_error_line_data(&peeked_file, &peeked_line, &peeked_data, 60 &peeked_flags); 61 uint32_t packed_error = ERR_get_error_line_data(&file, &line, &data, &flags); 62 63 if (peeked_packed_error != packed_error || 64 peeked_file != file || 65 peeked_data != data || 66 peeked_flags != flags) { 67 fprintf(stderr, "Bad peeked error data returned.\n"); 68 return false; 69 } 70 71 if (strcmp(file, "test") != 0 || 72 line != 4 || 73 (flags & ERR_FLAG_STRING) == 0 || 74 ERR_GET_LIB(packed_error) != 1 || 75 ERR_GET_FUNC(packed_error) != 2 || 76 ERR_GET_REASON(packed_error) != 3 || 77 strcmp(data, "testing") != 0) { 78 fprintf(stderr, "Bad error data returned.\n"); 79 return false; 80 } 81 82 return true; 83 } 84 85 static bool TestClearError() { 86 if (ERR_get_error() != 0) { 87 fprintf(stderr, "ERR_get_error returned value before an error was added.\n"); 88 return false; 89 } 90 91 ERR_put_error(1, 2, 3, "test", 4); 92 ERR_clear_error(); 93 94 if (ERR_get_error() != 0) { 95 fprintf(stderr, "Error remained after clearing.\n"); 96 return false; 97 } 98 99 return true; 100 } 101 102 static bool TestPrint() { 103 ERR_put_error(1, 2, 3, "test", 4); 104 ERR_add_error_data(1, "testing"); 105 uint32_t packed_error = ERR_get_error(); 106 107 char buf[256]; 108 for (size_t i = 0; i <= sizeof(buf); i++) { 109 ERR_error_string_n(packed_error, buf, i); 110 } 111 112 return true; 113 } 114 115 static bool TestRelease() { 116 ERR_put_error(1, 2, 3, "test", 4); 117 ERR_remove_thread_state(NULL); 118 return true; 119 } 120 121 int main() { 122 CRYPTO_library_init(); 123 124 if (!TestOverflow() || 125 !TestPutError() || 126 !TestClearError() || 127 !TestPrint() || 128 !TestRelease()) { 129 return 1; 130 } 131 132 printf("PASS\n"); 133 return 0; 134 } 135