JsonCommanderTest.cpp 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314
  1. #include <gmock/gmock.h>
  2. #include <gtest/gtest.h>
  3. #include "../include/JsonCommander.h"
  4. #include "../include/Notifications.h"
  5. #include "FileManagerMock.h"
  6. namespace {
  7. /* Version tests */
  8. TEST(testVersion, PositiveAllEqual) {
  9. FileManagerMock fileManager;
  10. JsonCommander jsonCommander(fileManager);
  11. Json::Value message;
  12. message["major"] = 0;
  13. message["minor"] = 1;
  14. JsonCommander::Response response = jsonCommander.testVersion(message);
  15. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  16. EXPECT_TRUE(response.json["accept"].asBool());
  17. EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
  18. EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
  19. }
  20. TEST(testVersion, Positive) {
  21. FileManagerMock fileManager;
  22. JsonCommander jsonCommander(fileManager);
  23. Json::Value message;
  24. message["major"] = jsonCommander.protocolMajorVersion;
  25. message["minor"] = jsonCommander.protocolMinorVersion - 1;
  26. JsonCommander::Response response = jsonCommander.testVersion(message);
  27. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  28. EXPECT_TRUE(response.json["accept"].asBool());
  29. EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
  30. EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
  31. }
  32. TEST(testVersion, InvalidRequest) {
  33. FileManagerMock fileManager;
  34. JsonCommander jsonCommander(fileManager);
  35. Json::Value message;
  36. message["major"] = "def";
  37. message["minor"] = "abc";
  38. JsonCommander::Response response = jsonCommander.testVersion(message);
  39. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  40. EXPECT_FALSE(response.json["accept"].asBool());
  41. EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
  42. EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
  43. }
  44. TEST(testVersion, NotEqualMajorNumber) {
  45. FileManagerMock fileManager;
  46. JsonCommander jsonCommander(fileManager);
  47. Json::Value message;
  48. message["major"] = jsonCommander.protocolMajorVersion + 1;
  49. message["minor"] = jsonCommander.protocolMinorVersion;
  50. JsonCommander::Response response = jsonCommander.testVersion(message);
  51. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  52. EXPECT_FALSE(response.json["accept"].asBool());
  53. EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
  54. EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
  55. }
  56. TEST(testVersion, BiggerMinorNumber) {
  57. FileManagerMock fileManager;
  58. JsonCommander jsonCommander(fileManager);
  59. Json::Value message;
  60. message["major"] = jsonCommander.protocolMajorVersion;
  61. message["minor"] = jsonCommander.protocolMinorVersion + 1;
  62. JsonCommander::Response response = jsonCommander.testVersion(message);
  63. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  64. EXPECT_FALSE(response.json["accept"].asBool());
  65. EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
  66. EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
  67. }
  68. /* Status tests */
  69. TEST(Status, Ok) {
  70. FileManagerMock fileManager;
  71. JsonCommander jsonCommander(fileManager);
  72. const std::string command = "status";
  73. Json::Value message;
  74. message["command"] = command;
  75. JsonCommander::Response response = jsonCommander.execute(message);
  76. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  77. EXPECT_EQ(response.json["command"].asString(), command);
  78. EXPECT_EQ(response.json["response"].asString(), "ok");
  79. }
  80. TEST(Status, Downloading) {
  81. FileManagerMock fileManager;
  82. JsonCommander jsonCommander(fileManager);
  83. const std::string command = "status";
  84. Json::Value message;
  85. message["command"] = command;
  86. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  87. JsonCommander::Response response = jsonCommander.execute(message);
  88. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  89. EXPECT_EQ(response.json["command"].asString(), command);
  90. EXPECT_EQ(response.json["response"].asString(), "download running");
  91. }
  92. TEST(Status, Uploading) {
  93. FileManagerMock fileManager;
  94. JsonCommander jsonCommander(fileManager);
  95. const std::string command = "status";
  96. Json::Value message;
  97. message["command"] = command;
  98. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  99. JsonCommander::Response response = jsonCommander.execute(message);
  100. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  101. EXPECT_EQ(response.json["command"].asString(), command);
  102. EXPECT_EQ(response.json["response"].asString(), "upload running");
  103. }
  104. TEST(Status, UploadingAndDownloading) {
  105. FileManagerMock fileManager;
  106. JsonCommander jsonCommander(fileManager);
  107. const std::string command = "status";
  108. Json::Value message;
  109. message["command"] = command;
  110. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  111. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  112. JsonCommander::Response response = jsonCommander.execute(message);
  113. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  114. EXPECT_EQ(response.json["command"].asString(), command);
  115. EXPECT_EQ(response.json["response"].asString(), "download and upload running");
  116. }
  117. /* Close tests */
  118. TEST(Close, Close) {
  119. FileManagerMock fileManager;
  120. JsonCommander jsonCommander(fileManager);
  121. const std::string command = "close";
  122. Json::Value message;
  123. message["command"] = command;
  124. JsonCommander::Response response = jsonCommander.execute(message);
  125. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  126. EXPECT_EQ(response.json["command"].asString(), command);
  127. EXPECT_EQ(response.json["response"].asString(), "bye");
  128. }
  129. /* Put tests */
  130. TEST(Put, Positive) {
  131. FileManagerMock fileManager;
  132. JsonCommander jsonCommander(fileManager);
  133. const std::string command = "put";
  134. const std::string filename = "cool.txt";
  135. Json::Value message;
  136. message["command"] = command;
  137. message["file"] = filename;
  138. message["size"] = 1337;
  139. message["chunks"] = 1;
  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. }
  148. TEST(Put, Negative) {
  149. FileManagerMock fileManager;
  150. JsonCommander jsonCommander(fileManager);
  151. const std::string command = "put";
  152. const std::string filename = "cool.txt";
  153. Json::Value message;
  154. message["command"] = command;
  155. message["file"] = filename;
  156. message["size"] = 1337;
  157. message["chunks"] = 1;
  158. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(false));
  159. JsonCommander::Response response = jsonCommander.execute(message);
  160. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  161. EXPECT_EQ(response.json["command"].asString(), command);
  162. EXPECT_FALSE(response.json["accept"].asBool());
  163. EXPECT_EQ(response.json["file"].asString(), filename);
  164. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  165. }
  166. /* Putdata tests */
  167. TEST(Putdata, Positive) {
  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. for (int remaining = chunks - 1; remaining >= 0; remaining--) {
  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. }
  205. }
  206. TEST(Putdata, Cancel) {
  207. FileManagerMock fileManager;
  208. JsonCommander jsonCommander(fileManager);
  209. /* start with put */
  210. std::string command = "put";
  211. const std::string filename = "cool.txt";
  212. Json::Value message;
  213. message["command"] = command;
  214. message["file"] = filename;
  215. message["size"] = 1337;
  216. const int chunks = 3;
  217. message["chunks"] = chunks;
  218. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(true));
  219. JsonCommander::Response response = jsonCommander.execute(message);
  220. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  221. EXPECT_EQ(response.json["command"].asString(), command);
  222. EXPECT_TRUE(response.json["accept"].asBool());
  223. EXPECT_EQ(response.json["file"].asString(), filename);
  224. EXPECT_EQ(response.json["error"].asString(), "");
  225. /* putdata */
  226. command = "putdata";
  227. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  228. ON_CALL(fileManager, getPutBaseFileName()).WillByDefault(testing::Return(filename));
  229. int remaining = chunks - 1;
  230. message = Json::Value();
  231. message["command"] = command;
  232. message["file"] = filename;
  233. message["data"] = "MTMzNw==";
  234. message["remaining"] = remaining;
  235. message["cancel"] = false;
  236. response = jsonCommander.execute(message);
  237. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  238. EXPECT_EQ(response.json["command"].asString(), command);
  239. EXPECT_FALSE(response.json["cancel"].asBool());
  240. EXPECT_EQ(response.json["received"].asInt(), remaining);
  241. EXPECT_EQ(response.json["file"].asString(), filename);
  242. EXPECT_EQ(response.json["error"].asString(), "");
  243. // cancel transfer
  244. message = Json::Value();
  245. message["command"] = command;
  246. message["file"] = filename;
  247. message["data"] = "MTMzNw==";
  248. message["remaining"] = --remaining;
  249. message["cancel"] = true;
  250. response = jsonCommander.execute(message);
  251. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  252. EXPECT_EQ(response.json["command"].asString(), command);
  253. EXPECT_TRUE(response.json["cancel"].asBool());
  254. EXPECT_EQ(response.json["received"].asInt(), remaining);
  255. EXPECT_EQ(response.json["file"].asString(), filename);
  256. EXPECT_EQ(response.json["error"].asString(), "");
  257. }
  258. TEST(Putdata, WrongRemaining) {
  259. FileManagerMock fileManager;
  260. JsonCommander jsonCommander(fileManager);
  261. /* start with put */
  262. std::string command = "put";
  263. const std::string filename = "cool.txt";
  264. Json::Value message;
  265. message["command"] = command;
  266. message["file"] = filename;
  267. message["size"] = 1337;
  268. const int chunks = 3;
  269. message["chunks"] = chunks;
  270. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(true));
  271. JsonCommander::Response response = jsonCommander.execute(message);
  272. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  273. EXPECT_EQ(response.json["command"].asString(), command);
  274. EXPECT_TRUE(response.json["accept"].asBool());
  275. EXPECT_EQ(response.json["file"].asString(), filename);
  276. EXPECT_EQ(response.json["error"].asString(), "");
  277. /* putdata */
  278. command = "putdata";
  279. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  280. ON_CALL(fileManager, getPutBaseFileName()).WillByDefault(testing::Return(filename));
  281. int remaining = chunks - 1;
  282. message = Json::Value();
  283. message["command"] = command;
  284. message["file"] = filename;
  285. message["data"] = "MTMzNw==";
  286. message["remaining"] = remaining;
  287. message["cancel"] = false;
  288. response = jsonCommander.execute(message);
  289. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  290. EXPECT_EQ(response.json["command"].asString(), command);
  291. EXPECT_FALSE(response.json["cancel"].asBool());
  292. EXPECT_EQ(response.json["received"].asInt(), remaining);
  293. EXPECT_EQ(response.json["file"].asString(), filename);
  294. EXPECT_EQ(response.json["error"].asString(), "");
  295. message = Json::Value();
  296. // skip remaining=1 and provoke an error
  297. remaining = 0;
  298. message = Json::Value();
  299. message["command"] = command;
  300. message["file"] = filename;
  301. message["data"] = "MTMzNw==";
  302. message["remaining"] = remaining;
  303. message["cancel"] = false;
  304. response = jsonCommander.execute(message);
  305. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  306. EXPECT_EQ(response.json["command"].asString(), command);
  307. EXPECT_TRUE(response.json["cancel"].asBool());
  308. EXPECT_EQ(response.json["received"].asInt(), remaining);
  309. EXPECT_EQ(response.json["file"].asString(), filename);
  310. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  311. }
  312. /* Get tests */
  313. TEST(Get, Positive) {
  314. FileManagerMock fileManager;
  315. JsonCommander jsonCommander(fileManager);
  316. const std::string command = "get";
  317. const std::string filename = "cool.txt";
  318. Json::Value message;
  319. message["command"] = command;
  320. message["file"] = filename;
  321. const int chunks = 3;
  322. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  323. JsonCommander::Response response = jsonCommander.execute(message);
  324. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  325. EXPECT_EQ(response.json["command"].asString(), command);
  326. EXPECT_TRUE(response.json["accept"].asBool());
  327. EXPECT_EQ(response.json["file"].asString(), filename);
  328. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  329. EXPECT_EQ(response.json["error"].asString(), "");
  330. }
  331. TEST(Get, Negative) {
  332. FileManagerMock fileManager;
  333. JsonCommander jsonCommander(fileManager);
  334. const std::string command = "get";
  335. const std::string filename = "cool.txt";
  336. Json::Value message;
  337. message["command"] = command;
  338. message["file"] = filename;
  339. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(false, -1)));
  340. JsonCommander::Response response = jsonCommander.execute(message);
  341. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  342. EXPECT_EQ(response.json["command"].asString(), command);
  343. EXPECT_FALSE(response.json["accept"].asBool());
  344. EXPECT_EQ(response.json["file"].asString(), filename);
  345. EXPECT_EQ(response.json["chunks"].asInt(), -1);
  346. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  347. }
  348. /* Getdata tests */
  349. TEST(Getdata, Positive) {
  350. FileManagerMock fileManager;
  351. JsonCommander jsonCommander(fileManager);
  352. std::string command = "get";
  353. const std::string filename = "cool.txt";
  354. Json::Value message;
  355. message["command"] = command;
  356. message["file"] = filename;
  357. const int chunks = 3;
  358. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  359. JsonCommander::Response response = jsonCommander.execute(message);
  360. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  361. EXPECT_EQ(response.json["command"].asString(), command);
  362. EXPECT_TRUE(response.json["accept"].asBool());
  363. EXPECT_EQ(response.json["file"].asString(), filename);
  364. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  365. EXPECT_EQ(response.json["error"].asString(), "");
  366. /* getdata */
  367. command = "getdata";
  368. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  369. ON_CALL(fileManager, getGetBaseFileName()).WillByDefault(testing::Return(filename));
  370. std::vector<char> data;
  371. data.push_back('1');
  372. data.push_back('3');
  373. data.push_back('3');
  374. data.push_back('7');
  375. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  376. for (int remaining = chunks - 1; remaining >= 0; remaining--) {
  377. message = Json::Value();
  378. message["command"] = command;
  379. message["file"] = filename;
  380. message["chunk"] = remaining;
  381. message["cancel"] = false;
  382. response = jsonCommander.execute(message);
  383. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  384. EXPECT_EQ(response.json["command"].asString(), command);
  385. EXPECT_FALSE(response.json["cancel"].asBool());
  386. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  387. EXPECT_EQ(response.json["file"].asString(), filename);
  388. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  389. EXPECT_EQ(response.json["error"].asString(), "");
  390. }
  391. }
  392. TEST(Getdata, Cancle) {
  393. FileManagerMock fileManager;
  394. JsonCommander jsonCommander(fileManager);
  395. std::string command = "get";
  396. const std::string filename = "cool.txt";
  397. Json::Value message;
  398. message["command"] = command;
  399. message["file"] = filename;
  400. const int chunks = 3;
  401. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  402. JsonCommander::Response response = jsonCommander.execute(message);
  403. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  404. EXPECT_EQ(response.json["command"].asString(), command);
  405. EXPECT_TRUE(response.json["accept"].asBool());
  406. EXPECT_EQ(response.json["file"].asString(), filename);
  407. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  408. EXPECT_EQ(response.json["error"].asString(), "");
  409. /* getdata */
  410. command = "getdata";
  411. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  412. ON_CALL(fileManager, getGetBaseFileName()).WillByDefault(testing::Return(filename));
  413. std::vector<char> data;
  414. data.push_back('1');
  415. data.push_back('3');
  416. data.push_back('3');
  417. data.push_back('7');
  418. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  419. int remaining = chunks - 1;
  420. message = Json::Value();
  421. message["command"] = command;
  422. message["file"] = filename;
  423. message["chunk"] = remaining;
  424. message["cancel"] = false;
  425. response = jsonCommander.execute(message);
  426. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  427. EXPECT_EQ(response.json["command"].asString(), command);
  428. EXPECT_FALSE(response.json["cancel"].asBool());
  429. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  430. EXPECT_EQ(response.json["file"].asString(), filename);
  431. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  432. EXPECT_EQ(response.json["error"].asString(), "");
  433. // set cancel to true
  434. message = Json::Value();
  435. message["command"] = command;
  436. message["file"] = filename;
  437. message["chunk"] = --remaining;
  438. message["cancel"] = true;
  439. response = jsonCommander.execute(message);
  440. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  441. EXPECT_EQ(response.json["command"].asString(), command);
  442. EXPECT_TRUE(response.json["cancel"].asBool());
  443. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  444. EXPECT_EQ(response.json["file"].asString(), filename);
  445. EXPECT_EQ(response.json["data"].asString(), "");
  446. EXPECT_EQ(response.json["error"].asString(), "");
  447. }
  448. TEST(Getdata, WrongChunk) {
  449. FileManagerMock fileManager;
  450. JsonCommander jsonCommander(fileManager);
  451. std::string command = "get";
  452. const std::string filename = "cool.txt";
  453. Json::Value message;
  454. message["command"] = command;
  455. message["file"] = filename;
  456. const int chunks = 3;
  457. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  458. JsonCommander::Response response = jsonCommander.execute(message);
  459. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  460. EXPECT_EQ(response.json["command"].asString(), command);
  461. EXPECT_TRUE(response.json["accept"].asBool());
  462. EXPECT_EQ(response.json["file"].asString(), filename);
  463. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  464. EXPECT_EQ(response.json["error"].asString(), "");
  465. /* getdata */
  466. command = "getdata";
  467. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  468. ON_CALL(fileManager, getGetBaseFileName()).WillByDefault(testing::Return(filename));
  469. std::vector<char> data;
  470. data.push_back('1');
  471. data.push_back('3');
  472. data.push_back('3');
  473. data.push_back('7');
  474. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  475. int remaining = chunks - 1;
  476. message = Json::Value();
  477. message["command"] = command;
  478. message["file"] = filename;
  479. message["chunk"] = remaining;
  480. message["cancel"] = false;
  481. response = jsonCommander.execute(message);
  482. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  483. EXPECT_EQ(response.json["command"].asString(), command);
  484. EXPECT_FALSE(response.json["cancel"].asBool());
  485. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  486. EXPECT_EQ(response.json["file"].asString(), filename);
  487. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  488. EXPECT_EQ(response.json["error"].asString(), "");
  489. // skip chunk=0
  490. remaining = 0;
  491. message = Json::Value();
  492. message["command"] = command;
  493. message["file"] = filename;
  494. message["chunk"] = remaining;
  495. message["cancel"] = false;
  496. response = jsonCommander.execute(message);
  497. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  498. EXPECT_EQ(response.json["command"].asString(), command);
  499. EXPECT_TRUE(response.json["cancel"].asBool());
  500. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  501. EXPECT_EQ(response.json["file"].asString(), filename);
  502. EXPECT_EQ(response.json["data"].asString(), "");
  503. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  504. }
  505. /* List tests */
  506. TEST(List, Positive) {
  507. FileManagerMock fileManager;
  508. JsonCommander jsonCommander(fileManager);
  509. const std::string command = "list";
  510. Json::Value message;
  511. message["command"] = command;
  512. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(1));
  513. EXPECT_CALL(fileManager, getListSize()).WillOnce(testing::Return(5));
  514. JsonCommander::Response response = jsonCommander.execute(message);
  515. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  516. EXPECT_EQ(response.json["command"].asString(), command);
  517. EXPECT_TRUE(response.json["accept"].asBool());
  518. EXPECT_EQ(response.json["chunks"].asInt(), 1);
  519. EXPECT_EQ(response.json["items"].asInt(), 5);
  520. EXPECT_EQ(response.json["error"].asString(), "");
  521. }
  522. TEST(List, Negative) {
  523. FileManagerMock fileManager;
  524. JsonCommander jsonCommander(fileManager);
  525. const std::string command = "list";
  526. Json::Value message;
  527. message["command"] = command;
  528. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(-1));
  529. JsonCommander::Response response = jsonCommander.execute(message);
  530. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  531. EXPECT_EQ(response.json["command"].asString(), command);
  532. EXPECT_FALSE(response.json["accept"].asBool());
  533. EXPECT_EQ(response.json["chunks"].asInt(), -1);
  534. EXPECT_EQ(response.json["items"].asInt(), -1);
  535. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  536. }
  537. TEST(List, EmptyList) {
  538. FileManagerMock fileManager;
  539. JsonCommander jsonCommander(fileManager);
  540. const std::string command = "list";
  541. Json::Value message;
  542. message["command"] = command;
  543. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(0));
  544. EXPECT_CALL(fileManager, getListSize()).WillOnce(testing::Return(0));
  545. JsonCommander::Response response = jsonCommander.execute(message);
  546. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  547. EXPECT_EQ(response.json["command"].asString(), command);
  548. EXPECT_TRUE(response.json["accept"].asBool());
  549. EXPECT_EQ(response.json["chunks"].asInt(), 0);
  550. EXPECT_EQ(response.json["items"].asInt(), 0);
  551. EXPECT_EQ(response.json["error"].asString(), "");
  552. }
  553. /* Listdata tests */
  554. void fillExampleFileList(std::vector<std::string> (&chunk)[3]) {
  555. chunk[0].push_back("file01.txt");
  556. chunk[0].push_back("bumdibumps");
  557. chunk[0].push_back("1");
  558. chunk[0].push_back("Ich habe Hunger.txt");
  559. chunk[0].push_back("answerIs42");
  560. chunk[0].push_back("123456789456115811");
  561. chunk[0].push_back("kek");
  562. chunk[1].push_back("1337");
  563. chunk[1].push_back("cats.png");
  564. chunk[1].push_back("more_cats.png");
  565. chunk[1].push_back("ugly dog.tiff");
  566. chunk[1].push_back("hello.txt");
  567. chunk[1].push_back("bye.exe");
  568. chunk[1].push_back("poster.pdf");
  569. chunk[2].push_back("headbang.gif");
  570. chunk[2].push_back("feelsbad.jpg");
  571. chunk[2].push_back("hack.s");
  572. chunk[2].push_back("SodiumChloride");
  573. chunk[2].push_back("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst"
  574. "uvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN"
  575. "OPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  576. }
  577. TEST(Listdata, Positive) {
  578. FileManagerMock fileManager;
  579. JsonCommander jsonCommander(fileManager);
  580. const std::string command = "listdata";
  581. const int chunks = 3;
  582. std::vector<std::string> chunk[chunks];
  583. fillExampleFileList(chunk);
  584. int remaining = chunks - 1;
  585. for (int k = 0; k < chunks; k++) {
  586. Json::Value message;
  587. message["command"] = command;
  588. message["chunk"] = remaining;
  589. message["cancel"] = false;
  590. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining + 1));
  591. EXPECT_CALL(fileManager, getNextChunkFromList()).WillOnce(testing::Return(chunk[k]));
  592. JsonCommander::Response response = jsonCommander.execute(message);
  593. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  594. EXPECT_EQ(response.json["command"].asString(), command);
  595. EXPECT_FALSE(response.json["cancel"].asBool());
  596. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  597. EXPECT_TRUE(response.json["names"].isArray());
  598. Json::Value array = response.json["names"];
  599. EXPECT_EQ(array.size(), chunk[k].size());
  600. for (int i = 0; i < 3; i++) {
  601. EXPECT_EQ(array[i].asString(), chunk[k][i]);
  602. }
  603. EXPECT_EQ(response.json["error"].asString(), "");
  604. }
  605. }
  606. TEST(Listdata, Cancel) {
  607. FileManagerMock fileManager;
  608. JsonCommander jsonCommander(fileManager);
  609. const std::string command = "listdata";
  610. const int chunks = 3;
  611. std::vector<std::string> chunk[chunks];
  612. fillExampleFileList(chunk);
  613. int remaining = chunks - 1;
  614. Json::Value message;
  615. message["command"] = command;
  616. message["chunk"] = remaining;
  617. message["cancel"] = false;
  618. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining + 1));
  619. EXPECT_CALL(fileManager, getNextChunkFromList()).WillOnce(testing::Return(chunk[0]));
  620. JsonCommander::Response response = jsonCommander.execute(message);
  621. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  622. EXPECT_EQ(response.json["command"].asString(), command);
  623. EXPECT_FALSE(response.json["cancel"].asBool());
  624. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  625. EXPECT_TRUE(response.json["names"].isArray());
  626. Json::Value array = response.json["names"];
  627. EXPECT_EQ(array.size(), chunk[0].size());
  628. for (int i = 0; i < 3; i++) {
  629. EXPECT_EQ(array[i].asString(), chunk[0][i]);
  630. }
  631. EXPECT_EQ(response.json["error"].asString(), "");
  632. message = Json::Value();
  633. message["command"] = command;
  634. message["chunk"] = remaining;
  635. message["cancel"] = true;
  636. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining + 1));
  637. 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(), remaining--);
  642. EXPECT_TRUE(response.json["names"].isArray());
  643. EXPECT_EQ(response.json["error"].asString(), "");
  644. }
  645. TEST(Listdata, WrongChunkNumber) {
  646. FileManagerMock fileManager;
  647. JsonCommander jsonCommander(fileManager);
  648. const std::string command = "listdata";
  649. const int chunks = 3;
  650. int remaining = chunks - 1;
  651. Json::Value message;
  652. message["command"] = command;
  653. message["chunk"] = remaining;
  654. message["cancel"] = false;
  655. // return smaller remaining
  656. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining));
  657. JsonCommander::Response response = jsonCommander.execute(message);
  658. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  659. EXPECT_EQ(response.json["command"].asString(), command);
  660. EXPECT_TRUE(response.json["cancel"].asBool());
  661. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  662. EXPECT_TRUE(response.json["names"].isArray());
  663. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  664. }
  665. TEST(Listdata, NoChunksToBeSend) {
  666. FileManagerMock fileManager;
  667. JsonCommander jsonCommander(fileManager);
  668. const std::string command = "listdata";
  669. const int chunks = 0;
  670. Json::Value message;
  671. message["command"] = command;
  672. message["chunk"] = 1;
  673. message["cancel"] = false;
  674. // return smaller remaining
  675. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(chunks));
  676. JsonCommander::Response response = jsonCommander.execute(message);
  677. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  678. EXPECT_EQ(response.json["command"].asString(), command);
  679. EXPECT_TRUE(response.json["cancel"].asBool());
  680. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  681. EXPECT_TRUE(response.json["names"].isArray());
  682. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  683. }
  684. TEST(Listdata, InvalidRequest) {
  685. FileManagerMock fileManager;
  686. JsonCommander jsonCommander(fileManager);
  687. const std::string command = "listdata";
  688. const int chunks = 3;
  689. Json::Value message;
  690. message["command"] = command;
  691. message["chunk"] = 1;
  692. // return smaller remaining
  693. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(chunks));
  694. JsonCommander::Response response = jsonCommander.execute(message);
  695. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  696. EXPECT_EQ(response.json["command"].asString(), command);
  697. EXPECT_TRUE(response.json["cancel"].asBool());
  698. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  699. EXPECT_TRUE(response.json["names"].isArray());
  700. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  701. }
  702. TEST(Head, Positive) {
  703. FileManagerMock fileManager;
  704. JsonCommander jsonCommander(fileManager);
  705. const std::string command = "head";
  706. const std::string file = "asdf.txt";
  707. Json::Value message;
  708. message["command"] = command;
  709. message["file"] = file;
  710. std::vector<char> bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
  711. const std::string bytesAsString = "YWJjZGVmZ2g=";
  712. EXPECT_CALL(fileManager, getBytesFromFile(testing::_, testing::_)).WillOnce(testing::Return(std::make_pair(bytes, FileManager::Error::no_error)));
  713. JsonCommander::Response response = jsonCommander.execute(message);
  714. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  715. EXPECT_EQ(response.json["command"].asString(), command);
  716. EXPECT_TRUE(response.json["accept"].asBool());
  717. EXPECT_EQ(response.json["file"].asString(), file);
  718. EXPECT_EQ(response.json["data"].asString(), bytesAsString);
  719. EXPECT_EQ(response.json["error"].asString(), "");
  720. }
  721. TEST(Head, InvalidRequest) {
  722. FileManagerMock fileManager;
  723. JsonCommander jsonCommander(fileManager);
  724. const std::string command = "head";
  725. const int file = 3641;
  726. Json::Value message;
  727. message["command"] = command;
  728. message["file"] = file;
  729. JsonCommander::Response response = jsonCommander.execute(message);
  730. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  731. EXPECT_EQ(response.json["command"].asString(), command);
  732. EXPECT_FALSE(response.json["accept"].asBool());
  733. EXPECT_EQ(response.json["file"].asString(), "");
  734. EXPECT_EQ(response.json["data"].asString(), "");
  735. EXPECT_NE(response.json["error"].asString(), "");
  736. }
  737. TEST(Head, NoSuchFile) {
  738. FileManagerMock fileManager;
  739. JsonCommander jsonCommander(fileManager);
  740. const std::string command = "head";
  741. const std::string file = "asdf.txt";
  742. Json::Value message;
  743. message["command"] = command;
  744. message["file"] = file;
  745. std::vector<char> bytes;
  746. EXPECT_CALL(fileManager, getBytesFromFile(testing::_, testing::_)).WillOnce(testing::Return(std::make_pair(bytes, FileManager::Error::no_such_file)));
  747. JsonCommander::Response response = jsonCommander.execute(message);
  748. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  749. EXPECT_EQ(response.json["command"].asString(), command);
  750. EXPECT_FALSE(response.json["accept"].asBool());
  751. EXPECT_EQ(response.json["file"].asString(), file);
  752. EXPECT_EQ(response.json["data"].asString(), "");
  753. EXPECT_NE(response.json["error"].asString(), "");
  754. }
  755. TEST(Head, FileTooSmall) {
  756. FileManagerMock fileManager;
  757. JsonCommander jsonCommander(fileManager);
  758. const std::string command = "head";
  759. const std::string file = "asdf.txt";
  760. Json::Value message;
  761. message["command"] = command;
  762. message["file"] = file;
  763. std::vector<char> bytes;
  764. EXPECT_CALL(fileManager, getBytesFromFile(testing::_, testing::_)).WillOnce(testing::Return(std::make_pair(bytes, FileManager::Error::file_too_small)));
  765. JsonCommander::Response response = jsonCommander.execute(message);
  766. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  767. EXPECT_EQ(response.json["command"].asString(), command);
  768. EXPECT_FALSE(response.json["accept"].asBool());
  769. EXPECT_EQ(response.json["file"].asString(), file);
  770. EXPECT_EQ(response.json["data"].asString(), "");
  771. EXPECT_NE(response.json["error"].asString(), "");
  772. }
  773. TEST(Deleteme, Positive) {
  774. FileManagerMock fileManager;
  775. JsonCommander jsonCommander(fileManager);
  776. // need to set currentUser in jsonCommander via calling checkLogin
  777. Json::Value login;
  778. login["login"] = true;
  779. login["user"] = "positive";
  780. login["pass"] = "positive";
  781. login["cancel"] = false;
  782. JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
  783. EXPECT_TRUE(loginRes.json["accept"].asBool());
  784. EXPECT_EQ(loginRes.json["error"].asString(), "");
  785. // now the actual test
  786. const std::string command = "deleteme";
  787. Json::Value message;
  788. message["command"] = command;
  789. message["pass"] = "positive";
  790. JsonCommander::Response response = jsonCommander.execute(message);
  791. EXPECT_EQ(response.action, JsonCommander::Action::closeAndSend);
  792. EXPECT_EQ(response.json["command"].asString(), command);
  793. EXPECT_TRUE(response.json["accept"].asBool());
  794. EXPECT_EQ(response.json["error"].asString(), "");
  795. }
  796. TEST(Deleteme, Negative) {
  797. FileManagerMock fileManager;
  798. JsonCommander jsonCommander(fileManager);
  799. // need to set currentUser in jsonCommander via calling checkLogin
  800. Json::Value login;
  801. login["login"] = true;
  802. login["user"] = "positive";
  803. login["pass"] = "positive";
  804. login["cancel"] = false;
  805. JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
  806. EXPECT_TRUE(loginRes.json["accept"].asBool());
  807. EXPECT_EQ(loginRes.json["error"].asString(), "");
  808. // now the actual test
  809. const std::string command = "deleteme";
  810. Json::Value message;
  811. message["command"] = command;
  812. message["pass"] = "negative";
  813. JsonCommander::Response response = jsonCommander.execute(message);
  814. EXPECT_EQ(response.action, JsonCommander::Action::send);
  815. EXPECT_EQ(response.json["command"].asString(), command);
  816. EXPECT_FALSE(response.json["accept"].asBool());
  817. EXPECT_NE(response.json["error"].asString(), "");
  818. }
  819. TEST(Deleteme, InvalidRequest) {
  820. FileManagerMock fileManager;
  821. JsonCommander jsonCommander(fileManager);
  822. // need to set currentUser in jsonCommander via calling checkLogin
  823. Json::Value login;
  824. login["login"] = true;
  825. login["user"] = "positive";
  826. login["pass"] = "positive";
  827. login["cancel"] = false;
  828. JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
  829. EXPECT_TRUE(loginRes.json["accept"].asBool());
  830. EXPECT_EQ(loginRes.json["error"].asString(), "");
  831. // now the actual test
  832. const std::string command = "deleteme";
  833. Json::Value message;
  834. message["command"] = command;
  835. message["pass"] = 123;
  836. JsonCommander::Response response = jsonCommander.execute(message);
  837. EXPECT_EQ(response.action, JsonCommander::Action::closeAndSend);
  838. EXPECT_EQ(response.json["command"].asString(), command);
  839. EXPECT_FALSE(response.json["accept"].asBool());
  840. EXPECT_NE(response.json["error"].asString(), "");
  841. }
  842. TEST(DeleteFile, Positive) {
  843. FileManagerMock fileManager;
  844. JsonCommander jsonCommander(fileManager);
  845. const std::string command = "deletefile";
  846. const std::string file = "asdf.txt";
  847. Json::Value message;
  848. message["command"] = command;
  849. message["file"] = file;
  850. EXPECT_CALL(fileManager, deleteFile(file)).WillOnce(testing::Return(FileManager::Error::no_error));
  851. JsonCommander::Response response = jsonCommander.execute(message);
  852. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  853. EXPECT_EQ(response.json["command"].asString(), command);
  854. EXPECT_EQ(response.json["file"].asString(), file);
  855. EXPECT_TRUE(response.json["accept"].asBool());
  856. EXPECT_EQ(response.json["error"].asString(), "");
  857. }
  858. TEST(DeleteFile, InvalidRequest) {
  859. FileManagerMock fileManager;
  860. JsonCommander jsonCommander(fileManager);
  861. const std::string command = "deletefile";
  862. const int file = 3641;
  863. Json::Value message;
  864. message["command"] = command;
  865. message["file"] = file;
  866. JsonCommander::Response response = jsonCommander.execute(message);
  867. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  868. EXPECT_EQ(response.json["command"].asString(), command);
  869. EXPECT_EQ(response.json["file"].asString(), "");
  870. EXPECT_FALSE(response.json["accept"].asBool());
  871. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  872. }
  873. TEST(DeleteFile, FileDoesNotExist) {
  874. FileManagerMock fileManager;
  875. JsonCommander jsonCommander(fileManager);
  876. const std::string command = "deletefile";
  877. const std::string file = "asdf.txt";
  878. Json::Value message;
  879. message["command"] = command;
  880. message["file"] = file;
  881. EXPECT_CALL(fileManager, deleteFile(file)).WillOnce(testing::Return(FileManager::Error::no_such_file));
  882. JsonCommander::Response response = jsonCommander.execute(message);
  883. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  884. EXPECT_EQ(response.json["command"].asString(), command);
  885. EXPECT_EQ(response.json["file"].asString(), file);
  886. EXPECT_FALSE(response.json["accept"].asBool());
  887. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  888. }
  889. TEST(DeleteFile, DisabledInConfig) {
  890. FileManagerMock fileManager;
  891. JsonCommander jsonCommander(fileManager);
  892. const std::string command = "deletefile";
  893. const std::string file = "asdf.txt";
  894. Json::Value message;
  895. message["command"] = command;
  896. message["file"] = file;
  897. EXPECT_CALL(fileManager, deleteFile(file)).WillOnce(testing::Return(FileManager::Error::not_allowed));
  898. JsonCommander::Response response = jsonCommander.execute(message);
  899. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  900. EXPECT_EQ(response.json["command"].asString(), command);
  901. EXPECT_EQ(response.json["file"].asString(), file);
  902. EXPECT_FALSE(response.json["accept"].asBool());
  903. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  904. }
  905. TEST(Notifications, NoMessage) {
  906. FileManagerMock fileManager;
  907. JsonCommander jsonCommander(fileManager);
  908. const std::string command = "notifications";
  909. Notifications::messages.clear();
  910. Notifications::userTimeStamps.clear();
  911. Json::Value message;
  912. message["command"] = command;
  913. JsonCommander::Response response = jsonCommander.execute(message);
  914. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  915. EXPECT_EQ(response.json["command"].asString(), command);
  916. EXPECT_TRUE(response.json["accept"].asBool());
  917. EXPECT_EQ(response.json["error"].asString(), "");
  918. EXPECT_TRUE(response.json["messages"].isNull());
  919. }
  920. TEST(Notifications, OneMessage) {
  921. FileManagerMock fileManager;
  922. JsonCommander jsonCommander(fileManager);
  923. const std::string command = "notifications";
  924. Notifications::messages.clear();
  925. Notifications::userTimeStamps.clear();
  926. Notifications::newNotification("asdf");
  927. Json::Value message;
  928. message["command"] = command;
  929. JsonCommander::Response response = jsonCommander.execute(message);
  930. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  931. EXPECT_EQ(response.json["command"].asString(), command);
  932. EXPECT_TRUE(response.json["accept"].asBool());
  933. EXPECT_EQ(response.json["error"].asString(), "");
  934. EXPECT_TRUE(response.json["messages"].isArray());
  935. EXPECT_EQ(response.json["messages"][0], "asdf");
  936. // cleaning up
  937. Notifications::messages.clear();
  938. Notifications::userTimeStamps.clear();
  939. }
  940. TEST(Notifications, OneMessageMultipleTimesCalled) {
  941. FileManagerMock fileManager;
  942. JsonCommander jsonCommander(fileManager);
  943. const std::string command = "notifications";
  944. Notifications::messages.clear();
  945. Notifications::userTimeStamps.clear();
  946. Notifications::newNotification("asdf");
  947. Json::Value message;
  948. message["command"] = command;
  949. JsonCommander::Response response = jsonCommander.execute(message);
  950. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  951. EXPECT_EQ(response.json["command"].asString(), command);
  952. EXPECT_TRUE(response.json["accept"].asBool());
  953. EXPECT_EQ(response.json["error"].asString(), "");
  954. EXPECT_TRUE(response.json["messages"].isArray());
  955. EXPECT_EQ(response.json["messages"][0], "asdf");
  956. // make suhre that timestamp from message is older
  957. Notifications::messages.at(0).first--;
  958. response = jsonCommander.execute(message);
  959. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  960. EXPECT_EQ(response.json["command"].asString(), command);
  961. EXPECT_TRUE(response.json["accept"].asBool());
  962. EXPECT_EQ(response.json["error"].asString(), "");
  963. EXPECT_TRUE(response.json["messages"].isNull());
  964. // cleaning up
  965. Notifications::messages.clear();
  966. Notifications::userTimeStamps.clear();
  967. }
  968. TEST(Notifications, TwoMessages) {
  969. FileManagerMock fileManager;
  970. JsonCommander jsonCommander(fileManager);
  971. const std::string command = "notifications";
  972. Notifications::messages.clear();
  973. Notifications::userTimeStamps.clear();
  974. Notifications::newNotification("asdf");
  975. Notifications::newNotification("qwer");
  976. Json::Value message;
  977. message["command"] = command;
  978. JsonCommander::Response response = jsonCommander.execute(message);
  979. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  980. EXPECT_EQ(response.json["command"].asString(), command);
  981. EXPECT_TRUE(response.json["accept"].asBool());
  982. EXPECT_EQ(response.json["error"].asString(), "");
  983. EXPECT_TRUE(response.json["messages"].isArray());
  984. EXPECT_EQ(response.json["messages"][0], "asdf");
  985. EXPECT_EQ(response.json["messages"][1], "qwer");
  986. // cleaning up
  987. Notifications::messages.clear();
  988. Notifications::userTimeStamps.clear();
  989. }
  990. } // namespace