JsonCommanderTest.cpp 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837
  1. #include <gmock/gmock.h>
  2. #include <gtest/gtest.h>
  3. #include "../include/Config.h"
  4. #include "../include/JsonCommander.h"
  5. #include "../include/Notifications.h"
  6. #include "ChannelControlsMock.h"
  7. #include "FileManagerMock.h"
  8. #include "QueueMock.h"
  9. namespace {
  10. /* Version tests */
  11. TEST(testVersion, PositiveAllEqual) {
  12. FileManagerMock fileManager;
  13. JsonCommander jsonCommander(fileManager);
  14. Json::Value message;
  15. message["major"] = 0;
  16. message["minor"] = 1;
  17. JsonCommander::Response response = jsonCommander.testVersion(message);
  18. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  19. EXPECT_TRUE(response.json["accept"].asBool());
  20. EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
  21. EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
  22. }
  23. TEST(testVersion, Positive) {
  24. FileManagerMock fileManager;
  25. JsonCommander jsonCommander(fileManager);
  26. Json::Value message;
  27. message["major"] = jsonCommander.protocolMajorVersion;
  28. message["minor"] = jsonCommander.protocolMinorVersion - 1;
  29. JsonCommander::Response response = jsonCommander.testVersion(message);
  30. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  31. EXPECT_TRUE(response.json["accept"].asBool());
  32. EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
  33. EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
  34. }
  35. TEST(testVersion, InvalidRequest) {
  36. FileManagerMock fileManager;
  37. JsonCommander jsonCommander(fileManager);
  38. Json::Value message;
  39. message["major"] = "def";
  40. message["minor"] = "abc";
  41. JsonCommander::Response response = jsonCommander.testVersion(message);
  42. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  43. EXPECT_FALSE(response.json["accept"].asBool());
  44. EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
  45. EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
  46. }
  47. TEST(testVersion, NotEqualMajorNumber) {
  48. FileManagerMock fileManager;
  49. JsonCommander jsonCommander(fileManager);
  50. Json::Value message;
  51. message["major"] = jsonCommander.protocolMajorVersion + 1;
  52. message["minor"] = jsonCommander.protocolMinorVersion;
  53. JsonCommander::Response response = jsonCommander.testVersion(message);
  54. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  55. EXPECT_FALSE(response.json["accept"].asBool());
  56. EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
  57. EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
  58. }
  59. TEST(testVersion, BiggerMinorNumber) {
  60. FileManagerMock fileManager;
  61. JsonCommander jsonCommander(fileManager);
  62. Json::Value message;
  63. message["major"] = jsonCommander.protocolMajorVersion;
  64. message["minor"] = jsonCommander.protocolMinorVersion + 1;
  65. JsonCommander::Response response = jsonCommander.testVersion(message);
  66. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  67. EXPECT_FALSE(response.json["accept"].asBool());
  68. EXPECT_EQ(response.json["major"].asInt(), jsonCommander.protocolMajorVersion);
  69. EXPECT_EQ(response.json["minor"].asInt(), jsonCommander.protocolMinorVersion);
  70. }
  71. /* Status tests */
  72. TEST(Status, Ok) {
  73. FileManagerMock fileManager;
  74. JsonCommander jsonCommander(fileManager);
  75. const std::string command = "status";
  76. Json::Value message;
  77. message["command"] = command;
  78. JsonCommander::Response response = jsonCommander.execute(message);
  79. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  80. EXPECT_EQ(response.json["command"].asString(), command);
  81. EXPECT_EQ(response.json["response"].asString(), "ok");
  82. }
  83. TEST(Status, Downloading) {
  84. FileManagerMock fileManager;
  85. JsonCommander jsonCommander(fileManager);
  86. const std::string command = "status";
  87. Json::Value message;
  88. message["command"] = command;
  89. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  90. JsonCommander::Response response = jsonCommander.execute(message);
  91. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  92. EXPECT_EQ(response.json["command"].asString(), command);
  93. EXPECT_EQ(response.json["response"].asString(), "download running");
  94. }
  95. TEST(Status, Uploading) {
  96. FileManagerMock fileManager;
  97. JsonCommander jsonCommander(fileManager);
  98. const std::string command = "status";
  99. Json::Value message;
  100. message["command"] = command;
  101. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  102. JsonCommander::Response response = jsonCommander.execute(message);
  103. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  104. EXPECT_EQ(response.json["command"].asString(), command);
  105. EXPECT_EQ(response.json["response"].asString(), "upload running");
  106. }
  107. TEST(Status, UploadingAndDownloading) {
  108. FileManagerMock fileManager;
  109. JsonCommander jsonCommander(fileManager);
  110. const std::string command = "status";
  111. Json::Value message;
  112. message["command"] = command;
  113. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  114. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  115. JsonCommander::Response response = jsonCommander.execute(message);
  116. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  117. EXPECT_EQ(response.json["command"].asString(), command);
  118. EXPECT_EQ(response.json["response"].asString(), "download and upload running");
  119. }
  120. /* Close tests */
  121. TEST(Close, Close) {
  122. FileManagerMock fileManager;
  123. JsonCommander jsonCommander(fileManager);
  124. const std::string command = "close";
  125. Json::Value message;
  126. message["command"] = command;
  127. JsonCommander::Response response = jsonCommander.execute(message);
  128. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  129. EXPECT_EQ(response.json["command"].asString(), command);
  130. EXPECT_EQ(response.json["response"].asString(), "bye");
  131. }
  132. /* Put tests */
  133. TEST(Put, Positive) {
  134. FileManagerMock fileManager;
  135. JsonCommander jsonCommander(fileManager);
  136. const std::string command = "put";
  137. const std::string filename = "cool.txt";
  138. Json::Value message;
  139. message["command"] = command;
  140. message["file"] = filename;
  141. message["size"] = 1337;
  142. message["chunks"] = 1;
  143. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(true));
  144. JsonCommander::Response response = jsonCommander.execute(message);
  145. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  146. EXPECT_EQ(response.json["command"].asString(), command);
  147. EXPECT_TRUE(response.json["accept"].asBool());
  148. EXPECT_EQ(response.json["file"].asString(), filename);
  149. EXPECT_EQ(response.json["error"].asString(), "");
  150. }
  151. TEST(Put, Negative) {
  152. FileManagerMock fileManager;
  153. JsonCommander jsonCommander(fileManager);
  154. const std::string command = "put";
  155. const std::string filename = "cool.txt";
  156. Json::Value message;
  157. message["command"] = command;
  158. message["file"] = filename;
  159. message["size"] = 1337;
  160. message["chunks"] = 1;
  161. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(false));
  162. JsonCommander::Response response = jsonCommander.execute(message);
  163. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  164. EXPECT_EQ(response.json["command"].asString(), command);
  165. EXPECT_FALSE(response.json["accept"].asBool());
  166. EXPECT_EQ(response.json["file"].asString(), filename);
  167. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  168. }
  169. /* Putdata tests */
  170. TEST(Putdata, Positive) {
  171. FileManagerMock fileManager;
  172. JsonCommander jsonCommander(fileManager);
  173. /* start with put */
  174. std::string command = "put";
  175. const std::string filename = "cool.txt";
  176. Json::Value message;
  177. message["command"] = command;
  178. message["file"] = filename;
  179. message["size"] = 1337;
  180. const int chunks = 3;
  181. message["chunks"] = chunks;
  182. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(true));
  183. JsonCommander::Response response = jsonCommander.execute(message);
  184. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  185. EXPECT_EQ(response.json["command"].asString(), command);
  186. EXPECT_TRUE(response.json["accept"].asBool());
  187. EXPECT_EQ(response.json["file"].asString(), filename);
  188. EXPECT_EQ(response.json["error"].asString(), "");
  189. /* putdata */
  190. command = "putdata";
  191. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  192. ON_CALL(fileManager, getPutBaseFileName()).WillByDefault(testing::Return(filename));
  193. for (int remaining = chunks - 1; remaining >= 0; remaining--) {
  194. message = Json::Value();
  195. message["command"] = command;
  196. message["file"] = filename;
  197. message["data"] = "MTMzNw==";
  198. message["remaining"] = remaining;
  199. message["cancel"] = false;
  200. response = jsonCommander.execute(message);
  201. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  202. EXPECT_EQ(response.json["command"].asString(), command);
  203. EXPECT_FALSE(response.json["cancel"].asBool());
  204. EXPECT_EQ(response.json["received"].asInt(), remaining);
  205. EXPECT_EQ(response.json["file"].asString(), filename);
  206. EXPECT_EQ(response.json["error"].asString(), "");
  207. }
  208. }
  209. TEST(Putdata, Cancel) {
  210. FileManagerMock fileManager;
  211. JsonCommander jsonCommander(fileManager);
  212. /* start with put */
  213. std::string command = "put";
  214. const std::string filename = "cool.txt";
  215. Json::Value message;
  216. message["command"] = command;
  217. message["file"] = filename;
  218. message["size"] = 1337;
  219. const int chunks = 3;
  220. message["chunks"] = chunks;
  221. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(true));
  222. JsonCommander::Response response = jsonCommander.execute(message);
  223. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  224. EXPECT_EQ(response.json["command"].asString(), command);
  225. EXPECT_TRUE(response.json["accept"].asBool());
  226. EXPECT_EQ(response.json["file"].asString(), filename);
  227. EXPECT_EQ(response.json["error"].asString(), "");
  228. /* putdata */
  229. command = "putdata";
  230. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  231. ON_CALL(fileManager, getPutBaseFileName()).WillByDefault(testing::Return(filename));
  232. int remaining = chunks - 1;
  233. message = Json::Value();
  234. message["command"] = command;
  235. message["file"] = filename;
  236. message["data"] = "MTMzNw==";
  237. message["remaining"] = remaining;
  238. message["cancel"] = false;
  239. response = jsonCommander.execute(message);
  240. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  241. EXPECT_EQ(response.json["command"].asString(), command);
  242. EXPECT_FALSE(response.json["cancel"].asBool());
  243. EXPECT_EQ(response.json["received"].asInt(), remaining);
  244. EXPECT_EQ(response.json["file"].asString(), filename);
  245. EXPECT_EQ(response.json["error"].asString(), "");
  246. // cancel transfer
  247. message = Json::Value();
  248. message["command"] = command;
  249. message["file"] = filename;
  250. message["data"] = "MTMzNw==";
  251. message["remaining"] = --remaining;
  252. message["cancel"] = true;
  253. response = jsonCommander.execute(message);
  254. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  255. EXPECT_EQ(response.json["command"].asString(), command);
  256. EXPECT_TRUE(response.json["cancel"].asBool());
  257. EXPECT_EQ(response.json["received"].asInt(), remaining);
  258. EXPECT_EQ(response.json["file"].asString(), filename);
  259. EXPECT_EQ(response.json["error"].asString(), "");
  260. }
  261. TEST(Putdata, WrongRemaining) {
  262. FileManagerMock fileManager;
  263. JsonCommander jsonCommander(fileManager);
  264. /* start with put */
  265. std::string command = "put";
  266. const std::string filename = "cool.txt";
  267. Json::Value message;
  268. message["command"] = command;
  269. message["file"] = filename;
  270. message["size"] = 1337;
  271. const int chunks = 3;
  272. message["chunks"] = chunks;
  273. ON_CALL(fileManager, openPutFile(testing::_)).WillByDefault(testing::Return(true));
  274. JsonCommander::Response 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["accept"].asBool());
  278. EXPECT_EQ(response.json["file"].asString(), filename);
  279. EXPECT_EQ(response.json["error"].asString(), "");
  280. /* putdata */
  281. command = "putdata";
  282. ON_CALL(fileManager, isUploading()).WillByDefault(testing::Return(true));
  283. ON_CALL(fileManager, getPutBaseFileName()).WillByDefault(testing::Return(filename));
  284. int remaining = chunks - 1;
  285. message = Json::Value();
  286. message["command"] = command;
  287. message["file"] = filename;
  288. message["data"] = "MTMzNw==";
  289. message["remaining"] = remaining;
  290. message["cancel"] = false;
  291. response = jsonCommander.execute(message);
  292. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  293. EXPECT_EQ(response.json["command"].asString(), command);
  294. EXPECT_FALSE(response.json["cancel"].asBool());
  295. EXPECT_EQ(response.json["received"].asInt(), remaining);
  296. EXPECT_EQ(response.json["file"].asString(), filename);
  297. EXPECT_EQ(response.json["error"].asString(), "");
  298. message = Json::Value();
  299. // skip remaining=1 and provoke an error
  300. remaining = 0;
  301. message = Json::Value();
  302. message["command"] = command;
  303. message["file"] = filename;
  304. message["data"] = "MTMzNw==";
  305. message["remaining"] = remaining;
  306. message["cancel"] = false;
  307. response = jsonCommander.execute(message);
  308. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  309. EXPECT_EQ(response.json["command"].asString(), command);
  310. EXPECT_TRUE(response.json["cancel"].asBool());
  311. EXPECT_EQ(response.json["received"].asInt(), remaining);
  312. EXPECT_EQ(response.json["file"].asString(), filename);
  313. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  314. }
  315. /* Get tests */
  316. TEST(Get, Positive) {
  317. FileManagerMock fileManager;
  318. JsonCommander jsonCommander(fileManager);
  319. const std::string command = "get";
  320. const std::string filename = "cool.txt";
  321. Json::Value message;
  322. message["command"] = command;
  323. message["file"] = filename;
  324. const int chunks = 3;
  325. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  326. JsonCommander::Response response = jsonCommander.execute(message);
  327. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  328. EXPECT_EQ(response.json["command"].asString(), command);
  329. EXPECT_TRUE(response.json["accept"].asBool());
  330. EXPECT_EQ(response.json["file"].asString(), filename);
  331. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  332. EXPECT_EQ(response.json["error"].asString(), "");
  333. }
  334. TEST(Get, Negative) {
  335. FileManagerMock fileManager;
  336. JsonCommander jsonCommander(fileManager);
  337. const std::string command = "get";
  338. const std::string filename = "cool.txt";
  339. Json::Value message;
  340. message["command"] = command;
  341. message["file"] = filename;
  342. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(false, -1)));
  343. JsonCommander::Response response = jsonCommander.execute(message);
  344. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  345. EXPECT_EQ(response.json["command"].asString(), command);
  346. EXPECT_FALSE(response.json["accept"].asBool());
  347. EXPECT_EQ(response.json["file"].asString(), filename);
  348. EXPECT_EQ(response.json["chunks"].asInt(), -1);
  349. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  350. }
  351. /* Getdata tests */
  352. TEST(Getdata, Positive) {
  353. FileManagerMock fileManager;
  354. JsonCommander jsonCommander(fileManager);
  355. std::string command = "get";
  356. const std::string filename = "cool.txt";
  357. Json::Value message;
  358. message["command"] = command;
  359. message["file"] = filename;
  360. const int chunks = 3;
  361. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  362. JsonCommander::Response response = jsonCommander.execute(message);
  363. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  364. EXPECT_EQ(response.json["command"].asString(), command);
  365. EXPECT_TRUE(response.json["accept"].asBool());
  366. EXPECT_EQ(response.json["file"].asString(), filename);
  367. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  368. EXPECT_EQ(response.json["error"].asString(), "");
  369. /* getdata */
  370. command = "getdata";
  371. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  372. ON_CALL(fileManager, getGetBaseFileName()).WillByDefault(testing::Return(filename));
  373. std::vector<char> data;
  374. data.push_back('1');
  375. data.push_back('3');
  376. data.push_back('3');
  377. data.push_back('7');
  378. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  379. for (int remaining = chunks - 1; remaining >= 0; remaining--) {
  380. message = Json::Value();
  381. message["command"] = command;
  382. message["file"] = filename;
  383. message["chunk"] = remaining;
  384. message["cancel"] = false;
  385. response = jsonCommander.execute(message);
  386. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  387. EXPECT_EQ(response.json["command"].asString(), command);
  388. EXPECT_FALSE(response.json["cancel"].asBool());
  389. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  390. EXPECT_EQ(response.json["file"].asString(), filename);
  391. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  392. EXPECT_EQ(response.json["error"].asString(), "");
  393. }
  394. }
  395. TEST(Getdata, Cancle) {
  396. FileManagerMock fileManager;
  397. JsonCommander jsonCommander(fileManager);
  398. std::string command = "get";
  399. const std::string filename = "cool.txt";
  400. Json::Value message;
  401. message["command"] = command;
  402. message["file"] = filename;
  403. const int chunks = 3;
  404. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  405. JsonCommander::Response response = jsonCommander.execute(message);
  406. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  407. EXPECT_EQ(response.json["command"].asString(), command);
  408. EXPECT_TRUE(response.json["accept"].asBool());
  409. EXPECT_EQ(response.json["file"].asString(), filename);
  410. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  411. EXPECT_EQ(response.json["error"].asString(), "");
  412. /* getdata */
  413. command = "getdata";
  414. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  415. ON_CALL(fileManager, getGetBaseFileName()).WillByDefault(testing::Return(filename));
  416. std::vector<char> data;
  417. data.push_back('1');
  418. data.push_back('3');
  419. data.push_back('3');
  420. data.push_back('7');
  421. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  422. int remaining = chunks - 1;
  423. message = Json::Value();
  424. message["command"] = command;
  425. message["file"] = filename;
  426. message["chunk"] = remaining;
  427. message["cancel"] = false;
  428. response = jsonCommander.execute(message);
  429. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  430. EXPECT_EQ(response.json["command"].asString(), command);
  431. EXPECT_FALSE(response.json["cancel"].asBool());
  432. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  433. EXPECT_EQ(response.json["file"].asString(), filename);
  434. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  435. EXPECT_EQ(response.json["error"].asString(), "");
  436. // set cancel to true
  437. message = Json::Value();
  438. message["command"] = command;
  439. message["file"] = filename;
  440. message["chunk"] = --remaining;
  441. message["cancel"] = true;
  442. response = jsonCommander.execute(message);
  443. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  444. EXPECT_EQ(response.json["command"].asString(), command);
  445. EXPECT_TRUE(response.json["cancel"].asBool());
  446. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  447. EXPECT_EQ(response.json["file"].asString(), filename);
  448. EXPECT_EQ(response.json["data"].asString(), "");
  449. EXPECT_EQ(response.json["error"].asString(), "");
  450. }
  451. TEST(Getdata, WrongChunk) {
  452. FileManagerMock fileManager;
  453. JsonCommander jsonCommander(fileManager);
  454. std::string command = "get";
  455. const std::string filename = "cool.txt";
  456. Json::Value message;
  457. message["command"] = command;
  458. message["file"] = filename;
  459. const int chunks = 3;
  460. EXPECT_CALL(fileManager, openGetFile(testing::_)).WillOnce(testing::Return(std::pair<bool, int>(true, chunks)));
  461. JsonCommander::Response response = jsonCommander.execute(message);
  462. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  463. EXPECT_EQ(response.json["command"].asString(), command);
  464. EXPECT_TRUE(response.json["accept"].asBool());
  465. EXPECT_EQ(response.json["file"].asString(), filename);
  466. EXPECT_EQ(response.json["chunks"].asInt(), chunks);
  467. EXPECT_EQ(response.json["error"].asString(), "");
  468. /* getdata */
  469. command = "getdata";
  470. ON_CALL(fileManager, isDownloading()).WillByDefault(testing::Return(true));
  471. ON_CALL(fileManager, getGetBaseFileName()).WillByDefault(testing::Return(filename));
  472. std::vector<char> data;
  473. data.push_back('1');
  474. data.push_back('3');
  475. data.push_back('3');
  476. data.push_back('7');
  477. ON_CALL(fileManager, readGet()).WillByDefault(testing::Return(data));
  478. int remaining = chunks - 1;
  479. message = Json::Value();
  480. message["command"] = command;
  481. message["file"] = filename;
  482. message["chunk"] = remaining;
  483. message["cancel"] = false;
  484. response = jsonCommander.execute(message);
  485. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  486. EXPECT_EQ(response.json["command"].asString(), command);
  487. EXPECT_FALSE(response.json["cancel"].asBool());
  488. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  489. EXPECT_EQ(response.json["file"].asString(), filename);
  490. EXPECT_EQ(response.json["data"].asString(), "MTMzNw==");
  491. EXPECT_EQ(response.json["error"].asString(), "");
  492. // skip chunk=0
  493. remaining = 0;
  494. message = Json::Value();
  495. message["command"] = command;
  496. message["file"] = filename;
  497. message["chunk"] = remaining;
  498. message["cancel"] = false;
  499. response = jsonCommander.execute(message);
  500. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  501. EXPECT_EQ(response.json["command"].asString(), command);
  502. EXPECT_TRUE(response.json["cancel"].asBool());
  503. EXPECT_EQ(response.json["remaining"].asInt(), remaining);
  504. EXPECT_EQ(response.json["file"].asString(), filename);
  505. EXPECT_EQ(response.json["data"].asString(), "");
  506. EXPECT_TRUE(response.json["error"].asString().length() > 0);
  507. }
  508. /* List tests */
  509. TEST(List, Positive) {
  510. FileManagerMock fileManager;
  511. JsonCommander jsonCommander(fileManager);
  512. const std::string command = "list";
  513. Json::Value message;
  514. message["command"] = command;
  515. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(1));
  516. EXPECT_CALL(fileManager, getListSize()).WillOnce(testing::Return(5));
  517. JsonCommander::Response response = jsonCommander.execute(message);
  518. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  519. EXPECT_EQ(response.json["command"].asString(), command);
  520. EXPECT_TRUE(response.json["accept"].asBool());
  521. EXPECT_EQ(response.json["chunks"].asInt(), 1);
  522. EXPECT_EQ(response.json["items"].asInt(), 5);
  523. EXPECT_EQ(response.json["error"].asString(), "");
  524. }
  525. TEST(List, Negative) {
  526. FileManagerMock fileManager;
  527. JsonCommander jsonCommander(fileManager);
  528. const std::string command = "list";
  529. Json::Value message;
  530. message["command"] = command;
  531. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(-1));
  532. JsonCommander::Response response = jsonCommander.execute(message);
  533. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  534. EXPECT_EQ(response.json["command"].asString(), command);
  535. EXPECT_FALSE(response.json["accept"].asBool());
  536. EXPECT_EQ(response.json["chunks"].asInt(), -1);
  537. EXPECT_EQ(response.json["items"].asInt(), -1);
  538. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  539. }
  540. TEST(List, EmptyList) {
  541. FileManagerMock fileManager;
  542. JsonCommander jsonCommander(fileManager);
  543. const std::string command = "list";
  544. Json::Value message;
  545. message["command"] = command;
  546. EXPECT_CALL(fileManager, openList()).WillOnce(testing::Return(0));
  547. EXPECT_CALL(fileManager, getListSize()).WillOnce(testing::Return(0));
  548. JsonCommander::Response response = jsonCommander.execute(message);
  549. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  550. EXPECT_EQ(response.json["command"].asString(), command);
  551. EXPECT_TRUE(response.json["accept"].asBool());
  552. EXPECT_EQ(response.json["chunks"].asInt(), 0);
  553. EXPECT_EQ(response.json["items"].asInt(), 0);
  554. EXPECT_EQ(response.json["error"].asString(), "");
  555. }
  556. /* Listdata tests */
  557. void fillExampleFileList(std::vector<std::string> (&chunk)[3]) {
  558. chunk[0].push_back("file01.txt");
  559. chunk[0].push_back("bumdibumps");
  560. chunk[0].push_back("1");
  561. chunk[0].push_back("Ich habe Hunger.txt");
  562. chunk[0].push_back("answerIs42");
  563. chunk[0].push_back("123456789456115811");
  564. chunk[0].push_back("kek");
  565. chunk[1].push_back("1337");
  566. chunk[1].push_back("cats.png");
  567. chunk[1].push_back("more_cats.png");
  568. chunk[1].push_back("ugly dog.tiff");
  569. chunk[1].push_back("hello.txt");
  570. chunk[1].push_back("bye.exe");
  571. chunk[1].push_back("poster.pdf");
  572. chunk[2].push_back("headbang.gif");
  573. chunk[2].push_back("feelsbad.jpg");
  574. chunk[2].push_back("hack.s");
  575. chunk[2].push_back("SodiumChloride");
  576. chunk[2].push_back("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrst"
  577. "uvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMN"
  578. "OPQRSTUVWXYZabcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
  579. }
  580. TEST(Listdata, Positive) {
  581. FileManagerMock fileManager;
  582. JsonCommander jsonCommander(fileManager);
  583. const std::string command = "listdata";
  584. const int chunks = 3;
  585. std::vector<std::string> chunk[chunks];
  586. fillExampleFileList(chunk);
  587. int remaining = chunks - 1;
  588. for (int k = 0; k < chunks; k++) {
  589. Json::Value message;
  590. message["command"] = command;
  591. message["chunk"] = remaining;
  592. message["cancel"] = false;
  593. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining + 1));
  594. EXPECT_CALL(fileManager, getNextChunkFromList()).WillOnce(testing::Return(chunk[k]));
  595. JsonCommander::Response response = jsonCommander.execute(message);
  596. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  597. EXPECT_EQ(response.json["command"].asString(), command);
  598. EXPECT_FALSE(response.json["cancel"].asBool());
  599. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  600. EXPECT_TRUE(response.json["names"].isArray());
  601. Json::Value array = response.json["names"];
  602. EXPECT_EQ(array.size(), chunk[k].size());
  603. for (int i = 0; i < 3; i++) {
  604. EXPECT_EQ(array[i].asString(), chunk[k][i]);
  605. }
  606. EXPECT_EQ(response.json["error"].asString(), "");
  607. }
  608. }
  609. TEST(Listdata, Cancel) {
  610. FileManagerMock fileManager;
  611. JsonCommander jsonCommander(fileManager);
  612. const std::string command = "listdata";
  613. const int chunks = 3;
  614. std::vector<std::string> chunk[chunks];
  615. fillExampleFileList(chunk);
  616. int remaining = chunks - 1;
  617. Json::Value message;
  618. message["command"] = command;
  619. message["chunk"] = remaining;
  620. message["cancel"] = false;
  621. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining + 1));
  622. EXPECT_CALL(fileManager, getNextChunkFromList()).WillOnce(testing::Return(chunk[0]));
  623. JsonCommander::Response response = jsonCommander.execute(message);
  624. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  625. EXPECT_EQ(response.json["command"].asString(), command);
  626. EXPECT_FALSE(response.json["cancel"].asBool());
  627. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  628. EXPECT_TRUE(response.json["names"].isArray());
  629. Json::Value array = response.json["names"];
  630. EXPECT_EQ(array.size(), chunk[0].size());
  631. for (int i = 0; i < 3; i++) {
  632. EXPECT_EQ(array[i].asString(), chunk[0][i]);
  633. }
  634. EXPECT_EQ(response.json["error"].asString(), "");
  635. message = Json::Value();
  636. message["command"] = command;
  637. message["chunk"] = remaining;
  638. message["cancel"] = true;
  639. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining + 1));
  640. response = jsonCommander.execute(message);
  641. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  642. EXPECT_EQ(response.json["command"].asString(), command);
  643. EXPECT_TRUE(response.json["cancel"].asBool());
  644. EXPECT_EQ(response.json["remaining"].asInt(), remaining--);
  645. EXPECT_TRUE(response.json["names"].isArray());
  646. EXPECT_EQ(response.json["error"].asString(), "");
  647. }
  648. TEST(Listdata, WrongChunkNumber) {
  649. FileManagerMock fileManager;
  650. JsonCommander jsonCommander(fileManager);
  651. const std::string command = "listdata";
  652. const int chunks = 3;
  653. int remaining = chunks - 1;
  654. Json::Value message;
  655. message["command"] = command;
  656. message["chunk"] = remaining;
  657. message["cancel"] = false;
  658. // return smaller remaining
  659. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(remaining));
  660. JsonCommander::Response response = jsonCommander.execute(message);
  661. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  662. EXPECT_EQ(response.json["command"].asString(), command);
  663. EXPECT_TRUE(response.json["cancel"].asBool());
  664. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  665. EXPECT_TRUE(response.json["names"].isArray());
  666. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  667. }
  668. TEST(Listdata, NoChunksToBeSend) {
  669. FileManagerMock fileManager;
  670. JsonCommander jsonCommander(fileManager);
  671. const std::string command = "listdata";
  672. const int chunks = 0;
  673. Json::Value message;
  674. message["command"] = command;
  675. message["chunk"] = 1;
  676. message["cancel"] = false;
  677. // return smaller remaining
  678. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(chunks));
  679. JsonCommander::Response response = jsonCommander.execute(message);
  680. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  681. EXPECT_EQ(response.json["command"].asString(), command);
  682. EXPECT_TRUE(response.json["cancel"].asBool());
  683. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  684. EXPECT_TRUE(response.json["names"].isArray());
  685. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  686. }
  687. TEST(Listdata, InvalidRequest) {
  688. FileManagerMock fileManager;
  689. JsonCommander jsonCommander(fileManager);
  690. const std::string command = "listdata";
  691. const int chunks = 3;
  692. Json::Value message;
  693. message["command"] = command;
  694. message["chunk"] = 1;
  695. // return smaller remaining
  696. EXPECT_CALL(fileManager, getRemainingListChunks()).WillOnce(testing::Return(chunks));
  697. JsonCommander::Response response = jsonCommander.execute(message);
  698. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  699. EXPECT_EQ(response.json["command"].asString(), command);
  700. EXPECT_TRUE(response.json["cancel"].asBool());
  701. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  702. EXPECT_TRUE(response.json["names"].isArray());
  703. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  704. }
  705. TEST(Head, Positive) {
  706. FileManagerMock fileManager;
  707. JsonCommander jsonCommander(fileManager);
  708. const std::string command = "head";
  709. const std::string file = "asdf.txt";
  710. Json::Value message;
  711. message["command"] = command;
  712. message["file"] = file;
  713. std::vector<char> bytes = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'};
  714. const std::string bytesAsString = "YWJjZGVmZ2g=";
  715. EXPECT_CALL(fileManager, getBytesFromFile(testing::_, testing::_)).WillOnce(testing::Return(std::make_pair(bytes, FileManager::Error::no_error)));
  716. JsonCommander::Response response = jsonCommander.execute(message);
  717. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  718. EXPECT_EQ(response.json["command"].asString(), command);
  719. EXPECT_TRUE(response.json["accept"].asBool());
  720. EXPECT_EQ(response.json["file"].asString(), file);
  721. EXPECT_EQ(response.json["data"].asString(), bytesAsString);
  722. EXPECT_EQ(response.json["error"].asString(), "");
  723. }
  724. TEST(Head, InvalidRequest) {
  725. FileManagerMock fileManager;
  726. JsonCommander jsonCommander(fileManager);
  727. const std::string command = "head";
  728. const int file = 3641;
  729. Json::Value message;
  730. message["command"] = command;
  731. message["file"] = file;
  732. JsonCommander::Response response = jsonCommander.execute(message);
  733. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  734. EXPECT_EQ(response.json["command"].asString(), command);
  735. EXPECT_FALSE(response.json["accept"].asBool());
  736. EXPECT_EQ(response.json["file"].asString(), "");
  737. EXPECT_EQ(response.json["data"].asString(), "");
  738. EXPECT_NE(response.json["error"].asString(), "");
  739. }
  740. TEST(Head, NoSuchFile) {
  741. FileManagerMock fileManager;
  742. JsonCommander jsonCommander(fileManager);
  743. const std::string command = "head";
  744. const std::string file = "asdf.txt";
  745. Json::Value message;
  746. message["command"] = command;
  747. message["file"] = file;
  748. std::vector<char> bytes;
  749. EXPECT_CALL(fileManager, getBytesFromFile(testing::_, testing::_)).WillOnce(testing::Return(std::make_pair(bytes, FileManager::Error::no_such_file)));
  750. JsonCommander::Response response = jsonCommander.execute(message);
  751. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  752. EXPECT_EQ(response.json["command"].asString(), command);
  753. EXPECT_FALSE(response.json["accept"].asBool());
  754. EXPECT_EQ(response.json["file"].asString(), file);
  755. EXPECT_EQ(response.json["data"].asString(), "");
  756. EXPECT_NE(response.json["error"].asString(), "");
  757. }
  758. TEST(Head, FileTooSmall) {
  759. FileManagerMock fileManager;
  760. JsonCommander jsonCommander(fileManager);
  761. const std::string command = "head";
  762. const std::string file = "asdf.txt";
  763. Json::Value message;
  764. message["command"] = command;
  765. message["file"] = file;
  766. std::vector<char> bytes;
  767. EXPECT_CALL(fileManager, getBytesFromFile(testing::_, testing::_))
  768. .Times(2)
  769. .WillRepeatedly(testing::Return(std::make_pair(bytes, FileManager::Error::file_too_small)));
  770. JsonCommander::Response response = jsonCommander.execute(message);
  771. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  772. EXPECT_EQ(response.json["command"].asString(), command);
  773. EXPECT_FALSE(response.json["accept"].asBool());
  774. EXPECT_EQ(response.json["file"].asString(), file);
  775. EXPECT_EQ(response.json["data"].asString(), "");
  776. EXPECT_NE(response.json["error"].asString(), "");
  777. }
  778. TEST(Deleteme, Positive) {
  779. FileManagerMock fileManager;
  780. JsonCommander jsonCommander(fileManager);
  781. // need to set currentUser in jsonCommander via calling checkLogin
  782. Json::Value login;
  783. login["login"] = true;
  784. login["user"] = "positive";
  785. login["pass"] = "positive";
  786. login["cancel"] = false;
  787. JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
  788. EXPECT_TRUE(loginRes.json["accept"].asBool());
  789. EXPECT_EQ(loginRes.json["error"].asString(), "");
  790. // now the actual test
  791. const std::string command = "deleteme";
  792. Json::Value message;
  793. message["command"] = command;
  794. message["pass"] = "positive";
  795. JsonCommander::Response response = jsonCommander.execute(message);
  796. EXPECT_EQ(response.action, JsonCommander::Action::closeAndSend);
  797. EXPECT_EQ(response.json["command"].asString(), command);
  798. EXPECT_TRUE(response.json["accept"].asBool());
  799. EXPECT_EQ(response.json["error"].asString(), "");
  800. }
  801. TEST(Deleteme, Negative) {
  802. FileManagerMock fileManager;
  803. JsonCommander jsonCommander(fileManager);
  804. // need to set currentUser in jsonCommander via calling checkLogin
  805. Json::Value login;
  806. login["login"] = true;
  807. login["user"] = "positive";
  808. login["pass"] = "positive";
  809. login["cancel"] = false;
  810. JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
  811. EXPECT_TRUE(loginRes.json["accept"].asBool());
  812. EXPECT_EQ(loginRes.json["error"].asString(), "");
  813. // now the actual test
  814. const std::string command = "deleteme";
  815. Json::Value message;
  816. message["command"] = command;
  817. message["pass"] = "negative";
  818. JsonCommander::Response response = jsonCommander.execute(message);
  819. EXPECT_EQ(response.action, JsonCommander::Action::send);
  820. EXPECT_EQ(response.json["command"].asString(), command);
  821. EXPECT_FALSE(response.json["accept"].asBool());
  822. EXPECT_NE(response.json["error"].asString(), "");
  823. }
  824. TEST(Deleteme, InvalidRequest) {
  825. FileManagerMock fileManager;
  826. JsonCommander jsonCommander(fileManager);
  827. // need to set currentUser in jsonCommander via calling checkLogin
  828. Json::Value login;
  829. login["login"] = true;
  830. login["user"] = "positive";
  831. login["pass"] = "positive";
  832. login["cancel"] = false;
  833. JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
  834. EXPECT_TRUE(loginRes.json["accept"].asBool());
  835. EXPECT_EQ(loginRes.json["error"].asString(), "");
  836. // now the actual test
  837. const std::string command = "deleteme";
  838. Json::Value message;
  839. message["command"] = command;
  840. message["pass"] = 123;
  841. JsonCommander::Response response = jsonCommander.execute(message);
  842. EXPECT_EQ(response.action, JsonCommander::Action::closeAndSend);
  843. EXPECT_EQ(response.json["command"].asString(), command);
  844. EXPECT_FALSE(response.json["accept"].asBool());
  845. EXPECT_NE(response.json["error"].asString(), "");
  846. }
  847. TEST(DeleteFile, Positive) {
  848. FileManagerMock fileManager;
  849. JsonCommander jsonCommander(fileManager);
  850. const std::string command = "deletefile";
  851. const std::string file = "asdf.txt";
  852. Json::Value message;
  853. message["command"] = command;
  854. message["file"] = file;
  855. EXPECT_CALL(fileManager, deleteFile(file)).WillOnce(testing::Return(FileManager::Error::no_error));
  856. JsonCommander::Response response = jsonCommander.execute(message);
  857. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  858. EXPECT_EQ(response.json["command"].asString(), command);
  859. EXPECT_EQ(response.json["file"].asString(), file);
  860. EXPECT_TRUE(response.json["accept"].asBool());
  861. EXPECT_EQ(response.json["error"].asString(), "");
  862. }
  863. TEST(DeleteFile, InvalidRequest) {
  864. FileManagerMock fileManager;
  865. JsonCommander jsonCommander(fileManager);
  866. const std::string command = "deletefile";
  867. const int file = 3641;
  868. Json::Value message;
  869. message["command"] = command;
  870. message["file"] = file;
  871. JsonCommander::Response response = jsonCommander.execute(message);
  872. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  873. EXPECT_EQ(response.json["command"].asString(), command);
  874. EXPECT_EQ(response.json["file"].asString(), "");
  875. EXPECT_FALSE(response.json["accept"].asBool());
  876. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  877. }
  878. TEST(DeleteFile, FileDoesNotExist) {
  879. FileManagerMock fileManager;
  880. JsonCommander jsonCommander(fileManager);
  881. const std::string command = "deletefile";
  882. const std::string file = "asdf.txt";
  883. Json::Value message;
  884. message["command"] = command;
  885. message["file"] = file;
  886. EXPECT_CALL(fileManager, deleteFile(file)).WillOnce(testing::Return(FileManager::Error::no_such_file));
  887. JsonCommander::Response response = jsonCommander.execute(message);
  888. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  889. EXPECT_EQ(response.json["command"].asString(), command);
  890. EXPECT_EQ(response.json["file"].asString(), file);
  891. EXPECT_FALSE(response.json["accept"].asBool());
  892. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  893. }
  894. TEST(DeleteFile, DisabledInConfig) {
  895. FileManagerMock fileManager;
  896. JsonCommander jsonCommander(fileManager);
  897. const std::string command = "deletefile";
  898. const std::string file = "asdf.txt";
  899. Json::Value message;
  900. message["command"] = command;
  901. message["file"] = file;
  902. EXPECT_CALL(fileManager, deleteFile(file)).WillOnce(testing::Return(FileManager::Error::not_allowed));
  903. JsonCommander::Response response = jsonCommander.execute(message);
  904. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  905. EXPECT_EQ(response.json["command"].asString(), command);
  906. EXPECT_EQ(response.json["file"].asString(), file);
  907. EXPECT_FALSE(response.json["accept"].asBool());
  908. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  909. }
  910. TEST(ExtendedStatus, NoUploadOrDownload) {
  911. FileManagerMock fileManager;
  912. JsonCommander jsonCommander(fileManager);
  913. ChannelControlsMock channelControls;
  914. Queue::channel = &channelControls;
  915. Queue::queue.clear();
  916. const std::string command = "extendedstatus";
  917. Json::Value message;
  918. message["command"] = command;
  919. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(false));
  920. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(false));
  921. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(false));
  922. JsonCommander::Response response = jsonCommander.execute(message);
  923. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  924. EXPECT_EQ(response.json["command"].asString(), command);
  925. EXPECT_TRUE(response.json["accept"].asBool());
  926. EXPECT_EQ(response.json["error"].asString(), "");
  927. EXPECT_TRUE(response.json["transfersclientserver"].isNull());
  928. EXPECT_TRUE(response.json["transfersserverserver"].isNull());
  929. }
  930. TEST(ExtendedStatus, ClientServerDownload) {
  931. FileManagerMock fileManager;
  932. JsonCommander jsonCommander(fileManager);
  933. ChannelControlsMock channelControls;
  934. Queue::channel = &channelControls;
  935. Queue::queue.clear();
  936. const std::string command = "extendedstatus";
  937. Json::Value message;
  938. message["command"] = command;
  939. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(false));
  940. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(true));
  941. EXPECT_CALL(fileManager, getGetBaseFileName()).WillOnce(testing::Return("asdf"));
  942. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(false));
  943. JsonCommander::Response response = jsonCommander.execute(message);
  944. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  945. EXPECT_EQ(response.json["command"].asString(), command);
  946. EXPECT_TRUE(response.json["accept"].asBool());
  947. EXPECT_EQ(response.json["error"].asString(), "");
  948. EXPECT_EQ(response.json["transfersclientserver"].size(), 1);
  949. EXPECT_FALSE(response.json["transfersclientserver"][0]["upload"].asBool());
  950. EXPECT_EQ(response.json["transfersclientserver"][0]["file"].asString(), "asdf");
  951. EXPECT_TRUE(response.json["transfersclientserver"][0]["progress"].isInt());
  952. }
  953. TEST(ExtendedStatus, ClientServerUpload) {
  954. FileManagerMock fileManager;
  955. JsonCommander jsonCommander(fileManager);
  956. ChannelControlsMock channelControls;
  957. Queue::channel = &channelControls;
  958. Queue::queue.clear();
  959. const std::string command = "extendedstatus";
  960. Json::Value message;
  961. message["command"] = command;
  962. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(true));
  963. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(false));
  964. EXPECT_CALL(fileManager, getPutBaseFileName()).WillOnce(testing::Return("asdf"));
  965. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(false));
  966. JsonCommander::Response response = jsonCommander.execute(message);
  967. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  968. EXPECT_EQ(response.json["command"].asString(), command);
  969. EXPECT_TRUE(response.json["accept"].asBool());
  970. EXPECT_EQ(response.json["error"].asString(), "");
  971. EXPECT_EQ(response.json["transfersclientserver"].size(), 1);
  972. EXPECT_TRUE(response.json["transfersclientserver"][0]["upload"].asBool());
  973. EXPECT_EQ(response.json["transfersclientserver"][0]["file"].asString(), "asdf");
  974. EXPECT_TRUE(response.json["transfersclientserver"][0]["progress"].isInt());
  975. }
  976. TEST(ExtendedStatus, ClientServerDonwloadAndUpload) {
  977. FileManagerMock fileManager;
  978. JsonCommander jsonCommander(fileManager);
  979. ChannelControlsMock channelControls;
  980. Queue::channel = &channelControls;
  981. Queue::queue.clear();
  982. const std::string command = "extendedstatus";
  983. Json::Value message;
  984. message["command"] = command;
  985. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(true));
  986. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(true));
  987. EXPECT_CALL(fileManager, getGetBaseFileName()).WillOnce(testing::Return("asdfGet"));
  988. EXPECT_CALL(fileManager, getPutBaseFileName()).WillOnce(testing::Return("asdfPut"));
  989. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(false));
  990. JsonCommander::Response response = jsonCommander.execute(message);
  991. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  992. EXPECT_EQ(response.json["command"].asString(), command);
  993. EXPECT_TRUE(response.json["accept"].asBool());
  994. EXPECT_EQ(response.json["error"].asString(), "");
  995. EXPECT_EQ(response.json["transfersclientserver"].size(), 2);
  996. EXPECT_TRUE(response.json["transfersclientserver"][0]["upload"].asBool());
  997. EXPECT_EQ(response.json["transfersclientserver"][0]["file"].asString(), "asdfPut");
  998. EXPECT_TRUE(response.json["transfersclientserver"][0]["progress"].isInt());
  999. EXPECT_FALSE(response.json["transfersclientserver"][1]["upload"].asBool());
  1000. EXPECT_EQ(response.json["transfersclientserver"][1]["file"].asString(), "asdfGet");
  1001. EXPECT_TRUE(response.json["transfersclientserver"][1]["progress"].isInt());
  1002. }
  1003. TEST(ExtendedStatus, ServerServerDownload) {
  1004. FileManagerMock fileManager;
  1005. JsonCommander jsonCommander(fileManager);
  1006. ChannelControlsMock channelControls;
  1007. Queue::channel = &channelControls;
  1008. Queue::queue.clear();
  1009. Queue::fileName = "a.txt";
  1010. Config::storage.clear();
  1011. Config::storage.insert(std::pair<std::string, std::string>("passiveMode", "true"));
  1012. Config::storage.insert(std::pair<std::string, std::string>("covertChannelMode", "m"));
  1013. const std::string command = "extendedstatus";
  1014. Json::Value message;
  1015. message["command"] = command;
  1016. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(false));
  1017. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(false));
  1018. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(true));
  1019. EXPECT_CALL(channelControls, getProgress()).WillOnce(testing::Return(std::pair<int, int>(1, 2)));
  1020. EXPECT_CALL(channelControls, getTransferStart()).WillOnce(testing::Return(0));
  1021. JsonCommander::Response response = jsonCommander.execute(message);
  1022. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1023. EXPECT_EQ(response.json["command"].asString(), command);
  1024. EXPECT_TRUE(response.json["accept"].asBool());
  1025. EXPECT_EQ(response.json["error"].asString(), "");
  1026. EXPECT_EQ(response.json["transfersclientserver"].size(), 0);
  1027. EXPECT_EQ(response.json["transfersserverserver"].size(), 1);
  1028. EXPECT_EQ(response.json["transfersserverserver"][0]["type"].asString(), "download");
  1029. EXPECT_EQ(response.json["transfersserverserver"][0]["file"].asString(), "a.txt");
  1030. EXPECT_EQ(response.json["transfersserverserver"][0]["progress"].asInt(), 50);
  1031. EXPECT_TRUE(response.json["transfersserverserver"][0]["speed"].isDouble());
  1032. EXPECT_EQ(response.json["transfersserverserver"][0]["method"].asString(), "m");
  1033. }
  1034. TEST(ExtendedStatus, ServerServerUpload) {
  1035. FileManagerMock fileManager;
  1036. JsonCommander jsonCommander(fileManager);
  1037. ChannelControlsMock channelControls;
  1038. Queue::channel = &channelControls;
  1039. Queue::queue.clear();
  1040. Queue::fileName = "a.txt";
  1041. Config::storage.clear();
  1042. Config::storage.insert(std::pair<std::string, std::string>("passiveMode", "false"));
  1043. Config::storage.insert(std::pair<std::string, std::string>("covertChannelMode", "m"));
  1044. const std::string command = "extendedstatus";
  1045. Json::Value message;
  1046. message["command"] = command;
  1047. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(false));
  1048. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(false));
  1049. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(true));
  1050. EXPECT_CALL(channelControls, getProgress()).WillOnce(testing::Return(std::pair<int, int>(1, 2)));
  1051. EXPECT_CALL(channelControls, getTransferStart()).WillOnce(testing::Return(0));
  1052. JsonCommander::Response response = jsonCommander.execute(message);
  1053. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1054. EXPECT_EQ(response.json["command"].asString(), command);
  1055. EXPECT_TRUE(response.json["accept"].asBool());
  1056. EXPECT_EQ(response.json["error"].asString(), "");
  1057. EXPECT_EQ(response.json["transfersclientserver"].size(), 0);
  1058. EXPECT_EQ(response.json["transfersserverserver"].size(), 1);
  1059. EXPECT_EQ(response.json["transfersserverserver"][0]["type"].asString(), "upload");
  1060. EXPECT_EQ(response.json["transfersserverserver"][0]["file"].asString(), "a.txt");
  1061. EXPECT_EQ(response.json["transfersserverserver"][0]["progress"].asInt(), 50);
  1062. EXPECT_TRUE(response.json["transfersserverserver"][0]["speed"].isDouble());
  1063. EXPECT_EQ(response.json["transfersserverserver"][0]["method"].asString(), "m");
  1064. }
  1065. TEST(ExtendedStatus, QueueNotEmpty) {
  1066. FileManagerMock fileManager;
  1067. JsonCommander jsonCommander(fileManager);
  1068. ChannelControlsMock channelControls;
  1069. Queue::channel = &channelControls;
  1070. Queue::queue.clear();
  1071. Queue::queue.push_back("a");
  1072. Queue::queue.push_back("b");
  1073. Config::storage.clear();
  1074. Config::storage.insert(std::pair<std::string, std::string>("covertChannelMode", "m"));
  1075. const std::string command = "extendedstatus";
  1076. Json::Value message;
  1077. message["command"] = command;
  1078. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(false));
  1079. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(false));
  1080. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(false));
  1081. JsonCommander::Response response = jsonCommander.execute(message);
  1082. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1083. EXPECT_EQ(response.json["command"].asString(), command);
  1084. EXPECT_TRUE(response.json["accept"].asBool());
  1085. EXPECT_EQ(response.json["error"].asString(), "");
  1086. EXPECT_EQ(response.json["transfersclientserver"].size(), 0);
  1087. EXPECT_EQ(response.json["transfersserverserver"].size(), 2);
  1088. EXPECT_EQ(response.json["transfersserverserver"][0]["type"], "queued");
  1089. EXPECT_EQ(response.json["transfersserverserver"][0]["file"], "a");
  1090. EXPECT_EQ(response.json["transfersserverserver"][0]["progress"], 0);
  1091. EXPECT_EQ(response.json["transfersserverserver"][0]["speed"], 0);
  1092. EXPECT_EQ(response.json["transfersserverserver"][0]["method"], "m");
  1093. EXPECT_EQ(response.json["transfersserverserver"][1]["type"], "queued");
  1094. EXPECT_EQ(response.json["transfersserverserver"][1]["file"], "b");
  1095. EXPECT_EQ(response.json["transfersserverserver"][1]["progress"], 0);
  1096. EXPECT_EQ(response.json["transfersserverserver"][1]["speed"], 0);
  1097. EXPECT_EQ(response.json["transfersserverserver"][1]["method"], "m");
  1098. }
  1099. TEST(Notifications, NoMessage) {
  1100. FileManagerMock fileManager;
  1101. JsonCommander jsonCommander(fileManager);
  1102. const std::string command = "notifications";
  1103. Notifications::messages.clear();
  1104. Notifications::userTimeStamps.clear();
  1105. Json::Value message;
  1106. message["command"] = command;
  1107. JsonCommander::Response response = jsonCommander.execute(message);
  1108. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1109. EXPECT_EQ(response.json["command"].asString(), command);
  1110. EXPECT_TRUE(response.json["accept"].asBool());
  1111. EXPECT_EQ(response.json["error"].asString(), "");
  1112. EXPECT_TRUE(response.json["messages"].isNull());
  1113. }
  1114. TEST(Notifications, OneMessage) {
  1115. FileManagerMock fileManager;
  1116. JsonCommander jsonCommander(fileManager);
  1117. const std::string command = "notifications";
  1118. Notifications::messages.clear();
  1119. Notifications::userTimeStamps.clear();
  1120. Notifications::newNotification("asdf");
  1121. Json::Value message;
  1122. message["command"] = command;
  1123. JsonCommander::Response response = jsonCommander.execute(message);
  1124. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1125. EXPECT_EQ(response.json["command"].asString(), command);
  1126. EXPECT_TRUE(response.json["accept"].asBool());
  1127. EXPECT_EQ(response.json["error"].asString(), "");
  1128. EXPECT_TRUE(response.json["messages"].isArray());
  1129. EXPECT_EQ(response.json["messages"][0], "asdf");
  1130. // cleaning up
  1131. Notifications::messages.clear();
  1132. Notifications::userTimeStamps.clear();
  1133. }
  1134. TEST(Notifications, OneMessageMultipleTimesCalled) {
  1135. FileManagerMock fileManager;
  1136. JsonCommander jsonCommander(fileManager);
  1137. const std::string command = "notifications";
  1138. Notifications::messages.clear();
  1139. Notifications::userTimeStamps.clear();
  1140. Notifications::newNotification("asdf");
  1141. Json::Value message;
  1142. message["command"] = command;
  1143. JsonCommander::Response response = jsonCommander.execute(message);
  1144. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1145. EXPECT_EQ(response.json["command"].asString(), command);
  1146. EXPECT_TRUE(response.json["accept"].asBool());
  1147. EXPECT_EQ(response.json["error"].asString(), "");
  1148. EXPECT_TRUE(response.json["messages"].isArray());
  1149. EXPECT_EQ(response.json["messages"][0], "asdf");
  1150. // make suhre that timestamp from message is older
  1151. Notifications::messages.at(0).first--;
  1152. response = jsonCommander.execute(message);
  1153. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1154. EXPECT_EQ(response.json["command"].asString(), command);
  1155. EXPECT_TRUE(response.json["accept"].asBool());
  1156. EXPECT_EQ(response.json["error"].asString(), "");
  1157. EXPECT_TRUE(response.json["messages"].isNull());
  1158. // cleaning up
  1159. Notifications::messages.clear();
  1160. Notifications::userTimeStamps.clear();
  1161. }
  1162. TEST(Notifications, TwoMessages) {
  1163. FileManagerMock fileManager;
  1164. JsonCommander jsonCommander(fileManager);
  1165. const std::string command = "notifications";
  1166. Notifications::messages.clear();
  1167. Notifications::userTimeStamps.clear();
  1168. Notifications::newNotification("asdf");
  1169. Notifications::newNotification("qwer");
  1170. Json::Value message;
  1171. message["command"] = command;
  1172. JsonCommander::Response response = jsonCommander.execute(message);
  1173. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1174. EXPECT_EQ(response.json["command"].asString(), command);
  1175. EXPECT_TRUE(response.json["accept"].asBool());
  1176. EXPECT_EQ(response.json["error"].asString(), "");
  1177. EXPECT_TRUE(response.json["messages"].isArray());
  1178. EXPECT_EQ(response.json["messages"][0], "asdf");
  1179. EXPECT_EQ(response.json["messages"][1], "qwer");
  1180. // cleaning up
  1181. Notifications::messages.clear();
  1182. Notifications::userTimeStamps.clear();
  1183. }
  1184. TEST(ExtendedList, positive) {
  1185. FileManagerMock fileManager;
  1186. JsonCommander jsonCommander(fileManager);
  1187. const std::string command = "extendedlist";
  1188. Json::Value message;
  1189. message["command"] = command;
  1190. EXPECT_CALL(fileManager, getRemainingExtendedListChunks()).WillOnce(testing::Return(0));
  1191. EXPECT_CALL(fileManager, openExtendedList()).WillOnce(testing::Return(1));
  1192. EXPECT_CALL(fileManager, getExtendedListSize()).WillOnce(testing::Return(5));
  1193. JsonCommander::Response response = jsonCommander.execute(message);
  1194. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1195. EXPECT_EQ(response.json["command"].asString(), command);
  1196. EXPECT_TRUE(response.json["accept"].asBool());
  1197. EXPECT_EQ(response.json["chunks"].asInt(), 1);
  1198. EXPECT_EQ(response.json["items"].asInt(), 5);
  1199. EXPECT_EQ(response.json["error"].asString(), "");
  1200. }
  1201. TEST(ExtendedList, negative) {
  1202. FileManagerMock fileManager;
  1203. JsonCommander jsonCommander(fileManager);
  1204. const std::string command = "extendedlist";
  1205. Json::Value message;
  1206. message["command"] = command;
  1207. EXPECT_CALL(fileManager, getRemainingExtendedListChunks()).WillOnce(testing::Return(0));
  1208. EXPECT_CALL(fileManager, openExtendedList()).WillOnce(testing::Return(-1));
  1209. JsonCommander::Response response = jsonCommander.execute(message);
  1210. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1211. EXPECT_EQ(response.json["command"].asString(), command);
  1212. EXPECT_FALSE(response.json["accept"].asBool());
  1213. EXPECT_EQ(response.json["chunks"].asInt(), -1);
  1214. EXPECT_EQ(response.json["items"].asInt(), -1);
  1215. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  1216. }
  1217. TEST(ExtendedList, EmptyList) {
  1218. FileManagerMock fileManager;
  1219. JsonCommander jsonCommander(fileManager);
  1220. const std::string command = "extendedlist";
  1221. Json::Value message;
  1222. message["command"] = command;
  1223. EXPECT_CALL(fileManager, getRemainingExtendedListChunks()).WillOnce(testing::Return(0));
  1224. EXPECT_CALL(fileManager, openExtendedList()).WillOnce(testing::Return(0));
  1225. EXPECT_CALL(fileManager, getExtendedListSize()).WillOnce(testing::Return(0));
  1226. JsonCommander::Response response = jsonCommander.execute(message);
  1227. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1228. EXPECT_EQ(response.json["command"].asString(), command);
  1229. EXPECT_TRUE(response.json["accept"].asBool());
  1230. EXPECT_EQ(response.json["chunks"].asInt(), 0);
  1231. EXPECT_EQ(response.json["items"].asInt(), 0);
  1232. EXPECT_EQ(response.json["error"].asString(), "");
  1233. }
  1234. TEST(ExtendedListData, Positive) {
  1235. FileManagerMock fileManager;
  1236. JsonCommander jsonCommander(fileManager);
  1237. const std::string command = "extendedlistdata";
  1238. Json::Value message;
  1239. message["command"] = command;
  1240. message["chunk"] = 0;
  1241. message["cancel"] = false;
  1242. std::vector<std::tuple<std::string, std::string, double>> chunks;
  1243. std::tuple<std::string, std::string, double> chunk = std::tuple("asdf", "asdfghjkasdfghjkasdfghjkasdfghjk", 41.2);
  1244. chunks.push_back(chunk);
  1245. EXPECT_CALL(fileManager, getRemainingExtendedListChunks()).WillOnce(testing::Return(1));
  1246. EXPECT_CALL(fileManager, getNextChunkFromExtendedList()).WillOnce(testing::Return(chunks));
  1247. JsonCommander::Response response = jsonCommander.execute(message);
  1248. EXPECT_EQ(response.json["command"].asString(), command);
  1249. EXPECT_FALSE(response.json["cancel"].asBool());
  1250. EXPECT_EQ(response.json["remaining"].asInt(), 0);
  1251. EXPECT_TRUE(response.json["files"].isArray());
  1252. EXPECT_EQ(response.json["files"].size(), 1);
  1253. EXPECT_EQ(response.json["files"][0]["name"].asString(), "asdf");
  1254. EXPECT_NE(response.json["files"][0]["head"].asString(), "");
  1255. EXPECT_EQ(response.json["files"][0]["size"].asDouble(), 41.2);
  1256. EXPECT_EQ(response.json["error"].asString(), "");
  1257. }
  1258. TEST(ExtendedListData, Cancel) {
  1259. FileManagerMock fileManager;
  1260. JsonCommander jsonCommander(fileManager);
  1261. const std::string command = "extendedlistdata";
  1262. Json::Value message;
  1263. message["command"] = command;
  1264. message["chunk"] = 0;
  1265. message["cancel"] = true;
  1266. EXPECT_CALL(fileManager, getRemainingExtendedListChunks()).WillOnce(testing::Return(1));
  1267. EXPECT_CALL(fileManager, cancelExtendedList());
  1268. JsonCommander::Response response = jsonCommander.execute(message);
  1269. EXPECT_EQ(response.json["command"].asString(), command);
  1270. EXPECT_TRUE(response.json["cancel"].asBool());
  1271. EXPECT_EQ(response.json["remaining"].asInt(), 0);
  1272. EXPECT_TRUE(response.json["files"].isArray());
  1273. EXPECT_EQ(response.json["error"].asString(), "");
  1274. }
  1275. TEST(ExtendedListData, WrongChunkNumber) {
  1276. FileManagerMock fileManager;
  1277. JsonCommander jsonCommander(fileManager);
  1278. const std::string command = "extendedlistdata";
  1279. Json::Value message;
  1280. message["command"] = command;
  1281. message["chunk"] = 1;
  1282. message["cancel"] = false;
  1283. EXPECT_CALL(fileManager, getRemainingExtendedListChunks()).WillOnce(testing::Return(1));
  1284. JsonCommander::Response response = jsonCommander.execute(message);
  1285. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  1286. EXPECT_EQ(response.json["command"].asString(), command);
  1287. EXPECT_TRUE(response.json["cancel"].asBool());
  1288. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  1289. EXPECT_TRUE(response.json["files"].isArray());
  1290. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  1291. }
  1292. TEST(ExtendedListData, NoChunksToBeSend) {
  1293. FileManagerMock fileManager;
  1294. JsonCommander jsonCommander(fileManager);
  1295. const std::string command = "extendedlistdata";
  1296. Json::Value message;
  1297. message["command"] = command;
  1298. message["chunk"] = 1;
  1299. message["cancel"] = false;
  1300. EXPECT_CALL(fileManager, getRemainingExtendedListChunks()).WillOnce(testing::Return(0));
  1301. JsonCommander::Response response = jsonCommander.execute(message);
  1302. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1303. EXPECT_EQ(response.json["command"].asString(), command);
  1304. EXPECT_TRUE(response.json["cancel"].asBool());
  1305. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  1306. EXPECT_TRUE(response.json["files"].isArray());
  1307. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  1308. }
  1309. TEST(ExtendedListData, InvalidRequest) {
  1310. FileManagerMock fileManager;
  1311. JsonCommander jsonCommander(fileManager);
  1312. const std::string command = "extendedlistdata";
  1313. Json::Value message;
  1314. message["command"] = command;
  1315. message["chunk"] = 5;
  1316. EXPECT_CALL(fileManager, getRemainingExtendedListChunks()).WillOnce(testing::Return(0));
  1317. JsonCommander::Response response = jsonCommander.execute(message);
  1318. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  1319. EXPECT_EQ(response.json["command"].asString(), command);
  1320. EXPECT_TRUE(response.json["cancel"].asBool());
  1321. EXPECT_EQ(response.json["remaining"].asInt(), -1);
  1322. EXPECT_TRUE(response.json["files"].isArray());
  1323. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  1324. }
  1325. TEST(Queue, InvalidRequest) {
  1326. FileManagerMock fileManager;
  1327. JsonCommander jsonCommander(fileManager);
  1328. const std::string command = "queue";
  1329. Json::Value message;
  1330. message["command"] = command;
  1331. message["file"] = 5;
  1332. JsonCommander::Response response = jsonCommander.execute(message);
  1333. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  1334. EXPECT_EQ(response.json["command"].asString(), command);
  1335. EXPECT_FALSE(response.json["accept"].asBool());
  1336. EXPECT_EQ(response.json["file"].asString(), "");
  1337. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  1338. }
  1339. TEST(Queue, PushToQueueFailed) {
  1340. FileManagerMock fileManager;
  1341. JsonCommander jsonCommander(fileManager);
  1342. const std::string command = "queue";
  1343. Json::Value message;
  1344. message["command"] = command;
  1345. message["file"] = "negative";
  1346. JsonCommander::Response response = jsonCommander.execute(message);
  1347. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1348. EXPECT_EQ(response.json["command"].asString(), command);
  1349. EXPECT_FALSE(response.json["accept"].asBool());
  1350. EXPECT_EQ(response.json["file"].asString(), "negative");
  1351. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  1352. }
  1353. TEST(Queue, Positive) {
  1354. FileManagerMock fileManager;
  1355. JsonCommander jsonCommander(fileManager);
  1356. const std::string command = "queue";
  1357. Json::Value message;
  1358. message["command"] = command;
  1359. message["file"] = "positive";
  1360. JsonCommander::Response response = jsonCommander.execute(message);
  1361. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1362. EXPECT_EQ(response.json["command"].asString(), command);
  1363. EXPECT_TRUE(response.json["accept"].asBool());
  1364. EXPECT_EQ(response.json["file"].asString(), "positive");
  1365. EXPECT_EQ(response.json["error"].asString(), "");
  1366. }
  1367. TEST(Dequeue, InvalidRequest) {
  1368. FileManagerMock fileManager;
  1369. JsonCommander jsonCommander(fileManager);
  1370. const std::string command = "dequeue";
  1371. Json::Value message;
  1372. message["command"] = command;
  1373. message["file"] = 5;
  1374. JsonCommander::Response response = jsonCommander.execute(message);
  1375. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  1376. EXPECT_EQ(response.json["command"].asString(), command);
  1377. EXPECT_FALSE(response.json["accept"].asBool());
  1378. EXPECT_EQ(response.json["file"].asString(), "");
  1379. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  1380. }
  1381. TEST(Dequeue, QueueRemoveFailed) {
  1382. FileManagerMock fileManager;
  1383. JsonCommander jsonCommander(fileManager);
  1384. const std::string command = "dequeue";
  1385. Json::Value message;
  1386. message["command"] = command;
  1387. message["file"] = "negative";
  1388. JsonCommander::Response response = jsonCommander.execute(message);
  1389. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1390. EXPECT_EQ(response.json["command"].asString(), command);
  1391. EXPECT_FALSE(response.json["accept"].asBool());
  1392. EXPECT_EQ(response.json["file"].asString(), "negative");
  1393. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  1394. }
  1395. TEST(Dequeue, Positive) {
  1396. FileManagerMock fileManager;
  1397. JsonCommander jsonCommander(fileManager);
  1398. const std::string command = "dequeue";
  1399. Json::Value message;
  1400. message["command"] = command;
  1401. message["file"] = "positive";
  1402. JsonCommander::Response response = jsonCommander.execute(message);
  1403. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1404. EXPECT_EQ(response.json["command"].asString(), command);
  1405. EXPECT_TRUE(response.json["accept"].asBool());
  1406. EXPECT_EQ(response.json["file"].asString(), "positive");
  1407. EXPECT_EQ(response.json["error"].asString(), "");
  1408. }
  1409. } // namespace