1 /* Copyright (c) 2011 The Chromium OS Authors. All rights reserved. 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 <stdint.h> 11 #include <stdio.h> 12 #include <string.h> 13 14 #include "cryptolib.h" 15 #include "file_keys.h" 16 #include "utility.h" 17 18 /* Global test success flag. */ 19 int gTestSuccess = 1; 20 21 int TEST_EQ(int result, int expected_result, const char* testname) { 22 if (result == expected_result) { 23 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); 24 return 1; 25 } else { 26 fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); 27 fprintf(stderr, " Expected: 0x%x (%d), got: 0x%x (%d)\n", 28 expected_result, expected_result, result, result); 29 gTestSuccess = 0; 30 return 0; 31 } 32 } 33 34 int TEST_NEQ(int result, int not_expected_result, const char* testname) { 35 if (result != not_expected_result) { 36 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); 37 return 1; 38 } else { 39 fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname); 40 fprintf(stderr, " Didn't expect 0x%x (%d), but got it.\n", 41 not_expected_result, not_expected_result); 42 gTestSuccess = 0; 43 return 0; 44 } 45 } 46 47 int TEST_PTR_EQ(const void* result, const void* expected_result, 48 const char* testname) { 49 if (result == expected_result) { 50 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname); 51 return 1; 52 } else { 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); 56 gTestSuccess = 0; 57 return 0; 58 } 59 } 60 61 int TEST_PTR_NEQ(const void* result, const void* not_expected_result, 62 const char* testname) { 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 75 int TEST_STR_EQ(const char* result, const char* expected_result, 76 const char* testname) { 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 } 95 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 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 } 128