test_index.c 4.5 KB

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