rpc_test.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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_i.h"
  7. #include "storage.pb.h"
  8. #include "storage/filesystem-api-defines.h"
  9. #include "storage/storage.h"
  10. #include <furi.h>
  11. #include "../minunit.h"
  12. #include <stdint.h>
  13. #include <stream_buffer.h>
  14. #include <pb.h>
  15. #include <pb_encode.h>
  16. #include <m-list.h>
  17. #include <lib/toolbox/md5.h>
  18. #include <cli/cli.h>
  19. #include <loader/loader.h>
  20. LIST_DEF(MsgList, PB_Main, M_POD_OPLIST)
  21. #define M_OPL_MsgList_t() LIST_OPLIST(MsgList)
  22. /* MinUnit test framework doesn't allow passing context into tests,
  23. * so we have to use global variables
  24. */
  25. static Rpc* rpc = NULL;
  26. static RpcSession* session = NULL;
  27. static StreamBufferHandle_t output_stream = NULL;
  28. static uint32_t command_id = 0;
  29. #define TAG "UnitTestsRpc"
  30. #define MAX_RECEIVE_OUTPUT_TIMEOUT 3000
  31. #define MAX_NAME_LENGTH 255
  32. #define MAX_DATA_SIZE 512 // have to be exact as in rpc_storage.c
  33. #define TEST_DIR TEST_DIR_NAME "/"
  34. #define TEST_DIR_NAME "/ext/unit_tests_tmp"
  35. #define MD5SUM_SIZE 16
  36. #define PING_REQUEST 0
  37. #define PING_RESPONSE 1
  38. #define WRITE_REQUEST 0
  39. #define READ_RESPONSE 1
  40. #define DEBUG_PRINT 0
  41. #define BYTES(x) (x), sizeof(x)
  42. static void output_bytes_callback(void* ctx, uint8_t* got_bytes, size_t got_size);
  43. static void clean_directory(Storage* fs_api, const char* clean_dir);
  44. static void
  45. test_rpc_add_empty_to_list(MsgList_t msg_list, PB_CommandStatus status, uint32_t command_id);
  46. static void test_rpc_encode_and_feed(MsgList_t msg_list);
  47. static void test_rpc_encode_and_feed_one(PB_Main* request);
  48. static void test_rpc_compare_messages(PB_Main* result, PB_Main* expected);
  49. static void test_rpc_decode_and_compare(MsgList_t expected_msg_list);
  50. static void test_rpc_free_msg_list(MsgList_t msg_list);
  51. static void test_rpc_setup(void) {
  52. furi_assert(!rpc);
  53. furi_assert(!session);
  54. furi_assert(!output_stream);
  55. rpc = furi_record_open("rpc");
  56. for(int i = 0; !session && (i < 10000); ++i) {
  57. session = rpc_session_open(rpc);
  58. delay(1);
  59. }
  60. furi_assert(session);
  61. output_stream = xStreamBufferCreate(1000, 1);
  62. mu_assert(session, "failed to start session");
  63. rpc_session_set_send_bytes_callback(session, output_bytes_callback);
  64. rpc_session_set_context(session, output_stream);
  65. }
  66. static void test_rpc_teardown(void) {
  67. rpc_session_close(session);
  68. furi_record_close("rpc");
  69. vStreamBufferDelete(output_stream);
  70. ++command_id;
  71. output_stream = NULL;
  72. rpc = NULL;
  73. session = NULL;
  74. }
  75. static void test_rpc_storage_setup(void) {
  76. test_rpc_setup();
  77. Storage* fs_api = furi_record_open("storage");
  78. clean_directory(fs_api, TEST_DIR_NAME);
  79. furi_record_close("storage");
  80. }
  81. static void test_rpc_storage_teardown(void) {
  82. Storage* fs_api = furi_record_open("storage");
  83. clean_directory(fs_api, TEST_DIR_NAME);
  84. furi_record_close("storage");
  85. test_rpc_teardown();
  86. }
  87. static void clean_directory(Storage* fs_api, const char* clean_dir) {
  88. furi_assert(fs_api);
  89. furi_assert(clean_dir);
  90. File* dir = storage_file_alloc(fs_api);
  91. if(storage_dir_open(dir, clean_dir)) {
  92. FileInfo fileinfo;
  93. char* name = furi_alloc(MAX_NAME_LENGTH + 1);
  94. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  95. char* fullname = furi_alloc(strlen(clean_dir) + strlen(name) + 1 + 1);
  96. sprintf(fullname, "%s/%s", clean_dir, name);
  97. if(fileinfo.flags & FSF_DIRECTORY) {
  98. clean_directory(fs_api, fullname);
  99. }
  100. FS_Error error = storage_common_remove(fs_api, fullname);
  101. furi_check(error == FSE_OK);
  102. free(fullname);
  103. }
  104. free(name);
  105. } else {
  106. FS_Error error = storage_common_mkdir(fs_api, clean_dir);
  107. (void)error;
  108. furi_assert(error == FSE_OK);
  109. }
  110. storage_dir_close(dir);
  111. storage_file_free(dir);
  112. }
  113. static void test_rpc_print_message_list(MsgList_t msg_list) {
  114. #if DEBUG_PRINT
  115. MsgList_reverse(msg_list);
  116. for
  117. M_EACH(msg, msg_list, MsgList_t) {
  118. rpc_print_message(msg);
  119. }
  120. MsgList_reverse(msg_list);
  121. #endif
  122. }
  123. static PB_CommandStatus test_rpc_storage_get_file_error(File* file) {
  124. FS_Error fs_error = storage_file_get_error(file);
  125. PB_CommandStatus pb_error;
  126. switch(fs_error) {
  127. case FSE_OK:
  128. pb_error = PB_CommandStatus_OK;
  129. break;
  130. case FSE_INVALID_NAME:
  131. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
  132. break;
  133. case FSE_INVALID_PARAMETER:
  134. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_PARAMETER;
  135. break;
  136. case FSE_INTERNAL:
  137. pb_error = PB_CommandStatus_ERROR_STORAGE_INTERNAL;
  138. break;
  139. case FSE_ALREADY_OPEN:
  140. pb_error = PB_CommandStatus_ERROR_STORAGE_ALREADY_OPEN;
  141. break;
  142. case FSE_DENIED:
  143. pb_error = PB_CommandStatus_ERROR_STORAGE_DENIED;
  144. break;
  145. case FSE_EXIST:
  146. pb_error = PB_CommandStatus_ERROR_STORAGE_EXIST;
  147. break;
  148. case FSE_NOT_EXIST:
  149. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_EXIST;
  150. break;
  151. case FSE_NOT_READY:
  152. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_READY;
  153. break;
  154. case FSE_NOT_IMPLEMENTED:
  155. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_IMPLEMENTED;
  156. break;
  157. default:
  158. pb_error = PB_CommandStatus_ERROR;
  159. break;
  160. }
  161. return pb_error;
  162. }
  163. static void output_bytes_callback(void* ctx, uint8_t* got_bytes, size_t got_size) {
  164. StreamBufferHandle_t stream_buffer = ctx;
  165. size_t bytes_sent = xStreamBufferSend(stream_buffer, got_bytes, got_size, osWaitForever);
  166. (void)bytes_sent;
  167. furi_assert(bytes_sent == got_size);
  168. }
  169. static void test_rpc_add_ping_to_list(MsgList_t msg_list, bool request, uint32_t command_id) {
  170. PB_Main* response = MsgList_push_new(msg_list);
  171. response->command_id = command_id;
  172. response->command_status = PB_CommandStatus_OK;
  173. response->cb_content.funcs.encode = NULL;
  174. response->has_next = false;
  175. response->which_content = (request == PING_REQUEST) ? PB_Main_system_ping_request_tag :
  176. PB_Main_system_ping_response_tag;
  177. }
  178. static void test_rpc_create_simple_message(
  179. PB_Main* message,
  180. uint16_t tag,
  181. const char* str,
  182. uint32_t command_id) {
  183. furi_assert(message);
  184. char* str_copy = NULL;
  185. if(str) {
  186. str_copy = strdup(str);
  187. }
  188. message->command_id = command_id;
  189. message->command_status = PB_CommandStatus_OK;
  190. message->cb_content.funcs.encode = NULL;
  191. message->which_content = tag;
  192. message->has_next = false;
  193. switch(tag) {
  194. case PB_Main_storage_info_request_tag:
  195. message->content.storage_info_request.path = str_copy;
  196. break;
  197. case PB_Main_storage_stat_request_tag:
  198. message->content.storage_stat_request.path = str_copy;
  199. break;
  200. case PB_Main_storage_list_request_tag:
  201. message->content.storage_list_request.path = str_copy;
  202. break;
  203. case PB_Main_storage_mkdir_request_tag:
  204. message->content.storage_mkdir_request.path = str_copy;
  205. break;
  206. case PB_Main_storage_read_request_tag:
  207. message->content.storage_read_request.path = str_copy;
  208. break;
  209. case PB_Main_storage_delete_request_tag:
  210. message->content.storage_delete_request.path = str_copy;
  211. break;
  212. case PB_Main_storage_md5sum_request_tag:
  213. message->content.storage_md5sum_request.path = str_copy;
  214. break;
  215. case PB_Main_storage_md5sum_response_tag: {
  216. char* md5sum = message->content.storage_md5sum_response.md5sum;
  217. size_t md5sum_size = sizeof(message->content.storage_md5sum_response.md5sum);
  218. furi_assert((strlen(str) + 1) <= md5sum_size);
  219. memcpy(md5sum, str_copy, md5sum_size);
  220. free(str_copy);
  221. break;
  222. }
  223. default:
  224. furi_assert(0);
  225. break;
  226. }
  227. }
  228. static void test_rpc_add_read_or_write_to_list(
  229. MsgList_t msg_list,
  230. bool write,
  231. const char* path,
  232. const uint8_t* pattern,
  233. size_t pattern_size,
  234. size_t pattern_repeats,
  235. uint32_t command_id) {
  236. furi_assert(pattern_repeats > 0);
  237. do {
  238. PB_Main* request = MsgList_push_new(msg_list);
  239. PB_Storage_File* msg_file = NULL;
  240. request->command_id = command_id;
  241. request->command_status = PB_CommandStatus_OK;
  242. if(write == WRITE_REQUEST) {
  243. request->content.storage_write_request.path = strdup(path);
  244. request->which_content = PB_Main_storage_write_request_tag;
  245. request->content.storage_write_request.has_file = true;
  246. msg_file = &request->content.storage_write_request.file;
  247. } else {
  248. request->which_content = PB_Main_storage_read_response_tag;
  249. request->content.storage_read_response.has_file = true;
  250. msg_file = &request->content.storage_read_response.file;
  251. }
  252. msg_file->data = furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(pattern_size));
  253. msg_file->data->size = pattern_size;
  254. memcpy(msg_file->data->bytes, pattern, pattern_size);
  255. --pattern_repeats;
  256. request->has_next = (pattern_repeats > 0);
  257. } while(pattern_repeats);
  258. }
  259. static void test_rpc_encode_and_feed_one(PB_Main* request) {
  260. furi_assert(request);
  261. pb_ostream_t ostream = PB_OSTREAM_SIZING;
  262. bool result = pb_encode_ex(&ostream, &PB_Main_msg, request, PB_ENCODE_DELIMITED);
  263. furi_check(result && ostream.bytes_written);
  264. uint8_t* buffer = furi_alloc(ostream.bytes_written);
  265. ostream = pb_ostream_from_buffer(buffer, ostream.bytes_written);
  266. pb_encode_ex(&ostream, &PB_Main_msg, request, PB_ENCODE_DELIMITED);
  267. size_t bytes_left = ostream.bytes_written;
  268. uint8_t* buffer_ptr = buffer;
  269. do {
  270. size_t bytes_sent = rpc_session_feed(session, buffer_ptr, bytes_left, 1000);
  271. mu_check(bytes_sent > 0);
  272. bytes_left -= bytes_sent;
  273. buffer_ptr += bytes_sent;
  274. } while(bytes_left);
  275. free(buffer);
  276. pb_release(&PB_Main_msg, request);
  277. }
  278. static void test_rpc_encode_and_feed(MsgList_t msg_list) {
  279. MsgList_reverse(msg_list);
  280. for
  281. M_EACH(request, msg_list, MsgList_t) {
  282. test_rpc_encode_and_feed_one(request);
  283. }
  284. MsgList_reverse(msg_list);
  285. }
  286. static void
  287. test_rpc_compare_file(PB_Storage_File* result_msg_file, PB_Storage_File* expected_msg_file) {
  288. mu_check(!result_msg_file->name == !expected_msg_file->name);
  289. if(result_msg_file->name) {
  290. mu_check(!strcmp(result_msg_file->name, expected_msg_file->name));
  291. }
  292. mu_check(result_msg_file->size == expected_msg_file->size);
  293. mu_check(result_msg_file->type == expected_msg_file->type);
  294. mu_check(!result_msg_file->data == !expected_msg_file->data);
  295. mu_check(result_msg_file->data->size == expected_msg_file->data->size);
  296. for(int i = 0; i < result_msg_file->data->size; ++i) {
  297. mu_check(result_msg_file->data->bytes[i] == expected_msg_file->data->bytes[i]);
  298. }
  299. }
  300. static void test_rpc_compare_messages(PB_Main* result, PB_Main* expected) {
  301. mu_check(result->command_id == expected->command_id);
  302. mu_check(result->command_status == expected->command_status);
  303. mu_check(result->has_next == expected->has_next);
  304. mu_check(result->which_content == expected->which_content);
  305. if(result->command_status != PB_CommandStatus_OK) {
  306. mu_check(result->which_content == PB_Main_empty_tag);
  307. }
  308. switch(result->which_content) {
  309. case PB_Main_empty_tag:
  310. case PB_Main_system_ping_response_tag:
  311. /* nothing to check */
  312. break;
  313. case PB_Main_system_ping_request_tag:
  314. case PB_Main_storage_list_request_tag:
  315. case PB_Main_storage_read_request_tag:
  316. case PB_Main_storage_write_request_tag:
  317. case PB_Main_storage_delete_request_tag:
  318. case PB_Main_storage_mkdir_request_tag:
  319. case PB_Main_storage_md5sum_request_tag:
  320. /* rpc doesn't send it */
  321. mu_check(0);
  322. break;
  323. case PB_Main_app_lock_status_response_tag: {
  324. bool result_locked = result->content.app_lock_status_response.locked;
  325. bool expected_locked = expected->content.app_lock_status_response.locked;
  326. mu_check(result_locked == expected_locked);
  327. break;
  328. }
  329. case PB_Main_storage_info_response_tag: {
  330. uint64_t result_total_space = result->content.storage_info_response.total_space;
  331. uint64_t expected_total_space = expected->content.storage_info_response.total_space;
  332. mu_check(result_total_space == expected_total_space);
  333. uint64_t result_free_space = result->content.storage_info_response.free_space;
  334. uint64_t expected_free_space = expected->content.storage_info_response.free_space;
  335. mu_check(result_free_space == expected_free_space);
  336. } break;
  337. case PB_Main_storage_stat_response_tag: {
  338. bool result_has_msg_file = result->content.storage_stat_response.has_file;
  339. bool expected_has_msg_file = expected->content.storage_stat_response.has_file;
  340. mu_check(result_has_msg_file == expected_has_msg_file);
  341. if(result_has_msg_file) {
  342. PB_Storage_File* result_msg_file = &result->content.storage_stat_response.file;
  343. PB_Storage_File* expected_msg_file = &expected->content.storage_stat_response.file;
  344. test_rpc_compare_file(result_msg_file, expected_msg_file);
  345. } else {
  346. mu_check(0);
  347. }
  348. } break;
  349. case PB_Main_storage_read_response_tag: {
  350. bool result_has_msg_file = result->content.storage_read_response.has_file;
  351. bool expected_has_msg_file = expected->content.storage_read_response.has_file;
  352. mu_check(result_has_msg_file == expected_has_msg_file);
  353. if(result_has_msg_file) {
  354. PB_Storage_File* result_msg_file = &result->content.storage_read_response.file;
  355. PB_Storage_File* expected_msg_file = &expected->content.storage_read_response.file;
  356. test_rpc_compare_file(result_msg_file, expected_msg_file);
  357. } else {
  358. mu_check(0);
  359. }
  360. } break;
  361. case PB_Main_storage_list_response_tag: {
  362. size_t expected_msg_files = expected->content.storage_list_response.file_count;
  363. size_t result_msg_files = result->content.storage_list_response.file_count;
  364. mu_check(result_msg_files == expected_msg_files);
  365. for(int i = 0; i < expected_msg_files; ++i) {
  366. PB_Storage_File* result_msg_file = &result->content.storage_list_response.file[i];
  367. PB_Storage_File* expected_msg_file = &expected->content.storage_list_response.file[i];
  368. test_rpc_compare_file(result_msg_file, expected_msg_file);
  369. }
  370. break;
  371. }
  372. case PB_Main_storage_md5sum_response_tag: {
  373. char* result_md5sum = result->content.storage_md5sum_response.md5sum;
  374. char* expected_md5sum = expected->content.storage_md5sum_response.md5sum;
  375. mu_check(!strcmp(result_md5sum, expected_md5sum));
  376. break;
  377. }
  378. default:
  379. furi_assert(0);
  380. break;
  381. }
  382. }
  383. static bool test_rpc_pb_stream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
  384. StreamBufferHandle_t stream_buffer = istream->state;
  385. size_t bytes_received = 0;
  386. bytes_received = xStreamBufferReceive(stream_buffer, buf, count, MAX_RECEIVE_OUTPUT_TIMEOUT);
  387. return (count == bytes_received);
  388. }
  389. static void
  390. test_rpc_storage_list_create_expected_list_root(MsgList_t msg_list, uint32_t command_id) {
  391. PB_Main* message = MsgList_push_new(msg_list);
  392. message->has_next = false;
  393. message->cb_content.funcs.encode = NULL;
  394. message->command_id = command_id;
  395. message->which_content = PB_Main_storage_list_response_tag;
  396. message->content.storage_list_response.file_count = 3;
  397. message->content.storage_list_response.file[0].data = NULL;
  398. message->content.storage_list_response.file[1].data = NULL;
  399. message->content.storage_list_response.file[2].data = NULL;
  400. message->content.storage_list_response.file[0].size = 0;
  401. message->content.storage_list_response.file[1].size = 0;
  402. message->content.storage_list_response.file[2].size = 0;
  403. message->content.storage_list_response.file[0].type = PB_Storage_File_FileType_DIR;
  404. message->content.storage_list_response.file[1].type = PB_Storage_File_FileType_DIR;
  405. message->content.storage_list_response.file[2].type = PB_Storage_File_FileType_DIR;
  406. char* str = furi_alloc(4);
  407. strcpy(str, "any");
  408. message->content.storage_list_response.file[0].name = str;
  409. str = furi_alloc(4);
  410. strcpy(str, "int");
  411. message->content.storage_list_response.file[1].name = str;
  412. str = furi_alloc(4);
  413. strcpy(str, "ext");
  414. message->content.storage_list_response.file[2].name = str;
  415. }
  416. static void test_rpc_storage_list_create_expected_list(
  417. MsgList_t msg_list,
  418. const char* path,
  419. uint32_t command_id) {
  420. Storage* fs_api = furi_record_open("storage");
  421. File* dir = storage_file_alloc(fs_api);
  422. PB_Main response = {
  423. .command_id = command_id,
  424. .has_next = false,
  425. .which_content = PB_Main_storage_list_response_tag,
  426. /* other fields (e.g. msg_files ptrs) explicitly initialized by 0 */
  427. };
  428. PB_Storage_ListResponse* list = &response.content.storage_list_response;
  429. bool finish = false;
  430. int i = 0;
  431. if(storage_dir_open(dir, path)) {
  432. response.command_status = PB_CommandStatus_OK;
  433. } else {
  434. response.command_status = test_rpc_storage_get_file_error(dir);
  435. response.which_content = PB_Main_empty_tag;
  436. finish = true;
  437. }
  438. while(!finish) {
  439. FileInfo fileinfo;
  440. char* name = furi_alloc(MAX_NAME_LENGTH + 1);
  441. if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  442. if(i == COUNT_OF(list->file)) {
  443. list->file_count = i;
  444. response.has_next = true;
  445. MsgList_push_back(msg_list, response);
  446. i = 0;
  447. }
  448. list->file[i].type = (fileinfo.flags & FSF_DIRECTORY) ? PB_Storage_File_FileType_DIR :
  449. PB_Storage_File_FileType_FILE;
  450. list->file[i].size = fileinfo.size;
  451. list->file[i].data = NULL;
  452. /* memory free inside rpc_encode_and_send() -> pb_release() */
  453. list->file[i].name = name;
  454. ++i;
  455. } else {
  456. finish = true;
  457. free(name);
  458. }
  459. }
  460. list->file_count = i;
  461. response.has_next = false;
  462. MsgList_push_back(msg_list, response);
  463. storage_dir_close(dir);
  464. storage_file_free(dir);
  465. furi_record_close("storage");
  466. }
  467. static void test_rpc_decode_and_compare(MsgList_t expected_msg_list) {
  468. furi_assert(!MsgList_empty_p(expected_msg_list));
  469. pb_istream_t istream = {
  470. .callback = test_rpc_pb_stream_read,
  471. .state = output_stream,
  472. .errmsg = NULL,
  473. .bytes_left = 0x7FFFFFFF,
  474. };
  475. /* other fields explicitly initialized by 0 */
  476. PB_Main result = {.cb_content.funcs.decode = NULL};
  477. /* mlib adds msg_files into start of list, so reverse it */
  478. MsgList_reverse(expected_msg_list);
  479. for
  480. M_EACH(expected_msg, expected_msg_list, MsgList_t) {
  481. if(!pb_decode_ex(&istream, &PB_Main_msg, &result, PB_DECODE_DELIMITED)) {
  482. mu_assert(
  483. 0,
  484. "not all expected messages decoded (maybe increase MAX_RECEIVE_OUTPUT_TIMEOUT)");
  485. break;
  486. }
  487. test_rpc_compare_messages(&result, expected_msg);
  488. pb_release(&PB_Main_msg, &result);
  489. }
  490. MsgList_reverse(expected_msg_list);
  491. }
  492. static void test_rpc_free_msg_list(MsgList_t msg_list) {
  493. for
  494. M_EACH(it, msg_list, MsgList_t) {
  495. pb_release(&PB_Main_msg, it);
  496. }
  497. MsgList_clear(msg_list);
  498. }
  499. static void test_rpc_storage_list_run(const char* path, uint32_t command_id) {
  500. PB_Main request;
  501. MsgList_t expected_msg_list;
  502. MsgList_init(expected_msg_list);
  503. test_rpc_create_simple_message(&request, PB_Main_storage_list_request_tag, path, command_id);
  504. if(!strcmp(path, "/")) {
  505. test_rpc_storage_list_create_expected_list_root(expected_msg_list, command_id);
  506. } else {
  507. test_rpc_storage_list_create_expected_list(expected_msg_list, path, command_id);
  508. }
  509. test_rpc_encode_and_feed_one(&request);
  510. test_rpc_decode_and_compare(expected_msg_list);
  511. pb_release(&PB_Main_msg, &request);
  512. test_rpc_free_msg_list(expected_msg_list);
  513. }
  514. MU_TEST(test_storage_list) {
  515. test_rpc_storage_list_run("/", ++command_id);
  516. test_rpc_storage_list_run("/ext/nfc", ++command_id);
  517. test_rpc_storage_list_run("/int", ++command_id);
  518. test_rpc_storage_list_run("/ext", ++command_id);
  519. test_rpc_storage_list_run("/ext/irda", ++command_id);
  520. test_rpc_storage_list_run("/ext/ibutton", ++command_id);
  521. test_rpc_storage_list_run("/ext/lfrfid", ++command_id);
  522. test_rpc_storage_list_run("error_path", ++command_id);
  523. }
  524. static void
  525. test_rpc_add_empty_to_list(MsgList_t msg_list, PB_CommandStatus status, uint32_t command_id) {
  526. PB_Main* response = MsgList_push_new(msg_list);
  527. response->command_id = command_id;
  528. response->command_status = status;
  529. response->cb_content.funcs.encode = NULL;
  530. response->has_next = false;
  531. response->which_content = PB_Main_empty_tag;
  532. }
  533. static void test_rpc_add_read_to_list_by_reading_real_file(
  534. MsgList_t msg_list,
  535. const char* path,
  536. uint32_t command_id) {
  537. furi_assert(MsgList_empty_p(msg_list));
  538. Storage* fs_api = furi_record_open("storage");
  539. File* file = storage_file_alloc(fs_api);
  540. bool result = false;
  541. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  542. size_t size_left = storage_file_size(file);
  543. do {
  544. PB_Main* response = MsgList_push_new(msg_list);
  545. response->command_id = command_id;
  546. response->command_status = PB_CommandStatus_OK;
  547. response->has_next = false;
  548. response->which_content = PB_Main_storage_read_response_tag;
  549. response->content.storage_read_response.has_file = true;
  550. response->content.storage_read_response.file.data =
  551. furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(MIN(size_left, MAX_DATA_SIZE)));
  552. uint8_t* buffer = response->content.storage_read_response.file.data->bytes;
  553. uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
  554. size_t read_size = MIN(size_left, MAX_DATA_SIZE);
  555. *read_size_msg = storage_file_read(file, buffer, read_size);
  556. size_left -= read_size;
  557. result = (*read_size_msg == read_size);
  558. if(result) {
  559. response->has_next = (size_left > 0);
  560. }
  561. } while((size_left != 0) && result);
  562. if(!result) {
  563. test_rpc_add_empty_to_list(
  564. msg_list, test_rpc_storage_get_file_error(file), command_id);
  565. }
  566. } else {
  567. test_rpc_add_empty_to_list(msg_list, test_rpc_storage_get_file_error(file), command_id);
  568. }
  569. storage_file_close(file);
  570. storage_file_free(file);
  571. furi_record_close("storage");
  572. }
  573. static void test_storage_read_run(const char* path, uint32_t command_id) {
  574. PB_Main request;
  575. MsgList_t expected_msg_list;
  576. MsgList_init(expected_msg_list);
  577. test_rpc_add_read_to_list_by_reading_real_file(expected_msg_list, path, command_id);
  578. test_rpc_create_simple_message(&request, PB_Main_storage_read_request_tag, path, command_id);
  579. test_rpc_encode_and_feed_one(&request);
  580. test_rpc_decode_and_compare(expected_msg_list);
  581. pb_release(&PB_Main_msg, &request);
  582. test_rpc_free_msg_list(expected_msg_list);
  583. }
  584. static bool test_is_exists(const char* path) {
  585. Storage* fs_api = furi_record_open("storage");
  586. FileInfo fileinfo;
  587. FS_Error result = storage_common_stat(fs_api, path, &fileinfo);
  588. furi_check((result == FSE_OK) || (result == FSE_NOT_EXIST));
  589. furi_record_close("storage");
  590. return result == FSE_OK;
  591. }
  592. static void test_create_dir(const char* path) {
  593. Storage* fs_api = furi_record_open("storage");
  594. FS_Error error = storage_common_mkdir(fs_api, path);
  595. (void)error;
  596. furi_assert((error == FSE_OK) || (error == FSE_EXIST));
  597. furi_record_close("storage");
  598. furi_check(test_is_exists(path));
  599. }
  600. static void test_create_file(const char* path, size_t size) {
  601. Storage* fs_api = furi_record_open("storage");
  602. File* file = storage_file_alloc(fs_api);
  603. if(storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  604. uint8_t buf[128] = {0};
  605. for(int i = 0; i < sizeof(buf); ++i) {
  606. buf[i] = '0' + (i % 10);
  607. }
  608. while(size) {
  609. size_t written = storage_file_write(file, buf, MIN(size, sizeof(buf)));
  610. furi_assert(written);
  611. size -= written;
  612. }
  613. }
  614. storage_file_close(file);
  615. storage_file_free(file);
  616. furi_record_close("storage");
  617. furi_check(test_is_exists(path));
  618. }
  619. static void test_rpc_storage_info_run(const char* path, uint32_t command_id) {
  620. PB_Main request;
  621. MsgList_t expected_msg_list;
  622. MsgList_init(expected_msg_list);
  623. test_rpc_create_simple_message(&request, PB_Main_storage_info_request_tag, path, command_id);
  624. PB_Main* response = MsgList_push_new(expected_msg_list);
  625. response->command_id = command_id;
  626. Storage* fs_api = furi_record_open("storage");
  627. FS_Error error = storage_common_fs_info(
  628. fs_api,
  629. path,
  630. &response->content.storage_info_response.total_space,
  631. &response->content.storage_info_response.free_space);
  632. response->command_status = rpc_system_storage_get_error(error);
  633. if(error == FSE_OK) {
  634. response->which_content = PB_Main_storage_info_response_tag;
  635. } else {
  636. response->which_content = PB_Main_empty_tag;
  637. }
  638. test_rpc_encode_and_feed_one(&request);
  639. test_rpc_decode_and_compare(expected_msg_list);
  640. pb_release(&PB_Main_msg, &request);
  641. test_rpc_free_msg_list(expected_msg_list);
  642. }
  643. static void test_rpc_storage_stat_run(const char* path, uint32_t command_id) {
  644. PB_Main request;
  645. MsgList_t expected_msg_list;
  646. MsgList_init(expected_msg_list);
  647. test_rpc_create_simple_message(&request, PB_Main_storage_stat_request_tag, path, command_id);
  648. Storage* fs_api = furi_record_open("storage");
  649. FileInfo fileinfo;
  650. FS_Error error = storage_common_stat(fs_api, path, &fileinfo);
  651. furi_record_close("storage");
  652. PB_Main* response = MsgList_push_new(expected_msg_list);
  653. response->command_id = command_id;
  654. response->command_status = rpc_system_storage_get_error(error);
  655. response->has_next = false;
  656. response->which_content = PB_Main_empty_tag;
  657. if(error == FSE_OK) {
  658. response->which_content = PB_Main_storage_stat_response_tag;
  659. response->content.storage_stat_response.has_file = true;
  660. response->content.storage_stat_response.file.type = (fileinfo.flags & FSF_DIRECTORY) ?
  661. PB_Storage_File_FileType_DIR :
  662. PB_Storage_File_FileType_FILE;
  663. response->content.storage_stat_response.file.size = fileinfo.size;
  664. }
  665. test_rpc_encode_and_feed_one(&request);
  666. test_rpc_decode_and_compare(expected_msg_list);
  667. pb_release(&PB_Main_msg, &request);
  668. test_rpc_free_msg_list(expected_msg_list);
  669. }
  670. MU_TEST(test_storage_info) {
  671. test_rpc_storage_info_run("/any", ++command_id);
  672. test_rpc_storage_info_run("/int", ++command_id);
  673. test_rpc_storage_info_run("/ext", ++command_id);
  674. }
  675. #define TEST_DIR_STAT_NAME TEST_DIR "stat_dir"
  676. #define TEST_DIR_STAT TEST_DIR_STAT_NAME "/"
  677. MU_TEST(test_storage_stat) {
  678. test_create_dir(TEST_DIR_STAT_NAME);
  679. test_create_file(TEST_DIR_STAT "empty.txt", 0);
  680. test_create_file(TEST_DIR_STAT "l33t.txt", 1337);
  681. test_rpc_storage_stat_run("/", ++command_id);
  682. test_rpc_storage_stat_run("/int", ++command_id);
  683. test_rpc_storage_stat_run("/ext", ++command_id);
  684. test_rpc_storage_stat_run(TEST_DIR_STAT "empty.txt", ++command_id);
  685. test_rpc_storage_stat_run(TEST_DIR_STAT "l33t.txt", ++command_id);
  686. test_rpc_storage_stat_run(TEST_DIR_STAT "missing", ++command_id);
  687. test_rpc_storage_stat_run(TEST_DIR_STAT_NAME, ++command_id);
  688. test_rpc_storage_stat_run(TEST_DIR_STAT, ++command_id);
  689. }
  690. MU_TEST(test_storage_read) {
  691. test_create_file(TEST_DIR "empty.txt", 0);
  692. test_create_file(TEST_DIR "file1.txt", 1);
  693. test_create_file(TEST_DIR "file2.txt", MAX_DATA_SIZE);
  694. test_create_file(TEST_DIR "file3.txt", MAX_DATA_SIZE + 1);
  695. test_create_file(TEST_DIR "file4.txt", (MAX_DATA_SIZE * 2) + 1);
  696. test_storage_read_run(TEST_DIR "empty.txt", ++command_id);
  697. test_storage_read_run(TEST_DIR "file1.txt", ++command_id);
  698. test_storage_read_run(TEST_DIR "file2.txt", ++command_id);
  699. test_storage_read_run(TEST_DIR "file3.txt", ++command_id);
  700. test_storage_read_run(TEST_DIR "file4.txt", ++command_id);
  701. }
  702. static void test_storage_write_run(
  703. const char* path,
  704. size_t write_size,
  705. size_t write_count,
  706. uint32_t command_id,
  707. PB_CommandStatus status) {
  708. MsgList_t input_msg_list;
  709. MsgList_init(input_msg_list);
  710. MsgList_t expected_msg_list;
  711. MsgList_init(expected_msg_list);
  712. uint8_t* buf = furi_alloc(write_size);
  713. for(int i = 0; i < write_size; ++i) {
  714. buf[i] = '0' + (i % 10);
  715. }
  716. test_rpc_add_read_or_write_to_list(
  717. input_msg_list, WRITE_REQUEST, path, buf, write_size, write_count, command_id);
  718. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  719. test_rpc_encode_and_feed(input_msg_list);
  720. test_rpc_decode_and_compare(expected_msg_list);
  721. test_rpc_free_msg_list(input_msg_list);
  722. test_rpc_free_msg_list(expected_msg_list);
  723. free(buf);
  724. }
  725. static void test_storage_write_read_run(
  726. const char* path,
  727. const uint8_t* pattern,
  728. size_t pattern_size,
  729. size_t pattern_repeats,
  730. uint32_t* command_id) {
  731. MsgList_t input_msg_list;
  732. MsgList_init(input_msg_list);
  733. MsgList_t expected_msg_list;
  734. MsgList_init(expected_msg_list);
  735. test_rpc_add_read_or_write_to_list(
  736. input_msg_list, WRITE_REQUEST, path, pattern, pattern_size, pattern_repeats, ++*command_id);
  737. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, *command_id);
  738. test_rpc_create_simple_message(
  739. MsgList_push_raw(input_msg_list), PB_Main_storage_read_request_tag, path, ++*command_id);
  740. test_rpc_add_read_or_write_to_list(
  741. expected_msg_list,
  742. READ_RESPONSE,
  743. path,
  744. pattern,
  745. pattern_size,
  746. pattern_repeats,
  747. *command_id);
  748. test_rpc_print_message_list(input_msg_list);
  749. test_rpc_print_message_list(expected_msg_list);
  750. test_rpc_encode_and_feed(input_msg_list);
  751. test_rpc_decode_and_compare(expected_msg_list);
  752. test_rpc_free_msg_list(input_msg_list);
  753. test_rpc_free_msg_list(expected_msg_list);
  754. }
  755. MU_TEST(test_storage_write_read) {
  756. uint8_t pattern1[] = "abcdefgh";
  757. test_storage_write_read_run(TEST_DIR "test1.txt", pattern1, sizeof(pattern1), 1, &command_id);
  758. test_storage_write_read_run(TEST_DIR "test2.txt", pattern1, 1, 1, &command_id);
  759. test_storage_write_read_run(TEST_DIR "test3.txt", pattern1, 0, 1, &command_id);
  760. }
  761. MU_TEST(test_storage_write) {
  762. test_storage_write_run(
  763. TEST_DIR "afaefo/aefaef/aef/aef/test1.txt",
  764. 1,
  765. 1,
  766. ++command_id,
  767. PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  768. test_storage_write_run(TEST_DIR "test1.txt", 100, 1, ++command_id, PB_CommandStatus_OK);
  769. test_storage_write_run(TEST_DIR "test2.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  770. test_storage_write_run(TEST_DIR "test1.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  771. test_storage_write_run(TEST_DIR "test2.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  772. test_storage_write_run(
  773. TEST_DIR "afaefo/aefaef/aef/aef/test1.txt",
  774. 1,
  775. 1,
  776. ++command_id,
  777. PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  778. test_storage_write_run(TEST_DIR "test2.txt", 1, 50, ++command_id, PB_CommandStatus_OK);
  779. test_storage_write_run(TEST_DIR "test2.txt", 512, 3, ++command_id, PB_CommandStatus_OK);
  780. }
  781. MU_TEST(test_storage_interrupt_continuous_same_system) {
  782. MsgList_t input_msg_list;
  783. MsgList_init(input_msg_list);
  784. MsgList_t expected_msg_list;
  785. MsgList_init(expected_msg_list);
  786. uint8_t pattern[16] = {0};
  787. test_rpc_add_read_or_write_to_list(
  788. input_msg_list,
  789. WRITE_REQUEST,
  790. TEST_DIR "test1.txt",
  791. pattern,
  792. sizeof(pattern),
  793. 3,
  794. command_id);
  795. /* replace last packet (has_next == false) with another command */
  796. PB_Main message_to_remove;
  797. MsgList_pop_back(&message_to_remove, input_msg_list);
  798. pb_release(&PB_Main_msg, &message_to_remove);
  799. test_rpc_create_simple_message(
  800. MsgList_push_new(input_msg_list),
  801. PB_Main_storage_mkdir_request_tag,
  802. TEST_DIR "dir1",
  803. command_id + 1);
  804. test_rpc_add_read_or_write_to_list(
  805. input_msg_list,
  806. WRITE_REQUEST,
  807. TEST_DIR "test2.txt",
  808. pattern,
  809. sizeof(pattern),
  810. 3,
  811. command_id);
  812. test_rpc_add_empty_to_list(
  813. expected_msg_list, PB_CommandStatus_ERROR_CONTINUOUS_COMMAND_INTERRUPTED, command_id);
  814. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id + 1);
  815. test_rpc_encode_and_feed(input_msg_list);
  816. test_rpc_decode_and_compare(expected_msg_list);
  817. test_rpc_free_msg_list(input_msg_list);
  818. test_rpc_free_msg_list(expected_msg_list);
  819. }
  820. MU_TEST(test_storage_interrupt_continuous_another_system) {
  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. uint8_t pattern[16] = {0};
  826. test_rpc_add_read_or_write_to_list(
  827. input_msg_list,
  828. WRITE_REQUEST,
  829. TEST_DIR "test1.txt",
  830. pattern,
  831. sizeof(pattern),
  832. 3,
  833. command_id);
  834. PB_Main message = {
  835. .command_id = command_id + 1,
  836. .command_status = PB_CommandStatus_OK,
  837. .cb_content.funcs.encode = NULL,
  838. .has_next = false,
  839. .which_content = PB_Main_system_ping_request_tag,
  840. };
  841. MsgList_it_t it;
  842. MsgList_it(it, input_msg_list);
  843. MsgList_next(it);
  844. MsgList_insert(input_msg_list, it, message);
  845. test_rpc_add_read_or_write_to_list(
  846. input_msg_list,
  847. WRITE_REQUEST,
  848. TEST_DIR "test2.txt",
  849. pattern,
  850. sizeof(pattern),
  851. 3,
  852. command_id + 2);
  853. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, command_id + 1);
  854. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id);
  855. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id + 2);
  856. test_rpc_encode_and_feed(input_msg_list);
  857. test_rpc_decode_and_compare(expected_msg_list);
  858. test_rpc_free_msg_list(input_msg_list);
  859. test_rpc_free_msg_list(expected_msg_list);
  860. }
  861. static void test_storage_delete_run(
  862. const char* path,
  863. size_t command_id,
  864. PB_CommandStatus status,
  865. bool recursive) {
  866. PB_Main request;
  867. MsgList_t expected_msg_list;
  868. MsgList_init(expected_msg_list);
  869. test_rpc_create_simple_message(&request, PB_Main_storage_delete_request_tag, path, command_id);
  870. request.content.storage_delete_request.recursive = recursive;
  871. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  872. test_rpc_encode_and_feed_one(&request);
  873. test_rpc_decode_and_compare(expected_msg_list);
  874. pb_release(&PB_Main_msg, &request);
  875. test_rpc_free_msg_list(expected_msg_list);
  876. }
  877. #define TEST_DIR_RMRF_NAME TEST_DIR "rmrf_test"
  878. #define TEST_DIR_RMRF TEST_DIR_RMRF_NAME "/"
  879. MU_TEST(test_storage_delete_recursive) {
  880. test_create_dir(TEST_DIR_RMRF_NAME);
  881. test_create_dir(TEST_DIR_RMRF "dir1");
  882. test_create_file(TEST_DIR_RMRF "dir1/file1", 1);
  883. test_create_dir(TEST_DIR_RMRF "dir1/dir1");
  884. test_create_dir(TEST_DIR_RMRF "dir1/dir2");
  885. test_create_file(TEST_DIR_RMRF "dir1/dir2/file1", 1);
  886. test_create_file(TEST_DIR_RMRF "dir1/dir2/file2", 1);
  887. test_create_dir(TEST_DIR_RMRF "dir1/dir3");
  888. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1");
  889. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1/dir1");
  890. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1/dir1/dir1");
  891. test_create_dir(TEST_DIR_RMRF "dir1/dir3/dir1/dir1/dir1/dir1");
  892. test_create_dir(TEST_DIR_RMRF "dir2");
  893. test_create_dir(TEST_DIR_RMRF "dir2/dir1");
  894. test_create_dir(TEST_DIR_RMRF "dir2/dir2");
  895. test_create_file(TEST_DIR_RMRF "dir2/dir2/file1", 1);
  896. test_create_dir(TEST_DIR_RMRF "dir2/dir2/dir1");
  897. test_create_dir(TEST_DIR_RMRF "dir2/dir2/dir1/dir1");
  898. test_create_dir(TEST_DIR_RMRF "dir2/dir2/dir1/dir1/dir1");
  899. test_create_file(TEST_DIR_RMRF "dir2/dir2/dir1/dir1/dir1/file1", 1);
  900. test_storage_delete_run(
  901. TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_ERROR_STORAGE_DIR_NOT_EMPTY, false);
  902. mu_check(test_is_exists(TEST_DIR_RMRF_NAME));
  903. test_storage_delete_run(TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_OK, true);
  904. mu_check(!test_is_exists(TEST_DIR_RMRF_NAME));
  905. test_storage_delete_run(TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_OK, false);
  906. mu_check(!test_is_exists(TEST_DIR_RMRF_NAME));
  907. test_create_dir(TEST_DIR_RMRF_NAME);
  908. test_storage_delete_run(TEST_DIR_RMRF_NAME, ++command_id, PB_CommandStatus_OK, true);
  909. mu_check(!test_is_exists(TEST_DIR_RMRF_NAME));
  910. test_create_dir(TEST_DIR "file1");
  911. test_storage_delete_run(TEST_DIR "file1", ++command_id, PB_CommandStatus_OK, true);
  912. mu_check(!test_is_exists(TEST_DIR "file1"));
  913. }
  914. MU_TEST(test_storage_delete) {
  915. test_storage_delete_run(NULL, ++command_id, PB_CommandStatus_ERROR_INVALID_PARAMETERS, false);
  916. furi_check(!test_is_exists(TEST_DIR "empty.txt"));
  917. test_storage_delete_run(TEST_DIR "empty.txt", ++command_id, PB_CommandStatus_OK, false);
  918. mu_check(!test_is_exists(TEST_DIR "empty.txt"));
  919. test_create_file(TEST_DIR "empty.txt", 0);
  920. test_storage_delete_run(TEST_DIR "empty.txt", ++command_id, PB_CommandStatus_OK, false);
  921. mu_check(!test_is_exists(TEST_DIR "empty.txt"));
  922. furi_check(!test_is_exists(TEST_DIR "dir1"));
  923. test_create_dir(TEST_DIR "dir1");
  924. test_storage_delete_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK, false);
  925. mu_check(!test_is_exists(TEST_DIR "dir1"));
  926. test_storage_delete_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK, false);
  927. mu_check(!test_is_exists(TEST_DIR "dir1"));
  928. }
  929. static void test_storage_mkdir_run(const char* path, size_t command_id, PB_CommandStatus status) {
  930. PB_Main request;
  931. MsgList_t expected_msg_list;
  932. MsgList_init(expected_msg_list);
  933. test_rpc_create_simple_message(&request, PB_Main_storage_mkdir_request_tag, path, command_id);
  934. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  935. test_rpc_encode_and_feed_one(&request);
  936. test_rpc_decode_and_compare(expected_msg_list);
  937. pb_release(&PB_Main_msg, &request);
  938. test_rpc_free_msg_list(expected_msg_list);
  939. }
  940. MU_TEST(test_storage_mkdir) {
  941. furi_check(!test_is_exists(TEST_DIR "dir1"));
  942. test_storage_mkdir_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK);
  943. mu_check(test_is_exists(TEST_DIR "dir1"));
  944. test_storage_mkdir_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_ERROR_STORAGE_EXIST);
  945. mu_check(test_is_exists(TEST_DIR "dir1"));
  946. furi_check(!test_is_exists(TEST_DIR "dir2"));
  947. test_create_dir(TEST_DIR "dir2");
  948. test_storage_mkdir_run(TEST_DIR "dir2", ++command_id, PB_CommandStatus_ERROR_STORAGE_EXIST);
  949. mu_check(test_is_exists(TEST_DIR "dir2"));
  950. }
  951. static void test_storage_calculate_md5sum(const char* path, char* md5sum) {
  952. Storage* api = furi_record_open("storage");
  953. File* file = storage_file_alloc(api);
  954. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  955. const uint16_t once_read_size = 512;
  956. const uint8_t hash_size = MD5SUM_SIZE;
  957. uint8_t* data = malloc(once_read_size);
  958. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  959. md5_context* md5_ctx = malloc(sizeof(md5_context));
  960. md5_starts(md5_ctx);
  961. while(true) {
  962. uint16_t read_size = storage_file_read(file, data, once_read_size);
  963. if(read_size == 0) break;
  964. md5_update(md5_ctx, data, read_size);
  965. }
  966. md5_finish(md5_ctx, hash);
  967. free(md5_ctx);
  968. for(uint8_t i = 0; i < hash_size; i++) {
  969. md5sum += sprintf(md5sum, "%02x", hash[i]);
  970. }
  971. free(hash);
  972. free(data);
  973. } else {
  974. furi_assert(0);
  975. }
  976. storage_file_close(file);
  977. storage_file_free(file);
  978. furi_record_close("storage");
  979. }
  980. static void test_storage_md5sum_run(
  981. const char* path,
  982. uint32_t command_id,
  983. const char* md5sum,
  984. PB_CommandStatus status) {
  985. PB_Main request;
  986. MsgList_t expected_msg_list;
  987. MsgList_init(expected_msg_list);
  988. test_rpc_create_simple_message(&request, PB_Main_storage_md5sum_request_tag, path, command_id);
  989. if(status == PB_CommandStatus_OK) {
  990. PB_Main* response = MsgList_push_new(expected_msg_list);
  991. test_rpc_create_simple_message(
  992. response, PB_Main_storage_md5sum_response_tag, md5sum, command_id);
  993. response->command_status = status;
  994. } else {
  995. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  996. }
  997. test_rpc_encode_and_feed_one(&request);
  998. test_rpc_decode_and_compare(expected_msg_list);
  999. pb_release(&PB_Main_msg, &request);
  1000. test_rpc_free_msg_list(expected_msg_list);
  1001. }
  1002. MU_TEST(test_storage_md5sum) {
  1003. char md5sum1[MD5SUM_SIZE * 2 + 1] = {0};
  1004. char md5sum2[MD5SUM_SIZE * 2 + 1] = {0};
  1005. char md5sum3[MD5SUM_SIZE * 2 + 1] = {0};
  1006. test_storage_md5sum_run(
  1007. TEST_DIR "test1.txt", ++command_id, "", PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  1008. test_create_file(TEST_DIR "file1.txt", 0);
  1009. test_create_file(TEST_DIR "file2.txt", 1);
  1010. test_create_file(TEST_DIR "file3.txt", 512);
  1011. test_storage_calculate_md5sum(TEST_DIR "file1.txt", md5sum1);
  1012. test_storage_calculate_md5sum(TEST_DIR "file2.txt", md5sum2);
  1013. test_storage_calculate_md5sum(TEST_DIR "file3.txt", md5sum3);
  1014. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  1015. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  1016. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  1017. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  1018. test_storage_md5sum_run(TEST_DIR "file3.txt", ++command_id, md5sum3, PB_CommandStatus_OK);
  1019. test_storage_md5sum_run(TEST_DIR "file3.txt", ++command_id, md5sum3, PB_CommandStatus_OK);
  1020. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  1021. test_storage_md5sum_run(TEST_DIR "file3.txt", ++command_id, md5sum3, PB_CommandStatus_OK);
  1022. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  1023. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  1024. }
  1025. static void test_rpc_storage_rename_run(
  1026. const char* old_path,
  1027. const char* new_path,
  1028. uint32_t command_id,
  1029. PB_CommandStatus status) {
  1030. PB_Main request;
  1031. MsgList_t expected_msg_list;
  1032. MsgList_init(expected_msg_list);
  1033. char* str_old_path = strdup(old_path);
  1034. char* str_new_path = strdup(new_path);
  1035. request.command_id = command_id;
  1036. request.command_status = PB_CommandStatus_OK;
  1037. request.cb_content.funcs.encode = NULL;
  1038. request.which_content = PB_Main_storage_rename_request_tag;
  1039. request.has_next = false;
  1040. request.content.storage_rename_request.old_path = str_old_path;
  1041. request.content.storage_rename_request.new_path = str_new_path;
  1042. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  1043. test_rpc_encode_and_feed_one(&request);
  1044. test_rpc_decode_and_compare(expected_msg_list);
  1045. pb_release(&PB_Main_msg, &request);
  1046. test_rpc_free_msg_list(expected_msg_list);
  1047. }
  1048. MU_TEST(test_storage_rename) {
  1049. test_rpc_storage_rename_run(
  1050. NULL, NULL, ++command_id, PB_CommandStatus_ERROR_STORAGE_INVALID_NAME);
  1051. furi_check(!test_is_exists(TEST_DIR "empty.txt"));
  1052. test_create_file(TEST_DIR "empty.txt", 0);
  1053. test_rpc_storage_rename_run(
  1054. TEST_DIR "empty.txt", TEST_DIR "empty2.txt", ++command_id, PB_CommandStatus_OK);
  1055. mu_check(!test_is_exists(TEST_DIR "empty.txt"));
  1056. mu_check(test_is_exists(TEST_DIR "empty2.txt"));
  1057. furi_check(!test_is_exists(TEST_DIR "dir1"));
  1058. test_create_dir(TEST_DIR "dir1");
  1059. test_rpc_storage_rename_run(
  1060. TEST_DIR "dir1", TEST_DIR "dir2", ++command_id, PB_CommandStatus_OK);
  1061. mu_check(!test_is_exists(TEST_DIR "dir1"));
  1062. mu_check(test_is_exists(TEST_DIR "dir2"));
  1063. }
  1064. MU_TEST(test_ping) {
  1065. MsgList_t input_msg_list;
  1066. MsgList_init(input_msg_list);
  1067. MsgList_t expected_msg_list;
  1068. MsgList_init(expected_msg_list);
  1069. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 0);
  1070. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 1);
  1071. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 0);
  1072. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 500);
  1073. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, (uint32_t)-1);
  1074. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 700);
  1075. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 1);
  1076. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 0);
  1077. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 1);
  1078. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 0);
  1079. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 500);
  1080. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, (uint32_t)-1);
  1081. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 700);
  1082. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 1);
  1083. test_rpc_encode_and_feed(input_msg_list);
  1084. test_rpc_decode_and_compare(expected_msg_list);
  1085. test_rpc_free_msg_list(input_msg_list);
  1086. test_rpc_free_msg_list(expected_msg_list);
  1087. }
  1088. // TODO: 1) test for rubbish data
  1089. // 2) test for unexpected end of packet
  1090. // 3) test for one push of several packets
  1091. // 4) test for fill buffer till end (great varint) and close connection
  1092. MU_TEST_SUITE(test_rpc_system) {
  1093. MU_SUITE_CONFIGURE(&test_rpc_setup, &test_rpc_teardown);
  1094. MU_RUN_TEST(test_ping);
  1095. }
  1096. MU_TEST_SUITE(test_rpc_storage) {
  1097. MU_SUITE_CONFIGURE(&test_rpc_storage_setup, &test_rpc_storage_teardown);
  1098. MU_RUN_TEST(test_storage_info);
  1099. MU_RUN_TEST(test_storage_stat);
  1100. MU_RUN_TEST(test_storage_list);
  1101. MU_RUN_TEST(test_storage_read);
  1102. MU_RUN_TEST(test_storage_write_read);
  1103. MU_RUN_TEST(test_storage_write);
  1104. MU_RUN_TEST(test_storage_delete);
  1105. MU_RUN_TEST(test_storage_delete_recursive);
  1106. MU_RUN_TEST(test_storage_mkdir);
  1107. MU_RUN_TEST(test_storage_md5sum);
  1108. MU_RUN_TEST(test_storage_rename);
  1109. MU_RUN_TEST(test_storage_interrupt_continuous_same_system);
  1110. MU_RUN_TEST(test_storage_interrupt_continuous_another_system);
  1111. }
  1112. static void test_app_create_request(
  1113. PB_Main* request,
  1114. const char* app_name,
  1115. const char* app_args,
  1116. uint32_t command_id) {
  1117. request->command_id = command_id;
  1118. request->command_status = PB_CommandStatus_OK;
  1119. request->cb_content.funcs.encode = NULL;
  1120. request->which_content = PB_Main_app_start_request_tag;
  1121. request->has_next = false;
  1122. if(app_name) {
  1123. char* msg_app_name = strdup(app_name);
  1124. request->content.app_start_request.name = msg_app_name;
  1125. } else {
  1126. request->content.app_start_request.name = NULL;
  1127. }
  1128. if(app_args) {
  1129. char* msg_app_args = strdup(app_args);
  1130. request->content.app_start_request.args = msg_app_args;
  1131. } else {
  1132. request->content.app_start_request.args = NULL;
  1133. }
  1134. }
  1135. static void test_app_start_run(
  1136. const char* app_name,
  1137. const char* app_args,
  1138. PB_CommandStatus status,
  1139. uint32_t command_id) {
  1140. PB_Main request;
  1141. MsgList_t expected_msg_list;
  1142. MsgList_init(expected_msg_list);
  1143. test_app_create_request(&request, app_name, app_args, command_id);
  1144. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  1145. test_rpc_encode_and_feed_one(&request);
  1146. test_rpc_decode_and_compare(expected_msg_list);
  1147. pb_release(&PB_Main_msg, &request);
  1148. test_rpc_free_msg_list(expected_msg_list);
  1149. }
  1150. static void test_app_get_status_lock_run(bool locked_expected, uint32_t command_id) {
  1151. PB_Main request = {
  1152. .command_id = command_id,
  1153. .command_status = PB_CommandStatus_OK,
  1154. .which_content = PB_Main_app_lock_status_request_tag,
  1155. .has_next = false,
  1156. };
  1157. MsgList_t expected_msg_list;
  1158. MsgList_init(expected_msg_list);
  1159. PB_Main* response = MsgList_push_new(expected_msg_list);
  1160. response->command_id = command_id;
  1161. response->command_status = PB_CommandStatus_OK;
  1162. response->which_content = PB_Main_app_lock_status_response_tag;
  1163. response->has_next = false;
  1164. response->content.app_lock_status_response.locked = locked_expected;
  1165. test_rpc_encode_and_feed_one(&request);
  1166. test_rpc_decode_and_compare(expected_msg_list);
  1167. pb_release(&PB_Main_msg, &request);
  1168. test_rpc_free_msg_list(expected_msg_list);
  1169. }
  1170. MU_TEST(test_app_start_and_lock_status) {
  1171. test_app_get_status_lock_run(false, ++command_id);
  1172. test_app_start_run(NULL, "/ext/file", PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1173. test_app_start_run(NULL, NULL, PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1174. test_app_get_status_lock_run(false, ++command_id);
  1175. test_app_start_run(
  1176. "skynet_destroy_world_app", NULL, PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1177. test_app_get_status_lock_run(false, ++command_id);
  1178. test_app_start_run("Delay Test", "0", PB_CommandStatus_OK, ++command_id);
  1179. delay(100);
  1180. test_app_get_status_lock_run(false, ++command_id);
  1181. test_app_start_run("Delay Test", "200", PB_CommandStatus_OK, ++command_id);
  1182. test_app_get_status_lock_run(true, ++command_id);
  1183. delay(100);
  1184. test_app_get_status_lock_run(true, ++command_id);
  1185. test_app_start_run("Delay Test", "0", PB_CommandStatus_ERROR_APP_SYSTEM_LOCKED, ++command_id);
  1186. delay(200);
  1187. test_app_get_status_lock_run(false, ++command_id);
  1188. test_app_start_run("Delay Test", "500", PB_CommandStatus_OK, ++command_id);
  1189. delay(100);
  1190. test_app_get_status_lock_run(true, ++command_id);
  1191. test_app_start_run("Infrared", "0", PB_CommandStatus_ERROR_APP_SYSTEM_LOCKED, ++command_id);
  1192. delay(100);
  1193. test_app_get_status_lock_run(true, ++command_id);
  1194. test_app_start_run(
  1195. "2_girls_1_app", "0", PB_CommandStatus_ERROR_INVALID_PARAMETERS, ++command_id);
  1196. delay(100);
  1197. test_app_get_status_lock_run(true, ++command_id);
  1198. delay(500);
  1199. test_app_get_status_lock_run(false, ++command_id);
  1200. }
  1201. MU_TEST_SUITE(test_rpc_app) {
  1202. MU_SUITE_CONFIGURE(&test_rpc_setup, &test_rpc_teardown);
  1203. MU_RUN_TEST(test_app_start_and_lock_status);
  1204. }
  1205. int run_minunit_test_rpc() {
  1206. Storage* storage = furi_record_open("storage");
  1207. furi_record_close("storage");
  1208. if(storage_sd_status(storage) != FSE_OK) {
  1209. FURI_LOG_E(TAG, "SD card not mounted - skip storage tests");
  1210. } else {
  1211. MU_RUN_SUITE(test_rpc_storage);
  1212. }
  1213. MU_RUN_SUITE(test_rpc_system);
  1214. MU_RUN_SUITE(test_rpc_app);
  1215. return MU_EXIT_CODE;
  1216. }
  1217. int32_t delay_test_app(void* p) {
  1218. int timeout = atoi((const char*)p);
  1219. if(timeout > 0) {
  1220. delay(timeout);
  1221. }
  1222. return 0;
  1223. }