rpc_test.c 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636
  1. #include "flipper.pb.h"
  2. #include "furi_hal_delay.h"
  3. #include "furi/check.h"
  4. #include "furi/record.h"
  5. #include "pb_decode.h"
  6. #include <rpc/rpc.h>
  7. #include "rpc/rpc_i.h"
  8. #include "storage.pb.h"
  9. #include "storage/filesystem_api_defines.h"
  10. #include "storage/storage.h"
  11. #include <furi.h>
  12. #include "../minunit.h"
  13. #include <stdint.h>
  14. #include <stream_buffer.h>
  15. #include <pb.h>
  16. #include <pb_encode.h>
  17. #include <m-list.h>
  18. #include <lib/toolbox/md5.h>
  19. #include <cli/cli.h>
  20. #include <loader/loader.h>
  21. #include <protobuf_version.h>
  22. #include <semphr.h>
  23. LIST_DEF(MsgList, PB_Main, M_POD_OPLIST)
  24. #define M_OPL_MsgList_t() LIST_OPLIST(MsgList)
  25. /* MinUnit test framework doesn't allow passing context into tests,
  26. * so we have to use global variables
  27. */
  28. static Rpc* rpc = NULL;
  29. static RpcSession* session = NULL;
  30. static uint32_t command_id = 0;
  31. typedef struct {
  32. StreamBufferHandle_t output_stream;
  33. SemaphoreHandle_t close_session_semaphore;
  34. TickType_t timeout;
  35. } RpcSessionContext;
  36. static RpcSessionContext rpc_session_context;
  37. #define TAG "UnitTestsRpc"
  38. #define MAX_RECEIVE_OUTPUT_TIMEOUT 3000
  39. #define MAX_NAME_LENGTH 255
  40. #define MAX_DATA_SIZE 512 // have to be exact as in rpc_storage.c
  41. #define TEST_DIR TEST_DIR_NAME "/"
  42. #define TEST_DIR_NAME "/ext/unit_tests_tmp"
  43. #define MD5SUM_SIZE 16
  44. #define PING_REQUEST 0
  45. #define PING_RESPONSE 1
  46. #define WRITE_REQUEST 0
  47. #define READ_RESPONSE 1
  48. #define DEBUG_PRINT 0
  49. #define BYTES(x) (x), sizeof(x)
  50. #define DISABLE_TEST(code) \
  51. do { \
  52. volatile int a = 0; \
  53. if(a) { \
  54. code \
  55. } \
  56. } while(0)
  57. static void output_bytes_callback(void* ctx, uint8_t* got_bytes, size_t got_size);
  58. static void clean_directory(Storage* fs_api, const char* clean_dir);
  59. static void
  60. test_rpc_add_empty_to_list(MsgList_t msg_list, PB_CommandStatus status, uint32_t command_id);
  61. static void test_rpc_encode_and_feed(MsgList_t msg_list);
  62. static void test_rpc_encode_and_feed_one(PB_Main* request);
  63. static void test_rpc_compare_messages(PB_Main* result, PB_Main* expected);
  64. static void test_rpc_decode_and_compare(MsgList_t expected_msg_list);
  65. static void test_rpc_free_msg_list(MsgList_t msg_list);
  66. static void test_rpc_session_close_callback(void* context);
  67. static void test_rpc_setup(void) {
  68. furi_check(!rpc);
  69. furi_check(!session);
  70. rpc = furi_record_open("rpc");
  71. for(int i = 0; !session && (i < 10000); ++i) {
  72. session = rpc_session_open(rpc);
  73. delay(1);
  74. }
  75. furi_check(session);
  76. rpc_session_context.output_stream = xStreamBufferCreate(1000, 1);
  77. rpc_session_set_send_bytes_callback(session, output_bytes_callback);
  78. rpc_session_context.close_session_semaphore = xSemaphoreCreateBinary();
  79. rpc_session_set_close_callback(session, test_rpc_session_close_callback);
  80. rpc_session_set_context(session, &rpc_session_context);
  81. }
  82. static void test_rpc_teardown(void) {
  83. furi_check(rpc_session_context.close_session_semaphore);
  84. rpc_session_close(session);
  85. furi_record_close("rpc");
  86. vStreamBufferDelete(rpc_session_context.output_stream);
  87. vSemaphoreDelete(rpc_session_context.close_session_semaphore);
  88. ++command_id;
  89. rpc_session_context.output_stream = NULL;
  90. rpc_session_context.close_session_semaphore = NULL;
  91. rpc = NULL;
  92. session = NULL;
  93. }
  94. static void test_rpc_storage_setup(void) {
  95. test_rpc_setup();
  96. Storage* fs_api = furi_record_open("storage");
  97. clean_directory(fs_api, TEST_DIR_NAME);
  98. furi_record_close("storage");
  99. }
  100. static void test_rpc_storage_teardown(void) {
  101. test_rpc_teardown();
  102. Storage* fs_api = furi_record_open("storage");
  103. clean_directory(fs_api, TEST_DIR_NAME);
  104. furi_record_close("storage");
  105. }
  106. static void test_rpc_session_close_callback(void* context) {
  107. furi_check(context);
  108. RpcSessionContext* callbacks_context = context;
  109. xSemaphoreGive(callbacks_context->close_session_semaphore);
  110. }
  111. static void clean_directory(Storage* fs_api, const char* clean_dir) {
  112. furi_check(fs_api);
  113. furi_check(clean_dir);
  114. File* dir = storage_file_alloc(fs_api);
  115. if(storage_dir_open(dir, clean_dir)) {
  116. FileInfo fileinfo;
  117. char* name = furi_alloc(MAX_NAME_LENGTH + 1);
  118. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  119. char* fullname = furi_alloc(strlen(clean_dir) + strlen(name) + 1 + 1);
  120. sprintf(fullname, "%s/%s", clean_dir, name);
  121. if(fileinfo.flags & FSF_DIRECTORY) {
  122. clean_directory(fs_api, fullname);
  123. }
  124. FS_Error error = storage_common_remove(fs_api, fullname);
  125. furi_check(error == FSE_OK);
  126. free(fullname);
  127. }
  128. free(name);
  129. } else {
  130. FS_Error error = storage_common_mkdir(fs_api, clean_dir);
  131. (void)error;
  132. furi_check(error == FSE_OK);
  133. }
  134. storage_dir_close(dir);
  135. storage_file_free(dir);
  136. }
  137. static void test_rpc_print_message_list(MsgList_t msg_list) {
  138. #if DEBUG_PRINT
  139. MsgList_reverse(msg_list);
  140. for
  141. M_EACH(msg, msg_list, MsgList_t) {
  142. rpc_print_message(msg);
  143. }
  144. MsgList_reverse(msg_list);
  145. #endif
  146. }
  147. static PB_CommandStatus test_rpc_storage_get_file_error(File* file) {
  148. FS_Error fs_error = storage_file_get_error(file);
  149. PB_CommandStatus pb_error;
  150. switch(fs_error) {
  151. case FSE_OK:
  152. pb_error = PB_CommandStatus_OK;
  153. break;
  154. case FSE_INVALID_NAME:
  155. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
  156. break;
  157. case FSE_INVALID_PARAMETER:
  158. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_PARAMETER;
  159. break;
  160. case FSE_INTERNAL:
  161. pb_error = PB_CommandStatus_ERROR_STORAGE_INTERNAL;
  162. break;
  163. case FSE_ALREADY_OPEN:
  164. pb_error = PB_CommandStatus_ERROR_STORAGE_ALREADY_OPEN;
  165. break;
  166. case FSE_DENIED:
  167. pb_error = PB_CommandStatus_ERROR_STORAGE_DENIED;
  168. break;
  169. case FSE_EXIST:
  170. pb_error = PB_CommandStatus_ERROR_STORAGE_EXIST;
  171. break;
  172. case FSE_NOT_EXIST:
  173. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_EXIST;
  174. break;
  175. case FSE_NOT_READY:
  176. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_READY;
  177. break;
  178. case FSE_NOT_IMPLEMENTED:
  179. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_IMPLEMENTED;
  180. break;
  181. default:
  182. pb_error = PB_CommandStatus_ERROR;
  183. break;
  184. }
  185. return pb_error;
  186. }
  187. static void output_bytes_callback(void* ctx, uint8_t* got_bytes, size_t got_size) {
  188. RpcSessionContext* callbacks_context = ctx;
  189. size_t bytes_sent =
  190. xStreamBufferSend(callbacks_context->output_stream, got_bytes, got_size, osWaitForever);
  191. (void)bytes_sent;
  192. furi_check(bytes_sent == got_size);
  193. }
  194. static void test_rpc_add_ping_to_list(MsgList_t msg_list, bool request, uint32_t command_id) {
  195. PB_Main* response = MsgList_push_new(msg_list);
  196. response->command_id = command_id;
  197. response->command_status = PB_CommandStatus_OK;
  198. response->cb_content.funcs.encode = NULL;
  199. response->has_next = false;
  200. response->which_content = (request == PING_REQUEST) ? PB_Main_system_ping_request_tag :
  201. PB_Main_system_ping_response_tag;
  202. }
  203. static void test_rpc_create_simple_message(
  204. PB_Main* message,
  205. uint16_t tag,
  206. const char* str,
  207. uint32_t command_id) {
  208. furi_check(message);
  209. char* str_copy = NULL;
  210. if(str) {
  211. str_copy = strdup(str);
  212. }
  213. message->command_id = command_id;
  214. message->command_status = PB_CommandStatus_OK;
  215. message->cb_content.funcs.encode = NULL;
  216. message->which_content = tag;
  217. message->has_next = false;
  218. switch(tag) {
  219. case PB_Main_storage_info_request_tag:
  220. message->content.storage_info_request.path = str_copy;
  221. break;
  222. case PB_Main_storage_stat_request_tag:
  223. message->content.storage_stat_request.path = str_copy;
  224. break;
  225. case PB_Main_storage_list_request_tag:
  226. message->content.storage_list_request.path = str_copy;
  227. break;
  228. case PB_Main_storage_mkdir_request_tag:
  229. message->content.storage_mkdir_request.path = str_copy;
  230. break;
  231. case PB_Main_storage_read_request_tag:
  232. message->content.storage_read_request.path = str_copy;
  233. break;
  234. case PB_Main_storage_delete_request_tag:
  235. message->content.storage_delete_request.path = str_copy;
  236. break;
  237. case PB_Main_storage_md5sum_request_tag:
  238. message->content.storage_md5sum_request.path = str_copy;
  239. break;
  240. case PB_Main_storage_md5sum_response_tag: {
  241. char* md5sum = message->content.storage_md5sum_response.md5sum;
  242. size_t md5sum_size = sizeof(message->content.storage_md5sum_response.md5sum);
  243. furi_check((strlen(str) + 1) <= md5sum_size);
  244. memcpy(md5sum, str_copy, md5sum_size);
  245. free(str_copy);
  246. break;
  247. }
  248. default:
  249. furi_check(0);
  250. break;
  251. }
  252. }
  253. static void test_rpc_add_read_or_write_to_list(
  254. MsgList_t msg_list,
  255. bool write,
  256. const char* path,
  257. const uint8_t* pattern,
  258. size_t pattern_size,
  259. size_t pattern_repeats,
  260. uint32_t command_id) {
  261. furi_check(pattern_repeats > 0);
  262. do {
  263. PB_Main* request = MsgList_push_new(msg_list);
  264. PB_Storage_File* msg_file = NULL;
  265. request->command_id = command_id;
  266. request->command_status = PB_CommandStatus_OK;
  267. if(write == WRITE_REQUEST) {
  268. request->content.storage_write_request.path = strdup(path);
  269. request->which_content = PB_Main_storage_write_request_tag;
  270. request->content.storage_write_request.has_file = true;
  271. msg_file = &request->content.storage_write_request.file;
  272. } else {
  273. request->which_content = PB_Main_storage_read_response_tag;
  274. request->content.storage_read_response.has_file = true;
  275. msg_file = &request->content.storage_read_response.file;
  276. }
  277. msg_file->data = furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(pattern_size));
  278. msg_file->data->size = pattern_size;
  279. memcpy(msg_file->data->bytes, pattern, pattern_size);
  280. --pattern_repeats;
  281. request->has_next = (pattern_repeats > 0);
  282. } while(pattern_repeats);
  283. }
  284. static void test_rpc_encode_and_feed_one(PB_Main* request) {
  285. furi_check(request);
  286. pb_ostream_t ostream = PB_OSTREAM_SIZING;
  287. bool result = pb_encode_ex(&ostream, &PB_Main_msg, request, PB_ENCODE_DELIMITED);
  288. furi_check(result && ostream.bytes_written);
  289. uint8_t* buffer = furi_alloc(ostream.bytes_written);
  290. ostream = pb_ostream_from_buffer(buffer, ostream.bytes_written);
  291. pb_encode_ex(&ostream, &PB_Main_msg, request, PB_ENCODE_DELIMITED);
  292. size_t bytes_left = ostream.bytes_written;
  293. uint8_t* buffer_ptr = buffer;
  294. do {
  295. size_t bytes_sent = rpc_session_feed(session, buffer_ptr, bytes_left, 1000);
  296. mu_check(bytes_sent > 0);
  297. bytes_left -= bytes_sent;
  298. buffer_ptr += bytes_sent;
  299. } while(bytes_left);
  300. free(buffer);
  301. pb_release(&PB_Main_msg, request);
  302. }
  303. static void test_rpc_encode_and_feed(MsgList_t msg_list) {
  304. MsgList_reverse(msg_list);
  305. for
  306. M_EACH(request, msg_list, MsgList_t) {
  307. test_rpc_encode_and_feed_one(request);
  308. }
  309. MsgList_reverse(msg_list);
  310. }
  311. static void
  312. test_rpc_compare_file(PB_Storage_File* result_msg_file, PB_Storage_File* expected_msg_file) {
  313. mu_check(!result_msg_file->name == !expected_msg_file->name);
  314. if(result_msg_file->name) {
  315. mu_check(!strcmp(result_msg_file->name, expected_msg_file->name));
  316. }
  317. mu_check(result_msg_file->size == expected_msg_file->size);
  318. mu_check(result_msg_file->type == expected_msg_file->type);
  319. mu_check(!result_msg_file->data == !expected_msg_file->data);
  320. mu_check(result_msg_file->data->size == expected_msg_file->data->size);
  321. for(int i = 0; i < result_msg_file->data->size; ++i) {
  322. mu_check(result_msg_file->data->bytes[i] == expected_msg_file->data->bytes[i]);
  323. }
  324. }
  325. static void test_rpc_compare_messages(PB_Main* result, PB_Main* expected) {
  326. mu_check(result->command_id == expected->command_id);
  327. mu_check(result->command_status == expected->command_status);
  328. mu_check(result->has_next == expected->has_next);
  329. mu_check(result->which_content == expected->which_content);
  330. if(result->command_status != PB_CommandStatus_OK) {
  331. mu_check(result->which_content == PB_Main_empty_tag);
  332. }
  333. switch(result->which_content) {
  334. case PB_Main_empty_tag:
  335. case PB_Main_system_ping_response_tag:
  336. /* nothing to check */
  337. break;
  338. case PB_Main_system_ping_request_tag:
  339. case PB_Main_storage_list_request_tag:
  340. case PB_Main_storage_read_request_tag:
  341. case PB_Main_storage_write_request_tag:
  342. case PB_Main_storage_delete_request_tag:
  343. case PB_Main_storage_mkdir_request_tag:
  344. case PB_Main_storage_md5sum_request_tag:
  345. /* rpc doesn't send it */
  346. mu_check(0);
  347. break;
  348. case PB_Main_app_lock_status_response_tag: {
  349. bool result_locked = result->content.app_lock_status_response.locked;
  350. bool expected_locked = expected->content.app_lock_status_response.locked;
  351. mu_check(result_locked == expected_locked);
  352. break;
  353. }
  354. case PB_Main_storage_info_response_tag: {
  355. uint64_t result_total_space = result->content.storage_info_response.total_space;
  356. uint64_t expected_total_space = expected->content.storage_info_response.total_space;
  357. mu_check(result_total_space == expected_total_space);
  358. uint64_t result_free_space = result->content.storage_info_response.free_space;
  359. uint64_t expected_free_space = expected->content.storage_info_response.free_space;
  360. mu_check(result_free_space == expected_free_space);
  361. } break;
  362. case PB_Main_storage_stat_response_tag: {
  363. bool result_has_msg_file = result->content.storage_stat_response.has_file;
  364. bool expected_has_msg_file = expected->content.storage_stat_response.has_file;
  365. mu_check(result_has_msg_file == expected_has_msg_file);
  366. if(result_has_msg_file) {
  367. PB_Storage_File* result_msg_file = &result->content.storage_stat_response.file;
  368. PB_Storage_File* expected_msg_file = &expected->content.storage_stat_response.file;
  369. test_rpc_compare_file(result_msg_file, expected_msg_file);
  370. } else {
  371. mu_check(0);
  372. }
  373. } break;
  374. case PB_Main_storage_read_response_tag: {
  375. bool result_has_msg_file = result->content.storage_read_response.has_file;
  376. bool expected_has_msg_file = expected->content.storage_read_response.has_file;
  377. mu_check(result_has_msg_file == expected_has_msg_file);
  378. if(result_has_msg_file) {
  379. PB_Storage_File* result_msg_file = &result->content.storage_read_response.file;
  380. PB_Storage_File* expected_msg_file = &expected->content.storage_read_response.file;
  381. test_rpc_compare_file(result_msg_file, expected_msg_file);
  382. } else {
  383. mu_check(0);
  384. }
  385. } break;
  386. case PB_Main_storage_list_response_tag: {
  387. size_t expected_msg_files = expected->content.storage_list_response.file_count;
  388. size_t result_msg_files = result->content.storage_list_response.file_count;
  389. mu_check(result_msg_files == expected_msg_files);
  390. for(int i = 0; i < expected_msg_files; ++i) {
  391. PB_Storage_File* result_msg_file = &result->content.storage_list_response.file[i];
  392. PB_Storage_File* expected_msg_file = &expected->content.storage_list_response.file[i];
  393. test_rpc_compare_file(result_msg_file, expected_msg_file);
  394. }
  395. break;
  396. }
  397. case PB_Main_storage_md5sum_response_tag: {
  398. char* result_md5sum = result->content.storage_md5sum_response.md5sum;
  399. char* expected_md5sum = expected->content.storage_md5sum_response.md5sum;
  400. mu_check(!strcmp(result_md5sum, expected_md5sum));
  401. break;
  402. }
  403. case PB_Main_system_protobuf_version_response_tag: {
  404. uint32_t major_version_expected = expected->content.system_protobuf_version_response.major;
  405. uint32_t minor_version_expected = expected->content.system_protobuf_version_response.minor;
  406. uint32_t major_version_result = result->content.system_protobuf_version_response.major;
  407. uint32_t minor_version_result = result->content.system_protobuf_version_response.minor;
  408. mu_check(major_version_expected == major_version_result);
  409. mu_check(minor_version_expected == minor_version_result);
  410. break;
  411. }
  412. default:
  413. furi_check(0);
  414. break;
  415. }
  416. }
  417. static bool test_rpc_pb_stream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
  418. RpcSessionContext* session_context = istream->state;
  419. size_t bytes_received = 0;
  420. TickType_t now = xTaskGetTickCount();
  421. int32_t time_left = session_context->timeout - now;
  422. time_left = MAX(time_left, 0);
  423. bytes_received = xStreamBufferReceive(session_context->output_stream, buf, count, time_left);
  424. return (count == bytes_received);
  425. }
  426. static void
  427. test_rpc_storage_list_create_expected_list_root(MsgList_t msg_list, uint32_t command_id) {
  428. PB_Main* message = MsgList_push_new(msg_list);
  429. message->has_next = false;
  430. message->cb_content.funcs.encode = NULL;
  431. message->command_id = command_id;
  432. message->which_content = PB_Main_storage_list_response_tag;
  433. message->content.storage_list_response.file_count = 3;
  434. message->content.storage_list_response.file[0].data = NULL;
  435. message->content.storage_list_response.file[1].data = NULL;
  436. message->content.storage_list_response.file[2].data = NULL;
  437. message->content.storage_list_response.file[0].size = 0;
  438. message->content.storage_list_response.file[1].size = 0;
  439. message->content.storage_list_response.file[2].size = 0;
  440. message->content.storage_list_response.file[0].type = PB_Storage_File_FileType_DIR;
  441. message->content.storage_list_response.file[1].type = PB_Storage_File_FileType_DIR;
  442. message->content.storage_list_response.file[2].type = PB_Storage_File_FileType_DIR;
  443. char* str = furi_alloc(4);
  444. strcpy(str, "any");
  445. message->content.storage_list_response.file[0].name = str;
  446. str = furi_alloc(4);
  447. strcpy(str, "int");
  448. message->content.storage_list_response.file[1].name = str;
  449. str = furi_alloc(4);
  450. strcpy(str, "ext");
  451. message->content.storage_list_response.file[2].name = str;
  452. }
  453. static void test_rpc_storage_list_create_expected_list(
  454. MsgList_t msg_list,
  455. const char* path,
  456. uint32_t command_id) {
  457. Storage* fs_api = furi_record_open("storage");
  458. File* dir = storage_file_alloc(fs_api);
  459. PB_Main response = {
  460. .command_id = command_id,
  461. .has_next = false,
  462. .which_content = PB_Main_storage_list_response_tag,
  463. /* other fields (e.g. msg_files ptrs) explicitly initialized by 0 */
  464. };
  465. PB_Storage_ListResponse* list = &response.content.storage_list_response;
  466. bool finish = false;
  467. int i = 0;
  468. if(storage_dir_open(dir, path)) {
  469. response.command_status = PB_CommandStatus_OK;
  470. } else {
  471. response.command_status = test_rpc_storage_get_file_error(dir);
  472. response.which_content = PB_Main_empty_tag;
  473. finish = true;
  474. }
  475. while(!finish) {
  476. FileInfo fileinfo;
  477. char* name = furi_alloc(MAX_NAME_LENGTH + 1);
  478. if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  479. if(i == COUNT_OF(list->file)) {
  480. list->file_count = i;
  481. response.has_next = true;
  482. MsgList_push_back(msg_list, response);
  483. i = 0;
  484. }
  485. list->file[i].type = (fileinfo.flags & FSF_DIRECTORY) ? PB_Storage_File_FileType_DIR :
  486. PB_Storage_File_FileType_FILE;
  487. list->file[i].size = fileinfo.size;
  488. list->file[i].data = NULL;
  489. /* memory free inside rpc_encode_and_send() -> pb_release() */
  490. list->file[i].name = name;
  491. ++i;
  492. } else {
  493. finish = true;
  494. free(name);
  495. }
  496. }
  497. list->file_count = i;
  498. response.has_next = false;
  499. MsgList_push_back(msg_list, response);
  500. storage_dir_close(dir);
  501. storage_file_free(dir);
  502. furi_record_close("storage");
  503. }
  504. static void test_rpc_decode_and_compare(MsgList_t expected_msg_list) {
  505. furi_check(!MsgList_empty_p(expected_msg_list));
  506. rpc_session_context.timeout = xTaskGetTickCount() + MAX_RECEIVE_OUTPUT_TIMEOUT;
  507. pb_istream_t istream = {
  508. .callback = test_rpc_pb_stream_read,
  509. .state = &rpc_session_context,
  510. .errmsg = NULL,
  511. .bytes_left = 0x7FFFFFFF,
  512. };
  513. /* other fields explicitly initialized by 0 */
  514. PB_Main result = {.cb_content.funcs.decode = NULL};
  515. /* mlib adds msg_files into start of list, so reverse it */
  516. MsgList_reverse(expected_msg_list);
  517. for
  518. M_EACH(expected_msg, expected_msg_list, MsgList_t) {
  519. if(!pb_decode_ex(&istream, &PB_Main_msg, &result, PB_DECODE_DELIMITED)) {
  520. mu_fail(
  521. "not all expected messages decoded (maybe increase MAX_RECEIVE_OUTPUT_TIMEOUT)");
  522. break;
  523. }
  524. test_rpc_compare_messages(&result, expected_msg);
  525. pb_release(&PB_Main_msg, &result);
  526. }
  527. rpc_session_context.timeout = xTaskGetTickCount() + 50;
  528. if(pb_decode_ex(&istream, &PB_Main_msg, &result, PB_DECODE_DELIMITED)) {
  529. mu_fail("decoded more than expected");
  530. }
  531. MsgList_reverse(expected_msg_list);
  532. }
  533. static void test_rpc_free_msg_list(MsgList_t msg_list) {
  534. for
  535. M_EACH(it, msg_list, MsgList_t) {
  536. pb_release(&PB_Main_msg, it);
  537. }
  538. MsgList_clear(msg_list);
  539. }
  540. static void test_rpc_storage_list_run(const char* path, uint32_t command_id) {
  541. PB_Main request;
  542. MsgList_t expected_msg_list;
  543. MsgList_init(expected_msg_list);
  544. test_rpc_create_simple_message(&request, PB_Main_storage_list_request_tag, path, command_id);
  545. if(!strcmp(path, "/")) {
  546. test_rpc_storage_list_create_expected_list_root(expected_msg_list, command_id);
  547. } else {
  548. test_rpc_storage_list_create_expected_list(expected_msg_list, path, command_id);
  549. }
  550. test_rpc_encode_and_feed_one(&request);
  551. test_rpc_decode_and_compare(expected_msg_list);
  552. pb_release(&PB_Main_msg, &request);
  553. test_rpc_free_msg_list(expected_msg_list);
  554. }
  555. MU_TEST(test_storage_list) {
  556. test_rpc_storage_list_run("/", ++command_id);
  557. test_rpc_storage_list_run("/ext/nfc", ++command_id);
  558. test_rpc_storage_list_run("/int", ++command_id);
  559. test_rpc_storage_list_run("/ext", ++command_id);
  560. test_rpc_storage_list_run("/ext/irda", ++command_id);
  561. test_rpc_storage_list_run("/ext/ibutton", ++command_id);
  562. test_rpc_storage_list_run("/ext/lfrfid", ++command_id);
  563. test_rpc_storage_list_run("error_path", ++command_id);
  564. }
  565. static void
  566. test_rpc_add_empty_to_list(MsgList_t msg_list, PB_CommandStatus status, uint32_t command_id) {
  567. PB_Main* response = MsgList_push_new(msg_list);
  568. response->command_id = command_id;
  569. response->command_status = status;
  570. response->cb_content.funcs.encode = NULL;
  571. response->has_next = false;
  572. response->which_content = PB_Main_empty_tag;
  573. }
  574. static void test_rpc_add_read_to_list_by_reading_real_file(
  575. MsgList_t msg_list,
  576. const char* path,
  577. uint32_t command_id) {
  578. furi_check(MsgList_empty_p(msg_list));
  579. Storage* fs_api = furi_record_open("storage");
  580. File* file = storage_file_alloc(fs_api);
  581. bool result = false;
  582. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  583. size_t size_left = storage_file_size(file);
  584. do {
  585. PB_Main* response = MsgList_push_new(msg_list);
  586. response->command_id = command_id;
  587. response->command_status = PB_CommandStatus_OK;
  588. response->has_next = false;
  589. response->which_content = PB_Main_storage_read_response_tag;
  590. response->content.storage_read_response.has_file = true;
  591. response->content.storage_read_response.file.data =
  592. furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(MIN(size_left, MAX_DATA_SIZE)));
  593. uint8_t* buffer = response->content.storage_read_response.file.data->bytes;
  594. uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
  595. size_t read_size = MIN(size_left, MAX_DATA_SIZE);
  596. *read_size_msg = storage_file_read(file, buffer, read_size);
  597. size_left -= read_size;
  598. result = (*read_size_msg == read_size);
  599. if(result) {
  600. response->has_next = (size_left > 0);
  601. }
  602. } while((size_left != 0) && result);
  603. if(!result) {
  604. test_rpc_add_empty_to_list(
  605. msg_list, test_rpc_storage_get_file_error(file), command_id);
  606. }
  607. } else {
  608. test_rpc_add_empty_to_list(msg_list, test_rpc_storage_get_file_error(file), command_id);
  609. }
  610. storage_file_close(file);
  611. storage_file_free(file);
  612. furi_record_close("storage");
  613. }
  614. static void test_storage_read_run(const char* path, uint32_t command_id) {
  615. PB_Main request;
  616. MsgList_t expected_msg_list;
  617. MsgList_init(expected_msg_list);
  618. test_rpc_add_read_to_list_by_reading_real_file(expected_msg_list, path, command_id);
  619. test_rpc_create_simple_message(&request, PB_Main_storage_read_request_tag, path, command_id);
  620. test_rpc_encode_and_feed_one(&request);
  621. test_rpc_decode_and_compare(expected_msg_list);
  622. pb_release(&PB_Main_msg, &request);
  623. test_rpc_free_msg_list(expected_msg_list);
  624. }
  625. static bool test_is_exists(const char* path) {
  626. Storage* fs_api = furi_record_open("storage");
  627. FileInfo fileinfo;
  628. FS_Error result = storage_common_stat(fs_api, path, &fileinfo);
  629. furi_check((result == FSE_OK) || (result == FSE_NOT_EXIST));
  630. furi_record_close("storage");
  631. return result == FSE_OK;
  632. }
  633. static void test_create_dir(const char* path) {
  634. Storage* fs_api = furi_record_open("storage");
  635. FS_Error error = storage_common_mkdir(fs_api, path);
  636. (void)error;
  637. furi_check((error == FSE_OK) || (error == FSE_EXIST));
  638. furi_record_close("storage");
  639. furi_check(test_is_exists(path));
  640. }
  641. static void test_create_file(const char* path, size_t size) {
  642. Storage* fs_api = furi_record_open("storage");
  643. File* file = storage_file_alloc(fs_api);
  644. if(storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  645. uint8_t buf[128] = {0};
  646. for(int i = 0; i < sizeof(buf); ++i) {
  647. buf[i] = '0' + (i % 10);
  648. }
  649. while(size) {
  650. size_t written = storage_file_write(file, buf, MIN(size, sizeof(buf)));
  651. furi_check(written);
  652. size -= written;
  653. }
  654. }
  655. storage_file_close(file);
  656. storage_file_free(file);
  657. furi_record_close("storage");
  658. furi_check(test_is_exists(path));
  659. }
  660. static void test_rpc_storage_info_run(const char* path, uint32_t command_id) {
  661. PB_Main request;
  662. MsgList_t expected_msg_list;
  663. MsgList_init(expected_msg_list);
  664. test_rpc_create_simple_message(&request, PB_Main_storage_info_request_tag, path, command_id);
  665. PB_Main* response = MsgList_push_new(expected_msg_list);
  666. response->command_id = command_id;
  667. Storage* fs_api = furi_record_open("storage");
  668. FS_Error error = storage_common_fs_info(
  669. fs_api,
  670. path,
  671. &response->content.storage_info_response.total_space,
  672. &response->content.storage_info_response.free_space);
  673. response->command_status = rpc_system_storage_get_error(error);
  674. if(error == FSE_OK) {
  675. response->which_content = PB_Main_storage_info_response_tag;
  676. } else {
  677. response->which_content = PB_Main_empty_tag;
  678. }
  679. test_rpc_encode_and_feed_one(&request);
  680. test_rpc_decode_and_compare(expected_msg_list);
  681. pb_release(&PB_Main_msg, &request);
  682. test_rpc_free_msg_list(expected_msg_list);
  683. }
  684. static void test_rpc_storage_stat_run(const char* path, uint32_t command_id) {
  685. PB_Main request;
  686. MsgList_t expected_msg_list;
  687. MsgList_init(expected_msg_list);
  688. test_rpc_create_simple_message(&request, PB_Main_storage_stat_request_tag, path, command_id);
  689. Storage* fs_api = furi_record_open("storage");
  690. FileInfo fileinfo;
  691. FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
  692. furi_record_close("storage");
  693. PB_Main* response = MsgList_push_new(expected_msg_list);
  694. response->command_id = command_id;
  695. response->command_status = rpc_system_storage_get_error(error);
  696. response->has_next = false;
  697. response->which_content = PB_Main_empty_tag;
  698. if(error == FSE_OK) {
  699. response->which_content = PB_Main_storage_stat_response_tag;
  700. response->content.storage_stat_response.has_file = true;
  701. response->content.storage_stat_response.file.type = (fileinfo.flags & FSF_DIRECTORY) ?
  702. PB_Storage_File_FileType_DIR :
  703. PB_Storage_File_FileType_FILE;
  704. response->content.storage_stat_response.file.size = fileinfo.size;
  705. }
  706. test_rpc_encode_and_feed_one(&request);
  707. test_rpc_decode_and_compare(expected_msg_list);
  708. pb_release(&PB_Main_msg, &request);
  709. test_rpc_free_msg_list(expected_msg_list);
  710. }
  711. MU_TEST(test_storage_info) {
  712. test_rpc_storage_info_run("/any", ++command_id);
  713. test_rpc_storage_info_run("/int", ++command_id);
  714. test_rpc_storage_info_run("/ext", ++command_id);
  715. }
  716. #define TEST_DIR_STAT_NAME TEST_DIR "stat_dir"
  717. #define TEST_DIR_STAT TEST_DIR_STAT_NAME "/"
  718. MU_TEST(test_storage_stat) {
  719. test_create_dir(TEST_DIR_STAT_NAME);
  720. test_create_file(TEST_DIR_STAT "empty.txt", 0);
  721. test_create_file(TEST_DIR_STAT "l33t.txt", 1337);
  722. test_rpc_storage_stat_run("/", ++command_id);
  723. test_rpc_storage_stat_run("/int", ++command_id);
  724. test_rpc_storage_stat_run("/ext", ++command_id);
  725. test_rpc_storage_stat_run(TEST_DIR_STAT "empty.txt", ++command_id);
  726. test_rpc_storage_stat_run(TEST_DIR_STAT "l33t.txt", ++command_id);
  727. test_rpc_storage_stat_run(TEST_DIR_STAT "missing", ++command_id);
  728. test_rpc_storage_stat_run(TEST_DIR_STAT_NAME, ++command_id);
  729. test_rpc_storage_stat_run(TEST_DIR_STAT, ++command_id);
  730. }
  731. MU_TEST(test_storage_read) {
  732. test_create_file(TEST_DIR "empty.txt", 0);
  733. test_create_file(TEST_DIR "file1.txt", 1);
  734. test_create_file(TEST_DIR "file2.txt", MAX_DATA_SIZE);
  735. test_create_file(TEST_DIR "file3.txt", MAX_DATA_SIZE + 1);
  736. test_create_file(TEST_DIR "file4.txt", (MAX_DATA_SIZE * 2) + 1);
  737. test_storage_read_run(TEST_DIR "empty.txt", ++command_id);
  738. test_storage_read_run(TEST_DIR "file1.txt", ++command_id);
  739. test_storage_read_run(TEST_DIR "file2.txt", ++command_id);
  740. test_storage_read_run(TEST_DIR "file3.txt", ++command_id);
  741. test_storage_read_run(TEST_DIR "file4.txt", ++command_id);
  742. }
  743. static void test_storage_write_run(
  744. const char* path,
  745. size_t write_size,
  746. size_t write_count,
  747. uint32_t command_id,
  748. PB_CommandStatus status) {
  749. MsgList_t input_msg_list;
  750. MsgList_init(input_msg_list);
  751. MsgList_t expected_msg_list;
  752. MsgList_init(expected_msg_list);
  753. uint8_t* buf = furi_alloc(write_size);
  754. for(int i = 0; i < write_size; ++i) {
  755. buf[i] = '0' + (i % 10);
  756. }
  757. test_rpc_add_read_or_write_to_list(
  758. input_msg_list, WRITE_REQUEST, path, buf, write_size, write_count, command_id);
  759. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  760. test_rpc_encode_and_feed(input_msg_list);
  761. test_rpc_decode_and_compare(expected_msg_list);
  762. test_rpc_free_msg_list(input_msg_list);
  763. test_rpc_free_msg_list(expected_msg_list);
  764. free(buf);
  765. }
  766. static void test_storage_write_read_run(
  767. const char* path,
  768. const uint8_t* pattern,
  769. size_t pattern_size,
  770. size_t pattern_repeats,
  771. uint32_t* command_id) {
  772. MsgList_t input_msg_list;
  773. MsgList_init(input_msg_list);
  774. MsgList_t expected_msg_list;
  775. MsgList_init(expected_msg_list);
  776. test_rpc_add_read_or_write_to_list(
  777. input_msg_list, WRITE_REQUEST, path, pattern, pattern_size, pattern_repeats, ++*command_id);
  778. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, *command_id);
  779. test_rpc_create_simple_message(
  780. MsgList_push_raw(input_msg_list), PB_Main_storage_read_request_tag, path, ++*command_id);
  781. test_rpc_add_read_or_write_to_list(
  782. expected_msg_list,
  783. READ_RESPONSE,
  784. path,
  785. pattern,
  786. pattern_size,
  787. pattern_repeats,
  788. *command_id);
  789. test_rpc_print_message_list(input_msg_list);
  790. test_rpc_print_message_list(expected_msg_list);
  791. test_rpc_encode_and_feed(input_msg_list);
  792. test_rpc_decode_and_compare(expected_msg_list);
  793. test_rpc_free_msg_list(input_msg_list);
  794. test_rpc_free_msg_list(expected_msg_list);
  795. }
  796. MU_TEST(test_storage_write_read) {
  797. uint8_t pattern1[] = "abcdefgh";
  798. test_storage_write_read_run(TEST_DIR "test1.txt", pattern1, sizeof(pattern1), 1, &command_id);
  799. test_storage_write_read_run(TEST_DIR "test2.txt", pattern1, 1, 1, &command_id);
  800. test_storage_write_read_run(TEST_DIR "test3.txt", pattern1, 0, 1, &command_id);
  801. }
  802. MU_TEST(test_storage_write) {
  803. test_storage_write_run(
  804. TEST_DIR "afaefo/aefaef/aef/aef/test1.txt",
  805. 1,
  806. 1,
  807. ++command_id,
  808. PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  809. test_storage_write_run(TEST_DIR "test1.txt", 100, 1, ++command_id, PB_CommandStatus_OK);
  810. test_storage_write_run(TEST_DIR "test2.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  811. test_storage_write_run(TEST_DIR "test1.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  812. test_storage_write_run(TEST_DIR "test2.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  813. test_storage_write_run(
  814. TEST_DIR "afaefo/aefaef/aef/aef/test1.txt",
  815. 1,
  816. 1,
  817. ++command_id,
  818. PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  819. test_storage_write_run(TEST_DIR "test2.txt", 1, 50, ++command_id, PB_CommandStatus_OK);
  820. test_storage_write_run(TEST_DIR "test2.txt", 512, 3, ++command_id, PB_CommandStatus_OK);
  821. }
  822. MU_TEST(test_storage_interrupt_continuous_same_system) {
  823. MsgList_t input_msg_list;
  824. MsgList_init(input_msg_list);
  825. MsgList_t expected_msg_list;
  826. MsgList_init(expected_msg_list);
  827. uint8_t pattern[16] = {0};
  828. test_rpc_add_read_or_write_to_list(
  829. input_msg_list,
  830. WRITE_REQUEST,
  831. TEST_DIR "test1.txt",
  832. pattern,
  833. sizeof(pattern),
  834. 3,
  835. command_id);
  836. /* replace last packet (has_next == false) with another command */
  837. PB_Main message_to_remove;
  838. MsgList_pop_back(&message_to_remove, input_msg_list);
  839. pb_release(&PB_Main_msg, &message_to_remove);
  840. test_rpc_create_simple_message(
  841. MsgList_push_new(input_msg_list),
  842. PB_Main_storage_mkdir_request_tag,
  843. TEST_DIR "dir1",
  844. command_id + 1);
  845. test_rpc_add_read_or_write_to_list(
  846. input_msg_list,
  847. WRITE_REQUEST,
  848. TEST_DIR "test2.txt",
  849. pattern,
  850. sizeof(pattern),
  851. 3,
  852. command_id);
  853. test_rpc_add_empty_to_list(
  854. expected_msg_list, PB_CommandStatus_ERROR_CONTINUOUS_COMMAND_INTERRUPTED, command_id);
  855. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id + 1);
  856. test_rpc_encode_and_feed(input_msg_list);
  857. test_rpc_decode_and_compare(expected_msg_list);
  858. test_rpc_free_msg_list(input_msg_list);
  859. test_rpc_free_msg_list(expected_msg_list);
  860. }
  861. MU_TEST(test_storage_interrupt_continuous_another_system) {
  862. MsgList_t input_msg_list;
  863. MsgList_init(input_msg_list);
  864. MsgList_t expected_msg_list;
  865. MsgList_init(expected_msg_list);
  866. uint8_t pattern[16] = {0};
  867. test_rpc_add_read_or_write_to_list(
  868. input_msg_list,
  869. WRITE_REQUEST,
  870. TEST_DIR "test1.txt",
  871. pattern,
  872. sizeof(pattern),
  873. 3,
  874. command_id);
  875. PB_Main message = {
  876. .command_id = command_id + 1,
  877. .command_status = PB_CommandStatus_OK,
  878. .cb_content.funcs.encode = NULL,
  879. .has_next = false,
  880. .which_content = PB_Main_system_ping_request_tag,
  881. };
  882. MsgList_it_t it;
  883. MsgList_it(it, input_msg_list);
  884. MsgList_next(it);
  885. MsgList_insert(input_msg_list, it, message);
  886. test_rpc_add_read_or_write_to_list(
  887. input_msg_list,
  888. WRITE_REQUEST,
  889. TEST_DIR "test2.txt",
  890. pattern,
  891. sizeof(pattern),
  892. 3,
  893. command_id + 2);
  894. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, command_id + 1);
  895. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id);
  896. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id + 2);
  897. test_rpc_encode_and_feed(input_msg_list);
  898. test_rpc_decode_and_compare(expected_msg_list);
  899. test_rpc_free_msg_list(input_msg_list);
  900. test_rpc_free_msg_list(expected_msg_list);
  901. }
  902. static void test_storage_delete_run(
  903. const char* path,
  904. size_t command_id,
  905. PB_CommandStatus status,
  906. bool recursive) {
  907. PB_Main request;
  908. MsgList_t expected_msg_list;
  909. MsgList_init(expected_msg_list);
  910. test_rpc_create_simple_message(&request, PB_Main_storage_delete_request_tag, path, command_id);
  911. request.content.storage_delete_request.recursive = recursive;
  912. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  913. test_rpc_encode_and_feed_one(&request);
  914. test_rpc_decode_and_compare(expected_msg_list);
  915. pb_release(&PB_Main_msg, &request);
  916. test_rpc_free_msg_list(expected_msg_list);
  917. }
  918. #define TEST_DIR_RMRF_NAME TEST_DIR "rmrf_test"
  919. #define TEST_DIR_RMRF TEST_DIR_RMRF_NAME "/"
  920. MU_TEST(test_storage_delete_recursive) {
  921. test_create_dir(TEST_DIR_RMRF_NAME);
  922. test_create_dir(TEST_DIR_RMRF "dir1");
  923. test_create_file(TEST_DIR_RMRF "dir1/file1", 1);
  924. test_create_dir(TEST_DIR_RMRF "dir1/dir1");
  925. test_create_dir(TEST_DIR_RMRF "dir1/dir2");
  926. test_create_file(TEST_DIR_RMRF "dir1/dir2/file1", 1);
  927. test_create_file(TEST_DIR_RMRF "dir1/dir2/file2", 1);
  928. test_create_dir(TEST_DIR_RMRF "dir1/dir3");
  929. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1");
  930. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1/dir1");
  931. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1/dir1/dir1");
  932. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1/dir1/dir1/dir1");
  933. test_create_dir(TEST_DIR_RMRF "dir2");
  934. test_create_dir(TEST_DIR_RMRF "dir2/dir1");
  935. test_create_dir(TEST_DIR_RMRF "dir2/dir2");
  936. test_create_file(TEST_DIR_RMRF "dir2/dir2/file1", 1);
  937. test_create_dir(TEST_DIR_RMRF "dir2/dir2/dir1");
  938. test_create_dir(TEST_DIR_RMRF "dir2/dir2/dir1/dir1");
  939. test_create_dir(TEST_DIR_RMRF "dir2/dir2/dir1/dir1/dir1");
  940. test_create_file(TEST_DIR_RMRF "dir2/dir2/dir1/dir1/dir1/file1", 1);
  941. test_storage_delete_run(
  942. TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_ERROR_STORAGE_DIR_NOT_EMPTY, false);
  943. mu_check(test_is_exists(TEST_DIR_RMRF_NAME));
  944. test_storage_delete_run(TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_OK, true);
  945. mu_check(!test_is_exists(TEST_DIR_RMRF_NAME));
  946. test_storage_delete_run(TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_OK, false);
  947. mu_check(!test_is_exists(TEST_DIR_RMRF_NAME));
  948. test_create_dir(TEST_DIR_RMRF_NAME);
  949. test_storage_delete_run(TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_OK, true);
  950. mu_check(!test_is_exists(TEST_DIR_RMRF_NAME));
  951. test_create_dir(TEST_DIR "file1");
  952. test_storage_delete_run(TEST_DIR "file1", ++command_id, PB_CommandStatus_OK, true);
  953. mu_check(!test_is_exists(TEST_DIR "file1"));
  954. }
  955. MU_TEST(test_storage_delete) {
  956. test_storage_delete_run(NULL, ++command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS, false);
  957. furi_check(!test_is_exists(TEST_DIR "empty.txt"));
  958. test_storage_delete_run(TEST_DIR "empty.txt", ++command_id, PB_CommandStatus_OK, false);
  959. mu_check(!test_is_exists(TEST_DIR "empty.txt"));
  960. test_create_file(TEST_DIR "empty.txt", 0);
  961. test_storage_delete_run(TEST_DIR "empty.txt", ++command_id, PB_CommandStatus_OK, false);
  962. mu_check(!test_is_exists(TEST_DIR "empty.txt"));
  963. furi_check(!test_is_exists(TEST_DIR "dir1"));
  964. test_create_dir(TEST_DIR "dir1");
  965. test_storage_delete_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK, false);
  966. mu_check(!test_is_exists(TEST_DIR "dir1"));
  967. test_storage_delete_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK, false);
  968. mu_check(!test_is_exists(TEST_DIR "dir1"));
  969. }
  970. static void test_storage_mkdir_run(const char* path, size_t command_id, PB_CommandStatus status) {
  971. PB_Main request;
  972. MsgList_t expected_msg_list;
  973. MsgList_init(expected_msg_list);
  974. test_rpc_create_simple_message(&request, PB_Main_storage_mkdir_request_tag, path, command_id);
  975. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  976. test_rpc_encode_and_feed_one(&request);
  977. test_rpc_decode_and_compare(expected_msg_list);
  978. pb_release(&PB_Main_msg, &request);
  979. test_rpc_free_msg_list(expected_msg_list);
  980. }
  981. MU_TEST(test_storage_mkdir) {
  982. furi_check(!test_is_exists(TEST_DIR "dir1"));
  983. test_storage_mkdir_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK);
  984. mu_check(test_is_exists(TEST_DIR "dir1"));
  985. test_storage_mkdir_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_ERROR_STORAGE_EXIST);
  986. mu_check(test_is_exists(TEST_DIR "dir1"));
  987. furi_check(!test_is_exists(TEST_DIR "dir2"));
  988. test_create_dir(TEST_DIR "dir2");
  989. test_storage_mkdir_run(TEST_DIR "dir2", ++command_id, PB_CommandStatus_ERROR_STORAGE_EXIST);
  990. mu_check(test_is_exists(TEST_DIR "dir2"));
  991. }
  992. static void test_storage_calculate_md5sum(const char* path, char* md5sum) {
  993. Storage* api = furi_record_open("storage");
  994. File* file = storage_file_alloc(api);
  995. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  996. const uint16_t once_read_size = 512;
  997. const uint8_t hash_size = MD5SUM_SIZE;
  998. uint8_t* data = malloc(once_read_size);
  999. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  1000. md5_context* md5_ctx = malloc(sizeof(md5_context));
  1001. md5_starts(md5_ctx);
  1002. while(true) {
  1003. uint16_t read_size = storage_file_read(file, data, once_read_size);
  1004. if(read_size == 0) break;
  1005. md5_update(md5_ctx, data, read_size);
  1006. }
  1007. md5_finish(md5_ctx, hash);
  1008. free(md5_ctx);
  1009. for(uint8_t i = 0; i < hash_size; i++) {
  1010. md5sum += sprintf(md5sum, "%02x", hash[i]);
  1011. }
  1012. free(hash);
  1013. free(data);
  1014. } else {
  1015. furi_check(0);
  1016. }
  1017. storage_file_close(file);
  1018. storage_file_free(file);
  1019. furi_record_close("storage");
  1020. }
  1021. static void test_storage_md5sum_run(
  1022. const char* path,
  1023. uint32_t command_id,
  1024. const char* md5sum,
  1025. PB_CommandStatus status) {
  1026. PB_Main request;
  1027. MsgList_t expected_msg_list;
  1028. MsgList_init(expected_msg_list);
  1029. test_rpc_create_simple_message(&request, PB_Main_storage_md5sum_request_tag, path, command_id);
  1030. if(status == PB_CommandStatus_OK) {
  1031. PB_Main* response = MsgList_push_new(expected_msg_list);
  1032. test_rpc_create_simple_message(
  1033. response, PB_Main_storage_md5sum_response_tag, md5sum, command_id);
  1034. response->command_status = status;
  1035. } else {
  1036. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  1037. }
  1038. test_rpc_encode_and_feed_one(&request);
  1039. test_rpc_decode_and_compare(expected_msg_list);
  1040. pb_release(&PB_Main_msg, &request);
  1041. test_rpc_free_msg_list(expected_msg_list);
  1042. }
  1043. MU_TEST(test_storage_md5sum) {
  1044. char md5sum1[MD5SUM_SIZE * 2 + 1] = {0};
  1045. char md5sum2[MD5SUM_SIZE * 2 + 1] = {0};
  1046. char md5sum3[MD5SUM_SIZE * 2 + 1] = {0};
  1047. test_storage_md5sum_run(
  1048. TEST_DIR "test1.txt", ++command_id, "", PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  1049. test_create_file(TEST_DIR "file1.txt", 0);
  1050. test_create_file(TEST_DIR "file2.txt", 1);
  1051. test_create_file(TEST_DIR "file3.txt", 512);
  1052. test_storage_calculate_md5sum(TEST_DIR "file1.txt", md5sum1);
  1053. test_storage_calculate_md5sum(TEST_DIR "file2.txt", md5sum2);
  1054. test_storage_calculate_md5sum(TEST_DIR "file3.txt", md5sum3);
  1055. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  1056. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  1057. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  1058. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  1059. test_storage_md5sum_run(TEST_DIR "file3.txt", ++command_id, md5sum3, PB_CommandStatus_OK);
  1060. test_storage_md5sum_run(TEST_DIR "file3.txt", ++command_id, md5sum3, PB_CommandStatus_OK);
  1061. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  1062. test_storage_md5sum_run(TEST_DIR "file3.txt", ++command_id, md5sum3, PB_CommandStatus_OK);
  1063. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  1064. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  1065. }
  1066. static void test_rpc_storage_rename_run(
  1067. const char* old_path,
  1068. const char* new_path,
  1069. uint32_t command_id,
  1070. PB_CommandStatus status) {
  1071. PB_Main request;
  1072. MsgList_t expected_msg_list;
  1073. MsgList_init(expected_msg_list);
  1074. char* str_old_path = strdup(old_path);
  1075. char* str_new_path = strdup(new_path);
  1076. request.command_id = command_id;
  1077. request.command_status = PB_CommandStatus_OK;
  1078. request.cb_content.funcs.encode = NULL;
  1079. request.which_content = PB_Main_storage_rename_request_tag;
  1080. request.has_next = false;
  1081. request.content.storage_rename_request.old_path = str_old_path;
  1082. request.content.storage_rename_request.new_path = str_new_path;
  1083. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  1084. test_rpc_encode_and_feed_one(&request);
  1085. test_rpc_decode_and_compare(expected_msg_list);
  1086. pb_release(&PB_Main_msg, &request);
  1087. test_rpc_free_msg_list(expected_msg_list);
  1088. }
  1089. MU_TEST(test_storage_rename) {
  1090. test_rpc_storage_rename_run(
  1091. NULL, NULL, ++command_id, PB_CommandStatus_ERROR_STORAGE_INVALID_NAME);
  1092. furi_check(!test_is_exists(TEST_DIR "empty.txt"));
  1093. test_create_file(TEST_DIR "empty.txt", 0);
  1094. test_rpc_storage_rename_run(
  1095. TEST_DIR "empty.txt", TEST_DIR "empty2.txt", ++command_id, PB_CommandStatus_OK);
  1096. mu_check(!test_is_exists(TEST_DIR "empty.txt"));
  1097. mu_check(test_is_exists(TEST_DIR "empty2.txt"));
  1098. furi_check(!test_is_exists(TEST_DIR "dir1"));
  1099. test_create_dir(TEST_DIR "dir1");
  1100. test_rpc_storage_rename_run(
  1101. TEST_DIR "dir1", TEST_DIR "dir2", ++command_id, PB_CommandStatus_OK);
  1102. mu_check(!test_is_exists(TEST_DIR "dir1"));
  1103. mu_check(test_is_exists(TEST_DIR "dir2"));
  1104. }
  1105. MU_TEST(test_ping) {
  1106. MsgList_t input_msg_list;
  1107. MsgList_init(input_msg_list);
  1108. MsgList_t expected_msg_list;
  1109. MsgList_init(expected_msg_list);
  1110. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 0);
  1111. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 1);
  1112. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 0);
  1113. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 500);
  1114. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, (uint32_t)-1);
  1115. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 700);
  1116. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 1);
  1117. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 0);
  1118. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 1);
  1119. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 0);
  1120. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 500);
  1121. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, (uint32_t)-1);
  1122. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 700);
  1123. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 1);
  1124. test_rpc_encode_and_feed(input_msg_list);
  1125. test_rpc_decode_and_compare(expected_msg_list);
  1126. test_rpc_free_msg_list(input_msg_list);
  1127. test_rpc_free_msg_list(expected_msg_list);
  1128. }
  1129. MU_TEST(test_system_protobuf_version) {
  1130. MsgList_t expected_msg_list;
  1131. MsgList_init(expected_msg_list);
  1132. PB_Main request;
  1133. request.command_id = ++command_id;
  1134. request.command_status = PB_CommandStatus_OK;
  1135. request.cb_content.funcs.decode = NULL;
  1136. request.has_next = false;
  1137. request.which_content = PB_Main_system_protobuf_version_request_tag;
  1138. PB_Main* response = MsgList_push_new(expected_msg_list);
  1139. response->command_id = command_id;
  1140. response->command_status = PB_CommandStatus_OK;
  1141. response->cb_content.funcs.encode = NULL;
  1142. response->has_next = false;
  1143. response->which_content = PB_Main_system_protobuf_version_response_tag;
  1144. response->content.system_protobuf_version_response.major = PROTOBUF_MAJOR_VERSION;
  1145. response->content.system_protobuf_version_response.minor = PROTOBUF_MINOR_VERSION;
  1146. test_rpc_encode_and_feed_one(&request);
  1147. test_rpc_decode_and_compare(expected_msg_list);
  1148. test_rpc_free_msg_list(expected_msg_list);
  1149. }
  1150. MU_TEST_SUITE(test_rpc_system) {
  1151. MU_SUITE_CONFIGURE(&test_rpc_setup, &test_rpc_teardown);
  1152. MU_RUN_TEST(test_ping);
  1153. MU_RUN_TEST(test_system_protobuf_version);
  1154. }
  1155. MU_TEST_SUITE(test_rpc_storage) {
  1156. MU_SUITE_CONFIGURE(&test_rpc_storage_setup, &test_rpc_storage_teardown);
  1157. MU_RUN_TEST(test_storage_info);
  1158. MU_RUN_TEST(test_storage_stat);
  1159. MU_RUN_TEST(test_storage_list);
  1160. MU_RUN_TEST(test_storage_read);
  1161. MU_RUN_TEST(test_storage_write_read);
  1162. MU_RUN_TEST(test_storage_write);
  1163. MU_RUN_TEST(test_storage_delete);
  1164. MU_RUN_TEST(test_storage_delete_recursive);
  1165. MU_RUN_TEST(test_storage_mkdir);
  1166. MU_RUN_TEST(test_storage_md5sum);
  1167. MU_RUN_TEST(test_storage_rename);
  1168. // TODO: repair test
  1169. DISABLE_TEST(MU_RUN_TEST(test_storage_interrupt_continuous_same_system););
  1170. MU_RUN_TEST(test_storage_interrupt_continuous_another_system);
  1171. }
  1172. static void test_app_create_request(
  1173. PB_Main* request,
  1174. const char* app_name,
  1175. const char* app_args,
  1176. uint32_t command_id) {
  1177. request->command_id = command_id;
  1178. request->command_status = PB_CommandStatus_OK;
  1179. request->cb_content.funcs.encode = NULL;
  1180. request->which_content = PB_Main_app_start_request_tag;
  1181. request->has_next = false;
  1182. if(app_name) {
  1183. char* msg_app_name = strdup(app_name);
  1184. request->content.app_start_request.name = msg_app_name;
  1185. } else {
  1186. request->content.app_start_request.name = NULL;
  1187. }
  1188. if(app_args) {
  1189. char* msg_app_args = strdup(app_args);
  1190. request->content.app_start_request.args = msg_app_args;
  1191. } else {
  1192. request->content.app_start_request.args = NULL;
  1193. }
  1194. }
  1195. static void test_app_start_run(
  1196. const char* app_name,
  1197. const char* app_args,
  1198. PB_CommandStatus status,
  1199. uint32_t command_id) {
  1200. PB_Main request;
  1201. MsgList_t expected_msg_list;
  1202. MsgList_init(expected_msg_list);
  1203. test_app_create_request(&request, app_name, app_args, command_id);
  1204. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  1205. test_rpc_encode_and_feed_one(&request);
  1206. test_rpc_decode_and_compare(expected_msg_list);
  1207. pb_release(&PB_Main_msg, &request);
  1208. test_rpc_free_msg_list(expected_msg_list);
  1209. }
  1210. static void test_app_get_status_lock_run(bool locked_expected, uint32_t command_id) {
  1211. PB_Main request = {
  1212. .command_id = command_id,
  1213. .command_status = PB_CommandStatus_OK,
  1214. .which_content = PB_Main_app_lock_status_request_tag,
  1215. .has_next = false,
  1216. };
  1217. MsgList_t expected_msg_list;
  1218. MsgList_init(expected_msg_list);
  1219. PB_Main* response = MsgList_push_new(expected_msg_list);
  1220. response->command_id = command_id;
  1221. response->command_status = PB_CommandStatus_OK;
  1222. response->which_content = PB_Main_app_lock_status_response_tag;
  1223. response->has_next = false;
  1224. response->content.app_lock_status_response.locked = locked_expected;
  1225. test_rpc_encode_and_feed_one(&request);
  1226. test_rpc_decode_and_compare(expected_msg_list);
  1227. pb_release(&PB_Main_msg, &request);
  1228. test_rpc_free_msg_list(expected_msg_list);
  1229. }
  1230. MU_TEST(test_app_start_and_lock_status) {
  1231. test_app_get_status_lock_run(false, ++command_id);
  1232. test_app_start_run(NULL, "/ext/file", PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1233. test_app_start_run(NULL, NULL, PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1234. test_app_get_status_lock_run(false, ++command_id);
  1235. test_app_start_run(
  1236. "skynet_destroy_world_app", NULL, PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1237. test_app_get_status_lock_run(false, ++command_id);
  1238. test_app_start_run("Delay Test", "0", PB_CommandStatus_OK, ++command_id);
  1239. delay(100);
  1240. test_app_get_status_lock_run(false, ++command_id);
  1241. test_app_start_run("Delay Test", "200", PB_CommandStatus_OK, ++command_id);
  1242. test_app_get_status_lock_run(true, ++command_id);
  1243. delay(100);
  1244. test_app_get_status_lock_run(true, ++command_id);
  1245. test_app_start_run("Delay Test", "0", PB_CommandStatus_ERROR_APP_SYSTEM_LOCKED, ++command_id);
  1246. delay(200);
  1247. test_app_get_status_lock_run(false, ++command_id);
  1248. test_app_start_run("Delay Test", "500", PB_CommandStatus_OK, ++command_id);
  1249. delay(100);
  1250. test_app_get_status_lock_run(true, ++command_id);
  1251. test_app_start_run("Infrared", "0", PB_CommandStatus_ERROR_APP_SYSTEM_LOCKED, ++command_id);
  1252. delay(100);
  1253. test_app_get_status_lock_run(true, ++command_id);
  1254. test_app_start_run(
  1255. "2_girls_1_app", "0", PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1256. delay(100);
  1257. test_app_get_status_lock_run(true, ++command_id);
  1258. delay(500);
  1259. test_app_get_status_lock_run(false, ++command_id);
  1260. }
  1261. MU_TEST_SUITE(test_rpc_app) {
  1262. MU_SUITE_CONFIGURE(&test_rpc_setup, &test_rpc_teardown);
  1263. DISABLE_TEST(MU_RUN_TEST(test_app_start_and_lock_status););
  1264. }
  1265. static void
  1266. test_send_rubbish(RpcSession* session, const char* pattern, size_t pattern_size, size_t size) {
  1267. uint8_t* buf = furi_alloc(size);
  1268. for(int i = 0; i < size; ++i) {
  1269. buf[i] = pattern[i % pattern_size];
  1270. }
  1271. size_t bytes_sent = rpc_session_feed(session, buf, size, 1000);
  1272. furi_check(bytes_sent == size);
  1273. free(buf);
  1274. }
  1275. static void test_rpc_feed_rubbish_run(
  1276. MsgList_t input_before,
  1277. MsgList_t input_after,
  1278. MsgList_t expected,
  1279. const char* pattern,
  1280. size_t pattern_size,
  1281. size_t size) {
  1282. test_rpc_setup();
  1283. test_rpc_add_empty_to_list(expected, PB_CommandStatus_ERROR_DECODE, 0);
  1284. furi_check(!xSemaphoreTake(rpc_session_context.close_session_semaphore, 0));
  1285. test_rpc_encode_and_feed(input_before);
  1286. test_send_rubbish(session, pattern, pattern_size, size);
  1287. test_rpc_encode_and_feed(input_after);
  1288. test_rpc_decode_and_compare(expected);
  1289. test_rpc_teardown();
  1290. }
  1291. #define RUN_TEST_RPC_FEED_RUBBISH(ib, ia, e, b, c) \
  1292. test_rpc_feed_rubbish_run(ib, ia, e, b, sizeof(b), c)
  1293. #define INIT_LISTS() \
  1294. MsgList_init(input_before); \
  1295. MsgList_init(input_after); \
  1296. MsgList_init(expected);
  1297. #define FREE_LISTS() \
  1298. test_rpc_free_msg_list(input_before); \
  1299. test_rpc_free_msg_list(input_after); \
  1300. test_rpc_free_msg_list(expected);
  1301. MU_TEST(test_rpc_feed_rubbish) {
  1302. MsgList_t input_before;
  1303. MsgList_t input_after;
  1304. MsgList_t expected;
  1305. INIT_LISTS();
  1306. // input is empty
  1307. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x12\x30rubbi\x42sh", 50);
  1308. FREE_LISTS();
  1309. INIT_LISTS();
  1310. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1311. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1312. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x2\x2\x2\x5\x99\x1", 30);
  1313. FREE_LISTS();
  1314. INIT_LISTS();
  1315. test_rpc_add_ping_to_list(input_after, PING_REQUEST, ++command_id);
  1316. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x12\x30rubbi\x42sh", 50);
  1317. FREE_LISTS();
  1318. INIT_LISTS();
  1319. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1320. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1321. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1322. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1323. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1324. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1325. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1326. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1327. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1328. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1329. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1330. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1331. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1332. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1333. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1334. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1335. test_rpc_add_ping_to_list(input_after, PING_REQUEST, command_id);
  1336. test_rpc_add_ping_to_list(input_after, PING_REQUEST, command_id);
  1337. test_rpc_add_ping_to_list(input_after, PING_REQUEST, command_id);
  1338. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x99\x2\x2\x5\x99\x1", 300);
  1339. FREE_LISTS();
  1340. INIT_LISTS();
  1341. test_rpc_add_ping_to_list(input_after, PING_REQUEST, ++command_id);
  1342. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x1\x99\x2\x5\x99\x1", 300);
  1343. FREE_LISTS();
  1344. INIT_LISTS();
  1345. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1346. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1347. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x2\x2\x2\x5\x99\x1", 30);
  1348. FREE_LISTS();
  1349. INIT_LISTS();
  1350. test_rpc_add_ping_to_list(input_before, PING_RESPONSE, ++command_id);
  1351. test_rpc_add_empty_to_list(expected, PB_CommandStatus_ERROR_NOT_IMPLEMENTED, command_id);
  1352. test_rpc_add_ping_to_list(input_before, PING_RESPONSE, ++command_id);
  1353. test_rpc_add_empty_to_list(expected, PB_CommandStatus_ERROR_NOT_IMPLEMENTED, command_id);
  1354. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x12\x30rubbi\x42sh", 50);
  1355. FREE_LISTS();
  1356. }
  1357. MU_TEST_SUITE(test_rpc_session) {
  1358. MU_RUN_TEST(test_rpc_feed_rubbish);
  1359. }
  1360. int run_minunit_test_rpc() {
  1361. Storage* storage = furi_record_open("storage");
  1362. if(storage_sd_status(storage) != FSE_OK) {
  1363. FURI_LOG_E(TAG, "SD card not mounted - skip storage tests");
  1364. } else {
  1365. MU_RUN_SUITE(test_rpc_storage);
  1366. }
  1367. furi_record_close("storage");
  1368. MU_RUN_SUITE(test_rpc_system);
  1369. MU_RUN_SUITE(test_rpc_app);
  1370. MU_RUN_SUITE(test_rpc_session);
  1371. return MU_EXIT_CODE;
  1372. }
  1373. int32_t delay_test_app(void* p) {
  1374. int timeout = atoi((const char*)p);
  1375. if(timeout > 0) {
  1376. delay(timeout);
  1377. }
  1378. return 0;
  1379. }