Skip to content
This repository was archived by the owner on Mar 5, 2024. It is now read-only.

Commit 69f5b19

Browse files
author
Constanza Heath
committed
Fix whitespace and consistency in test cases format, add macros for easy mapping of print functions to other projects
Signed-off-by: Constanza Heath <[email protected]>
1 parent 8e89499 commit 69f5b19

File tree

11 files changed

+3057
-2722
lines changed

11 files changed

+3057
-2722
lines changed

Makefile.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#
77
################################################################################
88

9-
CFLAGS = -Os -std=c99 -Wall -Wextra -I../lib -v
9+
CFLAGS = -Os -std=c99 -Wall -Wextra -I../lib -v -I../tests/include
1010
CC=gcc
1111

1212
export CC

tests/Makefile

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ clean:
3030

3131

3232
# Dependencies
33-
test_aes: test_aes.o test_utils.o ../lib/aes_encrypt.o \
33+
test_aes: test_aes.o ../lib/aes_encrypt.o \
3434
../lib/aes_decrypt.o ../lib/utils.o
3535

36-
test_cbc_mode: test_cbc_mode.o test_utils.o ../lib/cbc_mode.o \
36+
test_cbc_mode: test_cbc_mode.o ../lib/cbc_mode.o \
3737
../lib/aes_encrypt.o ../lib/aes_decrypt.o \
3838
../lib/utils.o
3939

40-
test_ctr_mode: test_ctr_mode.o test_utils.o ../lib/ctr_mode.o \
40+
test_ctr_mode: test_ctr_mode.o ../lib/ctr_mode.o \
4141
../lib/aes_encrypt.o ../lib/utils.o
4242

43-
test_hmac: test_hmac.o test_utils.o ../lib/hmac.o ../lib/sha256.o \
43+
test_hmac: test_hmac.o ../lib/hmac.o ../lib/sha256.o \
4444
../lib/utils.o
4545

4646
test_hmac_prng: test_hmac_prng.o ../lib/hmac_prng.o ../lib/hmac.o \
4747
../lib/sha256.o ../lib/utils.o
4848

49-
test_sha256: test_sha256.o test_utils.o ../lib/sha256.o ../lib/utils.o
49+
test_sha256: test_sha256.o ../lib/sha256.o ../lib/utils.o
5050

5151
# Sub-dependencies
5252
test_aes.o: test_aes.c ../lib/aes.h
@@ -55,5 +55,4 @@ test_ctr_mode.o: test_ctr_mode.c ../lib/aes.h ../lib/ctr_mode.h
5555
test_hmac.o: test_hmac.c ../lib/hmac.h ../lib/sha256.h
5656
test_hmac_prng.o: test_hmac_prng.c ../lib/hmac_prng.h ../lib/utils.h
5757
test_sha256.o: test_sha256.c ../lib/sha256.h
58-
test_utils.o: test_utils.c test_utils.h
5958

tests/include/test_utils.h

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/* test_utils.h - TinyCrypt interface to common functions for tests */
2+
3+
/*
4+
* Copyright (C) 2015 by Intel Corporation, All Rights Reserved.
5+
*
6+
* Redistribution and use in source and binary forms, with or without
7+
* modification, are permitted provided that the following conditions are met:
8+
*
9+
* - Redistributions of source code must retain the above copyright notice,
10+
* this list of conditions and the following disclaimer.
11+
*
12+
* - Redistributions in binary form must reproduce the above copyright
13+
* notice, this list of conditions and the following disclaimer in the
14+
* documentation and/or other materials provided with the distribution.
15+
*
16+
* - Neither the name of Intel Corporation nor the names of its contributors
17+
* may be used to endorse or promote products derived from this software
18+
* without specific prior written permission.
19+
*
20+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23+
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
24+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26+
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27+
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28+
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30+
*/
31+
32+
#ifndef __TEST_UTILS_H__
33+
#define __TEST_UTILS_H__
34+
35+
#include <string.h>
36+
#include <stdio.h>
37+
#include <stdint.h>
38+
39+
#define PRINT_DATA(fmt, ...) printf(fmt, ##__VA_ARGS__)
40+
41+
#define PRINT_LINE \
42+
PRINT_DATA( \
43+
"============================================================" \
44+
"=======\n")
45+
46+
47+
#define FAIL "FAIL"
48+
#define PASS "PASS"
49+
#define FMT_ERROR "%s - %s@%d. "
50+
51+
#define TC_PASS 0
52+
#define TC_FAIL 1
53+
54+
#define TC_ERROR(fmt, ...) \
55+
do { \
56+
PRINT_DATA(FMT_ERROR, FAIL, __func__, __LINE__); \
57+
PRINT_DATA(fmt, ##__VA_ARGS__); \
58+
} while (0)
59+
60+
#define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
61+
#define TC_START(name) PRINT_DATA("tc_start() - %s\n", name)
62+
#define TC_END(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
63+
64+
/* prints result and the function name */
65+
#define TC_END_RESULT(result) \
66+
do { \
67+
PRINT_LINE; \
68+
TC_END(result, "%s - %s.\n", \
69+
result == TC_PASS ? PASS : FAIL, __func__); \
70+
} while (0)
71+
72+
#define TC_END_REPORT(result) \
73+
do { \
74+
PRINT_LINE; \
75+
TC_END(result, \
76+
"PROJECT EXECUTION %s\n", \
77+
result == TC_PASS ? "SUCCESSFUL" : "FAILED"); \
78+
} while (0)
79+
80+
static inline void show_str(const char *label, const uint8_t *s, size_t len)
81+
{
82+
uint32_t i;
83+
84+
TC_PRINT("%s = ", label);
85+
for (i = 0; i < (uint32_t) len; ++i) {
86+
TC_PRINT("%02x", s[i]);
87+
}
88+
TC_PRINT("\n");
89+
}
90+
91+
static inline void fatal(uint32_t testnum, const void *expected, size_t expectedlen,
92+
const void *computed, size_t computedlen)
93+
{
94+
95+
TC_ERROR("\tTest #%d Failed!\n", testnum);
96+
show_str("\t\tExpected", expected, expectedlen);
97+
show_str("\t\tComputed ", computed, computedlen);
98+
TC_PRINT("\n");
99+
}
100+
101+
static inline uint32_t check_result(uint32_t testnum, const void *expected, size_t expectedlen,
102+
const void *computed, size_t computedlen)
103+
{
104+
uint32_t result = TC_PASS;
105+
106+
if (expectedlen != computedlen) {
107+
TC_ERROR("The length of the computed buffer (%zu)", computedlen);
108+
TC_ERROR("does not match the expected length (%zu).", expectedlen);
109+
result = TC_FAIL;
110+
} else if (memcmp(computed, expected, computedlen) != 0) {
111+
fatal(testnum, expected, expectedlen, computed, computedlen);
112+
result = TC_FAIL;
113+
}
114+
115+
return result;
116+
}
117+
118+
#endif

0 commit comments

Comments
 (0)