blob: e57fe21ef6eb69005b874f60f898174a750cd069 [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
10#include <stdio.h>
Randall Spanglercb3313e2011-08-26 12:53:16 -070011#include <string.h>
Gaurav Shahce0cc302010-03-24 13:48:55 -070012
Gaurav Shah5411c7a2010-03-31 10:56:49 -070013#include "cryptolib.h"
Gaurav Shah3199eed2010-03-25 13:04:45 -070014#include "file_keys.h"
Gaurav Shah3199eed2010-03-25 13:04:45 -070015#include "utility.h"
16
Gaurav Shahce0cc302010-03-24 13:48:55 -070017/* Global test success flag. */
18int gTestSuccess = 1;
19
Randall Spangler6c6babc2011-08-31 11:34:29 -070020int TEST_EQ(int result, int expected_result, const char* testname) {
Gaurav Shahce0cc302010-03-24 13:48:55 -070021 if (result == expected_result) {
Gaurav Shaha82bf262010-03-26 10:38:08 -070022 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
Gaurav Shahce0cc302010-03-24 13:48:55 -070023 return 1;
Randall Spanglercb3313e2011-08-26 12:53:16 -070024 } else {
Gaurav Shaha82bf262010-03-26 10:38:08 -070025 fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
Randall Spangler1f5d53f2011-08-24 12:07:43 -070026 fprintf(stderr, " Expected: %d, got: %d\n", expected_result, result);
Gaurav Shahce0cc302010-03-24 13:48:55 -070027 gTestSuccess = 0;
28 return 0;
29 }
30}
Gaurav Shah3199eed2010-03-25 13:04:45 -070031
Randall Spangler6c6babc2011-08-31 11:34:29 -070032int TEST_NEQ(int result, int not_expected_result, const char* testname) {
Gaurav Shahbcd8f4a2010-05-26 13:19:00 -070033 if (result != not_expected_result) {
34 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
35 return 1;
Randall Spanglercb3313e2011-08-26 12:53:16 -070036 } else {
Gaurav Shahbcd8f4a2010-05-26 13:19:00 -070037 fprintf(stderr, "%s Test " COL_RED "FAILED\n" COL_STOP, testname);
Randall Spangler1f5d53f2011-08-24 12:07:43 -070038 fprintf(stderr, " Didn't expect %d, but got it.\n", not_expected_result);
39 gTestSuccess = 0;
40 return 0;
41 }
42}
43
44int TEST_PTR_EQ(const void* result, const void* expected_result,
Randall Spangler6c6babc2011-08-31 11:34:29 -070045 const char* testname) {
Randall Spangler1f5d53f2011-08-24 12:07:43 -070046 if (result == expected_result) {
47 fprintf(stderr, "%s Test " COL_GREEN "PASSED\n" COL_STOP, testname);
48 return 1;
Randall Spanglercb3313e2011-08-26 12:53:16 -070049 } else {
Randall Spangler1f5d53f2011-08-24 12:07:43 -070050 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 Shahbcd8f4a2010-05-26 13:19:00 -070053 gTestSuccess = 0;
54 return 0;
55 }
56}
Randall Spanglercb3313e2011-08-26 12:53:16 -070057
Randall Spangler9c9606b2011-08-30 16:44:44 -070058int TEST_PTR_NEQ(const void* result, const void* not_expected_result,
Randall Spangler6c6babc2011-08-31 11:34:29 -070059 const char* testname) {
Randall Spangler9c9606b2011-08-30 16:44:44 -070060 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 Spanglercb3313e2011-08-26 12:53:16 -070072int TEST_STR_EQ(const char* result, const char* expected_result,
Randall Spangler6c6babc2011-08-31 11:34:29 -070073 const char* testname) {
Randall Spanglercb3313e2011-08-26 12:53:16 -070074
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 Richardson94d70342011-10-04 08:36:09 -070092
93int 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
104int 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}