JsonCommanderTest.cpp 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541
  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 "../include/Queue.h"
  7. #include "ChannelControlsMock.h"
  8. #include "FileManagerMock.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::_)).WillOnce(testing::Return(std::make_pair(bytes, FileManager::Error::file_too_small)));
  768. JsonCommander::Response response = jsonCommander.execute(message);
  769. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  770. EXPECT_EQ(response.json["command"].asString(), command);
  771. EXPECT_FALSE(response.json["accept"].asBool());
  772. EXPECT_EQ(response.json["file"].asString(), file);
  773. EXPECT_EQ(response.json["data"].asString(), "");
  774. EXPECT_NE(response.json["error"].asString(), "");
  775. }
  776. TEST(Deleteme, Positive) {
  777. FileManagerMock fileManager;
  778. JsonCommander jsonCommander(fileManager);
  779. // need to set currentUser in jsonCommander via calling checkLogin
  780. Json::Value login;
  781. login["login"] = true;
  782. login["user"] = "positive";
  783. login["pass"] = "positive";
  784. login["cancel"] = false;
  785. JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
  786. EXPECT_TRUE(loginRes.json["accept"].asBool());
  787. EXPECT_EQ(loginRes.json["error"].asString(), "");
  788. // now the actual test
  789. const std::string command = "deleteme";
  790. Json::Value message;
  791. message["command"] = command;
  792. message["pass"] = "positive";
  793. JsonCommander::Response response = jsonCommander.execute(message);
  794. EXPECT_EQ(response.action, JsonCommander::Action::closeAndSend);
  795. EXPECT_EQ(response.json["command"].asString(), command);
  796. EXPECT_TRUE(response.json["accept"].asBool());
  797. EXPECT_EQ(response.json["error"].asString(), "");
  798. }
  799. TEST(Deleteme, Negative) {
  800. FileManagerMock fileManager;
  801. JsonCommander jsonCommander(fileManager);
  802. // need to set currentUser in jsonCommander via calling checkLogin
  803. Json::Value login;
  804. login["login"] = true;
  805. login["user"] = "positive";
  806. login["pass"] = "positive";
  807. login["cancel"] = false;
  808. JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
  809. EXPECT_TRUE(loginRes.json["accept"].asBool());
  810. EXPECT_EQ(loginRes.json["error"].asString(), "");
  811. // now the actual test
  812. const std::string command = "deleteme";
  813. Json::Value message;
  814. message["command"] = command;
  815. message["pass"] = "negative";
  816. JsonCommander::Response response = jsonCommander.execute(message);
  817. EXPECT_EQ(response.action, JsonCommander::Action::send);
  818. EXPECT_EQ(response.json["command"].asString(), command);
  819. EXPECT_FALSE(response.json["accept"].asBool());
  820. EXPECT_NE(response.json["error"].asString(), "");
  821. }
  822. TEST(Deleteme, InvalidRequest) {
  823. FileManagerMock fileManager;
  824. JsonCommander jsonCommander(fileManager);
  825. // need to set currentUser in jsonCommander via calling checkLogin
  826. Json::Value login;
  827. login["login"] = true;
  828. login["user"] = "positive";
  829. login["pass"] = "positive";
  830. login["cancel"] = false;
  831. JsonCommander::Response loginRes = jsonCommander.checkLogin(login);
  832. EXPECT_TRUE(loginRes.json["accept"].asBool());
  833. EXPECT_EQ(loginRes.json["error"].asString(), "");
  834. // now the actual test
  835. const std::string command = "deleteme";
  836. Json::Value message;
  837. message["command"] = command;
  838. message["pass"] = 123;
  839. JsonCommander::Response response = jsonCommander.execute(message);
  840. EXPECT_EQ(response.action, JsonCommander::Action::closeAndSend);
  841. EXPECT_EQ(response.json["command"].asString(), command);
  842. EXPECT_FALSE(response.json["accept"].asBool());
  843. EXPECT_NE(response.json["error"].asString(), "");
  844. }
  845. TEST(DeleteFile, Positive) {
  846. FileManagerMock fileManager;
  847. JsonCommander jsonCommander(fileManager);
  848. const std::string command = "deletefile";
  849. const std::string file = "asdf.txt";
  850. Json::Value message;
  851. message["command"] = command;
  852. message["file"] = file;
  853. EXPECT_CALL(fileManager, deleteFile(file)).WillOnce(testing::Return(FileManager::Error::no_error));
  854. JsonCommander::Response response = jsonCommander.execute(message);
  855. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  856. EXPECT_EQ(response.json["command"].asString(), command);
  857. EXPECT_EQ(response.json["file"].asString(), file);
  858. EXPECT_TRUE(response.json["accept"].asBool());
  859. EXPECT_EQ(response.json["error"].asString(), "");
  860. }
  861. TEST(DeleteFile, InvalidRequest) {
  862. FileManagerMock fileManager;
  863. JsonCommander jsonCommander(fileManager);
  864. const std::string command = "deletefile";
  865. const int file = 3641;
  866. Json::Value message;
  867. message["command"] = command;
  868. message["file"] = file;
  869. JsonCommander::Response response = jsonCommander.execute(message);
  870. EXPECT_TRUE(response.action == JsonCommander::Action::closeAndSend);
  871. EXPECT_EQ(response.json["command"].asString(), command);
  872. EXPECT_EQ(response.json["file"].asString(), "");
  873. EXPECT_FALSE(response.json["accept"].asBool());
  874. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  875. }
  876. TEST(DeleteFile, FileDoesNotExist) {
  877. FileManagerMock fileManager;
  878. JsonCommander jsonCommander(fileManager);
  879. const std::string command = "deletefile";
  880. const std::string file = "asdf.txt";
  881. Json::Value message;
  882. message["command"] = command;
  883. message["file"] = file;
  884. EXPECT_CALL(fileManager, deleteFile(file)).WillOnce(testing::Return(FileManager::Error::no_such_file));
  885. JsonCommander::Response response = jsonCommander.execute(message);
  886. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  887. EXPECT_EQ(response.json["command"].asString(), command);
  888. EXPECT_EQ(response.json["file"].asString(), file);
  889. EXPECT_FALSE(response.json["accept"].asBool());
  890. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  891. }
  892. TEST(DeleteFile, DisabledInConfig) {
  893. FileManagerMock fileManager;
  894. JsonCommander jsonCommander(fileManager);
  895. const std::string command = "deletefile";
  896. const std::string file = "asdf.txt";
  897. Json::Value message;
  898. message["command"] = command;
  899. message["file"] = file;
  900. EXPECT_CALL(fileManager, deleteFile(file)).WillOnce(testing::Return(FileManager::Error::not_allowed));
  901. JsonCommander::Response response = jsonCommander.execute(message);
  902. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  903. EXPECT_EQ(response.json["command"].asString(), command);
  904. EXPECT_EQ(response.json["file"].asString(), file);
  905. EXPECT_FALSE(response.json["accept"].asBool());
  906. EXPECT_TRUE(response.json["error"].asString().compare("") != 0);
  907. }
  908. TEST(ExtendedStatus, NoUploadOrDownload) {
  909. FileManagerMock fileManager;
  910. JsonCommander jsonCommander(fileManager);
  911. ChannelControlsMock channelControls;
  912. Queue::channel = &channelControls;
  913. Queue::queue.clear();
  914. const std::string command = "extendedstatus";
  915. Json::Value message;
  916. message["command"] = command;
  917. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(false));
  918. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(false));
  919. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(false));
  920. JsonCommander::Response response = jsonCommander.execute(message);
  921. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  922. EXPECT_EQ(response.json["command"].asString(), command);
  923. EXPECT_TRUE(response.json["accept"].asBool());
  924. EXPECT_EQ(response.json["error"].asString(), "");
  925. EXPECT_TRUE(response.json["transfersclientserver"].isNull());
  926. EXPECT_TRUE(response.json["transfersserverserver"].isNull());
  927. }
  928. TEST(ExtendedStatus, ClientServerDownload) {
  929. FileManagerMock fileManager;
  930. JsonCommander jsonCommander(fileManager);
  931. ChannelControlsMock channelControls;
  932. Queue::channel = &channelControls;
  933. Queue::queue.clear();
  934. const std::string command = "extendedstatus";
  935. Json::Value message;
  936. message["command"] = command;
  937. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(false));
  938. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(true));
  939. EXPECT_CALL(fileManager, getGetBaseFileName()).WillOnce(testing::Return("asdf"));
  940. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(false));
  941. JsonCommander::Response response = jsonCommander.execute(message);
  942. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  943. EXPECT_EQ(response.json["command"].asString(), command);
  944. EXPECT_TRUE(response.json["accept"].asBool());
  945. EXPECT_EQ(response.json["error"].asString(), "");
  946. EXPECT_EQ(response.json["transfersclientserver"].size(), 1);
  947. EXPECT_FALSE(response.json["transfersclientserver"][0]["upload"].asBool());
  948. EXPECT_EQ(response.json["transfersclientserver"][0]["file"].asString(), "asdf");
  949. EXPECT_EQ(response.json["transfersclientserver"][0]["progress"].asInt(), 0);
  950. }
  951. TEST(ExtendedStatus, ClientServerUpload) {
  952. FileManagerMock fileManager;
  953. JsonCommander jsonCommander(fileManager);
  954. ChannelControlsMock channelControls;
  955. Queue::channel = &channelControls;
  956. Queue::queue.clear();
  957. const std::string command = "extendedstatus";
  958. Json::Value message;
  959. message["command"] = command;
  960. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(true));
  961. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(false));
  962. EXPECT_CALL(fileManager, getPutBaseFileName()).WillOnce(testing::Return("asdf"));
  963. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(false));
  964. JsonCommander::Response response = jsonCommander.execute(message);
  965. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  966. EXPECT_EQ(response.json["command"].asString(), command);
  967. EXPECT_TRUE(response.json["accept"].asBool());
  968. EXPECT_EQ(response.json["error"].asString(), "");
  969. EXPECT_EQ(response.json["transfersclientserver"].size(), 1);
  970. EXPECT_TRUE(response.json["transfersclientserver"][0]["upload"].asBool());
  971. EXPECT_EQ(response.json["transfersclientserver"][0]["file"].asString(), "asdf");
  972. EXPECT_EQ(response.json["transfersclientserver"][0]["progress"].asInt(), 0);
  973. }
  974. TEST(ExtendedStatus, ClientServerDonwloadAndUpload) {
  975. FileManagerMock fileManager;
  976. JsonCommander jsonCommander(fileManager);
  977. ChannelControlsMock channelControls;
  978. Queue::channel = &channelControls;
  979. Queue::queue.clear();
  980. const std::string command = "extendedstatus";
  981. Json::Value message;
  982. message["command"] = command;
  983. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(true));
  984. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(true));
  985. EXPECT_CALL(fileManager, getGetBaseFileName()).WillOnce(testing::Return("asdfGet"));
  986. EXPECT_CALL(fileManager, getPutBaseFileName()).WillOnce(testing::Return("asdfPut"));
  987. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(false));
  988. JsonCommander::Response response = jsonCommander.execute(message);
  989. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  990. EXPECT_EQ(response.json["command"].asString(), command);
  991. EXPECT_TRUE(response.json["accept"].asBool());
  992. EXPECT_EQ(response.json["error"].asString(), "");
  993. EXPECT_EQ(response.json["transfersclientserver"].size(), 2);
  994. EXPECT_TRUE(response.json["transfersclientserver"][0]["upload"].asBool());
  995. EXPECT_EQ(response.json["transfersclientserver"][0]["file"].asString(), "asdfPut");
  996. EXPECT_EQ(response.json["transfersclientserver"][0]["progress"].asInt(), 0);
  997. EXPECT_FALSE(response.json["transfersclientserver"][1]["upload"].asBool());
  998. EXPECT_EQ(response.json["transfersclientserver"][1]["file"].asString(), "asdfGet");
  999. EXPECT_EQ(response.json["transfersclientserver"][1]["progress"].asInt(), 0);
  1000. }
  1001. TEST(ExtendedStatus, ServerServerDownload) {
  1002. FileManagerMock fileManager;
  1003. JsonCommander jsonCommander(fileManager);
  1004. ChannelControlsMock channelControls;
  1005. Queue::channel = &channelControls;
  1006. Queue::queue.clear();
  1007. Queue::curTransfer = "a.txt";
  1008. Config::storage.clear();
  1009. Config::storage.insert(std::pair<std::string, std::string>("passiveMode", "true"));
  1010. Config::storage.insert(std::pair<std::string, std::string>("covertChannelMode", "m"));
  1011. const std::string command = "extendedstatus";
  1012. Json::Value message;
  1013. message["command"] = command;
  1014. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(false));
  1015. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(false));
  1016. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(true));
  1017. EXPECT_CALL(channelControls, getProgress()).WillOnce(testing::Return(std::pair<int, int>(1, 2)));
  1018. EXPECT_CALL(channelControls, getTransferStart()).WillOnce(testing::Return(0));
  1019. JsonCommander::Response response = jsonCommander.execute(message);
  1020. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1021. EXPECT_EQ(response.json["command"].asString(), command);
  1022. EXPECT_TRUE(response.json["accept"].asBool());
  1023. EXPECT_EQ(response.json["error"].asString(), "");
  1024. EXPECT_EQ(response.json["transfersclientserver"].size(), 0);
  1025. EXPECT_EQ(response.json["transfersserverserver"].size(), 1);
  1026. EXPECT_EQ(response.json["transfersserverserver"][0]["type"].asString(), "download");
  1027. EXPECT_EQ(response.json["transfersserverserver"][0]["file"].asString(), "a.txt");
  1028. EXPECT_EQ(response.json["transfersserverserver"][0]["progress"].asInt(), 50);
  1029. EXPECT_TRUE(response.json["transfersserverserver"][0]["speed"].isDouble());
  1030. EXPECT_EQ(response.json["transfersserverserver"][0]["method"].asString(), "m");
  1031. }
  1032. TEST(ExtendedStatus, ServerServerUpload) {
  1033. FileManagerMock fileManager;
  1034. JsonCommander jsonCommander(fileManager);
  1035. ChannelControlsMock channelControls;
  1036. Queue::channel = &channelControls;
  1037. Queue::queue.clear();
  1038. Queue::curTransfer = "a.txt";
  1039. Config::storage.clear();
  1040. Config::storage.insert(std::pair<std::string, std::string>("passiveMode", "false"));
  1041. Config::storage.insert(std::pair<std::string, std::string>("covertChannelMode", "m"));
  1042. const std::string command = "extendedstatus";
  1043. Json::Value message;
  1044. message["command"] = command;
  1045. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(false));
  1046. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(false));
  1047. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(true));
  1048. EXPECT_CALL(channelControls, getProgress()).WillOnce(testing::Return(std::pair<int, int>(1, 2)));
  1049. EXPECT_CALL(channelControls, getTransferStart()).WillOnce(testing::Return(0));
  1050. JsonCommander::Response response = jsonCommander.execute(message);
  1051. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1052. EXPECT_EQ(response.json["command"].asString(), command);
  1053. EXPECT_TRUE(response.json["accept"].asBool());
  1054. EXPECT_EQ(response.json["error"].asString(), "");
  1055. EXPECT_EQ(response.json["transfersclientserver"].size(), 0);
  1056. EXPECT_EQ(response.json["transfersserverserver"].size(), 1);
  1057. EXPECT_EQ(response.json["transfersserverserver"][0]["type"].asString(), "upload");
  1058. EXPECT_EQ(response.json["transfersserverserver"][0]["file"].asString(), "a.txt");
  1059. EXPECT_EQ(response.json["transfersserverserver"][0]["progress"].asInt(), 50);
  1060. EXPECT_TRUE(response.json["transfersserverserver"][0]["speed"].isDouble());
  1061. EXPECT_EQ(response.json["transfersserverserver"][0]["method"].asString(), "m");
  1062. }
  1063. TEST(ExtendedStatus, QueueNotEmpty) {
  1064. FileManagerMock fileManager;
  1065. JsonCommander jsonCommander(fileManager);
  1066. ChannelControlsMock channelControls;
  1067. Queue::channel = &channelControls;
  1068. Queue::queue.clear();
  1069. Queue::queue.push_back("a");
  1070. Queue::queue.push_back("b");
  1071. Config::storage.clear();
  1072. Config::storage.insert(std::pair<std::string, std::string>("covertChannelMode", "m"));
  1073. const std::string command = "extendedstatus";
  1074. Json::Value message;
  1075. message["command"] = command;
  1076. EXPECT_CALL(fileManager, isUploading()).WillOnce(testing::Return(false));
  1077. EXPECT_CALL(fileManager, isDownloading()).WillOnce(testing::Return(false));
  1078. EXPECT_CALL(channelControls, isTransferRunning()).WillOnce(testing::Return(false));
  1079. JsonCommander::Response response = jsonCommander.execute(message);
  1080. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1081. EXPECT_EQ(response.json["command"].asString(), command);
  1082. EXPECT_TRUE(response.json["accept"].asBool());
  1083. EXPECT_EQ(response.json["error"].asString(), "");
  1084. EXPECT_EQ(response.json["transfersclientserver"].size(), 0);
  1085. EXPECT_EQ(response.json["transfersserverserver"].size(), 2);
  1086. EXPECT_EQ(response.json["transfersserverserver"][0]["type"], "queued");
  1087. EXPECT_EQ(response.json["transfersserverserver"][0]["file"], "a");
  1088. EXPECT_EQ(response.json["transfersserverserver"][0]["progress"], 0);
  1089. EXPECT_EQ(response.json["transfersserverserver"][0]["speed"], 0);
  1090. EXPECT_EQ(response.json["transfersserverserver"][0]["method"], "m");
  1091. EXPECT_EQ(response.json["transfersserverserver"][1]["type"], "queued");
  1092. EXPECT_EQ(response.json["transfersserverserver"][1]["file"], "b");
  1093. EXPECT_EQ(response.json["transfersserverserver"][1]["progress"], 0);
  1094. EXPECT_EQ(response.json["transfersserverserver"][1]["speed"], 0);
  1095. EXPECT_EQ(response.json["transfersserverserver"][1]["method"], "m");
  1096. }
  1097. TEST(Notifications, NoMessage) {
  1098. FileManagerMock fileManager;
  1099. JsonCommander jsonCommander(fileManager);
  1100. const std::string command = "notifications";
  1101. Notifications::messages.clear();
  1102. Notifications::userTimeStamps.clear();
  1103. Json::Value message;
  1104. message["command"] = command;
  1105. JsonCommander::Response response = jsonCommander.execute(message);
  1106. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1107. EXPECT_EQ(response.json["command"].asString(), command);
  1108. EXPECT_TRUE(response.json["accept"].asBool());
  1109. EXPECT_EQ(response.json["error"].asString(), "");
  1110. EXPECT_TRUE(response.json["messages"].isNull());
  1111. }
  1112. TEST(Notifications, OneMessage) {
  1113. FileManagerMock fileManager;
  1114. JsonCommander jsonCommander(fileManager);
  1115. const std::string command = "notifications";
  1116. Notifications::messages.clear();
  1117. Notifications::userTimeStamps.clear();
  1118. Notifications::newNotification("asdf");
  1119. Json::Value message;
  1120. message["command"] = command;
  1121. JsonCommander::Response response = jsonCommander.execute(message);
  1122. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1123. EXPECT_EQ(response.json["command"].asString(), command);
  1124. EXPECT_TRUE(response.json["accept"].asBool());
  1125. EXPECT_EQ(response.json["error"].asString(), "");
  1126. EXPECT_TRUE(response.json["messages"].isArray());
  1127. EXPECT_EQ(response.json["messages"][0], "asdf");
  1128. // cleaning up
  1129. Notifications::messages.clear();
  1130. Notifications::userTimeStamps.clear();
  1131. }
  1132. TEST(Notifications, OneMessageMultipleTimesCalled) {
  1133. FileManagerMock fileManager;
  1134. JsonCommander jsonCommander(fileManager);
  1135. const std::string command = "notifications";
  1136. Notifications::messages.clear();
  1137. Notifications::userTimeStamps.clear();
  1138. Notifications::newNotification("asdf");
  1139. Json::Value message;
  1140. message["command"] = command;
  1141. JsonCommander::Response response = jsonCommander.execute(message);
  1142. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1143. EXPECT_EQ(response.json["command"].asString(), command);
  1144. EXPECT_TRUE(response.json["accept"].asBool());
  1145. EXPECT_EQ(response.json["error"].asString(), "");
  1146. EXPECT_TRUE(response.json["messages"].isArray());
  1147. EXPECT_EQ(response.json["messages"][0], "asdf");
  1148. // make suhre that timestamp from message is older
  1149. Notifications::messages.at(0).first--;
  1150. response = jsonCommander.execute(message);
  1151. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1152. EXPECT_EQ(response.json["command"].asString(), command);
  1153. EXPECT_TRUE(response.json["accept"].asBool());
  1154. EXPECT_EQ(response.json["error"].asString(), "");
  1155. EXPECT_TRUE(response.json["messages"].isNull());
  1156. // cleaning up
  1157. Notifications::messages.clear();
  1158. Notifications::userTimeStamps.clear();
  1159. }
  1160. TEST(Notifications, TwoMessages) {
  1161. FileManagerMock fileManager;
  1162. JsonCommander jsonCommander(fileManager);
  1163. const std::string command = "notifications";
  1164. Notifications::messages.clear();
  1165. Notifications::userTimeStamps.clear();
  1166. Notifications::newNotification("asdf");
  1167. Notifications::newNotification("qwer");
  1168. Json::Value message;
  1169. message["command"] = command;
  1170. JsonCommander::Response response = jsonCommander.execute(message);
  1171. EXPECT_TRUE(response.action == JsonCommander::Action::send);
  1172. EXPECT_EQ(response.json["command"].asString(), command);
  1173. EXPECT_TRUE(response.json["accept"].asBool());
  1174. EXPECT_EQ(response.json["error"].asString(), "");
  1175. EXPECT_TRUE(response.json["messages"].isArray());
  1176. EXPECT_EQ(response.json["messages"][0], "asdf");
  1177. EXPECT_EQ(response.json["messages"][1], "qwer");
  1178. // cleaning up
  1179. Notifications::messages.clear();
  1180. Notifications::userTimeStamps.clear();
  1181. }
  1182. } // namespace