test_index.c 4.5 KB

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