test_index.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <stdio.h>
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. #include "minunit_vars.h"
  5. #include <notification/notification_messages.h>
  6. #include <cli/cli.h>
  7. #include <loader/loader.h>
  8. #define TAG "UnitTests"
  9. int run_minunit_test_furi();
  10. int run_minunit_test_furi_hal();
  11. int run_minunit_test_furi_string();
  12. int run_minunit_test_infrared();
  13. int run_minunit_test_rpc();
  14. int run_minunit_test_flipper_format();
  15. int run_minunit_test_flipper_format_string();
  16. int run_minunit_test_stream();
  17. int run_minunit_test_storage();
  18. int run_minunit_test_subghz();
  19. int run_minunit_test_dirwalk();
  20. int run_minunit_test_power();
  21. int run_minunit_test_protocol_dict();
  22. int run_minunit_test_lfrfid_protocols();
  23. int run_minunit_test_nfc();
  24. int run_minunit_test_bit_lib();
  25. int run_minunit_test_bt();
  26. typedef int (*UnitTestEntry)();
  27. typedef struct {
  28. const char* name;
  29. const UnitTestEntry entry;
  30. } UnitTest;
  31. const UnitTest unit_tests[] = {
  32. {.name = "furi", .entry = run_minunit_test_furi},
  33. {.name = "furi_hal", .entry = run_minunit_test_furi_hal},
  34. {.name = "furi_string", .entry = run_minunit_test_furi_string},
  35. {.name = "storage", .entry = run_minunit_test_storage},
  36. {.name = "stream", .entry = run_minunit_test_stream},
  37. {.name = "dirwalk", .entry = run_minunit_test_dirwalk},
  38. {.name = "flipper_format", .entry = run_minunit_test_flipper_format},
  39. {.name = "flipper_format_string", .entry = run_minunit_test_flipper_format_string},
  40. {.name = "rpc", .entry = run_minunit_test_rpc},
  41. {.name = "subghz", .entry = run_minunit_test_subghz},
  42. {.name = "infrared", .entry = run_minunit_test_infrared},
  43. {.name = "nfc", .entry = run_minunit_test_nfc},
  44. {.name = "power", .entry = run_minunit_test_power},
  45. {.name = "protocol_dict", .entry = run_minunit_test_protocol_dict},
  46. {.name = "lfrfid", .entry = run_minunit_test_lfrfid_protocols},
  47. {.name = "bit_lib", .entry = run_minunit_test_bit_lib},
  48. {.name = "bt", .entry = run_minunit_test_bt},
  49. };
  50. void minunit_print_progress() {
  51. static const char progress[] = {'\\', '|', '/', '-'};
  52. static uint8_t progress_counter = 0;
  53. static TickType_t last_tick = 0;
  54. TickType_t current_tick = xTaskGetTickCount();
  55. if(current_tick - last_tick > 20) {
  56. last_tick = current_tick;
  57. printf("[%c]\033[3D", progress[++progress_counter % COUNT_OF(progress)]);
  58. fflush(stdout);
  59. }
  60. }
  61. void minunit_print_fail(const char* str) {
  62. printf(FURI_LOG_CLR_E "%s\r\n" FURI_LOG_CLR_RESET, str);
  63. }
  64. void unit_tests_cli(Cli* cli, FuriString* args, void* context) {
  65. UNUSED(cli);
  66. UNUSED(args);
  67. UNUSED(context);
  68. uint32_t failed_tests = 0;
  69. minunit_run = 0;
  70. minunit_assert = 0;
  71. minunit_fail = 0;
  72. minunit_status = 0;
  73. Loader* loader = furi_record_open(RECORD_LOADER);
  74. NotificationApp* notification = furi_record_open(RECORD_NOTIFICATION);
  75. // TODO: lock device while test running
  76. if(loader_is_locked(loader)) {
  77. printf("RPC: stop all applications to run tests\r\n");
  78. notification_message(notification, &sequence_blink_magenta_100);
  79. } else {
  80. notification_message_block(notification, &sequence_set_only_blue_255);
  81. uint32_t heap_before = memmgr_get_free_heap();
  82. uint32_t cycle_counter = furi_get_tick();
  83. for(size_t i = 0; i < COUNT_OF(unit_tests); i++) {
  84. if(cli_cmd_interrupt_received(cli)) {
  85. break;
  86. }
  87. if(furi_string_size(args)) {
  88. if(furi_string_cmp_str(args, unit_tests[i].name) == 0) {
  89. failed_tests += unit_tests[i].entry();
  90. } else {
  91. printf("Skipping %s\r\n", unit_tests[i].name);
  92. }
  93. } else {
  94. failed_tests += unit_tests[i].entry();
  95. }
  96. }
  97. printf("\r\nFailed tests: %lu\r\n", failed_tests);
  98. // Time report
  99. cycle_counter = (furi_get_tick() - cycle_counter);
  100. printf("Consumed: %lu ms\r\n", cycle_counter);
  101. // Wait for tested services and apps to deallocate memory
  102. furi_delay_ms(200);
  103. uint32_t heap_after = memmgr_get_free_heap();
  104. printf("Leaked: %ld\r\n", heap_before - heap_after);
  105. // Final Report
  106. if(failed_tests == 0) {
  107. notification_message(notification, &sequence_success);
  108. printf("Status: PASSED\r\n");
  109. } else {
  110. notification_message(notification, &sequence_error);
  111. printf("Status: FAILED\r\n");
  112. }
  113. }
  114. furi_record_close(RECORD_NOTIFICATION);
  115. furi_record_close(RECORD_LOADER);
  116. }
  117. void unit_tests_on_system_start() {
  118. #ifdef SRV_CLI
  119. Cli* cli = furi_record_open(RECORD_CLI);
  120. // We need to launch apps from tests, so we cannot lock loader
  121. cli_add_command(cli, "unit_tests", CliCommandFlagParallelSafe, unit_tests_cli, NULL);
  122. furi_record_close(RECORD_CLI);
  123. #endif
  124. }