JsonCommanderTest.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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. EXPECT_CALL(fileManager, openGetFile(testing::_, testing::_))
  292. .WillOnce(testing::DoAll(testing::SetArgReferee<1, int>(3),
  293. testing::Return(true)));
  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_TRUE(response.json["chunks"].asInt() > 0);
  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::_, testing::_))
  311. .WillOnce(testing::Return(false));
  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::_, testing::_))
  331. .WillOnce(testing::DoAll(testing::SetArgReferee<1, int>(chunks),
  332. testing::Return(true)));
  333. JsonCommander::Response response = jsonCommander.execute(message);
  334. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  335. EXPECT_EQ(response.json["command"].asString(), command);
  336. EXPECT_TRUE(response.json["accept"].asBool());
  337. EXPECT_EQ(response.json["file"].asString(), filename);
  338. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  339. EXPECT_EQ(response.json["error"].asString(), "");
  340. /* getdata */
  341. command = "getdata";
  342. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  343. ON_CALL(fileManager, getGetBaseFileName())
  344. .WillByDefault(testing::Return(filename));
  345. std::vector<char> data;
  346. data.push_back('1');
  347. data.push_back('3');
  348. data.push_back('3');
  349. data.push_back('7');
  350. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  351. for (int remaining = chunks - 1; remaining >= 0; remaining--) {
  352. message = Json::Value();
  353. message["command"] = command;
  354. message["file"] = filename;
  355. message["chunk"] = remaining;
  356. message["cancel"] = false;
  357. response = jsonCommander.execute(message);
  358. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  359. EXPECT_EQ(response.json["command"].asString(), command);
  360. EXPECT_FALSE(response.json["cancel"].asBool());
  361. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  362. EXPECT_EQ(response.json["file"].asString(), filename);
  363. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  364. EXPECT_EQ(response.json["error"].asString(), "");
  365. }
  366. }
  367. TEST(Getdata, Cancle) {
  368. FileManagerMock fileManager;
  369. JsonCommander jsonCommander(fileManager);
  370. std::string command = "get";
  371. const std::string filename = "cool.txt";
  372. Json::Value message;
  373. message["command"] = command;
  374. message["file"] = filename;
  375. const int chunks = 3;
  376. EXPECT_CALL(fileManager, openGetFile(testing::_, testing::_))
  377. .WillOnce(testing::DoAll(testing::SetArgReferee<1, int>(chunks),
  378. testing::Return(true)));
  379. JsonCommander::Response response = jsonCommander.execute(message);
  380. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  381. EXPECT_EQ(response.json["command"].asString(), command);
  382. EXPECT_TRUE(response.json["accept"].asBool());
  383. EXPECT_EQ(response.json["file"].asString(), filename);
  384. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  385. EXPECT_EQ(response.json["error"].asString(), "");
  386. /* getdata */
  387. command = "getdata";
  388. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  389. ON_CALL(fileManager, getGetBaseFileName())
  390. .WillByDefault(testing::Return(filename));
  391. std::vector<char> data;
  392. data.push_back('1');
  393. data.push_back('3');
  394. data.push_back('3');
  395. data.push_back('7');
  396. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  397. int remaining = chunks - 1;
  398. message = Json::Value();
  399. message["command"] = command;
  400. message["file"] = filename;
  401. message["chunk"] = remaining;
  402. message["cancel"] = false;
  403. response = jsonCommander.execute(message);
  404. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  405. EXPECT_EQ(response.json["command"].asString(), command);
  406. EXPECT_FALSE(response.json["cancel"].asBool());
  407. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  408. EXPECT_EQ(response.json["file"].asString(), filename);
  409. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  410. EXPECT_EQ(response.json["error"].asString(), "");
  411. // set cancel to true
  412. message = Json::Value();
  413. message["command"] = command;
  414. message["file"] = filename;
  415. message["chunk"] = --remaining;
  416. message["cancel"] = true;
  417. response = jsonCommander.execute(message);
  418. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  419. EXPECT_EQ(response.json["command"].asString(), command);
  420. EXPECT_TRUE(response.json["cancel"].asBool());
  421. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  422. EXPECT_EQ(response.json["file"].asString(), filename);
  423. EXPECT_EQ(response.json["data"].asString(), "");
  424. EXPECT_EQ(response.json["error"].asString(), "");
  425. }
  426. TEST(Getdata, WrongChunk) {
  427. FileManagerMock fileManager;
  428. JsonCommander jsonCommander(fileManager);
  429. std::string command = "get";
  430. const std::string filename = "cool.txt";
  431. Json::Value message;
  432. message["command"] = command;
  433. message["file"] = filename;
  434. const int chunks = 3;
  435. EXPECT_CALL(fileManager, openGetFile(testing::_, testing::_))
  436. .WillOnce(testing::DoAll(testing::SetArgReferee<1, int>(chunks),
  437. testing::Return(true)));
  438. JsonCommander::Response response = jsonCommander.execute(message);
  439. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  440. EXPECT_EQ(response.json["command"].asString(), command);
  441. EXPECT_TRUE(response.json["accept"].asBool());
  442. EXPECT_EQ(response.json["file"].asString(), filename);
  443. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  444. EXPECT_EQ(response.json["error"].asString(), "");
  445. /* getdata */
  446. command = "getdata";
  447. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  448. ON_CALL(fileManager, getGetBaseFileName())
  449. .WillByDefault(testing::Return(filename));
  450. std::vector<char> data;
  451. data.push_back('1');
  452. data.push_back('3');
  453. data.push_back('3');
  454. data.push_back('7');
  455. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  456. int remaining = chunks - 1;
  457. message = Json::Value();
  458. message["command"] = command;
  459. message["file"] = filename;
  460. message["chunk"] = remaining;
  461. message["cancel"] = false;
  462. response = jsonCommander.execute(message);
  463. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  464. EXPECT_EQ(response.json["command"].asString(), command);
  465. EXPECT_FALSE(response.json["cancel"].asBool());
  466. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  467. EXPECT_EQ(response.json["file"].asString(), filename);
  468. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  469. EXPECT_EQ(response.json["error"].asString(), "");
  470. // skip chunk=0
  471. remaining = 0;
  472. message = Json::Value();
  473. message["command"] = command;
  474. message["file"] = filename;
  475. message["chunk"] = remaining;
  476. message["cancel"] = false;
  477. response = jsonCommander.execute(message);
  478. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  479. EXPECT_EQ(response.json["command"].asString(), command);
  480. EXPECT_TRUE(response.json["cancel"].asBool());
  481. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  482. EXPECT_EQ(response.json["file"].asString(), filename);
  483. EXPECT_EQ(response.json["data"].asString(), "");
  484. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  485. }
  486. /* List tests */
  487. TEST(List, Positive) {
  488. FileManagerMock fileManager;
  489. JsonCommander jsonCommander(fileManager);
  490. const std::string command = "list";
  491. Json::Value message;
  492. message["command"] = command;
  493. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(1));
  494. EXPECT_CALL(fileManager, getListSize()).WillOnce(testing::Return(5));
  495. JsonCommander::Response response = jsonCommander.execute(message);
  496. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  497. EXPECT_EQ(response.json["command"].asString(), command);
  498. EXPECT_TRUE(response.json["accept"].asBool());
  499. EXPECT_EQ(response.json["chunks"].asInt(), 1);
  500. EXPECT_EQ(response.json["items"].asInt(), 5);
  501. EXPECT_EQ(response.json["error"].asString(), "");
  502. }
  503. TEST(List, Negative) {
  504. FileManagerMock fileManager;
  505. JsonCommander jsonCommander(fileManager);
  506. const std::string command = "list";
  507. Json::Value message;
  508. message["command"] = command;
  509. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(-1));
  510. JsonCommander::Response response = jsonCommander.execute(message);
  511. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  512. EXPECT_EQ(response.json["command"].asString(), command);
  513. EXPECT_FALSE(response.json["accept"].asBool());
  514. EXPECT_EQ(response.json["chunks"].asInt(), -1);
  515. EXPECT_EQ(response.json["items"].asInt(), -1);
  516. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  517. }
  518. TEST(List, EmptyList) {
  519. FileManagerMock fileManager;
  520. JsonCommander jsonCommander(fileManager);
  521. const std::string command = "list";
  522. Json::Value message;
  523. message["command"] = command;
  524. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(0));
  525. EXPECT_CALL(fileManager, getListSize()).WillOnce(testing::Return(0));
  526. JsonCommander::Response response = jsonCommander.execute(message);
  527. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  528. EXPECT_EQ(response.json["command"].asString(), command);
  529. EXPECT_TRUE(response.json["accept"].asBool());
  530. EXPECT_EQ(response.json["chunks"].asInt(), 0);
  531. EXPECT_EQ(response.json["items"].asInt(), 0);
  532. EXPECT_EQ(response.json["error"].asString(), "");
  533. }
  534. /* Listdata tests */
  535. void fillExampleFileList(std::vector<std::string> (&chunk)[3]) {
  536. chunk[0].push_back("file01.txt");
  537. chunk[0].push_back("bumdibumps");
  538. chunk[0].push_back("1");
  539. chunk[0].push_back("Ich habe Hunger.txt");
  540. chunk[0].push_back("answerIs42");
  541. chunk[0].push_back("123456789456115811");
  542. chunk[0].push_back("kek");
  543. chunk[1].push_back("1337");
  544. chunk[1].push_back("cats.png");
  545. chunk[1].push_back("more_cats.png");
  546. chunk[1].push_back("ugly dog.tiff");
  547. chunk[1].push_back("hello.txt");
  548. chunk[1].push_back("bye.exe");
  549. chunk[1].push_back("poster.pdf");
  550. chunk[2].push_back("headbang.gif");
  551. chunk[2].push_back("feelsbad.jpg");
  552. chunk[2].push_back("hack.s");
  553. chunk[2].push_back("SodiumChloride");
  554. chunk[2].push_back(
  555. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst"
  556. "uvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN"
  557. "OPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  558. }
  559. TEST(Listdata, Positive) {
  560. FileManagerMock fileManager;
  561. JsonCommander jsonCommander(fileManager);
  562. const std::string command = "listdata";
  563. const int chunks = 3;
  564. std::vector<std::string> chunk[chunks];
  565. fillExampleFileList(chunk);
  566. int remaining = chunks - 1;
  567. for (int k = 0; k < chunks; k++) {
  568. Json::Value message;
  569. message["command"] = command;
  570. message["chunk"] = remaining;
  571. message["cancel"] = false;
  572. EXPECT_CALL(fileManager, getRemainingListChunks())
  573. .WillOnce(testing::Return(remaining + 1));
  574. EXPECT_CALL(fileManager, getNextChunkFromList())
  575. .WillOnce(testing::Return(chunk[k]));
  576. JsonCommander::Response response = jsonCommander.execute(message);
  577. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  578. EXPECT_EQ(response.json["command"].asString(), command);
  579. EXPECT_FALSE(response.json["cancel"].asBool());
  580. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  581. EXPECT_TRUE(response.json["names"].isArray());
  582. Json::Value array = response.json["names"];
  583. EXPECT_EQ(array.size(), chunk[k].size());
  584. for (int i = 0; i < 3; i++) {
  585. EXPECT_EQ(array[i].asString(), chunk[k][i]);
  586. }
  587. EXPECT_EQ(response.json["error"].asString(), "");
  588. }
  589. }
  590. TEST(Listdata, Cancel) {
  591. FileManagerMock fileManager;
  592. JsonCommander jsonCommander(fileManager);
  593. const std::string command = "listdata";
  594. const int chunks = 3;
  595. std::vector<std::string> chunk[chunks];
  596. fillExampleFileList(chunk);
  597. int remaining = chunks - 1;
  598. Json::Value message;
  599. message["command"] = command;
  600. message["chunk"] = remaining;
  601. message["cancel"] = false;
  602. EXPECT_CALL(fileManager, getRemainingListChunks())
  603. .WillOnce(testing::Return(remaining + 1));
  604. EXPECT_CALL(fileManager, getNextChunkFromList())
  605. .WillOnce(testing::Return(chunk[0]));
  606. JsonCommander::Response response = jsonCommander.execute(message);
  607. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  608. EXPECT_EQ(response.json["command"].asString(), command);
  609. EXPECT_FALSE(response.json["cancel"].asBool());
  610. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  611. EXPECT_TRUE(response.json["names"].isArray());
  612. Json::Value array = response.json["names"];
  613. EXPECT_EQ(array.size(), chunk[0].size());
  614. for (int i = 0; i < 3; i++) {
  615. EXPECT_EQ(array[i].asString(), chunk[0][i]);
  616. }
  617. EXPECT_EQ(response.json["error"].asString(), "");
  618. message = Json::Value();
  619. message["command"] = command;
  620. message["chunk"] = remaining;
  621. message["cancel"] = true;
  622. EXPECT_CALL(fileManager, getRemainingListChunks())
  623. .WillOnce(testing::Return(remaining + 1));
  624. response = jsonCommander.execute(message);
  625. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  626. EXPECT_EQ(response.json["command"].asString(), command);
  627. EXPECT_TRUE(response.json["cancel"].asBool());
  628. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  629. EXPECT_TRUE(response.json["names"].isArray());
  630. EXPECT_EQ(response.json["error"].asString(), "");
  631. }
  632. TEST(Listdata, WrongChunkNumber) {
  633. FileManagerMock fileManager;
  634. JsonCommander jsonCommander(fileManager);
  635. const std::string command = "listdata";
  636. const int chunks = 3;
  637. int remaining = chunks - 1;
  638. Json::Value message;
  639. message["command"] = command;
  640. message["chunk"] = remaining;
  641. message["cancel"] = false;
  642. // return smaller remaining
  643. EXPECT_CALL(fileManager, getRemainingListChunks())
  644. .WillOnce(testing::Return(remaining));
  645. JsonCommander::Response response = jsonCommander.execute(message);
  646. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  647. EXPECT_EQ(response.json["command"].asString(), command);
  648. EXPECT_TRUE(response.json["cancel"].asBool());
  649. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  650. EXPECT_TRUE(response.json["names"].isArray());
  651. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  652. }
  653. TEST(Listdata, NoChunksToBeSend) {
  654. FileManagerMock fileManager;
  655. JsonCommander jsonCommander(fileManager);
  656. const std::string command = "listdata";
  657. const int chunks = 0;
  658. Json::Value message;
  659. message["command"] = command;
  660. message["chunk"] = 1;
  661. message["cancel"] = false;
  662. // return smaller remaining
  663. EXPECT_CALL(fileManager, getRemainingListChunks())
  664. .WillOnce(testing::Return(chunks));
  665. JsonCommander::Response response = jsonCommander.execute(message);
  666. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  667. EXPECT_EQ(response.json["command"].asString(), command);
  668. EXPECT_TRUE(response.json["cancel"].asBool());
  669. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  670. EXPECT_TRUE(response.json["names"].isArray());
  671. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  672. }
  673. TEST(Listdata, InvalidRequest) {
  674. FileManagerMock fileManager;
  675. JsonCommander jsonCommander(fileManager);
  676. const std::string command = "listdata";
  677. const int chunks = 3;
  678. Json::Value message;
  679. message["command"] = command;
  680. message["chunk"] = 1;
  681. // return smaller remaining
  682. EXPECT_CALL(fileManager, getRemainingListChunks())
  683. .WillOnce(testing::Return(chunks));
  684. JsonCommander::Response response = jsonCommander.execute(message);
  685. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  686. EXPECT_EQ(response.json["command"].asString(), command);
  687. EXPECT_TRUE(response.json["cancel"].asBool());
  688. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  689. EXPECT_TRUE(response.json["names"].isArray());
  690. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  691. }
  692. } // namespace