JsonCommanderTest.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. #include <gmock/gmock.h>
  2. #include <gtest/gtest.h>
  3. #include "../include/JsonCommander.h"
  4. #include "FileManagerMock.h"
  5. namespace {
  6. /* Version tests */
  7. TEST(testVersion, Positive) {
  8. FileManagerMock fileManager;
  9. JsonCommander jsonCommander(fileManager);
  10. const std::string versionString = "0.2";
  11. Json::Value version;
  12. version["version"] = versionString;
  13. JsonCommander::Response response = jsonCommander.testVersion(version);
  14. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  15. EXPECT_TRUE(response.json["accept"].asBool());
  16. EXPECT_EQ(response.json["version"].asString(), versionString);
  17. }
  18. TEST(testVersion, Negative) {
  19. FileManagerMock fileManager;
  20. JsonCommander jsonCommander(fileManager);
  21. const std::string versionString = "0.1";
  22. Json::Value version;
  23. version["version"] = versionString;
  24. JsonCommander::Response response = jsonCommander.testVersion(version);
  25. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  26. EXPECT_FALSE(response.json["accept"].asBool());
  27. EXPECT_FALSE(response.json["version"].asString().compare(versionString) == 0);
  28. }
  29. /* Status tests */
  30. TEST(Status, Ok) {
  31. FileManagerMock fileManager;
  32. JsonCommander jsonCommander(fileManager);
  33. const std::string command = "status";
  34. Json::Value message;
  35. message["command"] = command;
  36. JsonCommander::Response response = jsonCommander.execute(message);
  37. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  38. EXPECT_EQ(response.json["command"].asString(), command);
  39. EXPECT_EQ(response.json["response"].asString(), "ok");
  40. }
  41. TEST(Status, Downloading) {
  42. FileManagerMock fileManager;
  43. JsonCommander jsonCommander(fileManager);
  44. const std::string command = "status";
  45. Json::Value message;
  46. message["command"] = command;
  47. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  48. JsonCommander::Response response = jsonCommander.execute(message);
  49. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  50. EXPECT_EQ(response.json["command"].asString(), command);
  51. EXPECT_EQ(response.json["response"].asString(), "download running");
  52. }
  53. TEST(Status, Uploading) {
  54. FileManagerMock fileManager;
  55. JsonCommander jsonCommander(fileManager);
  56. const std::string command = "status";
  57. Json::Value message;
  58. message["command"] = command;
  59. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  60. JsonCommander::Response response = jsonCommander.execute(message);
  61. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  62. EXPECT_EQ(response.json["command"].asString(), command);
  63. EXPECT_EQ(response.json["response"].asString(), "upload running");
  64. }
  65. TEST(Status, UploadingAndDownloading) {
  66. FileManagerMock fileManager;
  67. JsonCommander jsonCommander(fileManager);
  68. const std::string command = "status";
  69. Json::Value message;
  70. message["command"] = command;
  71. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  72. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  73. JsonCommander::Response response = jsonCommander.execute(message);
  74. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  75. EXPECT_EQ(response.json["command"].asString(), command);
  76. EXPECT_EQ(response.json["response"].asString(), "download and upload running");
  77. }
  78. /* Close tests */
  79. TEST(Close, Close) {
  80. FileManagerMock fileManager;
  81. JsonCommander jsonCommander(fileManager);
  82. const std::string command = "close";
  83. Json::Value message;
  84. message["command"] = command;
  85. JsonCommander::Response response = jsonCommander.execute(message);
  86. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  87. EXPECT_EQ(response.json["command"].asString(), command);
  88. EXPECT_EQ(response.json["response"].asString(), "bye");
  89. }
  90. /* Put tests */
  91. TEST(Put, Positive) {
  92. FileManagerMock fileManager;
  93. JsonCommander jsonCommander(fileManager);
  94. const std::string command = "put";
  95. const std::string filename = "cool.txt";
  96. Json::Value message;
  97. message["command"] = command;
  98. message["file"] = filename;
  99. message["size"] = 1337;
  100. message["chunks"] = 1;
  101. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(true));
  102. JsonCommander::Response response = jsonCommander.execute(message);
  103. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  104. EXPECT_EQ(response.json["command"].asString(), command);
  105. EXPECT_TRUE(response.json["accept"].asBool());
  106. EXPECT_EQ(response.json["file"].asString(), filename);
  107. EXPECT_EQ(response.json["error"].asString(), "");
  108. }
  109. TEST(Put, Negative) {
  110. FileManagerMock fileManager;
  111. JsonCommander jsonCommander(fileManager);
  112. const std::string command = "put";
  113. const std::string filename = "cool.txt";
  114. Json::Value message;
  115. message["command"] = command;
  116. message["file"] = filename;
  117. message["size"] = 1337;
  118. message["chunks"] = 1;
  119. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(false));
  120. JsonCommander::Response response = jsonCommander.execute(message);
  121. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  122. EXPECT_EQ(response.json["command"].asString(), command);
  123. EXPECT_FALSE(response.json["accept"].asBool());
  124. EXPECT_EQ(response.json["file"].asString(), filename);
  125. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  126. }
  127. /* Putdata tests */
  128. TEST(Putdata, Positive) {
  129. FileManagerMock fileManager;
  130. JsonCommander jsonCommander(fileManager);
  131. /* start with put */
  132. std::string command = "put";
  133. const std::string filename = "cool.txt";
  134. Json::Value message;
  135. message["command"] = command;
  136. message["file"] = filename;
  137. message["size"] = 1337;
  138. const int chunks = 3;
  139. message["chunks"] = chunks;
  140. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(true));
  141. JsonCommander::Response response = jsonCommander.execute(message);
  142. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  143. EXPECT_EQ(response.json["command"].asString(), command);
  144. EXPECT_TRUE(response.json["accept"].asBool());
  145. EXPECT_EQ(response.json["file"].asString(), filename);
  146. EXPECT_EQ(response.json["error"].asString(), "");
  147. /* putdata */
  148. command = "putdata";
  149. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  150. ON_CALL(fileManager, getPutBaseFileName()).WillByDefault(testing::Return(filename));
  151. for (int remaining = chunks - 1; remaining >= 0; remaining--) {
  152. message = Json::Value();
  153. message["command"] = command;
  154. message["file"] = filename;
  155. message["data"] = "MTMzNw==";
  156. message["remaining"] = remaining;
  157. message["cancel"] = false;
  158. response = jsonCommander.execute(message);
  159. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  160. EXPECT_EQ(response.json["command"].asString(), command);
  161. EXPECT_FALSE(response.json["cancel"].asBool());
  162. EXPECT_EQ(response.json["received"].asInt(), remaining);
  163. EXPECT_EQ(response.json["file"].asString(), filename);
  164. EXPECT_EQ(response.json["error"].asString(), "");
  165. }
  166. }
  167. TEST(Putdata, Cancel) {
  168. FileManagerMock fileManager;
  169. JsonCommander jsonCommander(fileManager);
  170. /* start with put */
  171. std::string command = "put";
  172. const std::string filename = "cool.txt";
  173. Json::Value message;
  174. message["command"] = command;
  175. message["file"] = filename;
  176. message["size"] = 1337;
  177. const int chunks = 3;
  178. message["chunks"] = chunks;
  179. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(true));
  180. JsonCommander::Response response = jsonCommander.execute(message);
  181. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  182. EXPECT_EQ(response.json["command"].asString(), command);
  183. EXPECT_TRUE(response.json["accept"].asBool());
  184. EXPECT_EQ(response.json["file"].asString(), filename);
  185. EXPECT_EQ(response.json["error"].asString(), "");
  186. /* putdata */
  187. command = "putdata";
  188. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  189. ON_CALL(fileManager, getPutBaseFileName()).WillByDefault(testing::Return(filename));
  190. int remaining = chunks - 1;
  191. message = Json::Value();
  192. message["command"] = command;
  193. message["file"] = filename;
  194. message["data"] = "MTMzNw==";
  195. message["remaining"] = remaining;
  196. message["cancel"] = false;
  197. response = jsonCommander.execute(message);
  198. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  199. EXPECT_EQ(response.json["command"].asString(), command);
  200. EXPECT_FALSE(response.json["cancel"].asBool());
  201. EXPECT_EQ(response.json["received"].asInt(), remaining);
  202. EXPECT_EQ(response.json["file"].asString(), filename);
  203. EXPECT_EQ(response.json["error"].asString(), "");
  204. // cancel transfer
  205. message = Json::Value();
  206. message["command"] = command;
  207. message["file"] = filename;
  208. message["data"] = "MTMzNw==";
  209. message["remaining"] = --remaining;
  210. message["cancel"] = true;
  211. response = jsonCommander.execute(message);
  212. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  213. EXPECT_EQ(response.json["command"].asString(), command);
  214. EXPECT_TRUE(response.json["cancel"].asBool());
  215. EXPECT_EQ(response.json["received"].asInt(), remaining);
  216. EXPECT_EQ(response.json["file"].asString(), filename);
  217. EXPECT_EQ(response.json["error"].asString(), "");
  218. }
  219. TEST(Putdata, WrongRemaining) {
  220. FileManagerMock fileManager;
  221. JsonCommander jsonCommander(fileManager);
  222. /* start with put */
  223. std::string command = "put";
  224. const std::string filename = "cool.txt";
  225. Json::Value message;
  226. message["command"] = command;
  227. message["file"] = filename;
  228. message["size"] = 1337;
  229. const int chunks = 3;
  230. message["chunks"] = chunks;
  231. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(true));
  232. JsonCommander::Response response = jsonCommander.execute(message);
  233. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  234. EXPECT_EQ(response.json["command"].asString(), command);
  235. EXPECT_TRUE(response.json["accept"].asBool());
  236. EXPECT_EQ(response.json["file"].asString(), filename);
  237. EXPECT_EQ(response.json["error"].asString(), "");
  238. /* putdata */
  239. command = "putdata";
  240. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  241. ON_CALL(fileManager, getPutBaseFileName()).WillByDefault(testing::Return(filename));
  242. int remaining = chunks - 1;
  243. message = Json::Value();
  244. message["command"] = command;
  245. message["file"] = filename;
  246. message["data"] = "MTMzNw==";
  247. message["remaining"] = remaining;
  248. message["cancel"] = false;
  249. response = jsonCommander.execute(message);
  250. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  251. EXPECT_EQ(response.json["command"].asString(), command);
  252. EXPECT_FALSE(response.json["cancel"].asBool());
  253. EXPECT_EQ(response.json["received"].asInt(), remaining);
  254. EXPECT_EQ(response.json["file"].asString(), filename);
  255. EXPECT_EQ(response.json["error"].asString(), "");
  256. message = Json::Value();
  257. // skip remaining=1 and provoke an error
  258. remaining = 0;
  259. message = Json::Value();
  260. message["command"] = command;
  261. message["file"] = filename;
  262. message["data"] = "MTMzNw==";
  263. message["remaining"] = remaining;
  264. message["cancel"] = false;
  265. response = jsonCommander.execute(message);
  266. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  267. EXPECT_EQ(response.json["command"].asString(), command);
  268. EXPECT_TRUE(response.json["cancel"].asBool());
  269. EXPECT_EQ(response.json["received"].asInt(), remaining);
  270. EXPECT_EQ(response.json["file"].asString(), filename);
  271. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  272. }
  273. /* Get tests */
  274. TEST(Get, Positive) {
  275. FileManagerMock fileManager;
  276. JsonCommander jsonCommander(fileManager);
  277. const std::string command = "get";
  278. const std::string filename = "cool.txt";
  279. Json::Value message;
  280. message["command"] = command;
  281. message["file"] = filename;
  282. const int chunks = 3;
  283. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  284. JsonCommander::Response response = jsonCommander.execute(message);
  285. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  286. EXPECT_EQ(response.json["command"].asString(), command);
  287. EXPECT_TRUE(response.json["accept"].asBool());
  288. EXPECT_EQ(response.json["file"].asString(), filename);
  289. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  290. EXPECT_EQ(response.json["error"].asString(), "");
  291. }
  292. TEST(Get, Negative) {
  293. FileManagerMock fileManager;
  294. JsonCommander jsonCommander(fileManager);
  295. const std::string command = "get";
  296. const std::string filename = "cool.txt";
  297. Json::Value message;
  298. message["command"] = command;
  299. message["file"] = filename;
  300. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(false, -1)));
  301. JsonCommander::Response response = jsonCommander.execute(message);
  302. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  303. EXPECT_EQ(response.json["command"].asString(), command);
  304. EXPECT_FALSE(response.json["accept"].asBool());
  305. EXPECT_EQ(response.json["file"].asString(), filename);
  306. EXPECT_EQ(response.json["chunks"].asInt(), -1);
  307. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  308. }
  309. /* Getdata tests */
  310. TEST(Getdata, Positive) {
  311. FileManagerMock fileManager;
  312. JsonCommander jsonCommander(fileManager);
  313. std::string command = "get";
  314. const std::string filename = "cool.txt";
  315. Json::Value message;
  316. message["command"] = command;
  317. message["file"] = filename;
  318. const int chunks = 3;
  319. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  320. JsonCommander::Response response = jsonCommander.execute(message);
  321. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  322. EXPECT_EQ(response.json["command"].asString(), command);
  323. EXPECT_TRUE(response.json["accept"].asBool());
  324. EXPECT_EQ(response.json["file"].asString(), filename);
  325. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  326. EXPECT_EQ(response.json["error"].asString(), "");
  327. /* getdata */
  328. command = "getdata";
  329. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  330. ON_CALL(fileManager, getGetBaseFileName()).WillByDefault(testing::Return(filename));
  331. std::vector<char> data;
  332. data.push_back('1');
  333. data.push_back('3');
  334. data.push_back('3');
  335. data.push_back('7');
  336. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  337. for (int remaining = chunks - 1; remaining >= 0; remaining--) {
  338. message = Json::Value();
  339. message["command"] = command;
  340. message["file"] = filename;
  341. message["chunk"] = remaining;
  342. message["cancel"] = false;
  343. response = jsonCommander.execute(message);
  344. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  345. EXPECT_EQ(response.json["command"].asString(), command);
  346. EXPECT_FALSE(response.json["cancel"].asBool());
  347. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  348. EXPECT_EQ(response.json["file"].asString(), filename);
  349. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  350. EXPECT_EQ(response.json["error"].asString(), "");
  351. }
  352. }
  353. TEST(Getdata, Cancle) {
  354. FileManagerMock fileManager;
  355. JsonCommander jsonCommander(fileManager);
  356. std::string command = "get";
  357. const std::string filename = "cool.txt";
  358. Json::Value message;
  359. message["command"] = command;
  360. message["file"] = filename;
  361. const int chunks = 3;
  362. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  363. JsonCommander::Response response = jsonCommander.execute(message);
  364. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  365. EXPECT_EQ(response.json["command"].asString(), command);
  366. EXPECT_TRUE(response.json["accept"].asBool());
  367. EXPECT_EQ(response.json["file"].asString(), filename);
  368. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  369. EXPECT_EQ(response.json["error"].asString(), "");
  370. /* getdata */
  371. command = "getdata";
  372. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  373. ON_CALL(fileManager, getGetBaseFileName()).WillByDefault(testing::Return(filename));
  374. std::vector<char> data;
  375. data.push_back('1');
  376. data.push_back('3');
  377. data.push_back('3');
  378. data.push_back('7');
  379. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  380. int remaining = chunks - 1;
  381. message = Json::Value();
  382. message["command"] = command;
  383. message["file"] = filename;
  384. message["chunk"] = remaining;
  385. message["cancel"] = false;
  386. response = jsonCommander.execute(message);
  387. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  388. EXPECT_EQ(response.json["command"].asString(), command);
  389. EXPECT_FALSE(response.json["cancel"].asBool());
  390. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  391. EXPECT_EQ(response.json["file"].asString(), filename);
  392. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  393. EXPECT_EQ(response.json["error"].asString(), "");
  394. // set cancel to true
  395. message = Json::Value();
  396. message["command"] = command;
  397. message["file"] = filename;
  398. message["chunk"] = --remaining;
  399. message["cancel"] = true;
  400. response = jsonCommander.execute(message);
  401. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  402. EXPECT_EQ(response.json["command"].asString(), command);
  403. EXPECT_TRUE(response.json["cancel"].asBool());
  404. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  405. EXPECT_EQ(response.json["file"].asString(), filename);
  406. EXPECT_EQ(response.json["data"].asString(), "");
  407. EXPECT_EQ(response.json["error"].asString(), "");
  408. }
  409. TEST(Getdata, WrongChunk) {
  410. FileManagerMock fileManager;
  411. JsonCommander jsonCommander(fileManager);
  412. std::string command = "get";
  413. const std::string filename = "cool.txt";
  414. Json::Value message;
  415. message["command"] = command;
  416. message["file"] = filename;
  417. const int chunks = 3;
  418. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  419. JsonCommander::Response response = jsonCommander.execute(message);
  420. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  421. EXPECT_EQ(response.json["command"].asString(), command);
  422. EXPECT_TRUE(response.json["accept"].asBool());
  423. EXPECT_EQ(response.json["file"].asString(), filename);
  424. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  425. EXPECT_EQ(response.json["error"].asString(), "");
  426. /* getdata */
  427. command = "getdata";
  428. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  429. ON_CALL(fileManager, getGetBaseFileName()).WillByDefault(testing::Return(filename));
  430. std::vector<char> data;
  431. data.push_back('1');
  432. data.push_back('3');
  433. data.push_back('3');
  434. data.push_back('7');
  435. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  436. int remaining = chunks - 1;
  437. message = Json::Value();
  438. message["command"] = command;
  439. message["file"] = filename;
  440. message["chunk"] = remaining;
  441. message["cancel"] = false;
  442. response = jsonCommander.execute(message);
  443. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  444. EXPECT_EQ(response.json["command"].asString(), command);
  445. EXPECT_FALSE(response.json["cancel"].asBool());
  446. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  447. EXPECT_EQ(response.json["file"].asString(), filename);
  448. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  449. EXPECT_EQ(response.json["error"].asString(), "");
  450. // skip chunk=0
  451. remaining = 0;
  452. message = Json::Value();
  453. message["command"] = command;
  454. message["file"] = filename;
  455. message["chunk"] = remaining;
  456. message["cancel"] = false;
  457. response = jsonCommander.execute(message);
  458. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  459. EXPECT_EQ(response.json["command"].asString(), command);
  460. EXPECT_TRUE(response.json["cancel"].asBool());
  461. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  462. EXPECT_EQ(response.json["file"].asString(), filename);
  463. EXPECT_EQ(response.json["data"].asString(), "");
  464. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  465. }
  466. /* List tests */
  467. TEST(List, Positive) {
  468. FileManagerMock fileManager;
  469. JsonCommander jsonCommander(fileManager);
  470. const std::string command = "list";
  471. Json::Value message;
  472. message["command"] = command;
  473. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(1));
  474. EXPECT_CALL(fileManager, getListSize()).WillOnce(testing::Return(5));
  475. JsonCommander::Response response = jsonCommander.execute(message);
  476. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  477. EXPECT_EQ(response.json["command"].asString(), command);
  478. EXPECT_TRUE(response.json["accept"].asBool());
  479. EXPECT_EQ(response.json["chunks"].asInt(), 1);
  480. EXPECT_EQ(response.json["items"].asInt(), 5);
  481. EXPECT_EQ(response.json["error"].asString(), "");
  482. }
  483. TEST(List, Negative) {
  484. FileManagerMock fileManager;
  485. JsonCommander jsonCommander(fileManager);
  486. const std::string command = "list";
  487. Json::Value message;
  488. message["command"] = command;
  489. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(-1));
  490. JsonCommander::Response response = jsonCommander.execute(message);
  491. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  492. EXPECT_EQ(response.json["command"].asString(), command);
  493. EXPECT_FALSE(response.json["accept"].asBool());
  494. EXPECT_EQ(response.json["chunks"].asInt(), -1);
  495. EXPECT_EQ(response.json["items"].asInt(), -1);
  496. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  497. }
  498. TEST(List, EmptyList) {
  499. FileManagerMock fileManager;
  500. JsonCommander jsonCommander(fileManager);
  501. const std::string command = "list";
  502. Json::Value message;
  503. message["command"] = command;
  504. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(0));
  505. EXPECT_CALL(fileManager, getListSize()).WillOnce(testing::Return(0));
  506. JsonCommander::Response response = jsonCommander.execute(message);
  507. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  508. EXPECT_EQ(response.json["command"].asString(), command);
  509. EXPECT_TRUE(response.json["accept"].asBool());
  510. EXPECT_EQ(response.json["chunks"].asInt(), 0);
  511. EXPECT_EQ(response.json["items"].asInt(), 0);
  512. EXPECT_EQ(response.json["error"].asString(), "");
  513. }
  514. /* Listdata tests */
  515. void fillExampleFileList(std::vector<std::string> (&chunk)[3]) {
  516. chunk[0].push_back("file01.txt");
  517. chunk[0].push_back("bumdibumps");
  518. chunk[0].push_back("1");
  519. chunk[0].push_back("Ich habe Hunger.txt");
  520. chunk[0].push_back("answerIs42");
  521. chunk[0].push_back("123456789456115811");
  522. chunk[0].push_back("kek");
  523. chunk[1].push_back("1337");
  524. chunk[1].push_back("cats.png");
  525. chunk[1].push_back("more_cats.png");
  526. chunk[1].push_back("ugly dog.tiff");
  527. chunk[1].push_back("hello.txt");
  528. chunk[1].push_back("bye.exe");
  529. chunk[1].push_back("poster.pdf");
  530. chunk[2].push_back("headbang.gif");
  531. chunk[2].push_back("feelsbad.jpg");
  532. chunk[2].push_back("hack.s");
  533. chunk[2].push_back("SodiumChloride");
  534. chunk[2].push_back("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst"
  535. "uvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN"
  536. "OPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  537. }
  538. TEST(Listdata, Positive) {
  539. FileManagerMock fileManager;
  540. JsonCommander jsonCommander(fileManager);
  541. const std::string command = "listdata";
  542. const int chunks = 3;
  543. std::vector<std::string> chunk[chunks];
  544. fillExampleFileList(chunk);
  545. int remaining = chunks - 1;
  546. for (int k = 0; k < chunks; k++) {
  547. Json::Value message;
  548. message["command"] = command;
  549. message["chunk"] = remaining;
  550. message["cancel"] = false;
  551. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining + 1));
  552. EXPECT_CALL(fileManager, getNextChunkFromList()).WillOnce(testing::Return(chunk[k]));
  553. JsonCommander::Response response = jsonCommander.execute(message);
  554. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  555. EXPECT_EQ(response.json["command"].asString(), command);
  556. EXPECT_FALSE(response.json["cancel"].asBool());
  557. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  558. EXPECT_TRUE(response.json["names"].isArray());
  559. Json::Value array = response.json["names"];
  560. EXPECT_EQ(array.size(), chunk[k].size());
  561. for (int i = 0; i < 3; i++) {
  562. EXPECT_EQ(array[i].asString(), chunk[k][i]);
  563. }
  564. EXPECT_EQ(response.json["error"].asString(), "");
  565. }
  566. }
  567. TEST(Listdata, Cancel) {
  568. FileManagerMock fileManager;
  569. JsonCommander jsonCommander(fileManager);
  570. const std::string command = "listdata";
  571. const int chunks = 3;
  572. std::vector<std::string> chunk[chunks];
  573. fillExampleFileList(chunk);
  574. int remaining = chunks - 1;
  575. Json::Value message;
  576. message["command"] = command;
  577. message["chunk"] = remaining;
  578. message["cancel"] = false;
  579. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining + 1));
  580. EXPECT_CALL(fileManager, getNextChunkFromList()).WillOnce(testing::Return(chunk[0]));
  581. JsonCommander::Response response = jsonCommander.execute(message);
  582. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  583. EXPECT_EQ(response.json["command"].asString(), command);
  584. EXPECT_FALSE(response.json["cancel"].asBool());
  585. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  586. EXPECT_TRUE(response.json["names"].isArray());
  587. Json::Value array = response.json["names"];
  588. EXPECT_EQ(array.size(), chunk[0].size());
  589. for (int i = 0; i < 3; i++) {
  590. EXPECT_EQ(array[i].asString(), chunk[0][i]);
  591. }
  592. EXPECT_EQ(response.json["error"].asString(), "");
  593. message = Json::Value();
  594. message["command"] = command;
  595. message["chunk"] = remaining;
  596. message["cancel"] = true;
  597. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining + 1));
  598. response = jsonCommander.execute(message);
  599. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  600. EXPECT_EQ(response.json["command"].asString(), command);
  601. EXPECT_TRUE(response.json["cancel"].asBool());
  602. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  603. EXPECT_TRUE(response.json["names"].isArray());
  604. EXPECT_EQ(response.json["error"].asString(), "");
  605. }
  606. TEST(Listdata, WrongChunkNumber) {
  607. FileManagerMock fileManager;
  608. JsonCommander jsonCommander(fileManager);
  609. const std::string command = "listdata";
  610. const int chunks = 3;
  611. int remaining = chunks - 1;
  612. Json::Value message;
  613. message["command"] = command;
  614. message["chunk"] = remaining;
  615. message["cancel"] = false;
  616. // return smaller remaining
  617. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining));
  618. JsonCommander::Response response = jsonCommander.execute(message);
  619. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  620. EXPECT_EQ(response.json["command"].asString(), command);
  621. EXPECT_TRUE(response.json["cancel"].asBool());
  622. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  623. EXPECT_TRUE(response.json["names"].isArray());
  624. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  625. }
  626. TEST(Listdata, NoChunksToBeSend) {
  627. FileManagerMock fileManager;
  628. JsonCommander jsonCommander(fileManager);
  629. const std::string command = "listdata";
  630. const int chunks = 0;
  631. Json::Value message;
  632. message["command"] = command;
  633. message["chunk"] = 1;
  634. message["cancel"] = false;
  635. // return smaller remaining
  636. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(chunks));
  637. JsonCommander::Response response = jsonCommander.execute(message);
  638. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  639. EXPECT_EQ(response.json["command"].asString(), command);
  640. EXPECT_TRUE(response.json["cancel"].asBool());
  641. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  642. EXPECT_TRUE(response.json["names"].isArray());
  643. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  644. }
  645. TEST(Listdata, InvalidRequest) {
  646. FileManagerMock fileManager;
  647. JsonCommander jsonCommander(fileManager);
  648. const std::string command = "listdata";
  649. const int chunks = 3;
  650. Json::Value message;
  651. message["command"] = command;
  652. message["chunk"] = 1;
  653. // return smaller remaining
  654. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(chunks));
  655. JsonCommander::Response response = jsonCommander.execute(message);
  656. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  657. EXPECT_EQ(response.json["command"].asString(), command);
  658. EXPECT_TRUE(response.json["cancel"].asBool());
  659. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  660. EXPECT_TRUE(response.json["names"].isArray());
  661. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  662. }
  663. } // namespace