summaryrefslogtreecommitdiff
path: root/tests/test_common.c
blob: dd3f0eac034ecb0ac33836860f8b9a40f506e9bb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/* Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Common functions used by tests.
 */

#include "test_common.h"

#include <stdint.h>
#include <stdio.h>
#include <string.h>

#include "cryptolib.h"
#include "file_keys.h"
#include "utility.h"

/* Global test success flag. */
int gTestSuccess = 1;

int test_eq(int result, int expected,
	    const char *preamble, const char *desc, const char *comment)
{
  if (result == expected) {
    fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
	    preamble, desc, comment);
    return 1;
  } else {
    fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
	    preamble, desc, comment);
    fprintf(stderr, "  Expected: 0x%x (%d), got: 0x%x (%d)\n",
	    expected, expected, result, result);
    gTestSuccess = 0;
    return 0;
  }
}

int test_neq(int result, int not_expected,
	     const char *preamble, const char *desc, const char *comment)
{
  if (result != not_expected) {
    fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
	    preamble, desc, comment);
    return 1;
  } else {
    fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
	    preamble, desc, comment);
    fprintf(stderr, "  Didn't expect 0x%x (%d), but got it.\n",
	    not_expected, not_expected);
    gTestSuccess = 0;
    return 0;
  }
}

int test_ptr_eq(const void* result, const void* expected,
		const char *preamble, const char *desc, const char *comment)
{
  if (result == expected) {
    fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
	    preamble, desc, comment);
    return 1;
  } else {
    fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
	    preamble, desc, comment);
    fprintf(stderr, "  Expected: 0x%lx, got: 0x%lx\n", (long)expected,
            (long)result);
    gTestSuccess = 0;
    return 0;
  }
}

int test_ptr_neq(const void* result, const void* not_expected,
		 const char *preamble, const char *desc, const char *comment)
{
  if (result != not_expected) {
    fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
	    preamble, desc, comment);
    return 1;
  } else {
    fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
	    preamble, desc, comment);
    fprintf(stderr, "  Didn't expect 0x%lx, but got it\n",
            (long)not_expected);
    gTestSuccess = 0;
    return 0;
  }
}

int test_str_eq(const char* result, const char* expected,
		const char *preamble, const char *desc, const char *comment)
{
  if (!result || !expected) {
    fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
	    preamble, desc, comment);
    fprintf(stderr, "  String compare with NULL\n");
    gTestSuccess = 0;
    return 0;
  } else if (!strcmp(result, expected)) {
    fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
	    preamble, desc, comment);
    return 1;
  } else {
    fprintf(stderr, "%s " COL_RED "FAILED\n" COL_STOP, comment);
    fprintf(stderr, "  Expected: \"%s\", got: \"%s\"\n", expected,
            result);
    gTestSuccess = 0;
    return 0;
  }
}

int test_str_neq(const char* result, const char* not_expected,
		 const char *preamble, const char *desc, const char *comment)
{
  if (!result || !not_expected) {
    fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
	    preamble, desc, comment);
    fprintf(stderr, "  String compare with NULL\n");
    gTestSuccess = 0;
    return 0;
  } else if (strcmp(result, not_expected)) {
    fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
	    preamble, desc, comment);
    return 1;
  } else {
    fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
	    preamble, desc, comment);
    fprintf(stderr, "  Didn't expect: \"%s\", but got it\n", not_expected);
    gTestSuccess = 0;
    return 0;
  }
}

int test_succ(int result,
	      const char *preamble, const char *desc, const char *comment)
{
  if (result == 0) {
    fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
	    preamble, desc, comment);
  } else {
    fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
	    preamble, desc, comment);
    fprintf(stderr, "  Expected SUCCESS, got: 0x%lx\n", (long)result);
    gTestSuccess = 0;
  }
  return !result;
}

int test_true(int result,
	      const char *preamble, const char *desc, const char *comment)
{
  if (result) {
    fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
	    preamble, desc, comment);
  } else {
    fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
	    preamble, desc, comment);
    fprintf(stderr, "  Expected TRUE, got 0\n");
    gTestSuccess = 0;
  }
  return result;
}

int test_false(int result,
	       const char *preamble, const char *desc, const char *comment)
{
  if (!result) {
    fprintf(stderr, "%s: %s, %s ... " COL_GREEN "PASSED\n" COL_STOP,
	    preamble, desc, comment);
  } else {
    fprintf(stderr, "%s: %s, %s ... " COL_RED "FAILED\n" COL_STOP,
	    preamble, desc, comment);
    fprintf(stderr, "  Expected FALSE, got: 0x%lx\n", (long)result);
    gTestSuccess = 0;
  }
  return !result;
}