TEST test_multiple_entry_and_readback(void) { cmp_ctx_t *ctx; ctx = _log_entry_create(&test_logger, "a"); cmp_write_integer(ctx, 42); _log_entry_write_to_flash(&test_logger); uint32_t end_a = test_logger.flash_write_pos; ctx = _log_entry_create(&test_logger, "b"); cmp_write_integer(ctx, 23); _log_entry_write_to_flash(&test_logger); uint32_t end_b = test_logger.flash_write_pos; uint8_t buf[LOG_ENTRY_DATA_LEN]; uint32_t next_entry = 0; size_t len; if (!log_read_entry(next_entry, buf, &len, &next_entry)) { FAILm("CRC missmatch"); } ASSERT_EQ(end_a, next_entry); ASSERT_EQ(buf[len-1], 42); if (!log_read_entry(next_entry, buf, &len, &next_entry)) { FAILm("CRC missmatch"); } ASSERT_EQ(end_b, next_entry); ASSERT_EQ(buf[len-1], 23); if (log_read_entry(next_entry, buf, &len, &next_entry)) { FAILm("dont detect last entry"); } PASS(); }
// The tonewheel oscillator block expects these volumes to be // Q19. This test ensures we never overflow that range. TEST test_manual_volume_overflow() { uint8_t keys[62] = {0}; uint8_t drawbars[10] = {0}; uint16_t ret[92] = {0}; // Hold down all the keys on this manual. for (int i = 0; i < 62; i++) { keys[i] = 1; } // Pull out all the stops. for (int i = 0; i < 10; i++) { drawbars[i] = 8; } uint32_t total = manual_fill_volumes(keys, drawbars, ret); // The total volumes here cannot overflow the expected Q19 range. uint32_t sum = total_volume(ret); ASSERT_EQ_FMT(total, sum, "%d"); if (sum > (1 << 19)) { char *msg = calloc(128, 1); snprintf(msg, 128, "overflow volume=%d (limit %d)", sum, (1 << 19)); FAILm(msg); free(msg); } PASS(); }
TEST test_entry_crc_and_length(void) { cmp_ctx_t *ctx = _log_entry_create(&test_logger, "entry"); cmp_write_integer(ctx, 42); _log_entry_write_to_flash(&test_logger); size_t entry_length = cmp_mem_access_get_pos(&test_logger.cma); uint8_t len = flash_array[0]; uint8_t crc = flash_array[1]; if (crc8(0, &flash_array[2], len) != crc) { FAILm("CRC missmatch"); } if (entry_length != len) { FAILm("entry length missmatch"); } PASS(); }
TEST test_can_read_entry(void) { uint8_t buf[LOG_ENTRY_DATA_LEN]; size_t entry_len = 4; memset(&flash_array[2], 42, entry_len); flash_array[0] = entry_len; flash_array[1] = crc8(0, &flash_array[2], entry_len); size_t len = 0; uint32_t next_entry = 0; if (!log_read_entry(0, buf, &len, &next_entry)) { FAILm("broken CRC check"); } ASSERT_EQ(entry_len, len); if (memcmp(&flash_array[2], buf, len) != 0) { FAILm("wrong entry data"); } PASS(); }
TEST set1_challenge2(void) { char *buff1 = "1c0111001f010100061a024b53535009181c"; char *buff2 = "686974207468652062756c6c277320657965"; char *expected = "746865206b696420646f6e277420706c6179"; uint8_t *buff1_bytes = (uint8_t *)malloc(strlen(buff1)); uint8_t *buff2_bytes = (uint8_t *)malloc(strlen(buff2)); uint8_t *expected_bytes = (uint8_t *)malloc(strlen(expected)); size_t numbytes = strlen(expected) / 2; if(buff1_bytes == NULL || buff2_bytes == NULL || expected_bytes == NULL) { FAILm("Unable to allocate buffers."); } retval_t retval = hex2bytes(buff1, numbytes, buff1_bytes); if(retval != CALL_OK) { FAILm("Unable to convert buff1 to bytes."); } retval = hex2bytes(buff2, numbytes, buff2_bytes); if(retval != CALL_OK) { FAILm("Unable to convert buff2 to bytes."); } retval = hex2bytes(expected, numbytes, expected_bytes); if(retval != CALL_OK) { FAILm("Unable to convert expected to bytes."); } uint8_t *result = NULL; retval = fixed_xor(numbytes, buff1_bytes, buff2_bytes, &result); if(retval != CALL_OK) { FAILm("fixed_xor() failed"); } ASSERT_EQm("Result didn't match expected", 0, memcmp(result, expected_bytes, numbytes)); free(result); free(expected_bytes); free(buff2_bytes); free(buff1_bytes); PASS(); }
TEST test_crc_mismatch(void) { uint8_t buf[LOG_ENTRY_DATA_LEN]; size_t entry_len = 4; memset(&flash_array[2], 42, entry_len); flash_array[0] = entry_len; // write false crc flash_array[1] = crc8(0, &flash_array[2], entry_len) ^ 0xff; uint32_t next_entry = 0; size_t len; if (log_read_entry(0, buf, &len, &next_entry)) { FAILm("broken CRC check"); } PASS(); }
// Our core sine oscillator has Q12 bits of precision. To avoid // truncating its precision, volume output must be at least 1<<10. // This ensures we only scale those sines up rather than down. TEST test_manual_volume_underflow() { uint8_t keys[62] = {0}; uint8_t drawbars[10] = {0}; uint16_t ret[92] = {0}; // Track the key and drawbar where the minimum volume is found. int mink = -1; int mind = -1; int mint = -1; int minv = SHRT_MAX; for (int k = 13; k < 62; k++) { // Hold down one key. memset(keys, 0, 62); keys[k] = 1; for (int d = 1; d < 10; d++) { // Pull one drawbar out one stop. memset(drawbars, 0, 10); drawbars[d] = 1; manual_fill_volumes(keys, drawbars, ret); for (int t = 1; t < 92; t++) { if (ret[t] == 0 || ret[t] >= minv) { continue; } mink = k; mind = d; mint = t; minv = ret[t]; } } } // Enforce volumes of at least 128 so we don't truncate the // precision of our sine waves too much. if (minv < (1 << 7)) { char *msg = calloc(128, 1); snprintf(msg, 128, "underflow k=%d d=%d t=%d -> volume=%d", mink, mind, mint, minv); FAILm(msg); free(msg); } PASS(); }
TEST test_reg_sad_overflow(void) { unsigned width = sad_test_env.width; unsigned height = sad_test_env.height; unsigned stride = 64; unsigned correct_result = simple_sad(g_64x64_zero->y, g_64x64_max->y, stride, width, height); unsigned(*tested_func)(const kvz_pixel *, const kvz_pixel *, int, int, unsigned, unsigned) = sad_test_env.tested_func; unsigned result = tested_func(g_64x64_zero->y, g_64x64_max->y, width, height, stride, stride); sprintf(sad_test_env.msg, "overflow %s(%ux%u):%s", sad_test_env.strategy->type, width, height, sad_test_env.strategy->strategy_name); if (result != correct_result) { FAILm(sad_test_env.msg); } PASSm(sad_test_env.msg); }
TEST test_entry_write_and_readback(void) { cmp_ctx_t *ctx = _log_entry_create(&test_logger, "entry"); cmp_write_integer(ctx, 42); _log_entry_write_to_flash(&test_logger); uint8_t buf[LOG_ENTRY_DATA_LEN]; uint32_t next_entry = 0; size_t len = 0; if (!log_read_entry(0, buf, &len, &next_entry)) { FAILm("CRC missmatch"); } ASSERT_EQ(test_logger.flash_write_pos, next_entry); // readback entry cmp_ctx_t reader; cmp_mem_access_t cma; cmp_mem_access_ro_init(&reader, &cma, buf, sizeof(buf)); uint32_t size = 0; if (!cmp_read_array(&reader, &size) || size != 3) { FAIL(); } uint64_t timestamp = 0; if (!cmp_read_uinteger(&reader, ×tamp) || timestamp != logger_timestamp_sec()) { FAIL(); } char name[10]; size = sizeof(name); if (!cmp_read_str(&reader, name, &size) || strcmp(name, "entry") != 0) { FAIL(); } int64_t data = 0; if (!cmp_read_integer(&reader, &data) || data != 42) { FAIL(); } PASS(); }
TEST teardown_example_FAIL() { teardown_was_called = 0; GREATEST_SET_TEARDOWN_CB(teardown_cb, &teardown_was_called); FAILm("Failing to trigger teardown callback"); }
static int compress_and_expand_and_check(uint8_t *input, uint32_t input_size, cfg_info *cfg) { heatshrink_encoder *hse = heatshrink_encoder_alloc(cfg->window_sz2, cfg->lookahead_sz2); heatshrink_decoder *hsd = heatshrink_decoder_alloc(cfg->decoder_input_buffer_size, cfg->window_sz2, cfg->lookahead_sz2); size_t comp_sz = input_size + (input_size/2) + 4; size_t decomp_sz = input_size + (input_size/2) + 4; uint8_t *comp = malloc(comp_sz); uint8_t *decomp = malloc(decomp_sz); if (comp == NULL) FAILm("malloc fail"); if (decomp == NULL) FAILm("malloc fail"); memset(comp, 0, comp_sz); memset(decomp, 0, decomp_sz); uint16_t count = 0; if (cfg->log_lvl > 1) { printf("\n^^ COMPRESSING\n"); dump_buf("input", input, input_size); } uint32_t sunk = 0; uint32_t polled = 0; while (sunk < input_size) { ASSERT(heatshrink_encoder_sink(hse, &input[sunk], input_size - sunk, &count) >= 0); sunk += count; if (cfg->log_lvl > 1) printf("^^ sunk %d\n", count); if (sunk == input_size) { ASSERT_EQ(HSER_FINISH_MORE, heatshrink_encoder_finish(hse)); } HSE_poll_res pres; do { /* "turn the crank" */ pres = heatshrink_encoder_poll(hse, &comp[polled], comp_sz - polled, &count); ASSERT(pres >= 0); polled += count; if (cfg->log_lvl > 1) printf("^^ polled %d\n", count); } while (pres == HSER_POLL_MORE); ASSERT_EQ(HSER_POLL_EMPTY, pres); if (polled >= comp_sz) FAILm("compression should never expand that much"); if (sunk == input_size) { ASSERT_EQ(HSER_FINISH_DONE, heatshrink_encoder_finish(hse)); } } if (cfg->log_lvl > 0) printf("in: %u compressed: %u ", input_size, polled); uint32_t compressed_size = polled; sunk = 0; polled = 0; if (cfg->log_lvl > 1) { printf("\n^^ DECOMPRESSING\n"); dump_buf("comp", comp, compressed_size); } while (sunk < compressed_size) { ASSERT(heatshrink_decoder_sink(hsd, &comp[sunk], compressed_size - sunk, &count) >= 0); sunk += count; if (cfg->log_lvl > 1) printf("^^ sunk %d\n", count); if (sunk == compressed_size) { ASSERT_EQ(HSDR_FINISH_MORE, heatshrink_decoder_finish(hsd)); } HSD_poll_res pres; do { pres = heatshrink_decoder_poll(hsd, &decomp[polled], decomp_sz - polled, &count); ASSERT(pres >= 0); ASSERT(count > 0); polled += count; if (cfg->log_lvl > 1) printf("^^ polled %d\n", count); } while (pres == HSDR_POLL_MORE); ASSERT_EQ(HSDR_POLL_EMPTY, pres); if (sunk == compressed_size) { HSD_finish_res fres = heatshrink_decoder_finish(hsd); ASSERT_EQ(HSDR_FINISH_DONE, fres); } if (polled > input_size) { printf("\nExpected %d, got %d\n", input_size, polled); FAILm("Decompressed data is larger than original input"); } } if (cfg->log_lvl > 0) printf("decompressed: %u\n", polled); if (polled != input_size) { FAILm("Decompressed length does not match original input length"); } if (cfg->log_lvl > 1) dump_buf("decomp", decomp, polled); for (int i=0; i<input_size; i++) { if (input[i] != decomp[i]) { printf("*** mismatch at %d\n", i); if (0) { for (int j=0; j<=/*i*/ input_size; j++) { printf("in[%d] == 0x%02x ('%c') => out[%d] == 0x%02x ('%c') %c\n", j, input[j], isprint(input[j]) ? input[j] : '.', j, decomp[j], isprint(decomp[j]) ? decomp[j] : '.', input[j] == decomp[j] ? ' ' : 'X'); } } } ASSERT_EQ(input[i], decomp[i]); } free(comp); free(decomp); heatshrink_encoder_free(hse); heatshrink_decoder_free(hsd); PASS(); }
TEST teardown_example_FAIL() { teardown_was_called = 0; FAILm("Using FAIL to trigger teardown callback"); }
TEST standalone_test(void) { FAILm("(expected failure)"); }