David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 1 | /* test_utils.h - TinyCrypt interface to common functions for tests */ |
| 2 | |
| 3 | /* |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 4 | * Copyright (C) 2017 by Intel Corporation, All Rights Reserved. |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 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 |
| 23 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 24 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 25 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 26 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 29 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 30 | * POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * test_utils.h -- Interface to common functions for tests. |
| 33 | */ |
| 34 | |
| 35 | #ifndef __TEST_UTILS_H__ |
| 36 | #define __TEST_UTILS_H__ |
| 37 | |
| 38 | #include <tinycrypt/constants.h> |
| 39 | |
| 40 | #include <stdint.h> |
| 41 | #include <stdlib.h> |
| 42 | #include <stdio.h> |
| 43 | #include <string.h> |
| 44 | |
| 45 | #define PRINT_DATA(fmt, ...) printf(fmt, ##__VA_ARGS__) |
| 46 | |
| 47 | #define PRINT_LINE \ |
| 48 | PRINT_DATA( \ |
| 49 | "============================================================" \ |
| 50 | "=======\n") |
| 51 | |
| 52 | |
| 53 | #define FAIL "FAIL" |
| 54 | #define PASS "PASS" |
| 55 | #define FMT_ERROR "%s - %s@%d. " |
| 56 | |
| 57 | /* TC_ here stands for 'Test Case' not 'TinyCrypt' */ |
| 58 | #define TC_PASS 0 |
| 59 | #define TC_FAIL 1 |
| 60 | |
| 61 | #define TC_ERROR(fmt, ...) \ |
| 62 | do { \ |
| 63 | PRINT_DATA(FMT_ERROR, FAIL, __func__, __LINE__); \ |
| 64 | PRINT_DATA(fmt, ##__VA_ARGS__); \ |
| 65 | } while (0) |
| 66 | |
| 67 | #define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__) |
| 68 | #define TC_START(name) PRINT_DATA("tc_start() - %s\n", name) |
| 69 | #define TC_END(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__) |
| 70 | |
| 71 | /* prints result and the function name */ |
| 72 | #define TC_END_RESULT(result) \ |
| 73 | do { \ |
| 74 | PRINT_LINE; \ |
| 75 | TC_END(result, "%s - %s.\n", \ |
| 76 | result == TC_PASS ? PASS : FAIL, __func__); \ |
| 77 | } while (0) |
| 78 | |
| 79 | #define TC_END_REPORT(result) \ |
| 80 | do { \ |
| 81 | PRINT_LINE; \ |
| 82 | TC_END(result, \ |
| 83 | "PROJECT EXECUTION %s\n", \ |
| 84 | result == TC_PASS ? "SUCCESSFUL" : "FAILED"); \ |
| 85 | } while (0) |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 86 | |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 87 | static inline void show_str(const char *label, const uint8_t *s, size_t len) |
| 88 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 89 | unsigned int i; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 90 | |
| 91 | TC_PRINT("%s = ", label); |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 92 | for (i = 0; i < (unsigned int) len; ++i) { |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 93 | TC_PRINT("%02x", s[i]); |
| 94 | } |
| 95 | TC_PRINT("\n"); |
| 96 | } |
| 97 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 98 | static inline void fatal(unsigned int testnum, const void *expected, size_t expectedlen, |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 99 | const void *computed, size_t computedlen) |
| 100 | { |
| 101 | |
| 102 | TC_ERROR("\tTest #%d Failed!\n", testnum); |
| 103 | show_str("\t\tExpected", expected, expectedlen); |
| 104 | show_str("\t\tComputed ", computed, computedlen); |
| 105 | TC_PRINT("\n"); |
| 106 | } |
| 107 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 108 | static inline unsigned int check_result(unsigned int testnum, const void *expected, size_t expectedlen, |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 109 | const void *computed, size_t computedlen) |
| 110 | { |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 111 | unsigned int result = TC_PASS; |
David Brown | fecda2d | 2017-09-07 10:20:34 -0600 | [diff] [blame] | 112 | |
| 113 | if (expectedlen != computedlen) { |
| 114 | TC_ERROR("The length of the computed buffer (%zu)", computedlen); |
| 115 | TC_ERROR("does not match the expected length (%zu).", expectedlen); |
| 116 | result = TC_FAIL; |
| 117 | } else if (memcmp(computed, expected, computedlen) != 0) { |
| 118 | fatal(testnum, expected, expectedlen, computed, computedlen); |
| 119 | result = TC_FAIL; |
| 120 | } |
| 121 | |
| 122 | return result; |
| 123 | } |
| 124 | |
Fabio Utzig | 3efe6b6 | 2017-09-22 16:03:24 -0300 | [diff] [blame] | 125 | #endif /* __TEST_UTILS_H__ */ |