blob: 3804245eeea3ccf8536db64d0f89d33e6d948d1b [file] [log] [blame]
Randall Spangler1f5d53f2011-08-24 12:07:43 -07001/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
Gaurav Shahce0cc302010-03-24 13:48:55 -07002 * 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 Richardson0c3ba242013-03-29 11:09:30 -070010#include <stdint.h>
Gaurav Shahce0cc302010-03-24 13:48:55 -070011#include <stdio.h>
Randall Spanglercb3313e2011-08-26 12:53:16 -070012#include <string.h>
Gaurav Shahce0cc302010-03-24 13:48:55 -070013
Gaurav Shah5411c7a2010-03-31 10:56:49 -070014#include "cryptolib.h"
Gaurav Shah3199eed2010-03-25 13:04:45 -070015#include "file_keys.h"
Gaurav Shah3199eed2010-03-25 13:04:45 -070016#include "utility.h"
17
Gaurav Shahce0cc302010-03-24 13:48:55 -070018/* Global test success flag. */
19int gTestSuccess = 1;
20
Randall Spangler6c6babc2011-08-31 11:34:29 -070021int TEST_EQ(int result, int expected_result, const char* testname) {
Gaurav Shahce0cc302010-03-24 13:48:55 -070022 if (result == expected_result) {
Gaurav Shaha82bf262010-03-26 10:38:08 -070023 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
Gaurav Shahce0cc302010-03-24 13:48:55 -070024 return 1;
Randall Spanglercb3313e2011-08-26 12:53:16 -070025 } else {
Gaurav Shaha82bf262010-03-26 10:38:08 -070026 fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
Randall Spanglerb9be5362014-06-05 13:32:11 -070027 fprintf(stderr, " Expected: 0x%x (%d), got: 0x%x (%d)\n",
28 expected_result, expected_result, result, result);
Gaurav Shahce0cc302010-03-24 13:48:55 -070029 gTestSuccess = 0;
30 return 0;
31 }
32}
Gaurav Shah3199eed2010-03-25 13:04:45 -070033
Randall Spangler6c6babc2011-08-31 11:34:29 -070034int TEST_NEQ(int result, int not_expected_result, const char* testname) {
Gaurav Shahbcd8f4a2010-05-26 13:19:00 -070035 if (result != not_expected_result) {
36 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
37 return 1;
Randall Spanglercb3313e2011-08-26 12:53:16 -070038 } else {
Gaurav Shahbcd8f4a2010-05-26 13:19:00 -070039 fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
Randall Spanglerb9be5362014-06-05 13:32:11 -070040 fprintf(stderr, " Didn't expect 0x%x (%d), but got it.\n",
41 not_expected_result, not_expected_result);
Randall Spangler1f5d53f2011-08-24 12:07:43 -070042 gTestSuccess = 0;
43 return 0;
44 }
45}
46
47int TEST_PTR_EQ(const void* result, const void* expected_result,
Randall Spangler6c6babc2011-08-31 11:34:29 -070048 const char* testname) {
Randall Spangler1f5d53f2011-08-24 12:07:43 -070049 if (result == expected_result) {
50 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
51 return 1;
Randall Spanglercb3313e2011-08-26 12:53:16 -070052 } else {
Randall Spangler1f5d53f2011-08-24 12:07:43 -070053 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 Shahbcd8f4a2010-05-26 13:19:00 -070056 gTestSuccess = 0;
57 return 0;
58 }
59}
Randall Spanglercb3313e2011-08-26 12:53:16 -070060
Randall Spangler9c9606b2011-08-30 16:44:44 -070061int TEST_PTR_NEQ(const void* result, const void* not_expected_result,
Randall Spangler6c6babc2011-08-31 11:34:29 -070062 const char* testname) {
Randall Spangler9c9606b2011-08-30 16:44:44 -070063 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 Spanglercb3313e2011-08-26 12:53:16 -070075int TEST_STR_EQ(const char* result, const char* expected_result,
Randall Spangler6c6babc2011-08-31 11:34:29 -070076 const char* testname) {
Randall Spanglercb3313e2011-08-26 12:53:16 -070077
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 Richardson94d70342011-10-04 08:36:09 -070095
Randall Spanglerb9be5362014-06-05 13:32:11 -070096int 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 Richardson94d70342011-10-04 08:36:09 -0700107int 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
118int 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}