rpc_test.c 48 KB

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