rpc_test.c 64 KB

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