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 <zephyr/tc_util.h>
36 #include <tinycrypt/constants.h>
37
show_str(const char * label,const uint8_t * s,size_t len)38 static inline void show_str(const char *label, const uint8_t *s, size_t len)
39 {
40 uint32_t i;
41
42 TC_PRINT("%s = ", label);
43 for (i = 0U; i < (uint32_t)len; ++i) {
44 TC_PRINT("%02x", s[i]);
45 }
46 TC_PRINT("\n");
47 }
48
49 static inline
fatal(uint32_t testnum,const void * expected,size_t expectedlen,const void * computed,size_t computedlen)50 void fatal(uint32_t testnum, const void *expected, size_t expectedlen,
51 const void *computed, size_t computedlen)
52 {
53 TC_ERROR("\tTest #%d Failed!\n", testnum);
54 show_str("\t\tExpected", expected, expectedlen);
55 show_str("\t\tComputed ", computed, computedlen);
56 TC_PRINT("\n");
57 }
58
59 static inline
check_result(uint32_t testnum,const void * expected,size_t expectedlen,const void * computed,size_t computedlen,uint32_t verbose)60 uint32_t check_result(uint32_t testnum, const void *expected,
61 size_t expectedlen, const void *computed,
62 size_t computedlen, uint32_t verbose)
63 {
64 uint32_t result = TC_PASS;
65
66 ARG_UNUSED(verbose);
67
68 if (expectedlen != computedlen) {
69 TC_ERROR("The length of the computed buffer (%zu)",
70 computedlen);
71 TC_ERROR("does not match the expected length (%zu).",
72 expectedlen);
73 result = TC_FAIL;
74 } else {
75 if (memcmp(computed, expected, computedlen) != 0) {
76 fatal(testnum, expected, expectedlen,
77 computed, computedlen);
78 result = TC_FAIL;
79 }
80 }
81
82 return result;
83 }
84
85 #endif
86