Commit bb6be03c110cba2cabf0d0087a34b1aed3c1a1a4
1 parent
55bd5054
Convenient assert macro for unit testing
It's never too late to remove 261 lines of code ;)
Showing
1 changed file
with
105 additions
and
368 deletions
tests/unit-test-client.c
| @@ -32,6 +32,18 @@ enum { | @@ -32,6 +32,18 @@ enum { | ||
| 32 | 32 | ||
| 33 | int test_raw_request(modbus_t *, int); | 33 | int test_raw_request(modbus_t *, int); |
| 34 | 34 | ||
| 35 | +#define BUG_REPORT(_cond, _format, _args ...) \ | ||
| 36 | + printf("\nLine %d: assertion error for '%s': " _format "\n", __LINE__, # _cond, ## _args) | ||
| 37 | + | ||
| 38 | +#define ASSERT_TRUE(_cond, _format, __args...) { \ | ||
| 39 | + if (_cond) { \ | ||
| 40 | + printf("OK\n"); \ | ||
| 41 | + } else { \ | ||
| 42 | + BUG_REPORT(_cond, _format, ## __args); \ | ||
| 43 | + goto close; \ | ||
| 44 | + } \ | ||
| 45 | +}; | ||
| 46 | + | ||
| 35 | int main(int argc, char *argv[]) | 47 | int main(int argc, char *argv[]) |
| 36 | { | 48 | { |
| 37 | const int NB_REPORT_SLAVE_ID = 10; | 49 | const int NB_REPORT_SLAVE_ID = 10; |
| @@ -100,13 +112,8 @@ int main(int argc, char *argv[]) | @@ -100,13 +112,8 @@ int main(int argc, char *argv[]) | ||
| 100 | printf("** UNIT TESTING **\n"); | 112 | printf("** UNIT TESTING **\n"); |
| 101 | 113 | ||
| 102 | printf("1/1 No response timeout modification on connect: "); | 114 | printf("1/1 No response timeout modification on connect: "); |
| 103 | - if (old_response_to_sec == new_response_to_sec && | ||
| 104 | - old_response_to_usec == new_response_to_usec) { | ||
| 105 | - printf("OK\n"); | ||
| 106 | - } else { | ||
| 107 | - printf("FAILED\n"); | ||
| 108 | - goto close; | ||
| 109 | - } | 115 | + ASSERT_TRUE(old_response_to_sec == new_response_to_sec && |
| 116 | + old_response_to_usec == new_response_to_usec, ""); | ||
| 110 | 117 | ||
| 111 | /* Allocate and initialize the memory to store the bits */ | 118 | /* Allocate and initialize the memory to store the bits */ |
| 112 | nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB; | 119 | nb_points = (UT_BITS_NB > UT_INPUT_BITS_NB) ? UT_BITS_NB : UT_INPUT_BITS_NB; |
| @@ -126,25 +133,14 @@ int main(int argc, char *argv[]) | @@ -126,25 +133,14 @@ int main(int argc, char *argv[]) | ||
| 126 | /* Single */ | 133 | /* Single */ |
| 127 | rc = modbus_write_bit(ctx, UT_BITS_ADDRESS, ON); | 134 | rc = modbus_write_bit(ctx, UT_BITS_ADDRESS, ON); |
| 128 | printf("1/2 modbus_write_bit: "); | 135 | printf("1/2 modbus_write_bit: "); |
| 129 | - if (rc == 1) { | ||
| 130 | - printf("OK\n"); | ||
| 131 | - } else { | ||
| 132 | - printf("FAILED\n"); | ||
| 133 | - goto close; | ||
| 134 | - } | 136 | + ASSERT_TRUE(rc == 1, ""); |
| 135 | 137 | ||
| 136 | rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits); | 138 | rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, 1, tab_rp_bits); |
| 137 | printf("2/2 modbus_read_bits: "); | 139 | printf("2/2 modbus_read_bits: "); |
| 138 | - if (rc != 1) { | ||
| 139 | - printf("FAILED (nb points %d)\n", rc); | ||
| 140 | - goto close; | ||
| 141 | - } | 140 | + ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc); |
| 141 | + ASSERT_TRUE(tab_rp_bits[0] == ON, "FAILED (%0X != %0X)\n", | ||
| 142 | + tab_rp_bits[0], ON); | ||
| 142 | 143 | ||
| 143 | - if (tab_rp_bits[0] != ON) { | ||
| 144 | - printf("FAILED (%0X != %0X)\n", tab_rp_bits[0], ON); | ||
| 145 | - goto close; | ||
| 146 | - } | ||
| 147 | - printf("OK\n"); | ||
| 148 | /* End single */ | 144 | /* End single */ |
| 149 | 145 | ||
| 150 | /* Multiple bits */ | 146 | /* Multiple bits */ |
| @@ -155,20 +151,12 @@ int main(int argc, char *argv[]) | @@ -155,20 +151,12 @@ int main(int argc, char *argv[]) | ||
| 155 | rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, | 151 | rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, |
| 156 | UT_BITS_NB, tab_value); | 152 | UT_BITS_NB, tab_value); |
| 157 | printf("1/2 modbus_write_bits: "); | 153 | printf("1/2 modbus_write_bits: "); |
| 158 | - if (rc == UT_BITS_NB) { | ||
| 159 | - printf("OK\n"); | ||
| 160 | - } else { | ||
| 161 | - printf("FAILED\n"); | ||
| 162 | - goto close; | ||
| 163 | - } | 154 | + ASSERT_TRUE(rc == UT_BITS_NB, ""); |
| 164 | } | 155 | } |
| 165 | 156 | ||
| 166 | rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_rp_bits); | 157 | rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB, tab_rp_bits); |
| 167 | printf("2/2 modbus_read_bits: "); | 158 | printf("2/2 modbus_read_bits: "); |
| 168 | - if (rc != UT_BITS_NB) { | ||
| 169 | - printf("FAILED (nb points %d)\n", rc); | ||
| 170 | - goto close; | ||
| 171 | - } | 159 | + ASSERT_TRUE(rc == UT_BITS_NB, "FAILED (nb points %d)\n", rc); |
| 172 | 160 | ||
| 173 | i = 0; | 161 | i = 0; |
| 174 | nb_points = UT_BITS_NB; | 162 | nb_points = UT_BITS_NB; |
| @@ -176,10 +164,8 @@ int main(int argc, char *argv[]) | @@ -176,10 +164,8 @@ int main(int argc, char *argv[]) | ||
| 176 | int nb_bits = (nb_points > 8) ? 8 : nb_points; | 164 | int nb_bits = (nb_points > 8) ? 8 : nb_points; |
| 177 | 165 | ||
| 178 | value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits); | 166 | value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits); |
| 179 | - if (value != UT_BITS_TAB[i]) { | ||
| 180 | - printf("FAILED (%0X != %0X)\n", value, UT_BITS_TAB[i]); | ||
| 181 | - goto close; | ||
| 182 | - } | 167 | + ASSERT_TRUE(value == UT_BITS_TAB[i], "FAILED (%0X != %0X)\n", |
| 168 | + value, UT_BITS_TAB[i]); | ||
| 183 | 169 | ||
| 184 | nb_points -= nb_bits; | 170 | nb_points -= nb_bits; |
| 185 | i++; | 171 | i++; |
| @@ -191,22 +177,15 @@ int main(int argc, char *argv[]) | @@ -191,22 +177,15 @@ int main(int argc, char *argv[]) | ||
| 191 | rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, | 177 | rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, |
| 192 | UT_INPUT_BITS_NB, tab_rp_bits); | 178 | UT_INPUT_BITS_NB, tab_rp_bits); |
| 193 | printf("1/1 modbus_read_input_bits: "); | 179 | printf("1/1 modbus_read_input_bits: "); |
| 194 | - | ||
| 195 | - if (rc != UT_INPUT_BITS_NB) { | ||
| 196 | - printf("FAILED (nb points %d)\n", rc); | ||
| 197 | - goto close; | ||
| 198 | - } | 180 | + ASSERT_TRUE(rc == UT_INPUT_BITS_NB, "FAILED (nb points %d)\n", rc); |
| 199 | 181 | ||
| 200 | i = 0; | 182 | i = 0; |
| 201 | nb_points = UT_INPUT_BITS_NB; | 183 | nb_points = UT_INPUT_BITS_NB; |
| 202 | while (nb_points > 0) { | 184 | while (nb_points > 0) { |
| 203 | int nb_bits = (nb_points > 8) ? 8 : nb_points; | 185 | int nb_bits = (nb_points > 8) ? 8 : nb_points; |
| 204 | - | ||
| 205 | value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits); | 186 | value = modbus_get_byte_from_bits(tab_rp_bits, i*8, nb_bits); |
| 206 | - if (value != UT_INPUT_BITS_TAB[i]) { | ||
| 207 | - printf("FAILED (%0X != %0X)\n", value, UT_INPUT_BITS_TAB[i]); | ||
| 208 | - goto close; | ||
| 209 | - } | 187 | + ASSERT_TRUE(value == UT_INPUT_BITS_TAB[i], "FAILED (%0X != %0X)\n", |
| 188 | + value, UT_INPUT_BITS_TAB[i]); | ||
| 210 | 189 | ||
| 211 | nb_points -= nb_bits; | 190 | nb_points -= nb_bits; |
| 212 | i++; | 191 | i++; |
| @@ -218,66 +197,38 @@ int main(int argc, char *argv[]) | @@ -218,66 +197,38 @@ int main(int argc, char *argv[]) | ||
| 218 | /* Single register */ | 197 | /* Single register */ |
| 219 | rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x1234); | 198 | rc = modbus_write_register(ctx, UT_REGISTERS_ADDRESS, 0x1234); |
| 220 | printf("1/2 modbus_write_register: "); | 199 | printf("1/2 modbus_write_register: "); |
| 221 | - if (rc == 1) { | ||
| 222 | - printf("OK\n"); | ||
| 223 | - } else { | ||
| 224 | - printf("FAILED\n"); | ||
| 225 | - goto close; | ||
| 226 | - } | 200 | + ASSERT_TRUE(rc == 1, ""); |
| 227 | 201 | ||
| 228 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 202 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 229 | 1, tab_rp_registers); | 203 | 1, tab_rp_registers); |
| 230 | printf("2/2 modbus_read_registers: "); | 204 | printf("2/2 modbus_read_registers: "); |
| 231 | - if (rc != 1) { | ||
| 232 | - printf("FAILED (nb points %d)\n", rc); | ||
| 233 | - goto close; | ||
| 234 | - } | 205 | + ASSERT_TRUE(rc == 1, "FAILED (nb points %d)\n", rc); |
| 206 | + ASSERT_TRUE(tab_rp_registers[0] == 0x1234, "FAILED (%0X != %0X)\n", | ||
| 207 | + tab_rp_registers[0], 0x1234); | ||
| 235 | 208 | ||
| 236 | - if (tab_rp_registers[0] != 0x1234) { | ||
| 237 | - printf("FAILED (%0X != %0X)\n", | ||
| 238 | - tab_rp_registers[0], 0x1234); | ||
| 239 | - goto close; | ||
| 240 | - } | ||
| 241 | - printf("OK\n"); | ||
| 242 | /* End of single register */ | 209 | /* End of single register */ |
| 243 | 210 | ||
| 244 | /* Many registers */ | 211 | /* Many registers */ |
| 245 | rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS, | 212 | rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS, |
| 246 | UT_REGISTERS_NB, UT_REGISTERS_TAB); | 213 | UT_REGISTERS_NB, UT_REGISTERS_TAB); |
| 247 | printf("1/5 modbus_write_registers: "); | 214 | printf("1/5 modbus_write_registers: "); |
| 248 | - if (rc == UT_REGISTERS_NB) { | ||
| 249 | - printf("OK\n"); | ||
| 250 | - } else { | ||
| 251 | - printf("FAILED\n"); | ||
| 252 | - goto close; | ||
| 253 | - } | 215 | + ASSERT_TRUE(rc == UT_REGISTERS_NB, ""); |
| 254 | 216 | ||
| 255 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 217 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 256 | UT_REGISTERS_NB, tab_rp_registers); | 218 | UT_REGISTERS_NB, tab_rp_registers); |
| 257 | printf("2/5 modbus_read_registers: "); | 219 | printf("2/5 modbus_read_registers: "); |
| 258 | - if (rc != UT_REGISTERS_NB) { | ||
| 259 | - printf("FAILED (nb points %d)\n", rc); | ||
| 260 | - goto close; | ||
| 261 | - } | 220 | + ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d)\n", rc); |
| 262 | 221 | ||
| 263 | for (i=0; i < UT_REGISTERS_NB; i++) { | 222 | for (i=0; i < UT_REGISTERS_NB; i++) { |
| 264 | - if (tab_rp_registers[i] != UT_REGISTERS_TAB[i]) { | ||
| 265 | - printf("FAILED (%0X != %0X)\n", | ||
| 266 | - tab_rp_registers[i], | ||
| 267 | - UT_REGISTERS_TAB[i]); | ||
| 268 | - goto close; | ||
| 269 | - } | 223 | + ASSERT_TRUE(tab_rp_registers[i] == UT_REGISTERS_TAB[i], |
| 224 | + "FAILED (%0X != %0X)\n", | ||
| 225 | + tab_rp_registers[i], UT_REGISTERS_TAB[i]); | ||
| 270 | } | 226 | } |
| 271 | - printf("OK\n"); | ||
| 272 | 227 | ||
| 273 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 228 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 274 | 0, tab_rp_registers); | 229 | 0, tab_rp_registers); |
| 275 | printf("3/5 modbus_read_registers (0): "); | 230 | printf("3/5 modbus_read_registers (0): "); |
| 276 | - if (rc != -1) { | ||
| 277 | - printf("FAILED (nb_points %d)\n", rc); | ||
| 278 | - goto close; | ||
| 279 | - } | ||
| 280 | - printf("OK\n"); | 231 | + ASSERT_TRUE(rc == -1, "FAILED (nb_points %d)\n", rc); |
| 281 | 232 | ||
| 282 | nb_points = (UT_REGISTERS_NB > | 233 | nb_points = (UT_REGISTERS_NB > |
| 283 | UT_INPUT_REGISTERS_NB) ? | 234 | UT_INPUT_REGISTERS_NB) ? |
| @@ -295,24 +246,17 @@ int main(int argc, char *argv[]) | @@ -295,24 +246,17 @@ int main(int argc, char *argv[]) | ||
| 295 | UT_REGISTERS_NB, | 246 | UT_REGISTERS_NB, |
| 296 | tab_rp_registers); | 247 | tab_rp_registers); |
| 297 | printf("4/5 modbus_write_and_read_registers: "); | 248 | printf("4/5 modbus_write_and_read_registers: "); |
| 298 | - if (rc != UT_REGISTERS_NB) { | ||
| 299 | - printf("FAILED (nb points %d != %d)\n", rc, UT_REGISTERS_NB); | ||
| 300 | - goto close; | ||
| 301 | - } | 249 | + ASSERT_TRUE(rc == UT_REGISTERS_NB, "FAILED (nb points %d != %d)\n", |
| 250 | + rc, UT_REGISTERS_NB); | ||
| 302 | 251 | ||
| 303 | - if (tab_rp_registers[0] != UT_REGISTERS_TAB[0]) { | ||
| 304 | - printf("FAILED (%0X != %0X)\n", | ||
| 305 | - tab_rp_registers[0], UT_REGISTERS_TAB[0]); | ||
| 306 | - } | 252 | + ASSERT_TRUE(tab_rp_registers[0] == UT_REGISTERS_TAB[0], |
| 253 | + "FAILED (%0X != %0X)\n", | ||
| 254 | + tab_rp_registers[0], UT_REGISTERS_TAB[0]); | ||
| 307 | 255 | ||
| 308 | for (i=1; i < UT_REGISTERS_NB; i++) { | 256 | for (i=1; i < UT_REGISTERS_NB; i++) { |
| 309 | - if (tab_rp_registers[i] != 0) { | ||
| 310 | - printf("FAILED (%0X != %0X)\n", | ||
| 311 | - tab_rp_registers[i], 0); | ||
| 312 | - goto close; | ||
| 313 | - } | 257 | + ASSERT_TRUE(tab_rp_registers[i] == 0, "FAILED (%0X != %0X)\n", |
| 258 | + tab_rp_registers[i], 0); | ||
| 314 | } | 259 | } |
| 315 | - printf("OK\n"); | ||
| 316 | 260 | ||
| 317 | /* End of many registers */ | 261 | /* End of many registers */ |
| 318 | 262 | ||
| @@ -322,19 +266,13 @@ int main(int argc, char *argv[]) | @@ -322,19 +266,13 @@ int main(int argc, char *argv[]) | ||
| 322 | UT_INPUT_REGISTERS_NB, | 266 | UT_INPUT_REGISTERS_NB, |
| 323 | tab_rp_registers); | 267 | tab_rp_registers); |
| 324 | printf("1/1 modbus_read_input_registers: "); | 268 | printf("1/1 modbus_read_input_registers: "); |
| 325 | - if (rc != UT_INPUT_REGISTERS_NB) { | ||
| 326 | - printf("FAILED (nb points %d)\n", rc); | ||
| 327 | - goto close; | ||
| 328 | - } | 269 | + ASSERT_TRUE(rc == UT_INPUT_REGISTERS_NB, "FAILED (nb points %d)\n", rc); |
| 329 | 270 | ||
| 330 | for (i=0; i < UT_INPUT_REGISTERS_NB; i++) { | 271 | for (i=0; i < UT_INPUT_REGISTERS_NB; i++) { |
| 331 | - if (tab_rp_registers[i] != UT_INPUT_REGISTERS_TAB[i]) { | ||
| 332 | - printf("FAILED (%0X != %0X)\n", | ||
| 333 | - tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]); | ||
| 334 | - goto close; | ||
| 335 | - } | 272 | + ASSERT_TRUE(tab_rp_registers[i] == UT_INPUT_REGISTERS_TAB[i], |
| 273 | + "FAILED (%0X != %0X)\n", | ||
| 274 | + tab_rp_registers[i], UT_INPUT_REGISTERS_TAB[i]); | ||
| 336 | } | 275 | } |
| 337 | - printf("OK\n"); | ||
| 338 | 276 | ||
| 339 | printf("\nTEST FLOATS\n"); | 277 | printf("\nTEST FLOATS\n"); |
| 340 | /** FLOAT **/ | 278 | /** FLOAT **/ |
| @@ -354,33 +292,20 @@ int main(int argc, char *argv[]) | @@ -354,33 +292,20 @@ int main(int argc, char *argv[]) | ||
| 354 | 292 | ||
| 355 | printf("2/4 Get float: "); | 293 | printf("2/4 Get float: "); |
| 356 | real = modbus_get_float(tab_rp_registers); | 294 | real = modbus_get_float(tab_rp_registers); |
| 357 | - if (real == UT_REAL) { | ||
| 358 | - printf("OK\n"); | ||
| 359 | - } else { | ||
| 360 | - printf("FAILED (%f != %f)\n", real, UT_REAL); | ||
| 361 | - goto close; | ||
| 362 | - } | 295 | + ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL); |
| 363 | 296 | ||
| 364 | printf("3/4 Set float in DBCA order: "); | 297 | printf("3/4 Set float in DBCA order: "); |
| 365 | modbus_set_float_dcba(UT_REAL, tab_rp_registers); | 298 | modbus_set_float_dcba(UT_REAL, tab_rp_registers); |
| 366 | - if (tab_rp_registers[1] == (UT_IREAL_DCBA >> 16) && | ||
| 367 | - tab_rp_registers[0] == (UT_IREAL_DCBA & 0xFFFF)) { | ||
| 368 | - printf("OK\n"); | ||
| 369 | - } else { | ||
| 370 | - ireal = (uint32_t) tab_rp_registers[0] & 0xFFFF; | ||
| 371 | - ireal |= (uint32_t) tab_rp_registers[1] << 16; | ||
| 372 | - printf("FAILED (%x != %x)\n", ireal, UT_IREAL_DCBA); | ||
| 373 | - goto close; | ||
| 374 | - } | 299 | + ireal = (uint32_t) tab_rp_registers[0] & 0xFFFF; |
| 300 | + ireal |= (uint32_t) tab_rp_registers[1] << 16; | ||
| 301 | + ASSERT_TRUE(tab_rp_registers[1] == (UT_IREAL_DCBA >> 16) && | ||
| 302 | + tab_rp_registers[0] == (UT_IREAL_DCBA & 0xFFFF), | ||
| 303 | + "FAILED (%x != %x)\n", ireal, UT_IREAL_DCBA); | ||
| 375 | 304 | ||
| 376 | printf("4/4 Get float in DCBA order: "); | 305 | printf("4/4 Get float in DCBA order: "); |
| 377 | real = modbus_get_float_dcba(tab_rp_registers); | 306 | real = modbus_get_float_dcba(tab_rp_registers); |
| 378 | - if (real == UT_REAL) { | ||
| 379 | - printf("OK\n"); | ||
| 380 | - } else { | ||
| 381 | - printf("FAILED (%f != %f)\n", real, UT_REAL); | ||
| 382 | - goto close; | ||
| 383 | - } | 307 | + ASSERT_TRUE(real == UT_REAL, "FAILED (%f != %f)\n", real, UT_REAL); |
| 308 | + | ||
| 384 | printf("\nAt this point, error messages doesn't mean the test has failed\n"); | 309 | printf("\nAt this point, error messages doesn't mean the test has failed\n"); |
| 385 | 310 | ||
| 386 | /** ILLEGAL DATA ADDRESS **/ | 311 | /** ILLEGAL DATA ADDRESS **/ |
| @@ -389,75 +314,39 @@ int main(int argc, char *argv[]) | @@ -389,75 +314,39 @@ int main(int argc, char *argv[]) | ||
| 389 | /* The mapping begins at 0 and ends at address + nb_points so | 314 | /* The mapping begins at 0 and ends at address + nb_points so |
| 390 | * the addresses are not valid. */ | 315 | * the addresses are not valid. */ |
| 391 | 316 | ||
| 392 | - rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, | ||
| 393 | - UT_BITS_NB + 1, tab_rp_bits); | 317 | + rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, UT_BITS_NB + 1, tab_rp_bits); |
| 394 | printf("* modbus_read_bits: "); | 318 | printf("* modbus_read_bits: "); |
| 395 | - if (rc == -1 && errno == EMBXILADD) { | ||
| 396 | - printf("OK\n"); | ||
| 397 | - } else { | ||
| 398 | - printf("FAILED\n"); | ||
| 399 | - goto close; | ||
| 400 | - } | 319 | + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 401 | 320 | ||
| 402 | rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, | 321 | rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, |
| 403 | UT_INPUT_BITS_NB + 1, tab_rp_bits); | 322 | UT_INPUT_BITS_NB + 1, tab_rp_bits); |
| 404 | printf("* modbus_read_input_bits: "); | 323 | printf("* modbus_read_input_bits: "); |
| 405 | - if (rc == -1 && errno == EMBXILADD) | ||
| 406 | - printf("OK\n"); | ||
| 407 | - else { | ||
| 408 | - printf("FAILED\n"); | ||
| 409 | - goto close; | ||
| 410 | - } | 324 | + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 411 | 325 | ||
| 412 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 326 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 413 | UT_REGISTERS_NB + 1, tab_rp_registers); | 327 | UT_REGISTERS_NB + 1, tab_rp_registers); |
| 414 | printf("* modbus_read_registers: "); | 328 | printf("* modbus_read_registers: "); |
| 415 | - if (rc == -1 && errno == EMBXILADD) | ||
| 416 | - printf("OK\n"); | ||
| 417 | - else { | ||
| 418 | - printf("FAILED\n"); | ||
| 419 | - goto close; | ||
| 420 | - } | 329 | + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 421 | 330 | ||
| 422 | rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS, | 331 | rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS, |
| 423 | UT_INPUT_REGISTERS_NB + 1, | 332 | UT_INPUT_REGISTERS_NB + 1, |
| 424 | tab_rp_registers); | 333 | tab_rp_registers); |
| 425 | printf("* modbus_read_input_registers: "); | 334 | printf("* modbus_read_input_registers: "); |
| 426 | - if (rc == -1 && errno == EMBXILADD) | ||
| 427 | - printf("OK\n"); | ||
| 428 | - else { | ||
| 429 | - printf("FAILED\n"); | ||
| 430 | - goto close; | ||
| 431 | - } | 335 | + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 432 | 336 | ||
| 433 | rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON); | 337 | rc = modbus_write_bit(ctx, UT_BITS_ADDRESS + UT_BITS_NB, ON); |
| 434 | printf("* modbus_write_bit: "); | 338 | printf("* modbus_write_bit: "); |
| 435 | - if (rc == -1 && errno == EMBXILADD) { | ||
| 436 | - printf("OK\n"); | ||
| 437 | - } else { | ||
| 438 | - printf("FAILED\n"); | ||
| 439 | - goto close; | ||
| 440 | - } | 339 | + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 441 | 340 | ||
| 442 | rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB, | 341 | rc = modbus_write_bits(ctx, UT_BITS_ADDRESS + UT_BITS_NB, |
| 443 | UT_BITS_NB, tab_rp_bits); | 342 | UT_BITS_NB, tab_rp_bits); |
| 444 | printf("* modbus_write_coils: "); | 343 | printf("* modbus_write_coils: "); |
| 445 | - if (rc == -1 && errno == EMBXILADD) { | ||
| 446 | - printf("OK\n"); | ||
| 447 | - } else { | ||
| 448 | - printf("FAILED\n"); | ||
| 449 | - goto close; | ||
| 450 | - } | 344 | + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 451 | 345 | ||
| 452 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB, | 346 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS + UT_REGISTERS_NB, |
| 453 | UT_REGISTERS_NB, tab_rp_registers); | 347 | UT_REGISTERS_NB, tab_rp_registers); |
| 454 | printf("* modbus_write_registers: "); | 348 | printf("* modbus_write_registers: "); |
| 455 | - if (rc == -1 && errno == EMBXILADD) { | ||
| 456 | - printf("OK\n"); | ||
| 457 | - } else { | ||
| 458 | - printf("FAILED\n"); | ||
| 459 | - goto close; | ||
| 460 | - } | 349 | + ASSERT_TRUE(rc == -1 && errno == EMBXILADD, ""); |
| 461 | 350 | ||
| 462 | /** TOO MANY DATA **/ | 351 | /** TOO MANY DATA **/ |
| 463 | printf("\nTEST TOO MANY DATA ERROR:\n"); | 352 | printf("\nTEST TOO MANY DATA ERROR:\n"); |
| @@ -465,65 +354,35 @@ int main(int argc, char *argv[]) | @@ -465,65 +354,35 @@ int main(int argc, char *argv[]) | ||
| 465 | rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, | 354 | rc = modbus_read_bits(ctx, UT_BITS_ADDRESS, |
| 466 | MODBUS_MAX_READ_BITS + 1, tab_rp_bits); | 355 | MODBUS_MAX_READ_BITS + 1, tab_rp_bits); |
| 467 | printf("* modbus_read_bits: "); | 356 | printf("* modbus_read_bits: "); |
| 468 | - if (rc == -1 && errno == EMBMDATA) { | ||
| 469 | - printf("OK\n"); | ||
| 470 | - } else { | ||
| 471 | - printf("FAILED\n"); | ||
| 472 | - goto close; | ||
| 473 | - } | 357 | + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); |
| 474 | 358 | ||
| 475 | rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, | 359 | rc = modbus_read_input_bits(ctx, UT_INPUT_BITS_ADDRESS, |
| 476 | MODBUS_MAX_READ_BITS + 1, tab_rp_bits); | 360 | MODBUS_MAX_READ_BITS + 1, tab_rp_bits); |
| 477 | printf("* modbus_read_input_bits: "); | 361 | printf("* modbus_read_input_bits: "); |
| 478 | - if (rc == -1 && errno == EMBMDATA) { | ||
| 479 | - printf("OK\n"); | ||
| 480 | - } else { | ||
| 481 | - printf("FAILED\n"); | ||
| 482 | - goto close; | ||
| 483 | - } | 362 | + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); |
| 484 | 363 | ||
| 485 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 364 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 486 | MODBUS_MAX_READ_REGISTERS + 1, | 365 | MODBUS_MAX_READ_REGISTERS + 1, |
| 487 | tab_rp_registers); | 366 | tab_rp_registers); |
| 488 | printf("* modbus_read_registers: "); | 367 | printf("* modbus_read_registers: "); |
| 489 | - if (rc == -1 && errno == EMBMDATA) { | ||
| 490 | - printf("OK\n"); | ||
| 491 | - } else { | ||
| 492 | - printf("FAILED\n"); | ||
| 493 | - goto close; | ||
| 494 | - } | 368 | + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); |
| 495 | 369 | ||
| 496 | rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS, | 370 | rc = modbus_read_input_registers(ctx, UT_INPUT_REGISTERS_ADDRESS, |
| 497 | MODBUS_MAX_READ_REGISTERS + 1, | 371 | MODBUS_MAX_READ_REGISTERS + 1, |
| 498 | tab_rp_registers); | 372 | tab_rp_registers); |
| 499 | printf("* modbus_read_input_registers: "); | 373 | printf("* modbus_read_input_registers: "); |
| 500 | - if (rc == -1 && errno == EMBMDATA) { | ||
| 501 | - printf("OK\n"); | ||
| 502 | - } else { | ||
| 503 | - printf("FAILED\n"); | ||
| 504 | - goto close; | ||
| 505 | - } | 374 | + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); |
| 506 | 375 | ||
| 507 | rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, | 376 | rc = modbus_write_bits(ctx, UT_BITS_ADDRESS, |
| 508 | MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits); | 377 | MODBUS_MAX_WRITE_BITS + 1, tab_rp_bits); |
| 509 | printf("* modbus_write_bits: "); | 378 | printf("* modbus_write_bits: "); |
| 510 | - if (rc == -1 && errno == EMBMDATA) { | ||
| 511 | - printf("OK\n"); | ||
| 512 | - } else { | ||
| 513 | - goto close; | ||
| 514 | - printf("FAILED\n"); | ||
| 515 | - } | 379 | + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); |
| 516 | 380 | ||
| 517 | rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS, | 381 | rc = modbus_write_registers(ctx, UT_REGISTERS_ADDRESS, |
| 518 | MODBUS_MAX_WRITE_REGISTERS + 1, | 382 | MODBUS_MAX_WRITE_REGISTERS + 1, |
| 519 | tab_rp_registers); | 383 | tab_rp_registers); |
| 520 | printf("* modbus_write_registers: "); | 384 | printf("* modbus_write_registers: "); |
| 521 | - if (rc == -1 && errno == EMBMDATA) { | ||
| 522 | - printf("OK\n"); | ||
| 523 | - } else { | ||
| 524 | - printf("FAILED\n"); | ||
| 525 | - goto close; | ||
| 526 | - } | 385 | + ASSERT_TRUE(rc == -1 && errno == EMBMDATA, ""); |
| 527 | 386 | ||
| 528 | /** SLAVE REPLY **/ | 387 | /** SLAVE REPLY **/ |
| 529 | printf("\nTEST SLAVE REPLY:\n"); | 388 | printf("\nTEST SLAVE REPLY:\n"); |
| @@ -541,13 +400,7 @@ int main(int argc, char *argv[]) | @@ -541,13 +400,7 @@ int main(int argc, char *argv[]) | ||
| 541 | 400 | ||
| 542 | /* No response in RTU mode */ | 401 | /* No response in RTU mode */ |
| 543 | printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID); | 402 | printf("1-A/3 No response from slave %d: ", INVALID_SERVER_ID); |
| 544 | - | ||
| 545 | - if (rc == -1 && errno == ETIMEDOUT) { | ||
| 546 | - printf("OK\n"); | ||
| 547 | - } else { | ||
| 548 | - printf("FAILED\n"); | ||
| 549 | - goto close; | ||
| 550 | - } | 403 | + ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 551 | 404 | ||
| 552 | /* The slave raises a timeout on a confirmation to ignore because if an | 405 | /* The slave raises a timeout on a confirmation to ignore because if an |
| 553 | * indication for another slave is received, a confirmation must follow */ | 406 | * indication for another slave is received, a confirmation must follow */ |
| @@ -563,13 +416,7 @@ int main(int argc, char *argv[]) | @@ -563,13 +416,7 @@ int main(int argc, char *argv[]) | ||
| 563 | 416 | ||
| 564 | printf("1-B/3 No response from slave %d on indication/confirmation messages: ", | 417 | printf("1-B/3 No response from slave %d on indication/confirmation messages: ", |
| 565 | INVALID_SERVER_ID); | 418 | INVALID_SERVER_ID); |
| 566 | - | ||
| 567 | - if (rc == -1 && errno == ETIMEDOUT) { | ||
| 568 | - printf("OK\n"); | ||
| 569 | - } else { | ||
| 570 | - printf("FAILED (%d)\n", rc); | ||
| 571 | - goto close; | ||
| 572 | - } | 419 | + ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 573 | 420 | ||
| 574 | /* Send an INVALID request for another slave */ | 421 | /* Send an INVALID request for another slave */ |
| 575 | modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t)); | 422 | modbus_send_raw_request(ctx, raw_invalid_req, RAW_REQ_LENGTH * sizeof(uint8_t)); |
| @@ -577,40 +424,20 @@ int main(int argc, char *argv[]) | @@ -577,40 +424,20 @@ int main(int argc, char *argv[]) | ||
| 577 | 424 | ||
| 578 | printf("1-C/3 No response from slave %d with invalid request: ", | 425 | printf("1-C/3 No response from slave %d with invalid request: ", |
| 579 | INVALID_SERVER_ID); | 426 | INVALID_SERVER_ID); |
| 580 | - | ||
| 581 | - if (rc == -1 && errno == ETIMEDOUT) { | ||
| 582 | - printf("OK\n"); | ||
| 583 | - } else { | ||
| 584 | - printf("FAILED (%d)\n", rc); | ||
| 585 | - goto close; | ||
| 586 | - } | 427 | + ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 587 | } else { | 428 | } else { |
| 588 | /* Response in TCP mode */ | 429 | /* Response in TCP mode */ |
| 589 | printf("1/3 Response from slave %d: ", INVALID_SERVER_ID); | 430 | printf("1/3 Response from slave %d: ", INVALID_SERVER_ID); |
| 590 | - | ||
| 591 | - if (rc == UT_REGISTERS_NB) { | ||
| 592 | - printf("OK\n"); | ||
| 593 | - } else { | ||
| 594 | - printf("FAILED\n"); | ||
| 595 | - goto close; | ||
| 596 | - } | 431 | + ASSERT_TRUE(rc == UT_REGISTERS_NB, ""); |
| 597 | } | 432 | } |
| 598 | 433 | ||
| 599 | rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS); | 434 | rc = modbus_set_slave(ctx, MODBUS_BROADCAST_ADDRESS); |
| 600 | - if (rc == -1) { | ||
| 601 | - printf("Invalid broacast address\n"); | ||
| 602 | - goto close; | ||
| 603 | - } | 435 | + ASSERT_TRUE(rc != -1, "Invalid broacast address"); |
| 604 | 436 | ||
| 605 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 437 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 606 | UT_REGISTERS_NB, tab_rp_registers); | 438 | UT_REGISTERS_NB, tab_rp_registers); |
| 607 | printf("2/3 Reply after a broadcast query: "); | 439 | printf("2/3 Reply after a broadcast query: "); |
| 608 | - if (rc == UT_REGISTERS_NB) { | ||
| 609 | - printf("OK\n"); | ||
| 610 | - } else { | ||
| 611 | - printf("FAILED\n"); | ||
| 612 | - goto close; | ||
| 613 | - } | 440 | + ASSERT_TRUE(rc == UT_REGISTERS_NB, ""); |
| 614 | 441 | ||
| 615 | /* Restore slave */ | 442 | /* Restore slave */ |
| 616 | if (use_backend == RTU) { | 443 | if (use_backend == RTU) { |
| @@ -622,45 +449,28 @@ int main(int argc, char *argv[]) | @@ -622,45 +449,28 @@ int main(int argc, char *argv[]) | ||
| 622 | printf("3/3 Response with an invalid TID or slave: "); | 449 | printf("3/3 Response with an invalid TID or slave: "); |
| 623 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE, | 450 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_INVALID_TID_OR_SLAVE, |
| 624 | 1, tab_rp_registers); | 451 | 1, tab_rp_registers); |
| 625 | - if (rc == -1) { | ||
| 626 | - printf("OK\n"); | ||
| 627 | - } else { | ||
| 628 | - printf("FAILED\n"); | ||
| 629 | - goto close; | ||
| 630 | - } | 452 | + ASSERT_TRUE(rc == -1, ""); |
| 631 | 453 | ||
| 632 | printf("1/2 Report slave ID truncated: \n"); | 454 | printf("1/2 Report slave ID truncated: \n"); |
| 633 | /* Set a marker to ensure limit is respected */ | 455 | /* Set a marker to ensure limit is respected */ |
| 634 | tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42; | 456 | tab_rp_bits[NB_REPORT_SLAVE_ID - 1] = 42; |
| 635 | rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits); | 457 | rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID - 1, tab_rp_bits); |
| 636 | - if (rc != NB_REPORT_SLAVE_ID && tab_rp_bits[NB_REPORT_SLAVE_ID - 1] != 42) { | ||
| 637 | - printf("FAILED\n"); | ||
| 638 | - goto close; | ||
| 639 | - } | 458 | + /* Return the size required (response size) but respects the defined limit */ |
| 459 | + ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID && | ||
| 460 | + tab_rp_bits[NB_REPORT_SLAVE_ID - 1] == 42, | ||
| 461 | + "Return is rc %d (%d) and marker is %d (42)", | ||
| 462 | + rc, NB_REPORT_SLAVE_ID, tab_rp_bits[NB_REPORT_SLAVE_ID - 1]); | ||
| 640 | 463 | ||
| 641 | printf("2/2 Report slave ID: \n"); | 464 | printf("2/2 Report slave ID: \n"); |
| 642 | /* tab_rp_bits is used to store bytes */ | 465 | /* tab_rp_bits is used to store bytes */ |
| 643 | rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID, tab_rp_bits); | 466 | rc = modbus_report_slave_id(ctx, NB_REPORT_SLAVE_ID, tab_rp_bits); |
| 644 | - if (rc != NB_REPORT_SLAVE_ID) { | ||
| 645 | - printf("FAILED\n"); | ||
| 646 | - goto close; | ||
| 647 | - } | 467 | + ASSERT_TRUE(rc == NB_REPORT_SLAVE_ID, ""); |
| 648 | 468 | ||
| 649 | /* Slave ID is an arbitraty number for libmodbus */ | 469 | /* Slave ID is an arbitraty number for libmodbus */ |
| 650 | - if (rc > 0) { | ||
| 651 | - printf("OK Slave ID is %d\n", tab_rp_bits[0]); | ||
| 652 | - } else { | ||
| 653 | - printf("FAILED\n"); | ||
| 654 | - goto close; | ||
| 655 | - } | 470 | + ASSERT_TRUE(rc > 0, ""); |
| 656 | 471 | ||
| 657 | - /* Run status indicator */ | ||
| 658 | - if (rc > 1 && tab_rp_bits[1] == 0xFF) { | ||
| 659 | - printf("OK Run Status Indicator is %s\n", tab_rp_bits[1] ? "ON" : "OFF"); | ||
| 660 | - } else { | ||
| 661 | - printf("FAILED\n"); | ||
| 662 | - goto close; | ||
| 663 | - } | 472 | + /* Run status indicator is ON */ |
| 473 | + ASSERT_TRUE(rc > 1 && tab_rp_bits[1] == 0xFF, ""); | ||
| 664 | 474 | ||
| 665 | /* Print additional data as string */ | 475 | /* Print additional data as string */ |
| 666 | if (rc > 2) { | 476 | if (rc > 2) { |
| @@ -677,30 +487,15 @@ int main(int argc, char *argv[]) | @@ -677,30 +487,15 @@ int main(int argc, char *argv[]) | ||
| 677 | 487 | ||
| 678 | rc = modbus_set_response_timeout(ctx, 0, 0); | 488 | rc = modbus_set_response_timeout(ctx, 0, 0); |
| 679 | printf("1/6 Invalid response timeout (zero): "); | 489 | printf("1/6 Invalid response timeout (zero): "); |
| 680 | - if (rc == -1 && errno == EINVAL) { | ||
| 681 | - printf("OK\n"); | ||
| 682 | - } else { | ||
| 683 | - printf("FAILED\n"); | ||
| 684 | - goto close; | ||
| 685 | - } | 490 | + ASSERT_TRUE(rc == -1 && errno == EINVAL, ""); |
| 686 | 491 | ||
| 687 | rc = modbus_set_response_timeout(ctx, 0, 1000000); | 492 | rc = modbus_set_response_timeout(ctx, 0, 1000000); |
| 688 | printf("2/6 Invalid response timeout (too large us): "); | 493 | printf("2/6 Invalid response timeout (too large us): "); |
| 689 | - if (rc == -1 && errno == EINVAL) { | ||
| 690 | - printf("OK\n"); | ||
| 691 | - } else { | ||
| 692 | - printf("FAILED\n"); | ||
| 693 | - goto close; | ||
| 694 | - } | 494 | + ASSERT_TRUE(rc == -1 && errno == EINVAL, ""); |
| 695 | 495 | ||
| 696 | rc = modbus_set_byte_timeout(ctx, 0, 1000000); | 496 | rc = modbus_set_byte_timeout(ctx, 0, 1000000); |
| 697 | printf("3/6 Invalid byte timeout (too large us): "); | 497 | printf("3/6 Invalid byte timeout (too large us): "); |
| 698 | - if (rc == -1 && errno == EINVAL) { | ||
| 699 | - printf("OK\n"); | ||
| 700 | - } else { | ||
| 701 | - printf("FAILED\n"); | ||
| 702 | - goto close; | ||
| 703 | - } | 498 | + ASSERT_TRUE(rc == -1 && errno == EINVAL, ""); |
| 704 | 499 | ||
| 705 | modbus_set_response_timeout(ctx, 0, 1); | 500 | modbus_set_response_timeout(ctx, 0, 1); |
| 706 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 501 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| @@ -725,12 +520,7 @@ int main(int argc, char *argv[]) | @@ -725,12 +520,7 @@ int main(int argc, char *argv[]) | ||
| 725 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, | 520 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, |
| 726 | 1, tab_rp_registers); | 521 | 1, tab_rp_registers); |
| 727 | printf("5/6 Too short response timeout (0.2s < 0.5s): "); | 522 | printf("5/6 Too short response timeout (0.2s < 0.5s): "); |
| 728 | - if (rc == -1 && errno == ETIMEDOUT) { | ||
| 729 | - printf("OK\n"); | ||
| 730 | - } else { | ||
| 731 | - printf("FAILED\n"); | ||
| 732 | - goto close; | ||
| 733 | - } | 523 | + ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 734 | 524 | ||
| 735 | /* Wait for reply (0.2 + 0.4 > 0.5 s) and flush before continue */ | 525 | /* Wait for reply (0.2 + 0.4 > 0.5 s) and flush before continue */ |
| 736 | usleep(400000); | 526 | usleep(400000); |
| @@ -740,12 +530,7 @@ int main(int argc, char *argv[]) | @@ -740,12 +530,7 @@ int main(int argc, char *argv[]) | ||
| 740 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, | 530 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, |
| 741 | 1, tab_rp_registers); | 531 | 1, tab_rp_registers); |
| 742 | printf("6/6 Adequate response timeout (0.6s > 0.5s): "); | 532 | printf("6/6 Adequate response timeout (0.6s > 0.5s): "); |
| 743 | - if (rc == 1) { | ||
| 744 | - printf("OK\n"); | ||
| 745 | - } else { | ||
| 746 | - printf("FAILED\n"); | ||
| 747 | - goto close; | ||
| 748 | - } | 533 | + ASSERT_TRUE(rc == 1, ""); |
| 749 | 534 | ||
| 750 | /* Disable the byte timeout. | 535 | /* Disable the byte timeout. |
| 751 | The full response must be available in the 600ms interval */ | 536 | The full response must be available in the 600ms interval */ |
| @@ -753,12 +538,7 @@ int main(int argc, char *argv[]) | @@ -753,12 +538,7 @@ int main(int argc, char *argv[]) | ||
| 753 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, | 538 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_SLEEP_500_MS, |
| 754 | 1, tab_rp_registers); | 539 | 1, tab_rp_registers); |
| 755 | printf("7/7 Disable byte timeout: "); | 540 | printf("7/7 Disable byte timeout: "); |
| 756 | - if (rc == 1) { | ||
| 757 | - printf("OK\n"); | ||
| 758 | - } else { | ||
| 759 | - printf("FAILED\n"); | ||
| 760 | - goto close; | ||
| 761 | - } | 541 | + ASSERT_TRUE(rc == 1, ""); |
| 762 | 542 | ||
| 763 | /* Restore original response timeout */ | 543 | /* Restore original response timeout */ |
| 764 | modbus_set_response_timeout(ctx, old_response_to_sec, | 544 | modbus_set_response_timeout(ctx, old_response_to_sec, |
| @@ -772,12 +552,7 @@ int main(int argc, char *argv[]) | @@ -772,12 +552,7 @@ int main(int argc, char *argv[]) | ||
| 772 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, | 552 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, |
| 773 | 1, tab_rp_registers); | 553 | 1, tab_rp_registers); |
| 774 | printf("1/2 Too small byte timeout (3ms < 5ms): "); | 554 | printf("1/2 Too small byte timeout (3ms < 5ms): "); |
| 775 | - if (rc == -1 && errno == ETIMEDOUT) { | ||
| 776 | - printf("OK\n"); | ||
| 777 | - } else { | ||
| 778 | - printf("FAILED\n"); | ||
| 779 | - goto close; | ||
| 780 | - } | 555 | + ASSERT_TRUE(rc == -1 && errno == ETIMEDOUT, ""); |
| 781 | 556 | ||
| 782 | /* Wait remaing bytes before flushing */ | 557 | /* Wait remaing bytes before flushing */ |
| 783 | usleep(11 * 5000); | 558 | usleep(11 * 5000); |
| @@ -788,12 +563,7 @@ int main(int argc, char *argv[]) | @@ -788,12 +563,7 @@ int main(int argc, char *argv[]) | ||
| 788 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, | 563 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS_BYTE_SLEEP_5_MS, |
| 789 | 1, tab_rp_registers); | 564 | 1, tab_rp_registers); |
| 790 | printf("2/2 Adapted byte timeout (7ms > 5ms): "); | 565 | printf("2/2 Adapted byte timeout (7ms > 5ms): "); |
| 791 | - if (rc == 1) { | ||
| 792 | - printf("OK\n"); | ||
| 793 | - } else { | ||
| 794 | - printf("FAILED\n"); | ||
| 795 | - goto close; | ||
| 796 | - } | 566 | + ASSERT_TRUE(rc == 1, ""); |
| 797 | } | 567 | } |
| 798 | 568 | ||
| 799 | /* Restore original byte timeout */ | 569 | /* Restore original byte timeout */ |
| @@ -809,12 +579,7 @@ int main(int argc, char *argv[]) | @@ -809,12 +579,7 @@ int main(int argc, char *argv[]) | ||
| 809 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, | 579 | rc = modbus_read_registers(ctx, UT_REGISTERS_ADDRESS, |
| 810 | UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad); | 580 | UT_REGISTERS_NB_SPECIAL, tab_rp_registers_bad); |
| 811 | printf("* modbus_read_registers: "); | 581 | printf("* modbus_read_registers: "); |
| 812 | - if (rc == -1 && errno == EMBBADDATA) { | ||
| 813 | - printf("OK\n"); | ||
| 814 | - } else { | ||
| 815 | - printf("FAILED\n"); | ||
| 816 | - goto close; | ||
| 817 | - } | 582 | + ASSERT_TRUE(rc == -1 && errno == EMBBADDATA, ""); |
| 818 | free(tab_rp_registers_bad); | 583 | free(tab_rp_registers_bad); |
| 819 | 584 | ||
| 820 | /** MANUAL EXCEPTION **/ | 585 | /** MANUAL EXCEPTION **/ |
| @@ -823,12 +588,7 @@ int main(int argc, char *argv[]) | @@ -823,12 +588,7 @@ int main(int argc, char *argv[]) | ||
| 823 | UT_REGISTERS_NB, tab_rp_registers); | 588 | UT_REGISTERS_NB, tab_rp_registers); |
| 824 | 589 | ||
| 825 | printf("* modbus_read_registers at special address: "); | 590 | printf("* modbus_read_registers at special address: "); |
| 826 | - if (rc == -1 && errno == EMBXSBUSY) { | ||
| 827 | - printf("OK\n"); | ||
| 828 | - } else { | ||
| 829 | - printf("FAILED\n"); | ||
| 830 | - goto close; | ||
| 831 | - } | 591 | + ASSERT_TRUE(rc == -1 && errno == EMBXSBUSY, ""); |
| 832 | 592 | ||
| 833 | /** RAW REQUEST */ | 593 | /** RAW REQUEST */ |
| 834 | if (test_raw_request(ctx, use_backend) == -1) { | 594 | if (test_raw_request(ctx, use_backend) == -1) { |
| @@ -838,12 +598,7 @@ int main(int argc, char *argv[]) | @@ -838,12 +598,7 @@ int main(int argc, char *argv[]) | ||
| 838 | /* Test init functions */ | 598 | /* Test init functions */ |
| 839 | printf("\nTEST INVALID INITIALIZATION:\n"); | 599 | printf("\nTEST INVALID INITIALIZATION:\n"); |
| 840 | ctx = modbus_new_rtu(NULL, 0, 'A', 0, 0); | 600 | ctx = modbus_new_rtu(NULL, 0, 'A', 0, 0); |
| 841 | - if (ctx == NULL && errno == EINVAL) { | ||
| 842 | - printf("OK\n"); | ||
| 843 | - } else { | ||
| 844 | - printf("FAILED\n"); | ||
| 845 | - goto close; | ||
| 846 | - } | 601 | + ASSERT_TRUE(ctx == NULL && errno == EINVAL, ""); |
| 847 | 602 | ||
| 848 | printf("\nALL TESTS PASS WITH SUCCESS.\n"); | 603 | printf("\nALL TESTS PASS WITH SUCCESS.\n"); |
| 849 | 604 | ||
| @@ -920,21 +675,11 @@ int test_raw_request(modbus_t *ctx, int use_backend) | @@ -920,21 +675,11 @@ int test_raw_request(modbus_t *ctx, int use_backend) | ||
| 920 | req_length = modbus_send_raw_request(ctx, raw_req, | 675 | req_length = modbus_send_raw_request(ctx, raw_req, |
| 921 | RAW_REQ_LENGTH * sizeof(uint8_t)); | 676 | RAW_REQ_LENGTH * sizeof(uint8_t)); |
| 922 | printf("* modbus_send_raw_request: "); | 677 | printf("* modbus_send_raw_request: "); |
| 923 | - if (req_length == (length + 5)) { | ||
| 924 | - printf("OK\n"); | ||
| 925 | - } else { | ||
| 926 | - printf("FAILED (%d)\n", req_length); | ||
| 927 | - return -1; | ||
| 928 | - } | 678 | + ASSERT_TRUE(req_length == (length + 5), "FAILED (%d)\n", req_length); |
| 929 | 679 | ||
| 930 | printf("* modbus_receive_confirmation: "); | 680 | printf("* modbus_receive_confirmation: "); |
| 931 | rc = modbus_receive_confirmation(ctx, rsp); | 681 | rc = modbus_receive_confirmation(ctx, rsp); |
| 932 | - if (rc == (length + 12)) { | ||
| 933 | - printf("OK\n"); | ||
| 934 | - } else { | ||
| 935 | - printf("FAILED (%d)\n", rc); | ||
| 936 | - return -1; | ||
| 937 | - } | 682 | + ASSERT_TRUE(rc == (length + 12), "FAILED (%d)\n", rc); |
| 938 | 683 | ||
| 939 | /* Try to crash server with raw requests to bypass checks of client. */ | 684 | /* Try to crash server with raw requests to bypass checks of client. */ |
| 940 | 685 | ||
| @@ -967,14 +712,9 @@ int test_raw_request(modbus_t *ctx, int use_backend) | @@ -967,14 +712,9 @@ int test_raw_request(modbus_t *ctx, int use_backend) | ||
| 967 | printf("* try an exploit with function %d: ", tab_function[i]); | 712 | printf("* try an exploit with function %d: ", tab_function[i]); |
| 968 | } | 713 | } |
| 969 | rc = modbus_receive_confirmation(ctx, rsp); | 714 | rc = modbus_receive_confirmation(ctx, rsp); |
| 970 | - if (rc == (length + EXCEPTION_RC) && | ||
| 971 | - rsp[offset] == (0x80 + tab_function[i]) && | ||
| 972 | - rsp[offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE) { | ||
| 973 | - printf("OK\n"); | ||
| 974 | - } else { | ||
| 975 | - printf("FAILED\n"); | ||
| 976 | - return -1; | ||
| 977 | - } | 715 | + ASSERT_TRUE(rc == (length + EXCEPTION_RC) && |
| 716 | + rsp[offset] == (0x80 + tab_function[i]) && | ||
| 717 | + rsp[offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, ""); | ||
| 978 | } | 718 | } |
| 979 | } | 719 | } |
| 980 | 720 | ||
| @@ -998,15 +738,12 @@ int test_raw_request(modbus_t *ctx, int use_backend) | @@ -998,15 +738,12 @@ int test_raw_request(modbus_t *ctx, int use_backend) | ||
| 998 | printf("* try an exploit with function %d: ", tab_function[i]); | 738 | printf("* try an exploit with function %d: ", tab_function[i]); |
| 999 | } | 739 | } |
| 1000 | rc = modbus_receive_confirmation(ctx, rsp); | 740 | rc = modbus_receive_confirmation(ctx, rsp); |
| 1001 | - if (rc == length + EXCEPTION_RC && | ||
| 1002 | - rsp[offset] == (0x80 + tab_function[i]) && | ||
| 1003 | - rsp[offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE) { | ||
| 1004 | - printf("OK\n"); | ||
| 1005 | - } else { | ||
| 1006 | - printf("FAILED\n"); | ||
| 1007 | - return -1; | ||
| 1008 | - } | 741 | + ASSERT_TRUE(rc == length + EXCEPTION_RC && |
| 742 | + rsp[offset] == (0x80 + tab_function[i]) && | ||
| 743 | + rsp[offset + 1] == MODBUS_EXCEPTION_ILLEGAL_DATA_VALUE, ""); | ||
| 1009 | } | 744 | } |
| 1010 | 745 | ||
| 1011 | return 0; | 746 | return 0; |
| 747 | +close: | ||
| 748 | + return -1; | ||
| 1012 | } | 749 | } |