rpc_test.c 40 KB

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