JsonCommanderTest.cpp 28 KB

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