rpc_test.c 65 KB

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