|
| 1 | +#include <criterion/criterion.h> |
| 2 | + |
| 3 | +#include <stddef.h> |
| 4 | +#include <stdint.h> |
| 5 | + |
| 6 | +#include "../monitor.h" |
| 7 | + |
| 8 | +Test(monitor, empty) { |
| 9 | + const Monitor *monitors; |
| 10 | + size_t n = monitor_get(&monitors); |
| 11 | + cr_assert_neq(n, 0); |
| 12 | + |
| 13 | + for (size_t i = 0; i < n; ++i) { |
| 14 | + cr_expect_eq(monitors[i].valid, false); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +Test(monitor, deleteNonExistent) { |
| 19 | + bool ok = monitor_delete(0); |
| 20 | + cr_assert_eq(ok, false); |
| 21 | +} |
| 22 | + |
| 23 | +Test(monitor, exec_breakpoints) { |
| 24 | + const uint16_t ADDRESS = 0x1234; |
| 25 | + const uint16_t NOTADDRESS = 0x5678; |
| 26 | + |
| 27 | + bool ok = monitor_addBreakpoint(ADDRESS); |
| 28 | + cr_assert_eq(ok, true); |
| 29 | + |
| 30 | + const Monitor *monitors; |
| 31 | + monitor_get(&monitors); |
| 32 | + cr_assert_eq(monitors[0].valid, true); |
| 33 | + cr_assert_eq(monitors[0].address, ADDRESS); |
| 34 | + |
| 35 | + bool hit = false; |
| 36 | + |
| 37 | + // executing an address which does not have a breakpoint set |
| 38 | + hit = monitor_checkBreakpoint(NOTADDRESS); |
| 39 | + cr_assert_eq(hit, false); |
| 40 | + |
| 41 | + // hitting breakpoint for the first time |
| 42 | + hit = monitor_checkBreakpoint(ADDRESS); |
| 43 | + cr_assert_eq(hit, true); |
| 44 | + |
| 45 | + // hitting breakpoint for the second time |
| 46 | + // breakpoint must be disabled otherwise CPU can't continue |
| 47 | + hit = monitor_checkBreakpoint(ADDRESS); |
| 48 | + cr_assert_eq(hit, false); |
| 49 | + |
| 50 | + // hitting breakpoint for the third time |
| 51 | + // breakpoint must be restored |
| 52 | + hit = monitor_checkBreakpoint(ADDRESS); |
| 53 | + cr_assert_eq(hit, true); |
| 54 | +} |
| 55 | + |
| 56 | +Test(monitor, read_watchpoint) { |
| 57 | + const uint16_t ADDRESS = 0x1234; |
| 58 | + const uint16_t NOTADDRESS = 0x5678; |
| 59 | + |
| 60 | + bool ok = monitor_addReadWatchpoint(ADDRESS); |
| 61 | + cr_assert_eq(ok, true); |
| 62 | + |
| 63 | + bool hit = false; |
| 64 | + hit = monitor_checkReadWatchpoint(NOTADDRESS); |
| 65 | + cr_assert_eq(hit, false); |
| 66 | + |
| 67 | + hit = monitor_checkReadWatchpoint(ADDRESS); |
| 68 | + cr_assert_eq(hit, true); |
| 69 | + |
| 70 | + hit = monitor_checkWriteWatchpoint(ADDRESS, 0x00); |
| 71 | + cr_assert_eq(hit, false); |
| 72 | +} |
| 73 | + |
| 74 | +Test(monitor, write_watchpoint) { |
| 75 | + const uint16_t ADDRESS_WITH_VAL = 0xabcd; |
| 76 | + const uint16_t ADDRESS_NO_VAL = 0xef01; |
| 77 | + const uint8_t VALUE = 0x42; |
| 78 | + const uint8_t NOTVALUE = 0x77; |
| 79 | + |
| 80 | + bool ok = false; |
| 81 | + bool hit = false; |
| 82 | + |
| 83 | + ok = monitor_addWriteWatchpoint(ADDRESS_WITH_VAL, &VALUE); |
| 84 | + cr_assert_eq(ok, true); |
| 85 | + ok = monitor_addWriteWatchpoint(ADDRESS_NO_VAL, NULL); |
| 86 | + cr_assert_eq(ok, true); |
| 87 | + |
| 88 | + hit = monitor_checkWriteWatchpoint(ADDRESS_WITH_VAL, NOTVALUE); |
| 89 | + cr_assert_eq(hit, false); |
| 90 | + hit = monitor_checkWriteWatchpoint(ADDRESS_WITH_VAL, VALUE); |
| 91 | + cr_assert_eq(hit, true); |
| 92 | + hit = monitor_checkWriteWatchpoint(ADDRESS_NO_VAL, 0x00); |
| 93 | + cr_assert_eq(hit, true); |
| 94 | +} |
| 95 | + |
| 96 | +Test(monitor, deleteBreakpoint) { |
| 97 | + const Monitor *monitors = NULL; |
| 98 | + size_t n = 0; |
| 99 | + size_t valid_cnt = 0; |
| 100 | + |
| 101 | + const uint16_t ADDRESS = 0x1234; |
| 102 | + monitor_addBreakpoint(ADDRESS); |
| 103 | + |
| 104 | + n = monitor_get(&monitors); |
| 105 | + valid_cnt = 0; |
| 106 | + for (size_t i = 0; i < n; ++i) |
| 107 | + if (monitors[i].valid) |
| 108 | + valid_cnt += 1; |
| 109 | + cr_assert_eq(valid_cnt, 1); |
| 110 | + |
| 111 | + bool ok = monitor_delete(0); |
| 112 | + cr_assert_eq(ok, true); |
| 113 | + |
| 114 | + n = monitor_get(&monitors); |
| 115 | + valid_cnt = 0; |
| 116 | + for (size_t i = 0; i < n; ++i) |
| 117 | + if (monitors[i].valid) |
| 118 | + valid_cnt += 1; |
| 119 | + cr_assert_eq(valid_cnt, 0); |
| 120 | +} |
0 commit comments