rpc_test.c 65 KB

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