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 | |
| 10 | #include <stdio.h> |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 11 | #include <string.h> |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 12 | |
Gaurav Shah | 5411c7a | 2010-03-31 10:56:49 -0700 | [diff] [blame] | 13 | #include "cryptolib.h" |
Gaurav Shah | 3199eed | 2010-03-25 13:04:45 -0700 | [diff] [blame] | 14 | #include "file_keys.h" |
Gaurav Shah | 3199eed | 2010-03-25 13:04:45 -0700 | [diff] [blame] | 15 | #include "utility.h" |
| 16 | |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 17 | /* Global test success flag. */ |
| 18 | int gTestSuccess = 1; |
| 19 | |
Randall Spangler | 6c6babc | 2011-08-31 11:34:29 -0700 | [diff] [blame] | 20 | int TEST_EQ(int result, int expected_result, const char* testname) { |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 21 | if (result == expected_result) { |
Gaurav Shah | a82bf26 | 2010-03-26 10:38:08 -0700 | [diff] [blame] | 22 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 23 | return 1; |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 24 | } else { |
Gaurav Shah | a82bf26 | 2010-03-26 10:38:08 -0700 | [diff] [blame] | 25 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
Randall Spangler | 1f5d53f | 2011-08-24 12:07:43 -0700 | [diff] [blame] | 26 | fprintf(stderr, " Expected: %d, got: %d\n", expected_result, result); |
Gaurav Shah | ce0cc30 | 2010-03-24 13:48:55 -0700 | [diff] [blame] | 27 | gTestSuccess = 0; |
| 28 | return 0; |
| 29 | } |
| 30 | } |
Gaurav Shah | 3199eed | 2010-03-25 13:04:45 -0700 | [diff] [blame] | 31 | |
Randall Spangler | 6c6babc | 2011-08-31 11:34:29 -0700 | [diff] [blame] | 32 | int TEST_NEQ(int result, int not_expected_result, const char* testname) { |
Gaurav Shah | bcd8f4a | 2010-05-26 13:19:00 -0700 | [diff] [blame] | 33 | if (result != not_expected_result) { |
| 34 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 35 | return 1; |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 36 | } else { |
Gaurav Shah | bcd8f4a | 2010-05-26 13:19:00 -0700 | [diff] [blame] | 37 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
Randall Spangler | 1f5d53f | 2011-08-24 12:07:43 -0700 | [diff] [blame] | 38 | fprintf(stderr, " Didn't expect %d, but got it.\n", not_expected_result); |
| 39 | gTestSuccess = 0; |
| 40 | return 0; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | int TEST_PTR_EQ(const void* result, const void* expected_result, |
Randall Spangler | 6c6babc | 2011-08-31 11:34:29 -0700 | [diff] [blame] | 45 | const char* testname) { |
Randall Spangler | 1f5d53f | 2011-08-24 12:07:43 -0700 | [diff] [blame] | 46 | if (result == expected_result) { |
| 47 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 48 | return 1; |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 49 | } else { |
Randall Spangler | 1f5d53f | 2011-08-24 12:07:43 -0700 | [diff] [blame] | 50 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 51 | fprintf(stderr, " Expected: 0x%lx, got: 0x%lx\n", (long)expected_result, |
| 52 | (long)result); |
Gaurav Shah | bcd8f4a | 2010-05-26 13:19:00 -0700 | [diff] [blame] | 53 | gTestSuccess = 0; |
| 54 | return 0; |
| 55 | } |
| 56 | } |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 57 | |
Randall Spangler | 9c9606b | 2011-08-30 16:44:44 -0700 | [diff] [blame] | 58 | int TEST_PTR_NEQ(const void* result, const void* not_expected_result, |
Randall Spangler | 6c6babc | 2011-08-31 11:34:29 -0700 | [diff] [blame] | 59 | const char* testname) { |
Randall Spangler | 9c9606b | 2011-08-30 16:44:44 -0700 | [diff] [blame] | 60 | if (result != not_expected_result) { |
| 61 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 62 | return 1; |
| 63 | } else { |
| 64 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 65 | fprintf(stderr, " Didn't expect 0x%lx, but got it\n", |
| 66 | (long)not_expected_result); |
| 67 | gTestSuccess = 0; |
| 68 | return 0; |
| 69 | } |
| 70 | } |
| 71 | |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 72 | int TEST_STR_EQ(const char* result, const char* expected_result, |
Randall Spangler | 6c6babc | 2011-08-31 11:34:29 -0700 | [diff] [blame] | 73 | const char* testname) { |
Randall Spangler | cb3313e | 2011-08-26 12:53:16 -0700 | [diff] [blame] | 74 | |
| 75 | if (!result || !expected_result) { |
| 76 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 77 | fprintf(stderr, " String compare with NULL\n"); |
| 78 | gTestSuccess = 0; |
| 79 | return 0; |
| 80 | } else if (!strcmp(result, expected_result)) { |
| 81 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 82 | return 1; |
| 83 | } else { |
| 84 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 85 | fprintf(stderr, " Expected: \"%s\", got: \"%s\"\n", expected_result, |
| 86 | result); |
| 87 | gTestSuccess = 0; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | } |
Bill Richardson | 94d7034 | 2011-10-04 08:36:09 -0700 | [diff] [blame^] | 92 | |
| 93 | int TEST_TRUE(int result, const char* testname) { |
| 94 | if (result) { |
| 95 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 96 | } else { |
| 97 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 98 | fprintf(stderr, " Expected TRUE, got 0\n"); |
| 99 | gTestSuccess = 0; |
| 100 | } |
| 101 | return result; |
| 102 | } |
| 103 | |
| 104 | int TEST_FALSE(int result, const char* testname) { |
| 105 | if (!result) { |
| 106 | fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); |
| 107 | } else { |
| 108 | fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); |
| 109 | fprintf(stderr, " Expected FALSE, got: 0x%lx\n", (long)result); |
| 110 | gTestSuccess = 0; |
| 111 | } |
| 112 | return !result; |
| 113 | } |