rpc_test.c 40 KB

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