test_index.c 4.7 KB

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