rpc_test.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802
  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(file_info_is_dir(&fileinfo)) {
  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 = file_info_is_dir(&fileinfo) ? PB_Storage_File_FileType_DIR :
  538. PB_Storage_File_FileType_FILE;
  539. list->file[i].size = fileinfo.size;
  540. list->file[i].data = NULL;
  541. /* memory free inside rpc_encode_and_send() -> pb_release() */
  542. list->file[i].name = name;
  543. ++i;
  544. }
  545. } else {
  546. finish = true;
  547. free(name);
  548. }
  549. }
  550. list->file_count = i;
  551. response.has_next = false;
  552. MsgList_push_back(msg_list, response);
  553. storage_dir_close(dir);
  554. storage_file_free(dir);
  555. furi_record_close(RECORD_STORAGE);
  556. }
  557. static void test_rpc_decode_and_compare(MsgList_t expected_msg_list, uint8_t session) {
  558. furi_check(!MsgList_empty_p(expected_msg_list));
  559. furi_check(session < TEST_RPC_SESSIONS);
  560. rpc_session[session].timeout = xTaskGetTickCount() + MAX_RECEIVE_OUTPUT_TIMEOUT;
  561. pb_istream_t istream = {
  562. .callback = test_rpc_pb_stream_read,
  563. .state = &rpc_session[session],
  564. .errmsg = NULL,
  565. .bytes_left = 0x7FFFFFFF,
  566. };
  567. /* other fields explicitly initialized by 0 */
  568. PB_Main result = {.cb_content.funcs.decode = NULL};
  569. /* mlib adds msg_files into start of list, so reverse it */
  570. MsgList_reverse(expected_msg_list);
  571. for
  572. M_EACH(expected_msg, expected_msg_list, MsgList_t) {
  573. if(!pb_decode_ex(&istream, &PB_Main_msg, &result, PB_DECODE_DELIMITED)) {
  574. mu_fail(
  575. "not all expected messages decoded (maybe increase MAX_RECEIVE_OUTPUT_TIMEOUT)");
  576. break;
  577. }
  578. test_rpc_compare_messages(&result, expected_msg);
  579. pb_release(&PB_Main_msg, &result);
  580. }
  581. rpc_session[session].timeout = xTaskGetTickCount() + 50;
  582. if(pb_decode_ex(&istream, &PB_Main_msg, &result, PB_DECODE_DELIMITED)) {
  583. mu_fail("decoded more than expected");
  584. }
  585. MsgList_reverse(expected_msg_list);
  586. }
  587. static void test_rpc_free_msg_list(MsgList_t msg_list) {
  588. for
  589. M_EACH(it, msg_list, MsgList_t) {
  590. pb_release(&PB_Main_msg, it);
  591. }
  592. MsgList_clear(msg_list);
  593. }
  594. static void test_rpc_storage_list_run(const char* path, uint32_t command_id) {
  595. PB_Main request;
  596. MsgList_t expected_msg_list;
  597. MsgList_init(expected_msg_list);
  598. test_rpc_create_simple_message(&request, PB_Main_storage_list_request_tag, path, command_id);
  599. if(!strcmp(path, "/")) {
  600. test_rpc_storage_list_create_expected_list_root(expected_msg_list, command_id);
  601. } else {
  602. test_rpc_storage_list_create_expected_list(expected_msg_list, path, command_id);
  603. }
  604. test_rpc_encode_and_feed_one(&request, 0);
  605. test_rpc_decode_and_compare(expected_msg_list, 0);
  606. pb_release(&PB_Main_msg, &request);
  607. test_rpc_free_msg_list(expected_msg_list);
  608. }
  609. MU_TEST(test_storage_list) {
  610. test_rpc_storage_list_run("/", ++command_id);
  611. test_rpc_storage_list_run(EXT_PATH("nfc"), ++command_id);
  612. test_rpc_storage_list_run(STORAGE_INT_PATH_PREFIX, ++command_id);
  613. test_rpc_storage_list_run(STORAGE_EXT_PATH_PREFIX, ++command_id);
  614. test_rpc_storage_list_run(EXT_PATH("infrared"), ++command_id);
  615. test_rpc_storage_list_run(EXT_PATH("ibutton"), ++command_id);
  616. test_rpc_storage_list_run(EXT_PATH("lfrfid"), ++command_id);
  617. test_rpc_storage_list_run("error_path", ++command_id);
  618. }
  619. static void
  620. test_rpc_add_empty_to_list(MsgList_t msg_list, PB_CommandStatus status, uint32_t command_id) {
  621. PB_Main* response = MsgList_push_new(msg_list);
  622. response->command_id = command_id;
  623. response->command_status = status;
  624. response->cb_content.funcs.encode = NULL;
  625. response->has_next = false;
  626. response->which_content = PB_Main_empty_tag;
  627. }
  628. static void test_rpc_add_read_to_list_by_reading_real_file(
  629. MsgList_t msg_list,
  630. const char* path,
  631. uint32_t command_id) {
  632. furi_check(MsgList_empty_p(msg_list));
  633. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  634. File* file = storage_file_alloc(fs_api);
  635. bool result = false;
  636. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  637. size_t size_left = storage_file_size(file);
  638. do {
  639. PB_Main* response = MsgList_push_new(msg_list);
  640. response->command_id = command_id;
  641. response->command_status = PB_CommandStatus_OK;
  642. response->has_next = false;
  643. response->which_content = PB_Main_storage_read_response_tag;
  644. response->content.storage_read_response.has_file = true;
  645. response->content.storage_read_response.file.data =
  646. malloc(PB_BYTES_ARRAY_T_ALLOCSIZE(MIN(size_left, MAX_DATA_SIZE)));
  647. uint8_t* buffer = response->content.storage_read_response.file.data->bytes;
  648. uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
  649. size_t read_size = MIN(size_left, MAX_DATA_SIZE);
  650. *read_size_msg = storage_file_read(file, buffer, read_size);
  651. size_left -= read_size;
  652. result = (*read_size_msg == read_size);
  653. if(result) {
  654. response->has_next = (size_left > 0);
  655. }
  656. } while((size_left != 0) && result);
  657. if(!result) {
  658. test_rpc_add_empty_to_list(
  659. msg_list, test_rpc_storage_get_file_error(file), command_id);
  660. }
  661. } else {
  662. test_rpc_add_empty_to_list(msg_list, test_rpc_storage_get_file_error(file), command_id);
  663. }
  664. storage_file_close(file);
  665. storage_file_free(file);
  666. furi_record_close(RECORD_STORAGE);
  667. }
  668. static void test_storage_read_run(const char* path, uint32_t command_id) {
  669. PB_Main request;
  670. MsgList_t expected_msg_list;
  671. MsgList_init(expected_msg_list);
  672. test_rpc_add_read_to_list_by_reading_real_file(expected_msg_list, path, command_id);
  673. test_rpc_create_simple_message(&request, PB_Main_storage_read_request_tag, path, command_id);
  674. test_rpc_encode_and_feed_one(&request, 0);
  675. test_rpc_decode_and_compare(expected_msg_list, 0);
  676. pb_release(&PB_Main_msg, &request);
  677. test_rpc_free_msg_list(expected_msg_list);
  678. }
  679. static bool test_is_exists(const char* path) {
  680. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  681. FileInfo fileinfo;
  682. FS_Error result = storage_common_stat(fs_api, path, &fileinfo);
  683. furi_check((result == FSE_OK) || (result == FSE_NOT_EXIST));
  684. furi_record_close(RECORD_STORAGE);
  685. return result == FSE_OK;
  686. }
  687. static void test_create_dir(const char* path) {
  688. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  689. FS_Error error = storage_common_mkdir(fs_api, path);
  690. (void)error;
  691. furi_check((error == FSE_OK) || (error == FSE_EXIST));
  692. furi_record_close(RECORD_STORAGE);
  693. furi_check(test_is_exists(path));
  694. }
  695. static void test_create_file(const char* path, size_t size) {
  696. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  697. File* file = storage_file_alloc(fs_api);
  698. if(storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  699. uint8_t buf[128] = {0};
  700. for(size_t i = 0; i < sizeof(buf); ++i) {
  701. buf[i] = '0' + (i % 10);
  702. }
  703. while(size) {
  704. size_t written = storage_file_write(file, buf, MIN(size, sizeof(buf)));
  705. furi_check(written);
  706. size -= written;
  707. }
  708. }
  709. storage_file_close(file);
  710. storage_file_free(file);
  711. furi_record_close(RECORD_STORAGE);
  712. furi_check(test_is_exists(path));
  713. }
  714. static void test_rpc_storage_info_run(const char* path, uint32_t command_id) {
  715. PB_Main request;
  716. MsgList_t expected_msg_list;
  717. MsgList_init(expected_msg_list);
  718. test_rpc_create_simple_message(&request, PB_Main_storage_info_request_tag, path, command_id);
  719. PB_Main* response = MsgList_push_new(expected_msg_list);
  720. response->command_id = command_id;
  721. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  722. FS_Error error = storage_common_fs_info(
  723. fs_api,
  724. path,
  725. &response->content.storage_info_response.total_space,
  726. &response->content.storage_info_response.free_space);
  727. response->command_status = rpc_system_storage_get_error(error);
  728. if(error == FSE_OK) {
  729. response->which_content = PB_Main_storage_info_response_tag;
  730. } else {
  731. response->which_content = PB_Main_empty_tag;
  732. }
  733. test_rpc_encode_and_feed_one(&request, 0);
  734. test_rpc_decode_and_compare(expected_msg_list, 0);
  735. pb_release(&PB_Main_msg, &request);
  736. test_rpc_free_msg_list(expected_msg_list);
  737. }
  738. static void test_rpc_storage_stat_run(const char* path, uint32_t command_id) {
  739. PB_Main request;
  740. MsgList_t expected_msg_list;
  741. MsgList_init(expected_msg_list);
  742. test_rpc_create_simple_message(&request, PB_Main_storage_stat_request_tag, path, command_id);
  743. Storage* fs_api = furi_record_open(RECORD_STORAGE);
  744. FileInfo fileinfo;
  745. FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
  746. furi_record_close(RECORD_STORAGE);
  747. PB_Main* response = MsgList_push_new(expected_msg_list);
  748. response->command_id = command_id;
  749. response->command_status = rpc_system_storage_get_error(error);
  750. response->has_next = false;
  751. response->which_content = PB_Main_empty_tag;
  752. if(error == FSE_OK) {
  753. response->which_content = PB_Main_storage_stat_response_tag;
  754. response->content.storage_stat_response.has_file = true;
  755. response->content.storage_stat_response.file.type = file_info_is_dir(&fileinfo) ?
  756. PB_Storage_File_FileType_DIR :
  757. PB_Storage_File_FileType_FILE;
  758. response->content.storage_stat_response.file.size = fileinfo.size;
  759. }
  760. test_rpc_encode_and_feed_one(&request, 0);
  761. test_rpc_decode_and_compare(expected_msg_list, 0);
  762. pb_release(&PB_Main_msg, &request);
  763. test_rpc_free_msg_list(expected_msg_list);
  764. }
  765. MU_TEST(test_storage_info) {
  766. test_rpc_storage_info_run(STORAGE_ANY_PATH_PREFIX, ++command_id);
  767. test_rpc_storage_info_run(STORAGE_INT_PATH_PREFIX, ++command_id);
  768. test_rpc_storage_info_run(STORAGE_EXT_PATH_PREFIX, ++command_id);
  769. }
  770. #define TEST_DIR_STAT_NAME TEST_DIR "stat_dir"
  771. #define TEST_DIR_STAT TEST_DIR_STAT_NAME "/"
  772. MU_TEST(test_storage_stat) {
  773. test_create_dir(TEST_DIR_STAT_NAME);
  774. test_create_file(TEST_DIR_STAT "empty.txt", 0);
  775. test_create_file(TEST_DIR_STAT "l33t.txt", 1337);
  776. test_rpc_storage_stat_run("/", ++command_id);
  777. test_rpc_storage_stat_run(STORAGE_INT_PATH_PREFIX, ++command_id);
  778. test_rpc_storage_stat_run(STORAGE_EXT_PATH_PREFIX, ++command_id);
  779. test_rpc_storage_stat_run(TEST_DIR_STAT "empty.txt", ++command_id);
  780. test_rpc_storage_stat_run(TEST_DIR_STAT "l33t.txt", ++command_id);
  781. test_rpc_storage_stat_run(TEST_DIR_STAT "missing", ++command_id);
  782. test_rpc_storage_stat_run(TEST_DIR_STAT_NAME, ++command_id);
  783. test_rpc_storage_stat_run(TEST_DIR_STAT, ++command_id);
  784. }
  785. MU_TEST(test_storage_read) {
  786. test_create_file(TEST_DIR "empty.txt", 0);
  787. test_create_file(TEST_DIR "file1.txt", 1);
  788. test_create_file(TEST_DIR "file2.txt", MAX_DATA_SIZE);
  789. test_create_file(TEST_DIR "file3.txt", MAX_DATA_SIZE + 1);
  790. test_create_file(TEST_DIR "file4.txt", (MAX_DATA_SIZE * 2) + 1);
  791. test_storage_read_run(TEST_DIR "empty.txt", ++command_id);
  792. test_storage_read_run(TEST_DIR "file1.txt", ++command_id);
  793. test_storage_read_run(TEST_DIR "file2.txt", ++command_id);
  794. test_storage_read_run(TEST_DIR "file3.txt", ++command_id);
  795. test_storage_read_run(TEST_DIR "file4.txt", ++command_id);
  796. }
  797. static void test_storage_write_run(
  798. const char* path,
  799. size_t write_size,
  800. size_t write_count,
  801. uint32_t command_id,
  802. PB_CommandStatus status) {
  803. MsgList_t input_msg_list;
  804. MsgList_init(input_msg_list);
  805. MsgList_t expected_msg_list;
  806. MsgList_init(expected_msg_list);
  807. uint8_t* buf = malloc(write_size);
  808. for(size_t i = 0; i < write_size; ++i) {
  809. buf[i] = '0' + (i % 10);
  810. }
  811. test_rpc_add_read_or_write_to_list(
  812. input_msg_list, WRITE_REQUEST, path, buf, write_size, write_count, command_id);
  813. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  814. test_rpc_encode_and_feed(input_msg_list, 0);
  815. test_rpc_decode_and_compare(expected_msg_list, 0);
  816. test_rpc_free_msg_list(input_msg_list);
  817. test_rpc_free_msg_list(expected_msg_list);
  818. free(buf);
  819. }
  820. static void test_storage_write_read_run(
  821. const char* path,
  822. const uint8_t* pattern,
  823. size_t pattern_size,
  824. size_t pattern_repeats,
  825. uint32_t* command_id) {
  826. MsgList_t input_msg_list;
  827. MsgList_init(input_msg_list);
  828. MsgList_t expected_msg_list;
  829. MsgList_init(expected_msg_list);
  830. test_rpc_add_read_or_write_to_list(
  831. input_msg_list, WRITE_REQUEST, path, pattern, pattern_size, pattern_repeats, ++*command_id);
  832. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, *command_id);
  833. test_rpc_create_simple_message(
  834. MsgList_push_raw(input_msg_list), PB_Main_storage_read_request_tag, path, ++*command_id);
  835. test_rpc_add_read_or_write_to_list(
  836. expected_msg_list,
  837. READ_RESPONSE,
  838. path,
  839. pattern,
  840. pattern_size,
  841. pattern_repeats,
  842. *command_id);
  843. test_rpc_print_message_list(input_msg_list);
  844. test_rpc_print_message_list(expected_msg_list);
  845. test_rpc_encode_and_feed(input_msg_list, 0);
  846. test_rpc_decode_and_compare(expected_msg_list, 0);
  847. test_rpc_free_msg_list(input_msg_list);
  848. test_rpc_free_msg_list(expected_msg_list);
  849. }
  850. MU_TEST(test_storage_write_read) {
  851. uint8_t pattern1[] = "abcdefgh";
  852. test_storage_write_read_run(TEST_DIR "test1.txt", pattern1, sizeof(pattern1), 1, &command_id);
  853. test_storage_write_read_run(TEST_DIR "test2.txt", pattern1, 1, 1, &command_id);
  854. test_storage_write_read_run(TEST_DIR "test3.txt", pattern1, 0, 1, &command_id);
  855. }
  856. MU_TEST(test_storage_write) {
  857. test_storage_write_run(
  858. TEST_DIR "afaefo/aefaef/aef/aef/test1.txt",
  859. 1,
  860. 1,
  861. ++command_id,
  862. PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  863. test_storage_write_run(TEST_DIR "test1.txt", 100, 1, ++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(TEST_DIR "test1.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  866. test_storage_write_run(TEST_DIR "test2.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  867. test_storage_write_run(
  868. TEST_DIR "afaefo/aefaef/aef/aef/test1.txt",
  869. 1,
  870. 1,
  871. ++command_id,
  872. PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  873. test_storage_write_run(TEST_DIR "test2.txt", 1, 50, ++command_id, PB_CommandStatus_OK);
  874. test_storage_write_run(TEST_DIR "test2.txt", 512, 3, ++command_id, PB_CommandStatus_OK);
  875. }
  876. MU_TEST(test_storage_interrupt_continuous_same_system) {
  877. MsgList_t input_msg_list;
  878. MsgList_init(input_msg_list);
  879. MsgList_t expected_msg_list;
  880. MsgList_init(expected_msg_list);
  881. uint8_t pattern[16] = {0};
  882. test_rpc_add_read_or_write_to_list(
  883. input_msg_list,
  884. WRITE_REQUEST,
  885. TEST_DIR "test1.txt",
  886. pattern,
  887. sizeof(pattern),
  888. 3,
  889. command_id);
  890. /* replace last packet (has_next == false) with another command */
  891. PB_Main message_to_remove;
  892. MsgList_pop_back(&message_to_remove, input_msg_list);
  893. pb_release(&PB_Main_msg, &message_to_remove);
  894. test_rpc_create_simple_message(
  895. MsgList_push_new(input_msg_list),
  896. PB_Main_storage_mkdir_request_tag,
  897. TEST_DIR "dir1",
  898. command_id + 1);
  899. test_rpc_add_read_or_write_to_list(
  900. input_msg_list,
  901. WRITE_REQUEST,
  902. TEST_DIR "test2.txt",
  903. pattern,
  904. sizeof(pattern),
  905. 3,
  906. command_id);
  907. test_rpc_add_empty_to_list(
  908. expected_msg_list, PB_CommandStatus_ERROR_CONTINUOUS_COMMAND_INTERRUPTED, command_id);
  909. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id + 1);
  910. test_rpc_encode_and_feed(input_msg_list, 0);
  911. test_rpc_decode_and_compare(expected_msg_list, 0);
  912. test_rpc_free_msg_list(input_msg_list);
  913. test_rpc_free_msg_list(expected_msg_list);
  914. }
  915. MU_TEST(test_storage_interrupt_continuous_another_system) {
  916. MsgList_t input_msg_list;
  917. MsgList_init(input_msg_list);
  918. MsgList_t expected_msg_list;
  919. MsgList_init(expected_msg_list);
  920. uint8_t pattern[16] = {0};
  921. test_rpc_add_read_or_write_to_list(
  922. input_msg_list,
  923. WRITE_REQUEST,
  924. TEST_DIR "test1.txt",
  925. pattern,
  926. sizeof(pattern),
  927. 3,
  928. command_id);
  929. PB_Main message = {
  930. .command_id = command_id + 1,
  931. .command_status = PB_CommandStatus_OK,
  932. .cb_content.funcs.encode = NULL,
  933. .has_next = false,
  934. .which_content = PB_Main_system_ping_request_tag,
  935. };
  936. MsgList_it_t it;
  937. MsgList_it(it, input_msg_list);
  938. MsgList_next(it);
  939. MsgList_insert(input_msg_list, it, message);
  940. test_rpc_add_read_or_write_to_list(
  941. input_msg_list,
  942. WRITE_REQUEST,
  943. TEST_DIR "test2.txt",
  944. pattern,
  945. sizeof(pattern),
  946. 3,
  947. command_id + 2);
  948. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, command_id + 1);
  949. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id);
  950. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id + 2);
  951. test_rpc_encode_and_feed(input_msg_list, 0);
  952. test_rpc_decode_and_compare(expected_msg_list, 0);
  953. test_rpc_free_msg_list(input_msg_list);
  954. test_rpc_free_msg_list(expected_msg_list);
  955. }
  956. static void test_storage_delete_run(
  957. const char* path,
  958. size_t command_id,
  959. PB_CommandStatus status,
  960. bool recursive) {
  961. PB_Main request;
  962. MsgList_t expected_msg_list;
  963. MsgList_init(expected_msg_list);
  964. test_rpc_create_simple_message(&request, PB_Main_storage_delete_request_tag, path, command_id);
  965. request.content.storage_delete_request.recursive = recursive;
  966. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  967. test_rpc_encode_and_feed_one(&request, 0);
  968. test_rpc_decode_and_compare(expected_msg_list, 0);
  969. pb_release(&PB_Main_msg, &request);
  970. test_rpc_free_msg_list(expected_msg_list);
  971. }
  972. #define TEST_DIR_RMRF_NAME TEST_DIR "rmrf_test"
  973. #define TEST_DIR_RMRF TEST_DIR_RMRF_NAME "/"
  974. MU_TEST(test_storage_delete_recursive) {
  975. test_create_dir(TEST_DIR_RMRF_NAME);
  976. test_create_dir(TEST_DIR_RMRF "dir1");
  977. test_create_file(TEST_DIR_RMRF "dir1/file1", 1);
  978. test_create_dir(TEST_DIR_RMRF "dir1/dir1");
  979. test_create_dir(TEST_DIR_RMRF "dir1/dir2");
  980. test_create_file(TEST_DIR_RMRF "dir1/dir2/file1", 1);
  981. test_create_file(TEST_DIR_RMRF "dir1/dir2/file2", 1);
  982. test_create_dir(TEST_DIR_RMRF "dir1/dir3");
  983. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1");
  984. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1/dir1");
  985. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1/dir1/dir1");
  986. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1/dir1/dir1/dir1");
  987. test_create_dir(TEST_DIR_RMRF "dir2");
  988. test_create_dir(TEST_DIR_RMRF "dir2/dir1");
  989. test_create_dir(TEST_DIR_RMRF "dir2/dir2");
  990. test_create_file(TEST_DIR_RMRF "dir2/dir2/file1", 1);
  991. test_create_dir(TEST_DIR_RMRF "dir2/dir2/dir1");
  992. test_create_dir(TEST_DIR_RMRF "dir2/dir2/dir1/dir1");
  993. test_create_dir(TEST_DIR_RMRF "dir2/dir2/dir1/dir1/dir1");
  994. test_create_file(TEST_DIR_RMRF "dir2/dir2/dir1/dir1/dir1/file1", 1);
  995. test_storage_delete_run(
  996. TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_ERROR_STORAGE_DIR_NOT_EMPTY, false);
  997. mu_check(test_is_exists(TEST_DIR_RMRF_NAME));
  998. test_storage_delete_run(TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_OK, true);
  999. mu_check(!test_is_exists(TEST_DIR_RMRF_NAME));
  1000. test_storage_delete_run(TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_OK, false);
  1001. mu_check(!test_is_exists(TEST_DIR_RMRF_NAME));
  1002. test_create_dir(TEST_DIR_RMRF_NAME);
  1003. test_storage_delete_run(TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_OK, true);
  1004. mu_check(!test_is_exists(TEST_DIR_RMRF_NAME));
  1005. test_create_dir(TEST_DIR "file1");
  1006. test_storage_delete_run(TEST_DIR "file1", ++command_id, PB_CommandStatus_OK, true);
  1007. mu_check(!test_is_exists(TEST_DIR "file1"));
  1008. }
  1009. MU_TEST(test_storage_delete) {
  1010. test_storage_delete_run(NULL, ++command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS, false);
  1011. furi_check(!test_is_exists(TEST_DIR "empty.txt"));
  1012. test_storage_delete_run(TEST_DIR "empty.txt", ++command_id, PB_CommandStatus_OK, false);
  1013. mu_check(!test_is_exists(TEST_DIR "empty.txt"));
  1014. test_create_file(TEST_DIR "empty.txt", 0);
  1015. test_storage_delete_run(TEST_DIR "empty.txt", ++command_id, PB_CommandStatus_OK, false);
  1016. mu_check(!test_is_exists(TEST_DIR "empty.txt"));
  1017. furi_check(!test_is_exists(TEST_DIR "dir1"));
  1018. test_create_dir(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. test_storage_delete_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK, false);
  1022. mu_check(!test_is_exists(TEST_DIR "dir1"));
  1023. }
  1024. static void test_storage_mkdir_run(const char* path, size_t command_id, PB_CommandStatus status) {
  1025. PB_Main request;
  1026. MsgList_t expected_msg_list;
  1027. MsgList_init(expected_msg_list);
  1028. test_rpc_create_simple_message(&request, PB_Main_storage_mkdir_request_tag, path, command_id);
  1029. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  1030. test_rpc_encode_and_feed_one(&request, 0);
  1031. test_rpc_decode_and_compare(expected_msg_list, 0);
  1032. pb_release(&PB_Main_msg, &request);
  1033. test_rpc_free_msg_list(expected_msg_list);
  1034. }
  1035. MU_TEST(test_storage_mkdir) {
  1036. furi_check(!test_is_exists(TEST_DIR "dir1"));
  1037. test_storage_mkdir_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK);
  1038. mu_check(test_is_exists(TEST_DIR "dir1"));
  1039. test_storage_mkdir_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_ERROR_STORAGE_EXIST);
  1040. mu_check(test_is_exists(TEST_DIR "dir1"));
  1041. furi_check(!test_is_exists(TEST_DIR "dir2"));
  1042. test_create_dir(TEST_DIR "dir2");
  1043. test_storage_mkdir_run(TEST_DIR "dir2", ++command_id, PB_CommandStatus_ERROR_STORAGE_EXIST);
  1044. mu_check(test_is_exists(TEST_DIR "dir2"));
  1045. }
  1046. static void test_storage_calculate_md5sum(const char* path, char* md5sum, size_t md5sum_size) {
  1047. Storage* api = furi_record_open(RECORD_STORAGE);
  1048. File* file = storage_file_alloc(api);
  1049. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  1050. const uint16_t once_read_size = 512;
  1051. const uint8_t hash_size = MD5SUM_SIZE;
  1052. uint8_t* data = malloc(once_read_size);
  1053. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  1054. md5_context* md5_ctx = malloc(sizeof(md5_context));
  1055. md5_starts(md5_ctx);
  1056. while(true) {
  1057. uint16_t read_size = storage_file_read(file, data, once_read_size);
  1058. if(read_size == 0) break;
  1059. md5_update(md5_ctx, data, read_size);
  1060. }
  1061. md5_finish(md5_ctx, hash);
  1062. free(md5_ctx);
  1063. for(uint8_t i = 0; i < hash_size; i++) {
  1064. md5sum += snprintf(md5sum, md5sum_size, "%02x", hash[i]);
  1065. }
  1066. free(hash);
  1067. free(data);
  1068. } else {
  1069. furi_check(0);
  1070. }
  1071. storage_file_close(file);
  1072. storage_file_free(file);
  1073. furi_record_close(RECORD_STORAGE);
  1074. }
  1075. static void test_storage_md5sum_run(
  1076. const char* path,
  1077. uint32_t command_id,
  1078. const char* md5sum,
  1079. PB_CommandStatus status) {
  1080. PB_Main request;
  1081. MsgList_t expected_msg_list;
  1082. MsgList_init(expected_msg_list);
  1083. test_rpc_create_simple_message(&request, PB_Main_storage_md5sum_request_tag, path, command_id);
  1084. if(status == PB_CommandStatus_OK) {
  1085. PB_Main* response = MsgList_push_new(expected_msg_list);
  1086. test_rpc_create_simple_message(
  1087. response, PB_Main_storage_md5sum_response_tag, md5sum, command_id);
  1088. response->command_status = status;
  1089. } else {
  1090. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  1091. }
  1092. test_rpc_encode_and_feed_one(&request, 0);
  1093. test_rpc_decode_and_compare(expected_msg_list, 0);
  1094. pb_release(&PB_Main_msg, &request);
  1095. test_rpc_free_msg_list(expected_msg_list);
  1096. }
  1097. MU_TEST(test_storage_md5sum) {
  1098. char md5sum1[MD5SUM_SIZE * 2 + 1] = {0};
  1099. char md5sum2[MD5SUM_SIZE * 2 + 1] = {0};
  1100. char md5sum3[MD5SUM_SIZE * 2 + 1] = {0};
  1101. test_storage_md5sum_run(
  1102. TEST_DIR "test1.txt", ++command_id, "", PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  1103. test_create_file(TEST_DIR "file1.txt", 0);
  1104. test_create_file(TEST_DIR "file2.txt", 1);
  1105. test_create_file(TEST_DIR "file3.txt", 512);
  1106. test_storage_calculate_md5sum(TEST_DIR "file1.txt", md5sum1, MD5SUM_SIZE * 2 + 1);
  1107. test_storage_calculate_md5sum(TEST_DIR "file2.txt", md5sum2, MD5SUM_SIZE * 2 + 1);
  1108. test_storage_calculate_md5sum(TEST_DIR "file3.txt", md5sum3, MD5SUM_SIZE * 2 + 1);
  1109. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  1110. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  1111. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, 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 "file3.txt", ++command_id, md5sum3, 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 "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  1116. test_storage_md5sum_run(TEST_DIR "file3.txt", ++command_id, md5sum3, PB_CommandStatus_OK);
  1117. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  1118. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  1119. }
  1120. static void test_rpc_storage_rename_run(
  1121. const char* old_path,
  1122. const char* new_path,
  1123. uint32_t command_id,
  1124. PB_CommandStatus status) {
  1125. PB_Main request;
  1126. MsgList_t expected_msg_list;
  1127. MsgList_init(expected_msg_list);
  1128. char* str_old_path = strdup(old_path);
  1129. char* str_new_path = strdup(new_path);
  1130. request.command_id = command_id;
  1131. request.command_status = PB_CommandStatus_OK;
  1132. request.cb_content.funcs.encode = NULL;
  1133. request.which_content = PB_Main_storage_rename_request_tag;
  1134. request.has_next = false;
  1135. request.content.storage_rename_request.old_path = str_old_path;
  1136. request.content.storage_rename_request.new_path = str_new_path;
  1137. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  1138. test_rpc_encode_and_feed_one(&request, 0);
  1139. test_rpc_decode_and_compare(expected_msg_list, 0);
  1140. pb_release(&PB_Main_msg, &request);
  1141. test_rpc_free_msg_list(expected_msg_list);
  1142. }
  1143. MU_TEST(test_storage_rename) {
  1144. test_rpc_storage_rename_run("", "", ++command_id, PB_CommandStatus_ERROR_STORAGE_INVALID_NAME);
  1145. furi_check(!test_is_exists(TEST_DIR "empty.txt"));
  1146. test_create_file(TEST_DIR "empty.txt", 0);
  1147. test_rpc_storage_rename_run(
  1148. TEST_DIR "empty.txt", TEST_DIR "empty2.txt", ++command_id, PB_CommandStatus_OK);
  1149. mu_check(!test_is_exists(TEST_DIR "empty.txt"));
  1150. mu_check(test_is_exists(TEST_DIR "empty2.txt"));
  1151. furi_check(!test_is_exists(TEST_DIR "dir1"));
  1152. test_create_dir(TEST_DIR "dir1");
  1153. test_rpc_storage_rename_run(
  1154. TEST_DIR "dir1", TEST_DIR "dir2", ++command_id, PB_CommandStatus_OK);
  1155. mu_check(!test_is_exists(TEST_DIR "dir1"));
  1156. mu_check(test_is_exists(TEST_DIR "dir2"));
  1157. }
  1158. MU_TEST(test_ping) {
  1159. MsgList_t input_msg_list;
  1160. MsgList_init(input_msg_list);
  1161. MsgList_t expected_msg_list;
  1162. MsgList_init(expected_msg_list);
  1163. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 0);
  1164. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 1);
  1165. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 0);
  1166. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 500);
  1167. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, (uint32_t)-1);
  1168. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 700);
  1169. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 1);
  1170. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 0);
  1171. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 1);
  1172. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 0);
  1173. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 500);
  1174. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, (uint32_t)-1);
  1175. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 700);
  1176. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 1);
  1177. test_rpc_encode_and_feed(input_msg_list, 0);
  1178. test_rpc_decode_and_compare(expected_msg_list, 0);
  1179. test_rpc_free_msg_list(input_msg_list);
  1180. test_rpc_free_msg_list(expected_msg_list);
  1181. }
  1182. MU_TEST(test_system_protobuf_version) {
  1183. MsgList_t expected_msg_list;
  1184. MsgList_init(expected_msg_list);
  1185. PB_Main request;
  1186. request.command_id = ++command_id;
  1187. request.command_status = PB_CommandStatus_OK;
  1188. request.cb_content.funcs.decode = NULL;
  1189. request.has_next = false;
  1190. request.which_content = PB_Main_system_protobuf_version_request_tag;
  1191. PB_Main* response = MsgList_push_new(expected_msg_list);
  1192. response->command_id = command_id;
  1193. response->command_status = PB_CommandStatus_OK;
  1194. response->cb_content.funcs.encode = NULL;
  1195. response->has_next = false;
  1196. response->which_content = PB_Main_system_protobuf_version_response_tag;
  1197. response->content.system_protobuf_version_response.major = PROTOBUF_MAJOR_VERSION;
  1198. response->content.system_protobuf_version_response.minor = PROTOBUF_MINOR_VERSION;
  1199. test_rpc_encode_and_feed_one(&request, 0);
  1200. test_rpc_decode_and_compare(expected_msg_list, 0);
  1201. test_rpc_free_msg_list(expected_msg_list);
  1202. }
  1203. MU_TEST_SUITE(test_rpc_system) {
  1204. MU_SUITE_CONFIGURE(&test_rpc_setup, &test_rpc_teardown);
  1205. MU_RUN_TEST(test_ping);
  1206. MU_RUN_TEST(test_system_protobuf_version);
  1207. }
  1208. MU_TEST_SUITE(test_rpc_storage) {
  1209. MU_SUITE_CONFIGURE(&test_rpc_storage_setup, &test_rpc_storage_teardown);
  1210. MU_RUN_TEST(test_storage_info);
  1211. MU_RUN_TEST(test_storage_stat);
  1212. MU_RUN_TEST(test_storage_list);
  1213. MU_RUN_TEST(test_storage_read);
  1214. MU_RUN_TEST(test_storage_write_read);
  1215. MU_RUN_TEST(test_storage_write);
  1216. MU_RUN_TEST(test_storage_delete);
  1217. MU_RUN_TEST(test_storage_delete_recursive);
  1218. MU_RUN_TEST(test_storage_mkdir);
  1219. MU_RUN_TEST(test_storage_md5sum);
  1220. MU_RUN_TEST(test_storage_rename);
  1221. DISABLE_TEST(MU_RUN_TEST(test_storage_interrupt_continuous_same_system););
  1222. MU_RUN_TEST(test_storage_interrupt_continuous_another_system);
  1223. }
  1224. static void test_app_create_request(
  1225. PB_Main* request,
  1226. const char* app_name,
  1227. const char* app_args,
  1228. uint32_t command_id) {
  1229. request->command_id = command_id;
  1230. request->command_status = PB_CommandStatus_OK;
  1231. request->cb_content.funcs.encode = NULL;
  1232. request->which_content = PB_Main_app_start_request_tag;
  1233. request->has_next = false;
  1234. if(app_name) {
  1235. char* msg_app_name = strdup(app_name);
  1236. request->content.app_start_request.name = msg_app_name;
  1237. } else {
  1238. request->content.app_start_request.name = NULL;
  1239. }
  1240. if(app_args) {
  1241. char* msg_app_args = strdup(app_args);
  1242. request->content.app_start_request.args = msg_app_args;
  1243. } else {
  1244. request->content.app_start_request.args = NULL;
  1245. }
  1246. }
  1247. static void test_app_start_run(
  1248. const char* app_name,
  1249. const char* app_args,
  1250. PB_CommandStatus status,
  1251. uint32_t command_id) {
  1252. PB_Main request;
  1253. MsgList_t expected_msg_list;
  1254. MsgList_init(expected_msg_list);
  1255. test_app_create_request(&request, app_name, app_args, command_id);
  1256. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  1257. test_rpc_encode_and_feed_one(&request, 0);
  1258. test_rpc_decode_and_compare(expected_msg_list, 0);
  1259. pb_release(&PB_Main_msg, &request);
  1260. test_rpc_free_msg_list(expected_msg_list);
  1261. }
  1262. static void test_app_get_status_lock_run(bool locked_expected, uint32_t command_id) {
  1263. PB_Main request = {
  1264. .command_id = command_id,
  1265. .command_status = PB_CommandStatus_OK,
  1266. .which_content = PB_Main_app_lock_status_request_tag,
  1267. .has_next = false,
  1268. };
  1269. MsgList_t expected_msg_list;
  1270. MsgList_init(expected_msg_list);
  1271. PB_Main* response = MsgList_push_new(expected_msg_list);
  1272. response->command_id = command_id;
  1273. response->command_status = PB_CommandStatus_OK;
  1274. response->which_content = PB_Main_app_lock_status_response_tag;
  1275. response->has_next = false;
  1276. response->content.app_lock_status_response.locked = locked_expected;
  1277. test_rpc_encode_and_feed_one(&request, 0);
  1278. test_rpc_decode_and_compare(expected_msg_list, 0);
  1279. pb_release(&PB_Main_msg, &request);
  1280. test_rpc_free_msg_list(expected_msg_list);
  1281. }
  1282. MU_TEST(test_app_start_and_lock_status) {
  1283. test_app_get_status_lock_run(false, ++command_id);
  1284. test_app_start_run(
  1285. NULL, EXT_PATH("file"), PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1286. test_app_start_run(NULL, NULL, PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1287. test_app_get_status_lock_run(false, ++command_id);
  1288. test_app_start_run(
  1289. "skynet_destroy_world_app", NULL, PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1290. test_app_get_status_lock_run(false, ++command_id);
  1291. test_app_start_run("Delay Test", "0", PB_CommandStatus_OK, ++command_id);
  1292. furi_delay_ms(100);
  1293. test_app_get_status_lock_run(false, ++command_id);
  1294. test_app_start_run("Delay Test", "200", PB_CommandStatus_OK, ++command_id);
  1295. test_app_get_status_lock_run(true, ++command_id);
  1296. furi_delay_ms(100);
  1297. test_app_get_status_lock_run(true, ++command_id);
  1298. test_app_start_run("Delay Test", "0", PB_CommandStatus_ERROR_APP_SYSTEM_LOCKED, ++command_id);
  1299. furi_delay_ms(200);
  1300. test_app_get_status_lock_run(false, ++command_id);
  1301. test_app_start_run("Delay Test", "500", PB_CommandStatus_OK, ++command_id);
  1302. furi_delay_ms(100);
  1303. test_app_get_status_lock_run(true, ++command_id);
  1304. test_app_start_run("Infrared", "0", PB_CommandStatus_ERROR_APP_SYSTEM_LOCKED, ++command_id);
  1305. furi_delay_ms(100);
  1306. test_app_get_status_lock_run(true, ++command_id);
  1307. test_app_start_run(
  1308. "2_girls_1_app", "0", PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1309. furi_delay_ms(100);
  1310. test_app_get_status_lock_run(true, ++command_id);
  1311. furi_delay_ms(500);
  1312. test_app_get_status_lock_run(false, ++command_id);
  1313. }
  1314. MU_TEST_SUITE(test_rpc_app) {
  1315. MU_SUITE_CONFIGURE(&test_rpc_setup, &test_rpc_teardown);
  1316. DISABLE_TEST(MU_RUN_TEST(test_app_start_and_lock_status););
  1317. }
  1318. static void
  1319. test_send_rubbish(RpcSession* session, const char* pattern, size_t pattern_size, size_t size) {
  1320. UNUSED(session);
  1321. uint8_t* buf = malloc(size);
  1322. for(size_t i = 0; i < size; ++i) {
  1323. buf[i] = pattern[i % pattern_size];
  1324. }
  1325. size_t bytes_sent = rpc_session_feed(rpc_session[0].session, buf, size, 1000);
  1326. furi_check(bytes_sent == size);
  1327. free(buf);
  1328. }
  1329. static void test_rpc_feed_rubbish_run(
  1330. MsgList_t input_before,
  1331. MsgList_t input_after,
  1332. MsgList_t expected,
  1333. const char* pattern,
  1334. size_t pattern_size,
  1335. size_t size) {
  1336. test_rpc_setup();
  1337. test_rpc_add_empty_to_list(expected, PB_CommandStatus_ERROR_DECODE, 0);
  1338. furi_check(!xSemaphoreTake(rpc_session[0].close_session_semaphore, 0));
  1339. test_rpc_encode_and_feed(input_before, 0);
  1340. test_send_rubbish(rpc_session[0].session, pattern, pattern_size, size);
  1341. test_rpc_encode_and_feed(input_after, 0);
  1342. test_rpc_decode_and_compare(expected, 0);
  1343. test_rpc_teardown();
  1344. }
  1345. #define RUN_TEST_RPC_FEED_RUBBISH(ib, ia, e, b, c) \
  1346. test_rpc_feed_rubbish_run(ib, ia, e, b, sizeof(b), c)
  1347. #define INIT_LISTS() \
  1348. MsgList_init(input_before); \
  1349. MsgList_init(input_after); \
  1350. MsgList_init(expected);
  1351. #define FREE_LISTS() \
  1352. test_rpc_free_msg_list(input_before); \
  1353. test_rpc_free_msg_list(input_after); \
  1354. test_rpc_free_msg_list(expected);
  1355. MU_TEST(test_rpc_feed_rubbish) {
  1356. MsgList_t input_before;
  1357. MsgList_t input_after;
  1358. MsgList_t expected;
  1359. INIT_LISTS();
  1360. // input is empty
  1361. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x12\x30rubbi\x42sh", 50);
  1362. FREE_LISTS();
  1363. INIT_LISTS();
  1364. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1365. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1366. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x2\x2\x2\x5\x99\x1", 30);
  1367. FREE_LISTS();
  1368. INIT_LISTS();
  1369. test_rpc_add_ping_to_list(input_after, PING_REQUEST, ++command_id);
  1370. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x12\x30rubbi\x42sh", 50);
  1371. FREE_LISTS();
  1372. INIT_LISTS();
  1373. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1374. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1375. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1376. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1377. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1378. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1379. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1380. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1381. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1382. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1383. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1384. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1385. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1386. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1387. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1388. test_rpc_add_ping_to_list(expected, PING_RESPONSE, 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. test_rpc_add_ping_to_list(input_after, PING_REQUEST, command_id);
  1392. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x99\x2\x2\x5\x99\x1", 300);
  1393. FREE_LISTS();
  1394. INIT_LISTS();
  1395. test_rpc_add_ping_to_list(input_after, PING_REQUEST, ++command_id);
  1396. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x1\x99\x2\x5\x99\x1", 300);
  1397. FREE_LISTS();
  1398. INIT_LISTS();
  1399. test_rpc_add_ping_to_list(input_before, PING_REQUEST, ++command_id);
  1400. test_rpc_add_ping_to_list(expected, PING_RESPONSE, command_id);
  1401. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x2\x2\x2\x5\x99\x1", 30);
  1402. FREE_LISTS();
  1403. INIT_LISTS();
  1404. test_rpc_add_ping_to_list(input_before, PING_RESPONSE, ++command_id);
  1405. test_rpc_add_empty_to_list(expected, PB_CommandStatus_ERROR_NOT_IMPLEMENTED, command_id);
  1406. test_rpc_add_ping_to_list(input_before, PING_RESPONSE, ++command_id);
  1407. test_rpc_add_empty_to_list(expected, PB_CommandStatus_ERROR_NOT_IMPLEMENTED, command_id);
  1408. RUN_TEST_RPC_FEED_RUBBISH(input_before, input_after, expected, "\x12\x30rubbi\x42sh", 50);
  1409. FREE_LISTS();
  1410. }
  1411. MU_TEST(test_rpc_multisession_ping) {
  1412. MsgList_t input_0;
  1413. MsgList_init(input_0);
  1414. MsgList_t input_1;
  1415. MsgList_init(input_1);
  1416. MsgList_t expected_0;
  1417. MsgList_init(expected_0);
  1418. MsgList_t expected_1;
  1419. MsgList_init(expected_1);
  1420. test_rpc_setup();
  1421. test_rpc_setup_second_session();
  1422. test_rpc_teardown_second_session();
  1423. test_rpc_setup_second_session();
  1424. test_rpc_add_ping_to_list(input_0, PING_REQUEST, 0);
  1425. test_rpc_add_ping_to_list(input_1, PING_REQUEST, 1);
  1426. test_rpc_add_ping_to_list(expected_0, PING_RESPONSE, 0);
  1427. test_rpc_add_ping_to_list(expected_1, PING_RESPONSE, 1);
  1428. test_rpc_encode_and_feed(input_0, 0);
  1429. test_rpc_encode_and_feed(input_1, 1);
  1430. test_rpc_decode_and_compare(expected_0, 0);
  1431. test_rpc_decode_and_compare(expected_1, 1);
  1432. test_rpc_free_msg_list(input_0);
  1433. test_rpc_free_msg_list(input_1);
  1434. test_rpc_free_msg_list(expected_0);
  1435. test_rpc_free_msg_list(expected_1);
  1436. test_rpc_teardown_second_session();
  1437. test_rpc_teardown();
  1438. }
  1439. MU_TEST(test_rpc_multisession_storage) {
  1440. MsgList_t input_0;
  1441. MsgList_init(input_0);
  1442. MsgList_t input_1;
  1443. MsgList_init(input_1);
  1444. MsgList_t expected_0;
  1445. MsgList_init(expected_0);
  1446. MsgList_t expected_1;
  1447. MsgList_init(expected_1);
  1448. test_rpc_storage_setup();
  1449. test_rpc_setup_second_session();
  1450. uint8_t pattern[16] = "0123456789abcdef";
  1451. test_rpc_add_read_or_write_to_list(
  1452. input_0, WRITE_REQUEST, TEST_DIR "file0.txt", pattern, sizeof(pattern), 1, ++command_id);
  1453. test_rpc_add_empty_to_list(expected_0, PB_CommandStatus_OK, command_id);
  1454. test_rpc_add_read_or_write_to_list(
  1455. input_1, WRITE_REQUEST, TEST_DIR "file1.txt", pattern, sizeof(pattern), 1, ++command_id);
  1456. test_rpc_add_empty_to_list(expected_1, PB_CommandStatus_OK, command_id);
  1457. test_rpc_create_simple_message(
  1458. MsgList_push_raw(input_0),
  1459. PB_Main_storage_read_request_tag,
  1460. TEST_DIR "file0.txt",
  1461. ++command_id);
  1462. test_rpc_add_read_or_write_to_list(
  1463. expected_0, READ_RESPONSE, TEST_DIR "file0.txt", pattern, sizeof(pattern), 1, command_id);
  1464. test_rpc_create_simple_message(
  1465. MsgList_push_raw(input_1),
  1466. PB_Main_storage_read_request_tag,
  1467. TEST_DIR "file1.txt",
  1468. ++command_id);
  1469. test_rpc_add_read_or_write_to_list(
  1470. expected_1, READ_RESPONSE, TEST_DIR "file1.txt", pattern, sizeof(pattern), 1, command_id);
  1471. test_rpc_print_message_list(input_0);
  1472. test_rpc_print_message_list(input_1);
  1473. test_rpc_print_message_list(expected_0);
  1474. test_rpc_print_message_list(expected_1);
  1475. test_rpc_encode_and_feed(input_0, 0);
  1476. test_rpc_encode_and_feed(input_1, 1);
  1477. test_rpc_decode_and_compare(expected_0, 0);
  1478. test_rpc_decode_and_compare(expected_1, 1);
  1479. test_rpc_free_msg_list(input_0);
  1480. test_rpc_free_msg_list(input_1);
  1481. test_rpc_free_msg_list(expected_0);
  1482. test_rpc_free_msg_list(expected_1);
  1483. test_rpc_teardown_second_session();
  1484. test_rpc_storage_teardown();
  1485. }
  1486. MU_TEST_SUITE(test_rpc_session) {
  1487. MU_RUN_TEST(test_rpc_feed_rubbish);
  1488. MU_RUN_TEST(test_rpc_multisession_ping);
  1489. Storage* storage = furi_record_open(RECORD_STORAGE);
  1490. if(storage_sd_status(storage) != FSE_OK) {
  1491. FURI_LOG_E(TAG, "SD card not mounted - skip storage tests");
  1492. } else {
  1493. MU_RUN_TEST(test_rpc_multisession_storage);
  1494. }
  1495. furi_record_close(RECORD_STORAGE);
  1496. }
  1497. int run_minunit_test_rpc() {
  1498. Storage* storage = furi_record_open(RECORD_STORAGE);
  1499. if(storage_sd_status(storage) != FSE_OK) {
  1500. FURI_LOG_E(TAG, "SD card not mounted - skip storage tests");
  1501. } else {
  1502. MU_RUN_SUITE(test_rpc_storage);
  1503. }
  1504. furi_record_close(RECORD_STORAGE);
  1505. MU_RUN_SUITE(test_rpc_system);
  1506. MU_RUN_SUITE(test_rpc_app);
  1507. MU_RUN_SUITE(test_rpc_session);
  1508. return MU_EXIT_CODE;
  1509. }
  1510. int32_t delay_test_app(void* p) {
  1511. int timeout = atoi((const char*)p);
  1512. if(timeout > 0) {
  1513. furi_delay_ms(timeout);
  1514. }
  1515. return 0;
  1516. }