rpc_test.c 64 KB

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