Randall Spangler | 1f5d53f | 2011-08-24 12:07:43 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 2 | * Use of this source code is governed by a BSD-style license that can be |
| 3 | * found in the LICENSE file. |
| 4 | * |
| 5 | * Common functions used by tests. |
| 6 | */ |
| 7 | |
| 8 | #include "test_common.h" |
| 9 | |
Bill Richardson | 0c3ba24 | 2013-03-29 11:09:30 -0700 | [diff] [blame] | 10 | #include <stdint.h> |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 11 | #include <stdio.h> |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 12 | #include <string.h> |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 13 | |
Gaurav Shah | 5411c7a | 2010-03-31 10:56:49 -0700 | [diff] [blame] | 14 | #include "cryptolib.h" |
Gaurav Shah | 3199eed | 2010-03-25 13:04:45 -0700 | [diff] [blame] | 15 | #include "file_keys.h" |
Gaurav Shah | 3199eed | 2010-03-25 13:04:45 -0700 | [diff] [blame] | 16 | #include "utility.h" |
| 17 | |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 18 | /* Global test success flag. */ |
| 19 | int gTestSuccess = 1; |
| 20 | |
Randall Spangler | 6c6babc | 2011-08-31 11:34:29 -0700 | [diff] [blame] | 21 | int TEST_EQ(int result, int expected_result, const char* testname) { |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 22 | if (result == expected_result) { |
Gaurav Shah | a82bf26 | 2010-03-26 10:38:08 -0700 | [diff] [blame] | 23 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 24 | return 1; |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 25 | } else { |
Gaurav Shah | a82bf26 | 2010-03-26 10:38:08 -0700 | [diff] [blame] | 26 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
Randall Spangler | b9be536 | 2014-06-05 13:32:11 -0700 | [diff] [blame] | 27 | fprintf(stderr, " Expected: 0x%x (%d), got: 0x%x (%d)\n", |
| 28 | expected_result, expected_result, result, result); |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 29 | gTestSuccess = 0; |
| 30 | return 0; |
| 31 | } |
| 32 | } |
Gaurav Shah | 3199eed | 2010-03-25 13:04:45 -0700 | [diff] [blame] | 33 | |
Randall Spangler | 6c6babc | 2011-08-31 11:34:29 -0700 | [diff] [blame] | 34 | int TEST_NEQ(int result, int not_expected_result, const char* testname) { |
Gaurav Shah | bcd8f4a | 2010-05-26 13:19:00 -0700 | [diff] [blame] | 35 | if (result != not_expected_result) { |
| 36 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 37 | return 1; |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 38 | } else { |
Gaurav Shah | bcd8f4a | 2010-05-26 13:19:00 -0700 | [diff] [blame] | 39 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
Randall Spangler | b9be536 | 2014-06-05 13:32:11 -0700 | [diff] [blame] | 40 | fprintf(stderr, " Didn't expect 0x%x (%d), but got it.\n", |
| 41 | not_expected_result, not_expected_result); |
Randall Spangler | 1f5d53f | 2011-08-24 12:07:43 -0700 | [diff] [blame] | 42 | gTestSuccess = 0; |
| 43 | return 0; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | int TEST_PTR_EQ(const void* result, const void* expected_result, |
Randall Spangler | 6c6babc | 2011-08-31 11:34:29 -0700 | [diff] [blame] | 48 | const char* testname) { |
Randall Spangler | 1f5d53f | 2011-08-24 12:07:43 -0700 | [diff] [blame] | 49 | if (result == expected_result) { |
| 50 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 51 | return 1; |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 52 | } else { |
Randall Spangler | 1f5d53f | 2011-08-24 12:07:43 -0700 | [diff] [blame] | 53 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 54 | fprintf(stderr, " Expected: 0x%lx, got: 0x%lx\n", (long)expected_result, |
| 55 | (long)result); |
Gaurav Shah | bcd8f4a | 2010-05-26 13:19:00 -0700 | [diff] [blame] | 56 | gTestSuccess = 0; |
| 57 | return 0; |
| 58 | } |
| 59 | } |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 60 | |
Randall Spangler | 9c9606b | 2011-08-30 16:44:44 -0700 | [diff] [blame] | 61 | int TEST_PTR_NEQ(const void* result, const void* not_expected_result, |
Randall Spangler | 6c6babc | 2011-08-31 11:34:29 -0700 | [diff] [blame] | 62 | const char* testname) { |
Randall Spangler | 9c9606b | 2011-08-30 16:44:44 -0700 | [diff] [blame] | 63 | if (result != not_expected_result) { |
| 64 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 65 | return 1; |
| 66 | } else { |
| 67 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 68 | fprintf(stderr, " Didn't expect 0x%lx, but got it\n", |
| 69 | (long)not_expected_result); |
| 70 | gTestSuccess = 0; |
| 71 | return 0; |
| 72 | } |
| 73 | } |
| 74 | |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 75 | int TEST_STR_EQ(const char* result, const char* expected_result, |
Randall Spangler | 6c6babc | 2011-08-31 11:34:29 -0700 | [diff] [blame] | 76 | const char* testname) { |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 77 | |
| 78 | if (!result || !expected_result) { |
| 79 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 80 | fprintf(stderr, " String compare with NULL\n"); |
| 81 | gTestSuccess = 0; |
| 82 | return 0; |
| 83 | } else if (!strcmp(result, expected_result)) { |
| 84 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 85 | return 1; |
| 86 | } else { |
| 87 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 88 | fprintf(stderr, " Expected: \"%s\", got: \"%s\"\n", expected_result, |
| 89 | result); |
| 90 | gTestSuccess = 0; |
| 91 | return 0; |
| 92 | } |
| 93 | |
| 94 | } |
Bill Richardson | 94d7034 | 2011-10-04 08:36:09 -0700 | [diff] [blame] | 95 | |
Randall Spangler | b9be536 | 2014-06-05 13:32:11 -0700 | [diff] [blame] | 96 | int TEST_SUCC(int result, const char* testname) { |
| 97 | if (result == 0) { |
| 98 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 99 | } else { |
| 100 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 101 | fprintf(stderr, " Expected SUCCESS, got: 0x%lx\n", (long)result); |
| 102 | gTestSuccess = 0; |
| 103 | } |
| 104 | return !result; |
| 105 | } |
| 106 | |
Bill Richardson | 94d7034 | 2011-10-04 08:36:09 -0700 | [diff] [blame] | 107 | int TEST_TRUE(int result, const char* testname) { |
| 108 | if (result) { |
| 109 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 110 | } else { |
| 111 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 112 | fprintf(stderr, " Expected TRUE, got 0\n"); |
| 113 | gTestSuccess = 0; |
| 114 | } |
| 115 | return result; |
| 116 | } |
| 117 | |
| 118 | int TEST_FALSE(int result, const char* testname) { |
| 119 | if (!result) { |
| 120 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 121 | } else { |
| 122 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 123 | fprintf(stderr, " Expected FALSE, got: 0x%lx\n", (long)result); |
| 124 | gTestSuccess = 0; |
| 125 | } |
| 126 | return !result; |
| 127 | } |