rpc_test.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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/storage.h"
  9. #include <furi.h>
  10. #include "../minunit.h"
  11. #include <stdint.h>
  12. #include <stream_buffer.h>
  13. #include <pb.h>
  14. #include <pb_encode.h>
  15. #include <m-list.h>
  16. #include <lib/toolbox/md5.h>
  17. LIST_DEF(MsgList, PB_Main, M_POD_OPLIST)
  18. #define M_OPL_MsgList_t() LIST_OPLIST(MsgList)
  19. /* MinUnit test framework doesn't allow passing context into tests,
  20. * so we have to use global variables
  21. */
  22. static Rpc* rpc = NULL;
  23. static RpcSession* session = NULL;
  24. static StreamBufferHandle_t output_stream = NULL;
  25. static uint32_t command_id = 0;
  26. #define TEST_RPC_TAG "TEST_RPC"
  27. #define MAX_RECEIVE_OUTPUT_TIMEOUT 3000
  28. #define MAX_NAME_LENGTH 255
  29. #define MAX_DATA_SIZE 512 // have to be exact as in rpc_storage.c
  30. #define TEST_DIR TEST_DIR_NAME "/"
  31. #define TEST_DIR_NAME "/ext/unit_tests_tmp"
  32. #define MD5SUM_SIZE 16
  33. #define PING_REQUEST 0
  34. #define PING_RESPONSE 1
  35. #define WRITE_REQUEST 0
  36. #define READ_RESPONSE 1
  37. #define DEBUG_PRINT 0
  38. #define BYTES(x) (x), sizeof(x)
  39. static void output_bytes_callback(void* ctx, uint8_t* got_bytes, size_t got_size);
  40. static void clean_directory(Storage* fs_api, const char* clean_dir);
  41. static void
  42. test_rpc_add_empty_to_list(MsgList_t msg_list, PB_CommandStatus status, uint32_t command_id);
  43. static void test_rpc_encode_and_feed(MsgList_t msg_list);
  44. static void test_rpc_encode_and_feed_one(PB_Main* request);
  45. static void test_rpc_compare_messages(PB_Main* result, PB_Main* expected);
  46. static void test_rpc_decode_and_compare(MsgList_t expected_msg_list);
  47. static void test_rpc_free_msg_list(MsgList_t msg_list);
  48. static void test_rpc_storage_setup(void) {
  49. furi_assert(!rpc);
  50. furi_assert(!session);
  51. furi_assert(!output_stream);
  52. rpc = furi_record_open("rpc");
  53. for(int i = 0; !session && (i < 10000); ++i) {
  54. session = rpc_open_session(rpc);
  55. delay(1);
  56. }
  57. furi_assert(session);
  58. Storage* fs_api = furi_record_open("storage");
  59. clean_directory(fs_api, TEST_DIR_NAME);
  60. furi_record_close("storage");
  61. output_stream = xStreamBufferCreate(1000, 1);
  62. mu_assert(session, "failed to start session");
  63. rpc_set_send_bytes_callback(session, output_bytes_callback, output_stream);
  64. }
  65. static void test_rpc_storage_teardown(void) {
  66. Storage* fs_api = furi_record_open("storage");
  67. clean_directory(fs_api, TEST_DIR_NAME);
  68. furi_record_close("storage");
  69. rpc_close_session(session);
  70. furi_record_close("rpc");
  71. vStreamBufferDelete(output_stream);
  72. ++command_id;
  73. output_stream = NULL;
  74. rpc = NULL;
  75. session = NULL;
  76. }
  77. static void clean_directory(Storage* fs_api, const char* clean_dir) {
  78. furi_assert(fs_api);
  79. furi_assert(clean_dir);
  80. File* dir = storage_file_alloc(fs_api);
  81. if(storage_dir_open(dir, clean_dir)) {
  82. FileInfo fileinfo;
  83. char* name = furi_alloc(MAX_NAME_LENGTH + 1);
  84. while(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  85. char* fullname = furi_alloc(strlen(clean_dir) + strlen(name) + 1 + 1);
  86. sprintf(fullname, "%s/%s", clean_dir, name);
  87. if(fileinfo.flags & FSF_DIRECTORY) {
  88. clean_directory(fs_api, fullname);
  89. }
  90. storage_common_remove(fs_api, fullname);
  91. free(fullname);
  92. }
  93. free(name);
  94. } else {
  95. FS_Error error = storage_common_mkdir(fs_api, clean_dir);
  96. furi_assert(error == FSE_OK);
  97. }
  98. storage_dir_close(dir);
  99. storage_file_free(dir);
  100. }
  101. static void test_rpc_print_message_list(MsgList_t msg_list) {
  102. #if DEBUG_PRINT
  103. MsgList_reverse(msg_list);
  104. for
  105. M_EACH(msg, msg_list, MsgList_t) {
  106. rpc_print_message(msg);
  107. }
  108. MsgList_reverse(msg_list);
  109. #endif
  110. }
  111. static PB_CommandStatus test_rpc_storage_get_file_error(File* file) {
  112. FS_Error fs_error = storage_file_get_error(file);
  113. PB_CommandStatus pb_error;
  114. switch(fs_error) {
  115. case FSE_OK:
  116. pb_error = PB_CommandStatus_OK;
  117. break;
  118. case FSE_INVALID_NAME:
  119. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_NAME;
  120. break;
  121. case FSE_INVALID_PARAMETER:
  122. pb_error = PB_CommandStatus_ERROR_STORAGE_INVALID_PARAMETER;
  123. break;
  124. case FSE_INTERNAL:
  125. pb_error = PB_CommandStatus_ERROR_STORAGE_INTERNAL;
  126. break;
  127. case FSE_ALREADY_OPEN:
  128. pb_error = PB_CommandStatus_ERROR_STORAGE_ALREADY_OPEN;
  129. break;
  130. case FSE_DENIED:
  131. pb_error = PB_CommandStatus_ERROR_STORAGE_DENIED;
  132. break;
  133. case FSE_EXIST:
  134. pb_error = PB_CommandStatus_ERROR_STORAGE_EXIST;
  135. break;
  136. case FSE_NOT_EXIST:
  137. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_EXIST;
  138. break;
  139. case FSE_NOT_READY:
  140. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_READY;
  141. break;
  142. case FSE_NOT_IMPLEMENTED:
  143. pb_error = PB_CommandStatus_ERROR_STORAGE_NOT_IMPLEMENTED;
  144. break;
  145. default:
  146. pb_error = PB_CommandStatus_ERROR;
  147. break;
  148. }
  149. return pb_error;
  150. }
  151. static void output_bytes_callback(void* ctx, uint8_t* got_bytes, size_t got_size) {
  152. StreamBufferHandle_t stream_buffer = ctx;
  153. size_t bytes_sent = xStreamBufferSend(stream_buffer, got_bytes, got_size, osWaitForever);
  154. furi_assert(bytes_sent == got_size);
  155. }
  156. static void test_rpc_add_ping_to_list(MsgList_t msg_list, bool request, uint32_t command_id) {
  157. PB_Main* response = MsgList_push_new(msg_list);
  158. response->command_id = command_id;
  159. response->command_status = PB_CommandStatus_OK;
  160. response->cb_content.funcs.encode = NULL;
  161. response->has_next = false;
  162. response->which_content = (request == PING_REQUEST) ? PB_Main_ping_request_tag :
  163. PB_Main_ping_response_tag;
  164. }
  165. static void test_rpc_create_simple_message(
  166. PB_Main* message,
  167. uint16_t tag,
  168. const char* str,
  169. uint32_t command_id) {
  170. furi_assert(message);
  171. furi_assert(str);
  172. char* str_copy = furi_alloc(strlen(str) + 1);
  173. strcpy(str_copy, str);
  174. message->command_id = command_id;
  175. message->command_status = PB_CommandStatus_OK;
  176. message->cb_content.funcs.encode = NULL;
  177. message->which_content = tag;
  178. message->has_next = false;
  179. switch(tag) {
  180. case PB_Main_storage_list_request_tag:
  181. message->content.storage_list_request.path = str_copy;
  182. break;
  183. case PB_Main_storage_mkdir_request_tag:
  184. message->content.storage_mkdir_request.path = str_copy;
  185. break;
  186. case PB_Main_storage_read_request_tag:
  187. message->content.storage_read_request.path = str_copy;
  188. break;
  189. case PB_Main_storage_delete_request_tag:
  190. message->content.storage_delete_request.path = str_copy;
  191. break;
  192. case PB_Main_storage_md5sum_request_tag:
  193. message->content.storage_md5sum_request.path = str_copy;
  194. break;
  195. case PB_Main_storage_md5sum_response_tag: {
  196. char* md5sum = message->content.storage_md5sum_response.md5sum;
  197. size_t md5sum_size = sizeof(message->content.storage_md5sum_response.md5sum);
  198. furi_assert((strlen(str) + 1) <= md5sum_size);
  199. memcpy(md5sum, str_copy, md5sum_size);
  200. free(str_copy);
  201. break;
  202. }
  203. default:
  204. furi_assert(0);
  205. break;
  206. }
  207. }
  208. static void test_rpc_add_read_or_write_to_list(
  209. MsgList_t msg_list,
  210. bool write,
  211. const char* path,
  212. const uint8_t* pattern,
  213. size_t pattern_size,
  214. size_t pattern_repeats,
  215. uint32_t command_id) {
  216. furi_assert(pattern_repeats > 0);
  217. do {
  218. PB_Main* request = MsgList_push_new(msg_list);
  219. PB_Storage_File* msg_file = NULL;
  220. request->command_id = command_id;
  221. request->command_status = PB_CommandStatus_OK;
  222. if(write == WRITE_REQUEST) {
  223. size_t path_size = strlen(path) + 1;
  224. request->content.storage_write_request.path = furi_alloc(path_size);
  225. strncpy(request->content.storage_write_request.path, path, path_size);
  226. request->which_content = PB_Main_storage_write_request_tag;
  227. request->content.storage_write_request.has_file = true;
  228. msg_file = &request->content.storage_write_request.file;
  229. } else {
  230. request->which_content = PB_Main_storage_read_response_tag;
  231. request->content.storage_read_response.has_file = true;
  232. msg_file = &request->content.storage_read_response.file;
  233. }
  234. msg_file->data = furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(pattern_size));
  235. msg_file->data->size = pattern_size;
  236. memcpy(msg_file->data->bytes, pattern, pattern_size);
  237. --pattern_repeats;
  238. request->has_next = (pattern_repeats > 0);
  239. } while(pattern_repeats);
  240. }
  241. static void test_rpc_encode_and_feed_one(PB_Main* request) {
  242. furi_assert(request);
  243. pb_ostream_t ostream = PB_OSTREAM_SIZING;
  244. bool result = pb_encode_ex(&ostream, &PB_Main_msg, request, PB_ENCODE_DELIMITED);
  245. furi_check(result && ostream.bytes_written);
  246. uint8_t* buffer = furi_alloc(ostream.bytes_written);
  247. ostream = pb_ostream_from_buffer(buffer, ostream.bytes_written);
  248. pb_encode_ex(&ostream, &PB_Main_msg, request, PB_ENCODE_DELIMITED);
  249. size_t bytes_left = ostream.bytes_written;
  250. uint8_t* buffer_ptr = buffer;
  251. do {
  252. size_t bytes_sent = rpc_feed_bytes(session, buffer_ptr, bytes_left, 1000);
  253. mu_check(bytes_sent > 0);
  254. bytes_left -= bytes_sent;
  255. buffer_ptr += bytes_sent;
  256. } while(bytes_left);
  257. free(buffer);
  258. pb_release(&PB_Main_msg, request);
  259. }
  260. static void test_rpc_encode_and_feed(MsgList_t msg_list) {
  261. MsgList_reverse(msg_list);
  262. for
  263. M_EACH(request, msg_list, MsgList_t) {
  264. test_rpc_encode_and_feed_one(request);
  265. }
  266. MsgList_reverse(msg_list);
  267. }
  268. static void
  269. test_rpc_compare_file(PB_Storage_File* result_msg_file, PB_Storage_File* expected_msg_file) {
  270. mu_check(!result_msg_file->name == !expected_msg_file->name);
  271. if(result_msg_file->name) {
  272. mu_check(!strcmp(result_msg_file->name, expected_msg_file->name));
  273. }
  274. mu_check(result_msg_file->size == expected_msg_file->size);
  275. mu_check(result_msg_file->type == expected_msg_file->type);
  276. mu_check(!result_msg_file->data == !expected_msg_file->data);
  277. mu_check(result_msg_file->data->size == expected_msg_file->data->size);
  278. for(int i = 0; i < result_msg_file->data->size; ++i) {
  279. mu_check(result_msg_file->data->bytes[i] == expected_msg_file->data->bytes[i]);
  280. }
  281. }
  282. static void test_rpc_compare_messages(PB_Main* result, PB_Main* expected) {
  283. mu_check(result->command_id == expected->command_id);
  284. mu_check(result->command_status == expected->command_status);
  285. mu_check(result->has_next == expected->has_next);
  286. mu_check(result->which_content == expected->which_content);
  287. if(result->command_status != PB_CommandStatus_OK) {
  288. mu_check(result->which_content == PB_Main_empty_tag);
  289. }
  290. switch(result->which_content) {
  291. case PB_Main_empty_tag:
  292. case PB_Main_ping_response_tag:
  293. /* nothing to check */
  294. break;
  295. case PB_Main_ping_request_tag:
  296. case PB_Main_storage_list_request_tag:
  297. case PB_Main_storage_read_request_tag:
  298. case PB_Main_storage_write_request_tag:
  299. case PB_Main_storage_delete_request_tag:
  300. case PB_Main_storage_mkdir_request_tag:
  301. case PB_Main_storage_md5sum_request_tag:
  302. /* rpc doesn't send it */
  303. mu_check(0);
  304. break;
  305. case PB_Main_storage_read_response_tag: {
  306. bool result_has_msg_file = result->content.storage_read_response.has_file;
  307. bool expected_has_msg_file = expected->content.storage_read_response.has_file;
  308. mu_check(result_has_msg_file == expected_has_msg_file);
  309. if(result_has_msg_file) {
  310. PB_Storage_File* result_msg_file = &result->content.storage_read_response.file;
  311. PB_Storage_File* expected_msg_file = &expected->content.storage_read_response.file;
  312. test_rpc_compare_file(result_msg_file, expected_msg_file);
  313. } else {
  314. mu_check(0);
  315. }
  316. } break;
  317. case PB_Main_storage_list_response_tag: {
  318. size_t expected_msg_files = expected->content.storage_list_response.file_count;
  319. size_t result_msg_files = result->content.storage_list_response.file_count;
  320. mu_check(result_msg_files == expected_msg_files);
  321. for(int i = 0; i < expected_msg_files; ++i) {
  322. PB_Storage_File* result_msg_file = &result->content.storage_list_response.file[i];
  323. PB_Storage_File* expected_msg_file = &expected->content.storage_list_response.file[i];
  324. test_rpc_compare_file(result_msg_file, expected_msg_file);
  325. }
  326. break;
  327. }
  328. case PB_Main_storage_md5sum_response_tag: {
  329. char* result_md5sum = result->content.storage_md5sum_response.md5sum;
  330. char* expected_md5sum = expected->content.storage_md5sum_response.md5sum;
  331. mu_check(!strcmp(result_md5sum, expected_md5sum));
  332. break;
  333. }
  334. default:
  335. furi_assert(0);
  336. break;
  337. }
  338. }
  339. static bool test_rpc_pb_stream_read(pb_istream_t* istream, pb_byte_t* buf, size_t count) {
  340. StreamBufferHandle_t stream_buffer = istream->state;
  341. size_t bytes_received = 0;
  342. bytes_received = xStreamBufferReceive(stream_buffer, buf, count, MAX_RECEIVE_OUTPUT_TIMEOUT);
  343. return (count == bytes_received);
  344. }
  345. static void test_rpc_storage_list_create_expected_list(
  346. MsgList_t msg_list,
  347. const char* path,
  348. uint32_t command_id) {
  349. Storage* fs_api = furi_record_open("storage");
  350. File* dir = storage_file_alloc(fs_api);
  351. PB_Main response = {
  352. .command_id = command_id,
  353. .has_next = false,
  354. .which_content = PB_Main_storage_list_request_tag,
  355. /* other fields (e.g. msg_files ptrs) explicitly initialized by 0 */
  356. };
  357. PB_Storage_ListResponse* list = &response.content.storage_list_response;
  358. response.which_content = PB_Main_storage_list_response_tag;
  359. bool finish = false;
  360. int i = 0;
  361. if(storage_dir_open(dir, path)) {
  362. response.command_status = PB_CommandStatus_OK;
  363. } else {
  364. response.command_status = test_rpc_storage_get_file_error(dir);
  365. response.which_content = PB_Main_empty_tag;
  366. finish = true;
  367. }
  368. while(!finish) {
  369. FileInfo fileinfo;
  370. char* name = furi_alloc(MAX_NAME_LENGTH + 1);
  371. if(storage_dir_read(dir, &fileinfo, name, MAX_NAME_LENGTH)) {
  372. if(i == COUNT_OF(list->file)) {
  373. list->file_count = i;
  374. response.has_next = true;
  375. MsgList_push_back(msg_list, response);
  376. i = 0;
  377. }
  378. list->file[i].type = (fileinfo.flags & FSF_DIRECTORY) ? PB_Storage_File_FileType_DIR :
  379. PB_Storage_File_FileType_FILE;
  380. list->file[i].size = fileinfo.size;
  381. list->file[i].data = NULL;
  382. /* memory free inside rpc_encode_and_send() -> pb_release() */
  383. list->file[i].name = name;
  384. ++i;
  385. } else {
  386. finish = true;
  387. free(name);
  388. }
  389. }
  390. list->file_count = i;
  391. response.has_next = false;
  392. MsgList_push_back(msg_list, response);
  393. storage_dir_close(dir);
  394. storage_file_free(dir);
  395. furi_record_close("storage");
  396. }
  397. static void test_rpc_decode_and_compare(MsgList_t expected_msg_list) {
  398. furi_assert(!MsgList_empty_p(expected_msg_list));
  399. pb_istream_t istream = {
  400. .callback = test_rpc_pb_stream_read,
  401. .state = output_stream,
  402. .errmsg = NULL,
  403. .bytes_left = 0x7FFFFFFF,
  404. };
  405. /* other fields explicitly initialized by 0 */
  406. PB_Main result = {.cb_content.funcs.decode = NULL};
  407. /* mlib adds msg_files into start of list, so reverse it */
  408. MsgList_reverse(expected_msg_list);
  409. for
  410. M_EACH(expected_msg, expected_msg_list, MsgList_t) {
  411. if(!pb_decode_ex(&istream, &PB_Main_msg, &result, PB_DECODE_DELIMITED)) {
  412. mu_assert(
  413. 0,
  414. "not all expected messages decoded (maybe increase MAX_RECEIVE_OUTPUT_TIMEOUT)");
  415. break;
  416. }
  417. test_rpc_compare_messages(&result, expected_msg);
  418. pb_release(&PB_Main_msg, &result);
  419. }
  420. MsgList_reverse(expected_msg_list);
  421. }
  422. static void test_rpc_free_msg_list(MsgList_t msg_list) {
  423. for
  424. M_EACH(it, msg_list, MsgList_t) {
  425. pb_release(&PB_Main_msg, it);
  426. }
  427. MsgList_clear(msg_list);
  428. }
  429. static void test_rpc_storage_list_run(const char* path, uint32_t command_id) {
  430. PB_Main request;
  431. MsgList_t expected_msg_list;
  432. MsgList_init(expected_msg_list);
  433. test_rpc_create_simple_message(&request, PB_Main_storage_list_request_tag, path, command_id);
  434. test_rpc_storage_list_create_expected_list(expected_msg_list, path, command_id);
  435. test_rpc_encode_and_feed_one(&request);
  436. test_rpc_decode_and_compare(expected_msg_list);
  437. pb_release(&PB_Main_msg, &request);
  438. test_rpc_free_msg_list(expected_msg_list);
  439. }
  440. MU_TEST(test_storage_list) {
  441. test_rpc_storage_list_run("/ext/nfc", ++command_id);
  442. test_rpc_storage_list_run("/int", ++command_id);
  443. test_rpc_storage_list_run("/ext", ++command_id);
  444. test_rpc_storage_list_run("/ext/irda", ++command_id);
  445. test_rpc_storage_list_run("/ext/ibutton", ++command_id);
  446. test_rpc_storage_list_run("/ext/lfrfid", ++command_id);
  447. test_rpc_storage_list_run("error_path", ++command_id);
  448. }
  449. static void
  450. test_rpc_add_empty_to_list(MsgList_t msg_list, PB_CommandStatus status, uint32_t command_id) {
  451. PB_Main* response = MsgList_push_new(msg_list);
  452. response->command_id = command_id;
  453. response->command_status = status;
  454. response->cb_content.funcs.encode = NULL;
  455. response->has_next = false;
  456. response->which_content = PB_Main_empty_tag;
  457. }
  458. static void test_rpc_add_read_to_list_by_reading_real_file(
  459. MsgList_t msg_list,
  460. const char* path,
  461. uint32_t command_id) {
  462. furi_assert(MsgList_empty_p(msg_list));
  463. Storage* fs_api = furi_record_open("storage");
  464. File* file = storage_file_alloc(fs_api);
  465. bool result = false;
  466. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  467. size_t size_left = storage_file_size(file);
  468. do {
  469. PB_Main* response = MsgList_push_new(msg_list);
  470. response->command_id = command_id;
  471. response->command_status = PB_CommandStatus_OK;
  472. response->has_next = false;
  473. response->which_content = PB_Main_storage_read_response_tag;
  474. response->content.storage_read_response.has_file = true;
  475. response->content.storage_read_response.file.data =
  476. furi_alloc(PB_BYTES_ARRAY_T_ALLOCSIZE(MIN(size_left, MAX_DATA_SIZE)));
  477. uint8_t* buffer = response->content.storage_read_response.file.data->bytes;
  478. uint16_t* read_size_msg = &response->content.storage_read_response.file.data->size;
  479. size_t read_size = MIN(size_left, MAX_DATA_SIZE);
  480. *read_size_msg = storage_file_read(file, buffer, read_size);
  481. size_left -= read_size;
  482. result = (*read_size_msg == read_size);
  483. if(result) {
  484. response->has_next = (size_left > 0);
  485. }
  486. } while((size_left != 0) && result);
  487. if(!result) {
  488. test_rpc_add_empty_to_list(
  489. msg_list, test_rpc_storage_get_file_error(file), command_id);
  490. }
  491. } else {
  492. test_rpc_add_empty_to_list(msg_list, test_rpc_storage_get_file_error(file), command_id);
  493. }
  494. storage_file_close(file);
  495. storage_file_free(file);
  496. furi_record_close("storage");
  497. }
  498. static void test_storage_read_run(const char* path, uint32_t command_id) {
  499. PB_Main request;
  500. MsgList_t expected_msg_list;
  501. MsgList_init(expected_msg_list);
  502. test_rpc_add_read_to_list_by_reading_real_file(expected_msg_list, path, command_id);
  503. test_rpc_create_simple_message(&request, PB_Main_storage_read_request_tag, path, command_id);
  504. test_rpc_encode_and_feed_one(&request);
  505. test_rpc_decode_and_compare(expected_msg_list);
  506. pb_release(&PB_Main_msg, &request);
  507. test_rpc_free_msg_list(expected_msg_list);
  508. }
  509. static void test_create_dir(const char* path) {
  510. Storage* fs_api = furi_record_open("storage");
  511. FS_Error error = storage_common_mkdir(fs_api, path);
  512. furi_assert((error == FSE_OK) || (error == FSE_EXIST));
  513. furi_record_close("storage");
  514. }
  515. static void test_create_file(const char* path, size_t size) {
  516. Storage* fs_api = furi_record_open("storage");
  517. File* file = storage_file_alloc(fs_api);
  518. if(storage_file_open(file, path, FSAM_WRITE, FSOM_CREATE_ALWAYS)) {
  519. uint8_t buf[128] = {0};
  520. for(int i = 0; i < sizeof(buf); ++i) {
  521. buf[i] = '0' + (i % 10);
  522. }
  523. while(size) {
  524. size_t written = storage_file_write(file, buf, MIN(size, sizeof(buf)));
  525. furi_assert(written);
  526. size -= written;
  527. }
  528. }
  529. storage_file_close(file);
  530. storage_file_free(file);
  531. furi_record_close("storage");
  532. }
  533. MU_TEST(test_storage_read) {
  534. test_create_file(TEST_DIR "empty.txt", 0);
  535. test_create_file(TEST_DIR "file1.txt", 1);
  536. test_create_file(TEST_DIR "file2.txt", MAX_DATA_SIZE);
  537. test_create_file(TEST_DIR "file3.txt", MAX_DATA_SIZE + 1);
  538. test_create_file(TEST_DIR "file4.txt", (MAX_DATA_SIZE * 2) + 1);
  539. test_storage_read_run(TEST_DIR "empty.txt", ++command_id);
  540. test_storage_read_run(TEST_DIR "file1.txt", ++command_id);
  541. test_storage_read_run(TEST_DIR "file2.txt", ++command_id);
  542. test_storage_read_run(TEST_DIR "file3.txt", ++command_id);
  543. test_storage_read_run(TEST_DIR "file4.txt", ++command_id);
  544. }
  545. static void test_storage_write_run(
  546. const char* path,
  547. size_t write_size,
  548. size_t write_count,
  549. uint32_t command_id,
  550. PB_CommandStatus status) {
  551. MsgList_t input_msg_list;
  552. MsgList_init(input_msg_list);
  553. MsgList_t expected_msg_list;
  554. MsgList_init(expected_msg_list);
  555. uint8_t* buf = furi_alloc(write_size);
  556. for(int i = 0; i < write_size; ++i) {
  557. buf[i] = '0' + (i % 10);
  558. }
  559. test_rpc_add_read_or_write_to_list(
  560. input_msg_list, WRITE_REQUEST, path, buf, write_size, write_count, command_id);
  561. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  562. test_rpc_encode_and_feed(input_msg_list);
  563. test_rpc_decode_and_compare(expected_msg_list);
  564. test_rpc_free_msg_list(input_msg_list);
  565. test_rpc_free_msg_list(expected_msg_list);
  566. free(buf);
  567. }
  568. static void test_storage_write_read_run(
  569. const char* path,
  570. const uint8_t* pattern,
  571. size_t pattern_size,
  572. size_t pattern_repeats,
  573. uint32_t* command_id) {
  574. MsgList_t input_msg_list;
  575. MsgList_init(input_msg_list);
  576. MsgList_t expected_msg_list;
  577. MsgList_init(expected_msg_list);
  578. test_rpc_add_read_or_write_to_list(
  579. input_msg_list, WRITE_REQUEST, path, pattern, pattern_size, pattern_repeats, ++*command_id);
  580. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, *command_id);
  581. test_rpc_create_simple_message(
  582. MsgList_push_raw(input_msg_list), PB_Main_storage_read_request_tag, path, ++*command_id);
  583. test_rpc_add_read_or_write_to_list(
  584. expected_msg_list,
  585. READ_RESPONSE,
  586. path,
  587. pattern,
  588. pattern_size,
  589. pattern_repeats,
  590. *command_id);
  591. test_rpc_print_message_list(input_msg_list);
  592. test_rpc_print_message_list(expected_msg_list);
  593. test_rpc_encode_and_feed(input_msg_list);
  594. test_rpc_decode_and_compare(expected_msg_list);
  595. test_rpc_free_msg_list(input_msg_list);
  596. test_rpc_free_msg_list(expected_msg_list);
  597. }
  598. MU_TEST(test_storage_write_read) {
  599. uint8_t pattern1[] = "abcdefgh";
  600. test_storage_write_read_run(TEST_DIR "test1.txt", pattern1, sizeof(pattern1), 1, &command_id);
  601. test_storage_write_read_run(TEST_DIR "test2.txt", pattern1, 1, 1, &command_id);
  602. test_storage_write_read_run(TEST_DIR "test3.txt", pattern1, 0, 1, &command_id);
  603. }
  604. MU_TEST(test_storage_write) {
  605. test_storage_write_run(
  606. TEST_DIR "afaefo/aefaef/aef/aef/test1.txt",
  607. 1,
  608. 1,
  609. ++command_id,
  610. PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  611. test_storage_write_run(TEST_DIR "test1.txt", 100, 1, ++command_id, PB_CommandStatus_OK);
  612. test_storage_write_run(TEST_DIR "test2.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  613. test_storage_write_run(TEST_DIR "test1.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  614. test_storage_write_run(TEST_DIR "test2.txt", 100, 3, ++command_id, PB_CommandStatus_OK);
  615. test_storage_write_run(
  616. TEST_DIR "afaefo/aefaef/aef/aef/test1.txt",
  617. 1,
  618. 1,
  619. ++command_id,
  620. PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  621. test_storage_write_run(TEST_DIR "test2.txt", 1, 50, ++command_id, PB_CommandStatus_OK);
  622. test_storage_write_run(TEST_DIR "test2.txt", 4096, 1, ++command_id, PB_CommandStatus_OK);
  623. }
  624. MU_TEST(test_storage_interrupt_continuous_same_system) {
  625. MsgList_t input_msg_list;
  626. MsgList_init(input_msg_list);
  627. MsgList_t expected_msg_list;
  628. MsgList_init(expected_msg_list);
  629. uint8_t pattern[16] = {0};
  630. test_rpc_add_read_or_write_to_list(
  631. input_msg_list,
  632. WRITE_REQUEST,
  633. TEST_DIR "test1.txt",
  634. pattern,
  635. sizeof(pattern),
  636. 3,
  637. command_id);
  638. /* replace last packet (has_next == false) with another command */
  639. PB_Main message_to_remove;
  640. MsgList_pop_back(&message_to_remove, input_msg_list);
  641. pb_release(&PB_Main_msg, &message_to_remove);
  642. test_rpc_create_simple_message(
  643. MsgList_push_new(input_msg_list),
  644. PB_Main_storage_mkdir_request_tag,
  645. TEST_DIR "dir1",
  646. command_id + 1);
  647. test_rpc_add_read_or_write_to_list(
  648. input_msg_list,
  649. WRITE_REQUEST,
  650. TEST_DIR "test2.txt",
  651. pattern,
  652. sizeof(pattern),
  653. 3,
  654. command_id);
  655. test_rpc_add_empty_to_list(
  656. expected_msg_list, PB_CommandStatus_ERROR_CONTINUOUS_COMMAND_INTERRUPTED, command_id);
  657. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id + 1);
  658. test_rpc_encode_and_feed(input_msg_list);
  659. test_rpc_decode_and_compare(expected_msg_list);
  660. test_rpc_free_msg_list(input_msg_list);
  661. test_rpc_free_msg_list(expected_msg_list);
  662. }
  663. MU_TEST(test_storage_interrupt_continuous_another_system) {
  664. MsgList_t input_msg_list;
  665. MsgList_init(input_msg_list);
  666. MsgList_t expected_msg_list;
  667. MsgList_init(expected_msg_list);
  668. uint8_t pattern[16] = {0};
  669. test_rpc_add_read_or_write_to_list(
  670. input_msg_list,
  671. WRITE_REQUEST,
  672. TEST_DIR "test1.txt",
  673. pattern,
  674. sizeof(pattern),
  675. 3,
  676. command_id);
  677. PB_Main message = {
  678. .command_id = command_id + 1,
  679. .command_status = PB_CommandStatus_OK,
  680. .cb_content.funcs.encode = NULL,
  681. .has_next = false,
  682. .which_content = PB_Main_ping_request_tag,
  683. };
  684. MsgList_it_t it;
  685. MsgList_it(it, input_msg_list);
  686. MsgList_next(it);
  687. MsgList_insert(input_msg_list, it, message);
  688. test_rpc_add_read_or_write_to_list(
  689. input_msg_list,
  690. WRITE_REQUEST,
  691. TEST_DIR "test2.txt",
  692. pattern,
  693. sizeof(pattern),
  694. 3,
  695. command_id + 2);
  696. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, command_id + 1);
  697. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id);
  698. test_rpc_add_empty_to_list(expected_msg_list, PB_CommandStatus_OK, command_id + 2);
  699. test_rpc_encode_and_feed(input_msg_list);
  700. test_rpc_decode_and_compare(expected_msg_list);
  701. test_rpc_free_msg_list(input_msg_list);
  702. test_rpc_free_msg_list(expected_msg_list);
  703. }
  704. static void test_storage_delete_run(const char* path, size_t command_id, PB_CommandStatus status) {
  705. PB_Main request;
  706. MsgList_t expected_msg_list;
  707. MsgList_init(expected_msg_list);
  708. test_rpc_create_simple_message(&request, PB_Main_storage_delete_request_tag, path, command_id);
  709. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  710. test_rpc_encode_and_feed_one(&request);
  711. test_rpc_decode_and_compare(expected_msg_list);
  712. pb_release(&PB_Main_msg, &request);
  713. test_rpc_free_msg_list(expected_msg_list);
  714. }
  715. MU_TEST(test_storage_delete) {
  716. test_create_file(TEST_DIR "empty.txt", 0);
  717. test_storage_delete_run(TEST_DIR "empty.txt", ++command_id, PB_CommandStatus_OK);
  718. test_storage_delete_run(
  719. TEST_DIR "empty.txt", ++command_id, PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  720. test_create_dir(TEST_DIR "dir1");
  721. test_storage_delete_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK);
  722. test_storage_delete_run(
  723. TEST_DIR "dir1", ++command_id, PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  724. }
  725. static void test_storage_mkdir_run(const char* path, size_t command_id, PB_CommandStatus status) {
  726. PB_Main request;
  727. MsgList_t expected_msg_list;
  728. MsgList_init(expected_msg_list);
  729. test_rpc_create_simple_message(&request, PB_Main_storage_mkdir_request_tag, path, command_id);
  730. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  731. test_rpc_encode_and_feed_one(&request);
  732. test_rpc_decode_and_compare(expected_msg_list);
  733. pb_release(&PB_Main_msg, &request);
  734. test_rpc_free_msg_list(expected_msg_list);
  735. }
  736. MU_TEST(test_storage_mkdir) {
  737. test_storage_mkdir_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK);
  738. test_storage_mkdir_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_ERROR_STORAGE_EXIST);
  739. test_create_dir(TEST_DIR "dir2");
  740. test_storage_mkdir_run(TEST_DIR "dir2", ++command_id, PB_CommandStatus_ERROR_STORAGE_EXIST);
  741. Storage* fs_api = furi_record_open("storage");
  742. FS_Error error = storage_common_remove(fs_api, TEST_DIR "dir1");
  743. furi_assert(error == FSE_OK);
  744. furi_record_close("storage");
  745. test_storage_mkdir_run(TEST_DIR "dir1", ++command_id, PB_CommandStatus_OK);
  746. }
  747. static void test_storage_calculate_md5sum(const char* path, char* md5sum) {
  748. Storage* api = furi_record_open("storage");
  749. File* file = storage_file_alloc(api);
  750. if(storage_file_open(file, path, FSAM_READ, FSOM_OPEN_EXISTING)) {
  751. const uint16_t once_read_size = 512;
  752. const uint8_t hash_size = MD5SUM_SIZE;
  753. uint8_t* data = malloc(once_read_size);
  754. uint8_t* hash = malloc(sizeof(uint8_t) * hash_size);
  755. md5_context* md5_ctx = malloc(sizeof(md5_context));
  756. md5_starts(md5_ctx);
  757. while(true) {
  758. uint16_t read_size = storage_file_read(file, data, once_read_size);
  759. if(read_size == 0) break;
  760. md5_update(md5_ctx, data, read_size);
  761. }
  762. md5_finish(md5_ctx, hash);
  763. free(md5_ctx);
  764. for(uint8_t i = 0; i < hash_size; i++) {
  765. md5sum += sprintf(md5sum, "%02x", hash[i]);
  766. }
  767. free(hash);
  768. free(data);
  769. } else {
  770. furi_assert(0);
  771. }
  772. storage_file_close(file);
  773. storage_file_free(file);
  774. furi_record_close("storage");
  775. }
  776. static void test_storage_md5sum_run(
  777. const char* path,
  778. uint32_t command_id,
  779. const char* md5sum,
  780. PB_CommandStatus status) {
  781. PB_Main request;
  782. MsgList_t expected_msg_list;
  783. MsgList_init(expected_msg_list);
  784. test_rpc_create_simple_message(&request, PB_Main_storage_md5sum_request_tag, path, command_id);
  785. if(status == PB_CommandStatus_OK) {
  786. PB_Main* response = MsgList_push_new(expected_msg_list);
  787. test_rpc_create_simple_message(
  788. response, PB_Main_storage_md5sum_response_tag, md5sum, command_id);
  789. response->command_status = status;
  790. } else {
  791. test_rpc_add_empty_to_list(expected_msg_list, status, command_id);
  792. }
  793. test_rpc_encode_and_feed_one(&request);
  794. test_rpc_decode_and_compare(expected_msg_list);
  795. pb_release(&PB_Main_msg, &request);
  796. test_rpc_free_msg_list(expected_msg_list);
  797. }
  798. MU_TEST(test_storage_md5sum) {
  799. char md5sum1[MD5SUM_SIZE * 2 + 1] = {0};
  800. char md5sum2[MD5SUM_SIZE * 2 + 1] = {0};
  801. char md5sum3[MD5SUM_SIZE * 2 + 1] = {0};
  802. test_storage_md5sum_run(
  803. TEST_DIR "test1.txt", ++command_id, "", PB_CommandStatus_ERROR_STORAGE_NOT_EXIST);
  804. test_create_file(TEST_DIR "file1.txt", 0);
  805. test_create_file(TEST_DIR "file2.txt", 1);
  806. test_create_file(TEST_DIR "file3.txt", 512);
  807. test_storage_calculate_md5sum(TEST_DIR "file1.txt", md5sum1);
  808. test_storage_calculate_md5sum(TEST_DIR "file2.txt", md5sum2);
  809. test_storage_calculate_md5sum(TEST_DIR "file3.txt", md5sum3);
  810. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  811. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  812. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  813. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  814. test_storage_md5sum_run(TEST_DIR "file3.txt", ++command_id, md5sum3, PB_CommandStatus_OK);
  815. test_storage_md5sum_run(TEST_DIR "file3.txt", ++command_id, md5sum3, PB_CommandStatus_OK);
  816. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  817. test_storage_md5sum_run(TEST_DIR "file3.txt", ++command_id, md5sum3, PB_CommandStatus_OK);
  818. test_storage_md5sum_run(TEST_DIR "file1.txt", ++command_id, md5sum1, PB_CommandStatus_OK);
  819. test_storage_md5sum_run(TEST_DIR "file2.txt", ++command_id, md5sum2, PB_CommandStatus_OK);
  820. }
  821. MU_TEST(test_ping) {
  822. MsgList_t input_msg_list;
  823. MsgList_init(input_msg_list);
  824. MsgList_t expected_msg_list;
  825. MsgList_init(expected_msg_list);
  826. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 0);
  827. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 1);
  828. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 0);
  829. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 500);
  830. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, (uint32_t)-1);
  831. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 700);
  832. test_rpc_add_ping_to_list(input_msg_list, PING_REQUEST, 1);
  833. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 0);
  834. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 1);
  835. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 0);
  836. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 500);
  837. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, (uint32_t)-1);
  838. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 700);
  839. test_rpc_add_ping_to_list(expected_msg_list, PING_RESPONSE, 1);
  840. test_rpc_encode_and_feed(input_msg_list);
  841. test_rpc_decode_and_compare(expected_msg_list);
  842. test_rpc_free_msg_list(input_msg_list);
  843. test_rpc_free_msg_list(expected_msg_list);
  844. }
  845. // TODO: 1) test for rubbish data
  846. // 2) test for unexpected end of packet
  847. // 3) test for one push of several packets
  848. // 4) test for fill buffer till end (great varint) and close connection
  849. MU_TEST_SUITE(test_rpc_status) {
  850. MU_SUITE_CONFIGURE(&test_rpc_storage_setup, &test_rpc_storage_teardown);
  851. MU_RUN_TEST(test_ping);
  852. }
  853. MU_TEST_SUITE(test_rpc_storage) {
  854. MU_SUITE_CONFIGURE(&test_rpc_storage_setup, &test_rpc_storage_teardown);
  855. MU_RUN_TEST(test_storage_list);
  856. MU_RUN_TEST(test_storage_read);
  857. MU_RUN_TEST(test_storage_write_read);
  858. MU_RUN_TEST(test_storage_write);
  859. MU_RUN_TEST(test_storage_delete);
  860. MU_RUN_TEST(test_storage_mkdir);
  861. MU_RUN_TEST(test_storage_md5sum);
  862. MU_RUN_TEST(test_storage_interrupt_continuous_same_system);
  863. MU_RUN_TEST(test_storage_interrupt_continuous_another_system);
  864. }
  865. int run_minunit_test_rpc() {
  866. MU_RUN_SUITE(test_rpc_storage);
  867. MU_RUN_SUITE(test_rpc_status);
  868. MU_REPORT();
  869. return MU_EXIT_CODE;
  870. }