Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2014, STMicroelectronics International N.V. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Redistribution and use in source and binary forms, with or without |
| 6 | * modification, are permitted provided that the following conditions are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright notice, |
| 9 | * this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 12 | * this list of conditions and the following disclaimer in the documentation |
| 13 | * and/or other materials provided with the distribution. |
| 14 | * |
| 15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 25 | * POSSIBILITY OF SUCH DAMAGE. |
| 26 | */ |
| 27 | |
| 28 | #ifndef TB_ASSERTS_H |
| 29 | #define TB_ASSERTS_H |
| 30 | |
| 31 | #include <trace.h> |
| 32 | #include "tb_macros.h" |
| 33 | |
| 34 | /* |
| 35 | * TB_ASSERT_MSG general assert function with a message. |
| 36 | */ |
| 37 | #define TB_ASSERT_MSG(cond, str) \ |
| 38 | do { \ |
| 39 | if (!(cond)) { \ |
| 40 | EMSG("Assertion failed at line %d in file:\n%s\n", \ |
| 41 | __LINE__, __FILE__); \ |
| 42 | EMSG("Message: %s\n", str); \ |
| 43 | HALT; \ |
| 44 | }; \ |
| 45 | } while (0) |
| 46 | |
| 47 | /* |
| 48 | * TB_ASSERT general assert function. |
| 49 | */ |
| 50 | #define TB_ASSERT(cond) \ |
| 51 | do { \ |
| 52 | if (!(cond)) { \ |
| 53 | EMSG("Assertion failed at line %d in file:\n%s\n", \ |
| 54 | __LINE__, __FILE__); \ |
| 55 | HALT; \ |
| 56 | }; \ |
| 57 | } while (0) |
| 58 | |
| 59 | /* |
| 60 | * TB_ASSERT_EQ_SHORT checks that src equals the short value. |
| 61 | */ |
| 62 | #define TB_ASSERT_EQ_SHORT(src, short) \ |
| 63 | do { \ |
| 64 | if (((short) == 0) && (__mpanum_size((mpanum)src) != 0)) { \ |
| 65 | EMSG("Assertion failed at line %d in file:\n%s\n", \ |
| 66 | __LINE__, __FILE__); \ |
| 67 | EMSG("short == 0, but size != 0\n"); \ |
| 68 | HALT; \ |
| 69 | } else if (__mpanum_size((mpanum)src) > 1) { \ |
| 70 | EMSG("Assertion failed at line %d in file:\n%s\n", \ |
| 71 | __LINE__, __FILE__); \ |
| 72 | EMSG("size > 1, cannot be equal to a short.\n"); \ |
| 73 | HALT; \ |
| 74 | } else if ( \ |
| 75 | (int)(__mpanum_lsw((mpanum)src)*__mpanum_sign((mpanum)src)) != \ |
| 76 | (int)(short)) { \ |
| 77 | EMSG("Assertion failed at line %d in file:\n%s\n", \ |
| 78 | __LINE__, __FILE__); \ |
| 79 | EMSG("short == %d, but src == %d\n", (short), \ |
| 80 | (int)(__mpanum_lsw((mpanum)src) \ |
| 81 | *__mpanum_sign((mpanum)src))); \ |
| 82 | HALT; \ |
| 83 | }; \ |
| 84 | } while (0) |
| 85 | |
| 86 | /* |
| 87 | * TB_ASSERT_STR_EQ checks that the two strings a and b are equal. |
| 88 | */ |
| 89 | #define TB_ASSERT_STR_EQ(a, b) \ |
| 90 | do { \ |
| 91 | if (my_strcmp((a), (b)) != 0) { \ |
| 92 | EMSG("Assertion failed %s != %s\n", (a), (b)); \ |
| 93 | HALT; \ |
| 94 | }; \ |
| 95 | } while (0) |
| 96 | |
| 97 | /* |
| 98 | * TB_ASSERT_HEX_VALUE checks that a prints to the string v in hex. |
| 99 | */ |
Jens Wiklander | 50657b7 | 2015-07-22 16:02:43 +0200 | [diff] [blame^] | 100 | #define TB_ASSERT_HEX_PRINT_VALUE(a, v) \ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 101 | do { \ |
| 102 | char *_str_; \ |
| 103 | _str_ = TEE_BigIntConvertToString(NULL, \ |
Jens Wiklander | 50657b7 | 2015-07-22 16:02:43 +0200 | [diff] [blame^] | 104 | TEE_STRING_MODE_HEX_UC, (a)); \ |
Pascal Brand | c639ac8 | 2015-07-02 08:53:34 +0200 | [diff] [blame] | 105 | TB_ASSERT_STR_EQ(_str_, (v)); \ |
| 106 | TEE_Free(_str_); \ |
| 107 | } while (0) |
| 108 | |
| 109 | /* |
| 110 | * TB_ASSERT_POINTER_NULL(p) checks that p is null |
| 111 | */ |
| 112 | #define TB_ASSERT_POINTER_NULL(p) \ |
| 113 | do { \ |
| 114 | if ((p) != 0) { \ |
| 115 | EMSG("Assertion failed, pointer was not null.\n"); \ |
| 116 | HALT; \ |
| 117 | }; \ |
| 118 | } while (0) |
| 119 | |
| 120 | /* |
| 121 | * TB_ASSERT_POINTERS_EQ checks that p, q are pointing to the same element |
| 122 | */ |
| 123 | #define TB_ASSERT_POINTERS_EQ(p, q) \ |
| 124 | do { \ |
| 125 | if ((p) != (q)) { \ |
| 126 | EMSG("Assertion failed, pointers are not equal.\n"); \ |
| 127 | HALT; \ |
| 128 | }; \ |
| 129 | } while (0) |
| 130 | |
| 131 | /* |
| 132 | * TB_ASSERT_POINTERS_NEQ checks that p, q are not pointing to the same element |
| 133 | */ |
| 134 | #define TB_ASSERT_POINTERS_NEQ(p, q) \ |
| 135 | do { \ |
| 136 | if ((p) == (q)) { \ |
| 137 | EMSG("Assertion failed, pointers are equal.\n"); \ |
| 138 | HALT; \ |
| 139 | }; \ |
| 140 | } while (0) |
| 141 | |
| 142 | /* |
| 143 | * TB_ASSERT_BIGINT_EQ Checks that a and b are equal |
| 144 | */ |
| 145 | #define TB_ASSERT_BIGINT_EQ(a, b) \ |
| 146 | do { \ |
| 147 | if (TEE_BigIntCmp((a), (b)) != 0) { \ |
| 148 | EMSG("Assertion failed, numbers are not equal.\n"); \ |
| 149 | HALT; \ |
| 150 | }; \ |
| 151 | } while (0) |
| 152 | |
| 153 | /* |
| 154 | * TB_ASSERT_BIGINT_NEQ Checks that a and b are different |
| 155 | */ |
| 156 | #define TB_ASSERT_BIGINT_NEQ(a, b) \ |
| 157 | do { \ |
| 158 | if (TEE_BigIntCmp((a), (b)) == 0) { \ |
| 159 | EMSG("Assertion failed, numbers are equal.\n"); \ |
| 160 | HALT; \ |
| 161 | }; \ |
| 162 | } while (0) |
| 163 | |
| 164 | /* |
| 165 | * TB_ASSERT_BIGINT_LESS Checks that a < b |
| 166 | */ |
| 167 | #define TB_ASSERT_BIGINT_LESS(a, b) \ |
| 168 | do { \ |
| 169 | if (TEE_BigIntCmp((a), (b)) >= 0) { \ |
| 170 | EMSG("Assertion failed, first is not less than second.\n"); \ |
| 171 | HALT; \ |
| 172 | }; \ |
| 173 | } while (0) |
| 174 | |
| 175 | /* |
| 176 | * TB_ASSERT_INT_EQ Checks that a and be are equal |
| 177 | */ |
| 178 | #define TB_ASSERT_INT_EQ(a, b) \ |
| 179 | do { \ |
| 180 | if ((a) != (b)) { \ |
| 181 | EMSG("Assertion failed, numbers are not equal.\n"); \ |
| 182 | HALT; \ |
| 183 | }; \ |
| 184 | } while (0) |
| 185 | |
| 186 | #endif |