cmdman_test.cpp 76 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843
  1. #include <gmock/gmock.h>
  2. #include <gtest/gtest.h>
  3. #include "../include/cmdman.h"
  4. #include "../include/global.h"
  5. #include "fileman_mock.h"
  6. #include "CmdManForTest.cpp"
  7. /* template
  8. TEST(testVersion, Positive) {
  9. FileManMock fm;
  10. CmdManForTest cm(fm, dummyDebugPrint);
  11. std::string cmd;
  12. std::vector<std::string> args;
  13. Json::Value root;
  14. CmdMan::CmdRet retval;
  15. // prepare root/cmd/args
  16. cmd = "";
  17. args = {"", ""};
  18. root[""] = "";
  19. // stick into cmdman
  20. cm.execute(cmd, args);
  21. cm.handle(root);
  22. // check things
  23. EXPECT_EQ(retval.type, sometypes);
  24. EXPECT_EQ(retval.nextcommand, somestring);
  25. EXPECT_TRUE(retval.msg[""].asBool());
  26. EXPECT_FALSE(retval.msg[""].asBool());
  27. ON_CALL(fm, methodname()).WillByDefault(testing::Return(true));
  28. }
  29. */
  30. /* holds all tests */
  31. namespace {
  32. void dummyDebugPrint(string x) {
  33. // do nothing
  34. }
  35. /*
  36. * =====================================
  37. * tests for connect
  38. */
  39. TEST(testConnect, TooFewArgs) {
  40. FileManMock fm;
  41. CmdManForTest cm(fm, dummyDebugPrint);
  42. std::string cmd;
  43. std::vector<std::string> args;
  44. CmdMan::CmdRet retvalCmd;
  45. // prepare cmd/args
  46. cmd = "connect";
  47. args = {};
  48. // stick into cmdman
  49. retvalCmd = cm.execute(cmd, args);
  50. // check things
  51. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  52. EXPECT_EQ(retvalCmd.msg["command"].asString(), "connect");
  53. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  54. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  55. EXPECT_TRUE(cm.isNotConnected());
  56. }
  57. TEST(testConnect, Positive_NoPortSpecified) {
  58. FileManMock fm;
  59. CmdManForTest cm(fm, dummyDebugPrint);
  60. std::string cmd;
  61. std::vector<std::string> args;
  62. CmdMan::CmdRet retvalCmd;
  63. // prepare cmd/args
  64. cmd = "connect";
  65. args = {"1.222.33.4"};
  66. // stick into cmdman
  67. retvalCmd = cm.execute(cmd, args);
  68. cm.stateSetConnectionOk();
  69. // check things
  70. EXPECT_EQ(retvalCmd.type, CmdMan::connect);
  71. EXPECT_EQ(retvalCmd.msg["address"].asString(), "1.222.33.4");
  72. EXPECT_EQ(retvalCmd.msg["port"].asString(), "1234");
  73. EXPECT_TRUE(cm.isConnected());
  74. }
  75. TEST(testConnect, Positive_PortSpecified) {
  76. FileManMock fm;
  77. CmdManForTest cm(fm, dummyDebugPrint);
  78. std::string cmd;
  79. std::vector<std::string> args;
  80. CmdMan::CmdRet retvalCmd;
  81. // prepare cmd/args
  82. cmd = "connect";
  83. args = {"1.222.33.4", "1337"};
  84. // stick into cmdman
  85. retvalCmd = cm.execute(cmd, args);
  86. cm.stateSetConnectionOk();
  87. // check things
  88. EXPECT_EQ(retvalCmd.type, CmdMan::connect);
  89. EXPECT_EQ(retvalCmd.msg["address"].asString(), "1.222.33.4");
  90. EXPECT_EQ(retvalCmd.msg["port"].asString(), "1337");
  91. EXPECT_TRUE(cm.isConnected());
  92. }
  93. TEST(testConnect, Negative) {
  94. FileManMock fm;
  95. CmdManForTest cm(fm, dummyDebugPrint);
  96. std::string cmd;
  97. std::vector<std::string> args;
  98. CmdMan::CmdRet retvalCmd;
  99. // prepare cmd/args
  100. cmd = "connect";
  101. args = {"1.222.33.4", "1337"};
  102. // stick into cmdman
  103. retvalCmd = cm.execute(cmd, args);
  104. // cm.stateSetConnectionOk() not called
  105. // check things
  106. EXPECT_EQ(retvalCmd.type, CmdMan::connect);
  107. EXPECT_EQ(retvalCmd.msg["address"].asString(), "1.222.33.4");
  108. EXPECT_EQ(retvalCmd.msg["port"].asString(), "1337");
  109. EXPECT_TRUE(cm.isNotConnected());
  110. }
  111. /* =====================================
  112. * tests for
  113. * version check, signup, login
  114. */
  115. TEST(testVersion, Positive) {
  116. FileManMock fm;
  117. CmdManForTest cm(fm, dummyDebugPrint);
  118. cm.initConnected();
  119. std::string cmd;
  120. std::vector<std::string> args;
  121. Json::Value root;
  122. CmdMan::CmdRet retvalCmd;
  123. CmdMan::CmdRet retvalHdl;
  124. // prepare root/cmd/args
  125. cmd = "version";
  126. args = {};
  127. root["major"] = protocolMajorVersion;
  128. root["minor"] = protocolMinorVersion;
  129. root["accept"] = true;
  130. // stick into cmdman
  131. retvalCmd = cm.execute(cmd, args);
  132. retvalHdl = cm.handle(root);
  133. // check things
  134. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  135. EXPECT_EQ(retvalCmd.msg["major"], protocolMajorVersion);
  136. EXPECT_EQ(retvalCmd.msg["minor"], protocolMinorVersion);
  137. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  138. EXPECT_EQ(retvalHdl.msg["command"].asString(), "version");
  139. EXPECT_EQ(retvalHdl.msg["serverversion"].asString(), std::to_string(protocolMajorVersion) + "." + std::to_string(protocolMinorVersion));
  140. EXPECT_EQ(retvalHdl.msg["clientversion"].asString(), std::to_string(protocolMajorVersion) + "." + std::to_string(protocolMinorVersion));
  141. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  142. EXPECT_TRUE(cm.isVersionChecked());
  143. }
  144. TEST(testVersion, Negative) {
  145. FileManMock fm;
  146. CmdManForTest cm(fm, dummyDebugPrint);
  147. cm.initConnected();
  148. std::string cmd;
  149. std::vector<std::string> args;
  150. Json::Value root;
  151. CmdMan::CmdRet retvalCmd;
  152. CmdMan::CmdRet retvalHdl;
  153. int servermajor, serverminor;
  154. // prepare root/cmd/args
  155. cmd = "version";
  156. args = {};
  157. servermajor = (protocolMajorVersion == 0) ? 42 : 0;
  158. serverminor = (protocolMinorVersion == 3) ? 42 : 3;
  159. root["major"] = servermajor;
  160. root["minor"] = serverminor;
  161. root["accept"] = false;
  162. // stick into cmdman
  163. retvalCmd = cm.execute(cmd, args);
  164. retvalHdl = cm.handle(root);
  165. // check things
  166. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  167. EXPECT_EQ(retvalCmd.msg["major"], protocolMajorVersion);
  168. EXPECT_EQ(retvalCmd.msg["minor"], protocolMinorVersion);
  169. EXPECT_EQ(retvalHdl.type, CmdMan::error | CmdMan::close);
  170. EXPECT_EQ(retvalHdl.msg["command"].asString(), "version");
  171. EXPECT_EQ(retvalHdl.msg["serverversion"].asString(), std::to_string(servermajor) + "." + std::to_string(serverminor));
  172. EXPECT_EQ(retvalHdl.msg["clientversion"].asString(), std::to_string(protocolMajorVersion) + "." + std::to_string(protocolMinorVersion));
  173. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  174. EXPECT_TRUE(cm.isNotConnected());
  175. }
  176. TEST(testVersion, AlreadyLoggedIn) {
  177. FileManMock fm;
  178. CmdManForTest cm(fm, dummyDebugPrint);
  179. cm.initLoggedIn();
  180. std::string cmd;
  181. std::vector<std::string> args;
  182. Json::Value root;
  183. CmdMan::CmdRet retvalCmd;
  184. CmdMan::CmdRet retvalHdl;
  185. // prepare cmd/args
  186. cmd = "version";
  187. args = {};
  188. // stick into cmdman
  189. retvalCmd = cm.execute(cmd, args);
  190. // check things
  191. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  192. // TODO: check some more things maybe
  193. EXPECT_TRUE(cm.isLoggedIn());
  194. }
  195. TEST(testLogin, Positive) {
  196. FileManMock fm;
  197. CmdManForTest cm(fm, dummyDebugPrint);
  198. cm.initVersionChecked();
  199. std::string cmd;
  200. std::vector<std::string> args;
  201. Json::Value root;
  202. CmdMan::CmdRet retvalCmd;
  203. CmdMan::CmdRet retvalHdl;
  204. // prepare root/cmd/args
  205. cmd = "login";
  206. args = {"usernem", "paswod"};
  207. root["accept"] = true;
  208. // stick into cmdman
  209. retvalCmd = cm.execute(cmd, args);
  210. retvalHdl = cm.handle(root);
  211. // check things
  212. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  213. EXPECT_TRUE(retvalCmd.msg["login"].asBool());
  214. EXPECT_EQ(retvalCmd.msg["user"].asString(), "usernem");
  215. EXPECT_EQ(retvalCmd.msg["pass"].asString(), "paswod");
  216. EXPECT_FALSE(retvalCmd.msg["cancel"].asBool());
  217. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  218. EXPECT_EQ(retvalHdl.msg["command"].asString(), "login");
  219. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  220. EXPECT_TRUE(cm.isLoggedIn());
  221. }
  222. TEST(testLogin, Negative) {
  223. FileManMock fm;
  224. CmdManForTest cm(fm, dummyDebugPrint);
  225. cm.initVersionChecked();
  226. std::string cmd;
  227. std::vector<std::string> args;
  228. Json::Value root;
  229. CmdMan::CmdRet retvalCmd;
  230. CmdMan::CmdRet retvalHdl;
  231. // prepare root/cmd/args
  232. cmd = "login";
  233. args = {"usernem", "paswod"};
  234. root["accept"] = false;
  235. root["error"] = "fancy error string";
  236. // stick into cmdman
  237. retvalCmd = cm.execute(cmd, args);
  238. retvalHdl = cm.handle(root);
  239. // check things
  240. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  241. EXPECT_TRUE(retvalCmd.msg["login"].asBool());
  242. EXPECT_EQ(retvalCmd.msg["user"].asString(), "usernem");
  243. EXPECT_EQ(retvalCmd.msg["pass"].asString(), "paswod");
  244. EXPECT_FALSE(retvalCmd.msg["cancel"].asBool());
  245. EXPECT_EQ(retvalHdl.type, CmdMan::error | CmdMan::close);
  246. EXPECT_EQ(retvalHdl.msg["command"].asString(), "login");
  247. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  248. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("fancy error string"));
  249. EXPECT_TRUE(cm.isNotConnected());
  250. }
  251. TEST(testLogin, TooFewArgs) {
  252. FileManMock fm;
  253. CmdManForTest cm(fm, dummyDebugPrint);
  254. cm.initVersionChecked();
  255. std::string cmd;
  256. std::vector<std::string> args;
  257. CmdMan::CmdRet retvalCmd;
  258. // prepare cmd/args
  259. cmd = "login";
  260. args = {"usernem"};
  261. // stick into cmdman
  262. retvalCmd = cm.execute(cmd, args);
  263. // check things
  264. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  265. EXPECT_EQ(retvalCmd.msg["command"].asString(), "login");
  266. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  267. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  268. EXPECT_TRUE(cm.isVersionChecked());
  269. }
  270. TEST(testLogin, AlreadyLoggedIn) {
  271. FileManMock fm;
  272. CmdManForTest cm(fm, dummyDebugPrint);
  273. cm.initLoggedIn();
  274. std::string cmd;
  275. std::vector<std::string> args;
  276. CmdMan::CmdRet retvalCmd;
  277. // prepare root/cmd/args
  278. cmd = "login";
  279. args = {"usernem", "paswod"};
  280. // stick into cmdman
  281. retvalCmd = cm.execute(cmd, args);
  282. // check things
  283. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  284. EXPECT_EQ(retvalCmd.msg["command"].asString(), "login");
  285. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  286. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  287. EXPECT_TRUE(cm.isLoggedIn());
  288. }
  289. TEST(testLogin, TwoRequestsInRow) {
  290. FileManMock fm;
  291. CmdManForTest cm(fm, dummyDebugPrint);
  292. cm.initVersionChecked();
  293. std::string cmd;
  294. std::vector<std::string> args;
  295. Json::Value root;
  296. CmdMan::CmdRet retvalCmd1;
  297. CmdMan::CmdRet retvalCmd2;
  298. CmdMan::CmdRet retvalHdl;
  299. // prepare root/cmd/args
  300. cmd = "login";
  301. args = {"usernem", "paswod"};
  302. root["accept"] = true;
  303. // stick into cmdman
  304. retvalCmd1 = cm.execute(cmd, args);
  305. retvalCmd2 = cm.execute(cmd, args);
  306. retvalHdl = cm.handle(root);
  307. // check things
  308. EXPECT_EQ(retvalCmd1.type, CmdMan::send);
  309. EXPECT_TRUE(retvalCmd1.msg["login"].asBool());
  310. EXPECT_EQ(retvalCmd1.msg["user"].asString(), "usernem");
  311. EXPECT_EQ(retvalCmd1.msg["pass"].asString(), "paswod");
  312. EXPECT_FALSE(retvalCmd1.msg["cancel"].asBool());
  313. EXPECT_EQ(retvalCmd2.type, CmdMan::error);
  314. EXPECT_EQ(retvalCmd2.msg["command"].asString(), "login");
  315. EXPECT_FALSE(retvalCmd2.msg["accept"].asBool());
  316. EXPECT_NE(retvalCmd2.msg["error"].asString(), "");
  317. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  318. EXPECT_EQ(retvalHdl.msg["command"].asString(), "login");
  319. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  320. EXPECT_TRUE(cm.isLoggedIn());
  321. }
  322. TEST(testSignup, Positive) {
  323. FileManMock fm;
  324. CmdManForTest cm(fm, dummyDebugPrint);
  325. cm.initVersionChecked();
  326. std::string cmd;
  327. std::vector<std::string> args;
  328. Json::Value root;
  329. CmdMan::CmdRet retvalCmd;
  330. CmdMan::CmdRet retvalHdl;
  331. // prepare root/cmd/args
  332. cmd = "signup";
  333. args = {"usernem", "paswod"};
  334. root["accept"] = true;
  335. // stick into cmdman
  336. retvalCmd = cm.execute(cmd, args);
  337. retvalHdl = cm.handle(root);
  338. // check things
  339. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  340. EXPECT_FALSE(retvalCmd.msg["login"].asBool());
  341. EXPECT_EQ(retvalCmd.msg["user"].asString(), "usernem");
  342. EXPECT_EQ(retvalCmd.msg["pass"].asString(), "paswod");
  343. EXPECT_FALSE(retvalCmd.msg["cancel"].asBool());
  344. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  345. EXPECT_EQ(retvalHdl.msg["command"].asString(), "signup");
  346. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  347. EXPECT_TRUE(cm.isLoggedIn());
  348. }
  349. TEST(testSignup, Negative) {
  350. FileManMock fm;
  351. CmdManForTest cm(fm, dummyDebugPrint);
  352. cm.initVersionChecked();
  353. std::string cmd;
  354. std::vector<std::string> args;
  355. Json::Value root;
  356. CmdMan::CmdRet retvalCmd;
  357. CmdMan::CmdRet retvalHdl;
  358. // prepare root/cmd/args
  359. cmd = "signup";
  360. args = {"usernem", "paswod"};
  361. root["accept"] = false;
  362. root["error"] = "fancy error string";
  363. // stick into cmdman
  364. retvalCmd = cm.execute(cmd, args);
  365. retvalHdl = cm.handle(root);
  366. // check things
  367. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  368. EXPECT_FALSE(retvalCmd.msg["login"].asBool());
  369. EXPECT_EQ(retvalCmd.msg["user"].asString(), "usernem");
  370. EXPECT_EQ(retvalCmd.msg["pass"].asString(), "paswod");
  371. EXPECT_FALSE(retvalCmd.msg["cancel"].asBool());
  372. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  373. EXPECT_EQ(retvalHdl.msg["command"].asString(), "signup");
  374. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  375. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("fancy error string"));
  376. EXPECT_TRUE(cm.isVersionChecked());
  377. }
  378. TEST(testSignup, TooFewArgs) {
  379. FileManMock fm;
  380. CmdManForTest cm(fm, dummyDebugPrint);
  381. cm.initVersionChecked();
  382. std::string cmd;
  383. std::vector<std::string> args;
  384. CmdMan::CmdRet retvalCmd;
  385. // prepare cmd/args
  386. cmd = "signup";
  387. args = {"usernem"};
  388. // stick into cmdman
  389. retvalCmd = cm.execute(cmd, args);
  390. // check things
  391. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  392. EXPECT_EQ(retvalCmd.msg["command"].asString(), "signup");
  393. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  394. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  395. EXPECT_TRUE(cm.isVersionChecked());
  396. }
  397. /*
  398. * =====================================
  399. * tests for disconnect and exit
  400. */
  401. TEST(testDisconnect, LoggedIn) {
  402. FileManMock fm;
  403. CmdManForTest cm(fm, dummyDebugPrint);
  404. cm.initLoggedIn();
  405. std::string cmd;
  406. std::vector<std::string> args;
  407. Json::Value root;
  408. CmdMan::CmdRet retvalCmd;
  409. CmdMan::CmdRet retvalHdl;
  410. // prepare root/cmd/args
  411. cmd = "disconnect";
  412. args = {};
  413. root["command"] = "close";
  414. root["response"] = "bye";
  415. // stick into cmdman
  416. retvalCmd = cm.execute(cmd, args);
  417. retvalHdl = cm.handle(root);
  418. // check things
  419. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  420. EXPECT_EQ(retvalCmd.msg["command"].asString(), "close");
  421. EXPECT_EQ(retvalHdl.type, CmdMan::close | CmdMan::print);
  422. EXPECT_EQ(retvalHdl.msg["command"].asString(), "disconnect");
  423. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  424. EXPECT_TRUE(cm.isNotConnected());
  425. }
  426. TEST(testDisconnect, NotLoggedIn) {
  427. FileManMock fm;
  428. CmdManForTest cm(fm, dummyDebugPrint);
  429. cm.initVersionChecked();
  430. std::string cmd;
  431. std::vector<std::string> args;
  432. Json::Value root;
  433. CmdMan::CmdRet retvalCmd;
  434. CmdMan::CmdRet retvalHdl;
  435. // prepare root/cmd/args
  436. cmd = "disconnect";
  437. args = {};
  438. // stick into cmdman
  439. retvalCmd = cm.execute(cmd, args);
  440. // check things
  441. EXPECT_EQ(retvalCmd.type, CmdMan::send | CmdMan::close);
  442. EXPECT_FALSE(retvalCmd.msg["login"].asBool());
  443. EXPECT_EQ(retvalCmd.msg["user"].asString(), "");
  444. EXPECT_EQ(retvalCmd.msg["pass"].asString(), "");
  445. EXPECT_TRUE(retvalCmd.msg["cancel"].asBool());
  446. EXPECT_TRUE(cm.isNotConnected());
  447. }
  448. TEST(testExit, LoggedIn) {
  449. FileManMock fm;
  450. CmdManForTest cm(fm, dummyDebugPrint);
  451. cm.initLoggedIn();
  452. std::string cmd;
  453. std::vector<std::string> args;
  454. Json::Value root;
  455. CmdMan::CmdRet retvalCmdExit;
  456. CmdMan::CmdRet retvalCmdDisconnect;
  457. CmdMan::CmdRet retvalHdl;
  458. CmdMan::CmdRet retvalLastExit;
  459. // prepare root/cmd/args
  460. cmd = "exit";
  461. args = {};
  462. root["command"] = "close";
  463. root["response"] = "bye";
  464. // stick into cmdman and check things
  465. retvalCmdExit = cm.execute(cmd, args);
  466. EXPECT_EQ(retvalCmdExit.type, CmdMan::none);
  467. EXPECT_EQ(retvalCmdExit.nextcommand, "disconnect");
  468. retvalCmdDisconnect = cm.execute("disconnect", args);
  469. EXPECT_EQ(retvalCmdDisconnect.type, CmdMan::send);
  470. EXPECT_EQ(retvalCmdDisconnect.msg["command"].asString(), "close");
  471. retvalHdl = cm.handle(root);
  472. EXPECT_EQ(retvalHdl.type, CmdMan::close | CmdMan::print);
  473. EXPECT_EQ(retvalHdl.msg["command"].asString(), "disconnect");
  474. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  475. EXPECT_EQ(retvalHdl.nextcommand, "exit");
  476. EXPECT_TRUE(cm.isNotConnected());
  477. retvalLastExit = cm.execute("exit", args);
  478. EXPECT_EQ(retvalLastExit.type, CmdMan::exit);
  479. EXPECT_EQ(retvalLastExit.msg["command"].asString(), "exit");
  480. }
  481. TEST(testExit, NotLoggedIn) {
  482. FileManMock fm;
  483. CmdManForTest cm(fm, dummyDebugPrint);
  484. cm.initVersionChecked();
  485. std::string cmd;
  486. std::vector<std::string> args;
  487. Json::Value root;
  488. CmdMan::CmdRet retvalCmdExit;
  489. CmdMan::CmdRet retvalCmdDisconnect;
  490. CmdMan::CmdRet retvalHdl;
  491. CmdMan::CmdRet retvalLastExit;
  492. // prepare root/cmd/args
  493. cmd = "exit";
  494. args = {};
  495. root["command"] = "close";
  496. root["response"] = "bye";
  497. // stick into cmdman and check things
  498. retvalCmdExit = cm.execute(cmd, args);
  499. EXPECT_EQ(retvalCmdExit.type, CmdMan::none);
  500. EXPECT_EQ(retvalCmdExit.nextcommand, "disconnect");
  501. retvalCmdDisconnect = cm.execute("disconnect", args);
  502. EXPECT_EQ(retvalCmdDisconnect.type, CmdMan::send | CmdMan::close);
  503. EXPECT_FALSE(retvalCmdDisconnect.msg["login"].asBool());
  504. EXPECT_EQ(retvalCmdDisconnect.msg["user"].asString(), "");
  505. EXPECT_EQ(retvalCmdDisconnect.msg["pass"].asString(), "");
  506. EXPECT_TRUE(retvalCmdDisconnect.msg["cancel"].asBool());
  507. EXPECT_EQ(retvalCmdDisconnect.nextcommand, "exit");
  508. EXPECT_TRUE(cm.isNotConnected());
  509. retvalLastExit = cm.execute("exit", args);
  510. EXPECT_EQ(retvalLastExit.type, CmdMan::exit);
  511. EXPECT_EQ(retvalLastExit.msg["command"].asString(), "exit");
  512. }
  513. TEST(testExit, NotConnected) {
  514. FileManMock fm;
  515. CmdManForTest cm(fm, dummyDebugPrint);
  516. std::string cmd;
  517. std::vector<std::string> args;
  518. CmdMan::CmdRet retvalCmd;
  519. // prepare root/cmd/args
  520. cmd = "exit";
  521. args = {};
  522. // stick into cmdman and check things
  523. retvalCmd = cm.execute(cmd, args);
  524. EXPECT_EQ(retvalCmd.type, CmdMan::exit);
  525. EXPECT_EQ(retvalCmd.msg["command"].asString(), "exit");
  526. }
  527. /* =====================================
  528. * tests for put[data]
  529. */
  530. TEST(testPut, TooFewArgs) {
  531. FileManMock fm;
  532. CmdManForTest cm(fm, dummyDebugPrint);
  533. cm.initLoggedIn();
  534. std::string cmd;
  535. std::vector<std::string> args;
  536. CmdMan::CmdRet retvalCmd;
  537. // prepare cmd/args
  538. cmd = "put";
  539. args = {};
  540. // stick into cmdman
  541. retvalCmd = cm.execute(cmd, args);
  542. // check things
  543. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  544. EXPECT_EQ(retvalCmd.msg["command"].asString(), "put");
  545. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  546. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  547. EXPECT_TRUE(cm.isLoggedIn());
  548. }
  549. TEST(testPut, Positive) {
  550. FileManMock fm;
  551. CmdManForTest cm(fm, dummyDebugPrint);
  552. cm.initLoggedIn();
  553. std::string cmd;
  554. std::vector<std::string> args;
  555. Json::Value root;
  556. CmdMan::CmdRet retvalCmd;
  557. CmdMan::CmdRet retvalHdl;
  558. // prepare root/cmd/args
  559. cmd = "put";
  560. args = {"../some/nice/filepath/filename.txt"};
  561. root["command"] = "put";
  562. root["file"] = "filename.txt";
  563. root["accept"] = true;
  564. EXPECT_CALL(fm, openPut(testing::_)).WillOnce(testing::Return(true));
  565. ON_CALL(fm, getPutSize()).WillByDefault(testing::Return(5));
  566. ON_CALL(fm, getPutChunks()).WillByDefault(testing::Return(1));
  567. ON_CALL(fm, getPutName()).WillByDefault(testing::Return("filename.txt"));
  568. EXPECT_CALL(fm, cancelGet).Times(0); // not called
  569. // stick into cmdman and check things
  570. retvalCmd = cm.execute(cmd, args);
  571. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  572. EXPECT_EQ(retvalCmd.msg["command"].asString(), "put");
  573. EXPECT_EQ(retvalCmd.msg["file"].asString(), "filename.txt");
  574. EXPECT_EQ(retvalCmd.msg["size"].asInt(), 5);
  575. EXPECT_EQ(retvalCmd.msg["chunks"].asInt(), 1);
  576. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(true));
  577. retvalHdl = cm.handle(root);
  578. EXPECT_EQ(retvalHdl.type, CmdMan::print | CmdMan::send);
  579. EXPECT_EQ(retvalHdl.msg["command"].asString(), "put");
  580. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  581. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  582. EXPECT_EQ(retvalHdl.nextcommand, "putdata");
  583. EXPECT_TRUE(cm.isLoggedIn());
  584. }
  585. TEST(testPutdata, Positive_LastChunk) {
  586. FileManMock fm;
  587. CmdManForTest cm(fm, dummyDebugPrint);
  588. cm.initLoggedIn();
  589. std::string cmd;
  590. std::vector<std::string> args;
  591. Json::Value root;
  592. CmdMan::CmdRet retvalCmd;
  593. CmdMan::CmdRet retvalHdl;
  594. /**logically continuation of TEST(testPut, Positive)**/
  595. // prepare root/cmd/args
  596. cmd = "putdata";
  597. args = {};
  598. root["command"] = "putdata";
  599. root["file"] = "filename.txt";
  600. root["received"] = 0;
  601. root["cancel"] = false;
  602. ON_CALL(fm, getPutName()).WillByDefault(testing::Return("filename.txt"));
  603. ON_CALL(fm, readBase64()).WillByDefault(testing::Return("cool Base64 string"));
  604. ON_CALL(fm, getPutRemainingChunks()).WillByDefault(testing::Return(0));
  605. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(true));
  606. EXPECT_CALL(fm, cancelPut).Times(0); // not called
  607. EXPECT_CALL(fm, closePut);
  608. // stick into cmdman and check things
  609. retvalCmd = cm.execute(cmd, args);
  610. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  611. EXPECT_EQ(retvalCmd.msg["command"].asString(), "putdata");
  612. EXPECT_EQ(retvalCmd.msg["file"].asString(), "filename.txt");
  613. EXPECT_EQ(retvalCmd.msg["data"].asString(), "cool Base64 string");
  614. EXPECT_EQ(retvalCmd.msg["remaining"].asInt(), 0);
  615. EXPECT_FALSE(retvalCmd.msg["cancel"].asBool());
  616. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(true));
  617. retvalHdl = cm.handle(root);
  618. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  619. EXPECT_EQ(retvalHdl.msg["command"].asString(), "putdata");
  620. EXPECT_FALSE(retvalHdl.msg["cancel"].asBool());
  621. EXPECT_EQ(retvalHdl.msg["speed"].asInt(), 0);
  622. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  623. EXPECT_EQ(retvalHdl.msg["error"].asString(), "");
  624. EXPECT_TRUE(cm.isLoggedIn());
  625. }
  626. TEST(testPutdata, Positive_ChunksRemaining) {
  627. FileManMock fm;
  628. CmdManForTest cm(fm, dummyDebugPrint);
  629. cm.initLoggedIn();
  630. std::string cmd;
  631. std::vector<std::string> args;
  632. Json::Value root;
  633. CmdMan::CmdRet retvalCmd;
  634. CmdMan::CmdRet retvalHdl;
  635. // prepare root/cmd/args
  636. cmd = "putdata";
  637. args = {};
  638. root["command"] = "putdata";
  639. root["file"] = "filename.txt";
  640. root["received"] = 42;
  641. root["cancel"] = false;
  642. ON_CALL(fm, getPutName()).WillByDefault(testing::Return("filename.txt"));
  643. ON_CALL(fm, readBase64()).WillByDefault(testing::Return("cool Base64 string"));
  644. ON_CALL(fm, getPutRemainingChunks()).WillByDefault(testing::Return(42));
  645. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(true));
  646. EXPECT_CALL(fm, cancelGet).Times(0); // not called
  647. // stick into cmdman and check things
  648. retvalCmd = cm.execute(cmd, args);
  649. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  650. EXPECT_EQ(retvalCmd.msg["command"].asString(), "putdata");
  651. EXPECT_EQ(retvalCmd.msg["file"].asString(), "filename.txt");
  652. EXPECT_EQ(retvalCmd.msg["data"].asString(), "cool Base64 string");
  653. EXPECT_EQ(retvalCmd.msg["remaining"].asInt(), 42);
  654. EXPECT_FALSE(retvalCmd.msg["cancel"].asBool());
  655. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(true));
  656. retvalHdl = cm.handle(root);
  657. EXPECT_EQ(retvalHdl.type, CmdMan::print | CmdMan::send);
  658. EXPECT_EQ(retvalHdl.msg["command"].asString(), "putdata");
  659. EXPECT_FALSE(retvalHdl.msg["cancel"].asBool());
  660. EXPECT_EQ(retvalHdl.msg["speed"].asInt(), 0);
  661. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  662. EXPECT_EQ(retvalHdl.msg["error"].asString(), "");
  663. EXPECT_EQ(retvalHdl.nextcommand, "putdata");
  664. EXPECT_TRUE(cm.isLoggedIn());
  665. }
  666. TEST(testPut, Negative_FileNotExisting) {
  667. FileManMock fm;
  668. CmdManForTest cm(fm, dummyDebugPrint);
  669. cm.initLoggedIn();
  670. std::string cmd;
  671. std::vector<std::string> args;
  672. Json::Value root;
  673. CmdMan::CmdRet retvalCmd;
  674. CmdMan::CmdRet retvalHdl;
  675. // prepare root/cmd/args
  676. cmd = "put";
  677. args = {"../some/nice/filepath/filename.txt"};
  678. EXPECT_CALL(fm, openPut(testing::_)).WillOnce(testing::Return(false));
  679. ON_CALL(fm, pathToFilename("../some/nice/filepath/filename.txt")).WillByDefault(testing::Return("filename.txt"));
  680. // stick into cmdman and check things
  681. retvalCmd = cm.execute(cmd, args);
  682. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  683. EXPECT_EQ(retvalCmd.msg["command"].asString(), "put");
  684. EXPECT_EQ(retvalCmd.msg["file"].asString(), "filename.txt");
  685. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  686. EXPECT_TRUE(cm.isLoggedIn());
  687. }
  688. TEST(testPut, Negative_ServerError) {
  689. FileManMock fm;
  690. CmdManForTest cm(fm, dummyDebugPrint);
  691. cm.initLoggedIn();
  692. /* tests the handling of server reply only,
  693. * initial request assumed as correct (tested in other tests) */
  694. std::string cmd;
  695. std::vector<std::string> args;
  696. Json::Value root;
  697. CmdMan::CmdRet retvalHdl;
  698. // prepare root
  699. root["command"] = "put";
  700. root["file"] = "filename.txt";
  701. root["accept"] = false;
  702. root["error"] = "foobar";
  703. ON_CALL(fm, getPutSize()).WillByDefault(testing::Return(5));
  704. ON_CALL(fm, getPutChunks()).WillByDefault(testing::Return(1));
  705. ON_CALL(fm, getPutName()).WillByDefault(testing::Return("filename.txt"));
  706. EXPECT_CALL(fm, cancelPut);
  707. retvalHdl = cm.handle(root);
  708. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  709. EXPECT_EQ(retvalHdl.msg["command"].asString(), "put");
  710. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  711. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  712. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("foobar"));
  713. EXPECT_TRUE(cm.isLoggedIn());
  714. }
  715. TEST(testPut, Negative_ServerWrongFile) {
  716. FileManMock fm;
  717. CmdManForTest cm(fm, dummyDebugPrint);
  718. cm.initLoggedIn();
  719. /* tests the handling of server reply only,
  720. * initial request assumed as correct (tested in other tests) */
  721. std::string cmd;
  722. std::vector<std::string> args;
  723. Json::Value root;
  724. CmdMan::CmdRet retvalHdl;
  725. // prepare root
  726. root["command"] = "put";
  727. root["file"] = "completely_different_filename.txt";
  728. root["accept"] = true;
  729. ON_CALL(fm, getPutSize()).WillByDefault(testing::Return(5));
  730. ON_CALL(fm, getPutChunks()).WillByDefault(testing::Return(1));
  731. ON_CALL(fm, getPutName()).WillByDefault(testing::Return("filename.txt"));
  732. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(true));
  733. EXPECT_CALL(fm, cancelPut);
  734. retvalHdl = cm.handle(root);
  735. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  736. EXPECT_EQ(retvalHdl.msg["command"].asString(), "put");
  737. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  738. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  739. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  740. EXPECT_TRUE(cm.isLoggedIn());
  741. }
  742. TEST(testPutdata, Negative_ServerError) {
  743. FileManMock fm;
  744. CmdManForTest cm(fm, dummyDebugPrint);
  745. cm.initLoggedIn();
  746. /* tests the handling of server reply only,
  747. * initial request assumed as correct (tested in other tests) */
  748. std::string cmd;
  749. std::vector<std::string> args;
  750. Json::Value root;
  751. CmdMan::CmdRet retvalHdl;
  752. // prepare root
  753. root["command"] = "putdata";
  754. root["file"] = "filename.txt";
  755. root["received"] = 42;
  756. root["cancel"] = true;
  757. root["error"] = "cool foobar";
  758. ON_CALL(fm, getPutName()).WillByDefault(testing::Return("filename.txt"));
  759. ON_CALL(fm, getPutRemainingChunks()).WillByDefault(testing::Return(42));
  760. ON_CALL(fm, isPutting()).WillByDefault(testing::Return(true));
  761. retvalHdl = cm.handle(root);
  762. EXPECT_CALL(fm, cancelPut);
  763. retvalHdl = cm.handle(root);
  764. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  765. EXPECT_EQ(retvalHdl.msg["command"].asString(), "putdata");
  766. EXPECT_TRUE(retvalHdl.msg["cancel"].asBool());
  767. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  768. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("cool foobar"));
  769. EXPECT_TRUE(cm.isLoggedIn());
  770. }
  771. TEST(testPutdata, Negative_ServerWrongFile) {
  772. FileManMock fm;
  773. CmdManForTest cm(fm, dummyDebugPrint);
  774. cm.initLoggedIn();
  775. /* tests the handling of server reply only,
  776. * initial request assumed as correct (tested in other tests) */
  777. std::string cmd;
  778. std::vector<std::string> args;
  779. Json::Value root;
  780. CmdMan::CmdRet retvalHdl;
  781. // prepare root
  782. root["command"] = "putdata";
  783. root["file"] = "completely_different_filename.txt";
  784. root["received"] = 42;
  785. root["cancel"] = false;
  786. ON_CALL(fm, getPutName()).WillByDefault(testing::Return("filename.txt"));
  787. ON_CALL(fm, getPutRemainingChunks()).WillByDefault(testing::Return(42));
  788. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(true));
  789. EXPECT_CALL(fm, cancelPut);
  790. retvalHdl = cm.handle(root);
  791. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  792. EXPECT_EQ(retvalHdl.msg["command"].asString(), "putdata");
  793. EXPECT_TRUE(retvalHdl.msg["cancel"].asBool());
  794. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  795. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  796. EXPECT_TRUE(cm.isLoggedIn());
  797. }
  798. TEST(testPutdata, Negative_ServerWrongChunkNumber) {
  799. FileManMock fm;
  800. CmdManForTest cm(fm, dummyDebugPrint);
  801. cm.initLoggedIn();
  802. /* tests the handling of server reply only,
  803. * initial request assumed as correct (tested in other tests) */
  804. std::string cmd;
  805. std::vector<std::string> args;
  806. Json::Value root;
  807. CmdMan::CmdRet retvalHdl;
  808. // prepare root
  809. root["command"] = "putdata";
  810. root["file"] = "filename.txt";
  811. root["received"] = 23;
  812. root["cancel"] = false;
  813. ON_CALL(fm, getPutName()).WillByDefault(testing::Return("filename.txt"));
  814. ON_CALL(fm, getPutRemainingChunks()).WillByDefault(testing::Return(42));
  815. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(true));
  816. EXPECT_CALL(fm, cancelPut);
  817. retvalHdl = cm.handle(root);
  818. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  819. EXPECT_EQ(retvalHdl.msg["command"].asString(), "putdata");
  820. EXPECT_TRUE(retvalHdl.msg["cancel"].asBool());
  821. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  822. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  823. EXPECT_TRUE(cm.isLoggedIn());
  824. }
  825. TEST(testPut, Negative_NoFileOpen_ServerMessage) {
  826. FileManMock fm;
  827. CmdManForTest cm(fm, dummyDebugPrint);
  828. cm.initLoggedIn();
  829. std::string cmd;
  830. std::vector<std::string> args;
  831. Json::Value root;
  832. CmdMan::CmdRet retvalCmd;
  833. CmdMan::CmdRet retvalHdl;
  834. // prepare root
  835. root["command"] = "put";
  836. root["file"] = "filename.txt";
  837. root["accept"] = true;
  838. // handle reply
  839. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(false));
  840. retvalHdl = cm.handle(root);
  841. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  842. EXPECT_EQ(retvalHdl.msg["command"].asString(), "put");
  843. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  844. EXPECT_TRUE(cm.isLoggedIn());
  845. }
  846. TEST(testPutdata, Negative_NoFileOpen_ServerMessage) {
  847. FileManMock fm;
  848. CmdManForTest cm(fm, dummyDebugPrint);
  849. cm.initLoggedIn();
  850. std::string cmd;
  851. std::vector<std::string> args;
  852. Json::Value root;
  853. CmdMan::CmdRet retvalCmd;
  854. CmdMan::CmdRet retvalHdl;
  855. // prepare root
  856. root["command"] = "putdata";
  857. root["file"] = "filename.txt";
  858. root["received"] = 23;
  859. root["cancel"] = false;
  860. // handle reply
  861. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(false));
  862. retvalHdl = cm.handle(root);
  863. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  864. EXPECT_EQ(retvalHdl.msg["command"].asString(), "putdata");
  865. // EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt"); // no guarantee should be given...
  866. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  867. EXPECT_TRUE(cm.isLoggedIn());
  868. }
  869. TEST(testPutdata, Negative_NoFileOpen_UserRequest) {
  870. FileManMock fm;
  871. CmdManForTest cm(fm, dummyDebugPrint);
  872. cm.initLoggedIn();
  873. std::string cmd;
  874. std::vector<std::string> args;
  875. Json::Value root;
  876. CmdMan::CmdRet retvalCmd;
  877. CmdMan::CmdRet retvalHdl;
  878. // prepare root
  879. cmd = "putdata";
  880. args = {};
  881. EXPECT_CALL(fm, isPutting()).WillOnce(testing::Return(false));
  882. // stick into cmdman and check things
  883. retvalCmd = cm.execute(cmd, args);
  884. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  885. EXPECT_EQ(retvalCmd.msg["command"].asString(), "putdata");
  886. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  887. EXPECT_TRUE(cm.isLoggedIn());
  888. }
  889. /* =====================================
  890. * tests for head
  891. */
  892. TEST(testHead, TooFewArgs) {
  893. FileManMock fm;
  894. CmdManForTest cm(fm, dummyDebugPrint);
  895. cm.initLoggedIn();
  896. std::string cmd;
  897. std::vector<std::string> args;
  898. Json::Value root;
  899. CmdMan::CmdRet retvalCmd;
  900. CmdMan::CmdRet retvalHdl;
  901. // prepare cmd/args
  902. cmd = "head";
  903. args = {};
  904. // stick into cmdman and check things
  905. retvalCmd = cm.execute(cmd, args);
  906. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  907. EXPECT_EQ(retvalCmd.msg["command"].asString(), "head");
  908. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  909. retvalHdl = cm.handle(root);
  910. EXPECT_TRUE(cm.isLoggedIn());
  911. }
  912. TEST(testHead, Positive) {
  913. FileManMock fm;
  914. CmdManForTest cm(fm, dummyDebugPrint);
  915. cm.initLoggedIn();
  916. std::string cmd;
  917. std::vector<std::string> args;
  918. Json::Value root;
  919. CmdMan::CmdRet retvalCmd;
  920. CmdMan::CmdRet retvalHdl;
  921. // prepare root/cmd/args
  922. cmd = "head";
  923. args = {"filename.txt"};
  924. root["command"] = "head";
  925. root["file"] = "filename.txt";
  926. root["accept"] = true;
  927. root["data"] = "this string is 4 bytes (obviously)";
  928. // stick into cmdman and check things
  929. retvalCmd = cm.execute(cmd, args);
  930. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  931. EXPECT_EQ(retvalCmd.msg["command"].asString(), "head");
  932. EXPECT_EQ(retvalCmd.msg["file"].asString(), "filename.txt");
  933. retvalHdl = cm.handle(root);
  934. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  935. EXPECT_EQ(retvalHdl.msg["command"].asString(), "head");
  936. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  937. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  938. EXPECT_EQ(retvalHdl.msg["data"].asString(), "this string is 4 bytes (obviously)");
  939. EXPECT_TRUE(cm.isLoggedIn());
  940. }
  941. TEST(testHead, Negative) {
  942. FileManMock fm;
  943. CmdManForTest cm(fm, dummyDebugPrint);
  944. cm.initLoggedIn();
  945. std::string cmd;
  946. std::vector<std::string> args;
  947. Json::Value root;
  948. CmdMan::CmdRet retvalCmd;
  949. CmdMan::CmdRet retvalHdl;
  950. // prepare root/cmd/args
  951. cmd = "head";
  952. args = {"filename.txt"};
  953. root["command"] = "head";
  954. root["file"] = "filename.txt";
  955. root["accept"] = false;
  956. root["error"] = "this is a fancy error message";
  957. // stick into cmdman and check things
  958. retvalCmd = cm.execute(cmd, args);
  959. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  960. EXPECT_EQ(retvalCmd.msg["command"].asString(), "head");
  961. EXPECT_EQ(retvalCmd.msg["file"].asString(), "filename.txt");
  962. retvalHdl = cm.handle(root);
  963. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  964. EXPECT_EQ(retvalHdl.msg["command"].asString(), "head");
  965. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  966. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  967. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("this is a fancy error message"));
  968. EXPECT_TRUE(cm.isLoggedIn());
  969. }
  970. /* =====================================
  971. * tests for get[data]
  972. */
  973. TEST(testGet, TooFewArgs) {
  974. FileManMock fm;
  975. CmdManForTest cm(fm, dummyDebugPrint);
  976. cm.initLoggedIn();
  977. std::string cmd;
  978. std::vector<std::string> args;
  979. CmdMan::CmdRet retvalCmd;
  980. // prepare cmd/args
  981. cmd = "get";
  982. args = {};
  983. // stick into cmdman
  984. retvalCmd = cm.execute(cmd, args);
  985. // check things
  986. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  987. EXPECT_EQ(retvalCmd.msg["command"].asString(), "get");
  988. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  989. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  990. EXPECT_TRUE(cm.isLoggedIn());
  991. }
  992. TEST(testGet, Positive) {
  993. FileManMock fm;
  994. CmdManForTest cm(fm, dummyDebugPrint);
  995. cm.initLoggedIn();
  996. std::string cmd;
  997. std::vector<std::string> args;
  998. Json::Value root;
  999. CmdMan::CmdRet retvalCmd;
  1000. CmdMan::CmdRet retvalHdl;
  1001. // prepare root/cmd/args
  1002. cmd = "get";
  1003. args = {"../some/nice/filepath/filename.txt"};
  1004. root["command"] = "get";
  1005. root["file"] = "filename.txt";
  1006. root["accept"] = true;
  1007. root["chunks"] = 42;
  1008. EXPECT_CALL(fm, openGet("../some/nice/filepath/filename.txt")).WillOnce(testing::Return(true));
  1009. ON_CALL(fm, getGetName()).WillByDefault(testing::Return("filename.txt"));
  1010. EXPECT_CALL(fm, cancelGet).Times(0); // not called
  1011. // stick into cmdman and check things
  1012. retvalCmd = cm.execute(cmd, args);
  1013. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1014. EXPECT_EQ(retvalCmd.msg["command"].asString(), "get");
  1015. EXPECT_EQ(retvalCmd.msg["file"].asString(), "filename.txt");
  1016. EXPECT_CALL(fm, isGetting()).WillOnce(testing::Return(true));
  1017. EXPECT_CALL(fm, setGetChunks(42));
  1018. retvalHdl = cm.handle(root);
  1019. EXPECT_EQ(retvalHdl.type, CmdMan::print | CmdMan::send);
  1020. EXPECT_EQ(retvalHdl.msg["command"].asString(), "get");
  1021. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1022. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  1023. EXPECT_EQ(retvalHdl.nextcommand, "getdata");
  1024. EXPECT_TRUE(cm.isLoggedIn());
  1025. }
  1026. TEST(testGetdata, Positive_LastChunk) {
  1027. FileManMock fm;
  1028. CmdManForTest cm(fm, dummyDebugPrint);
  1029. cm.initLoggedIn();
  1030. std::string cmd;
  1031. std::vector<std::string> args;
  1032. Json::Value root;
  1033. CmdMan::CmdRet retvalCmd;
  1034. CmdMan::CmdRet retvalHdl;
  1035. /**logically continuation of TEST(testGet, Positive)**/
  1036. // prepare root/cmd/args
  1037. cmd = "getdata";
  1038. args = {};
  1039. root["command"] = "getdata";
  1040. root["file"] = "filename.txt";
  1041. root["remaining"] = 0;
  1042. root["cancel"] = false;
  1043. root["data"] = "cool Base64 string";
  1044. ON_CALL(fm, getGetName()).WillByDefault(testing::Return("filename.txt"));
  1045. EXPECT_CALL(fm, writeBase64("cool Base64 string"));
  1046. ON_CALL(fm, getGetRemainingChunks()).WillByDefault(testing::Return(0));
  1047. EXPECT_CALL(fm, isGetting()).WillOnce(testing::Return(true));
  1048. EXPECT_CALL(fm, cancelGet).Times(0); // not called
  1049. // stick into cmdman and check things
  1050. retvalCmd = cm.execute(cmd, args);
  1051. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1052. EXPECT_EQ(retvalCmd.msg["command"].asString(), "getdata");
  1053. EXPECT_EQ(retvalCmd.msg["file"].asString(), "filename.txt");
  1054. EXPECT_EQ(retvalCmd.msg["chunk"].asInt(), 0);
  1055. EXPECT_FALSE(retvalCmd.msg["cancel"].asBool());
  1056. EXPECT_CALL(fm, isGetting()).WillOnce(testing::Return(true));
  1057. retvalHdl = cm.handle(root);
  1058. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1059. EXPECT_EQ(retvalHdl.msg["command"].asString(), "getdata");
  1060. EXPECT_FALSE(retvalHdl.msg["cancel"].asBool());
  1061. EXPECT_EQ(retvalHdl.msg["speed"].asInt(), 0);
  1062. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  1063. EXPECT_EQ(retvalHdl.msg["error"].asString(), "");
  1064. EXPECT_TRUE(cm.isLoggedIn());
  1065. }
  1066. TEST(testGetdata, Positive_ChunksRemaining) {
  1067. FileManMock fm;
  1068. CmdManForTest cm(fm, dummyDebugPrint);
  1069. cm.initLoggedIn();
  1070. std::string cmd;
  1071. std::vector<std::string> args;
  1072. Json::Value root;
  1073. CmdMan::CmdRet retvalCmd;
  1074. CmdMan::CmdRet retvalHdl;
  1075. // prepare root/cmd/args
  1076. cmd = "getdata";
  1077. args = {};
  1078. root["command"] = "getdata";
  1079. root["file"] = "filename.txt";
  1080. root["remaining"] = 42;
  1081. root["cancel"] = false;
  1082. root["data"] = "cool Base64 string";
  1083. ON_CALL(fm, getGetName()).WillByDefault(testing::Return("filename.txt"));
  1084. EXPECT_CALL(fm, writeBase64("cool Base64 string"));
  1085. ON_CALL(fm, getGetRemainingChunks()).WillByDefault(testing::Return(42));
  1086. EXPECT_CALL(fm, isGetting()).WillOnce(testing::Return(true));
  1087. EXPECT_CALL(fm, cancelGet).Times(0); // not called
  1088. // stick into cmdman and check things
  1089. retvalCmd = cm.execute(cmd, args);
  1090. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1091. EXPECT_EQ(retvalCmd.msg["command"].asString(), "getdata");
  1092. EXPECT_EQ(retvalCmd.msg["file"].asString(), "filename.txt");
  1093. EXPECT_EQ(retvalCmd.msg["chunk"].asInt(), 42);
  1094. EXPECT_FALSE(retvalCmd.msg["cancel"].asBool());
  1095. EXPECT_CALL(fm, isGetting()).WillOnce(testing::Return(true));
  1096. retvalHdl = cm.handle(root);
  1097. EXPECT_EQ(retvalHdl.type, CmdMan::print | CmdMan::send);
  1098. EXPECT_EQ(retvalHdl.msg["command"].asString(), "getdata");
  1099. EXPECT_FALSE(retvalHdl.msg["cancel"].asBool());
  1100. EXPECT_EQ(retvalHdl.msg["speed"].asInt(), 0);
  1101. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  1102. EXPECT_EQ(retvalHdl.msg["error"].asString(), "");
  1103. EXPECT_EQ(retvalHdl.nextcommand, "getdata");
  1104. EXPECT_TRUE(cm.isLoggedIn());
  1105. }
  1106. TEST(testGet, Negative_FileAlreadyExisting) {
  1107. FileManMock fm;
  1108. CmdManForTest cm(fm, dummyDebugPrint);
  1109. cm.initLoggedIn();
  1110. std::string cmd;
  1111. std::vector<std::string> args;
  1112. Json::Value root;
  1113. CmdMan::CmdRet retvalCmd;
  1114. CmdMan::CmdRet retvalHdl;
  1115. // prepare root/cmd/args
  1116. cmd = "get";
  1117. args = {"../some/nice/filepath/filename.txt"};
  1118. EXPECT_CALL(fm, openGet(testing::_)).WillOnce(testing::Return(false));
  1119. ON_CALL(fm, pathToFilename("../some/nice/filepath/filename.txt")).WillByDefault(testing::Return("filename.txt"));
  1120. // stick into cmdman and check things
  1121. retvalCmd = cm.execute(cmd, args);
  1122. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1123. EXPECT_EQ(retvalCmd.msg["command"].asString(), "get");
  1124. EXPECT_EQ(retvalCmd.msg["file"].asString(), "filename.txt");
  1125. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1126. EXPECT_TRUE(cm.isLoggedIn());
  1127. }
  1128. TEST(testGet, Negative_ServerError) {
  1129. FileManMock fm;
  1130. CmdManForTest cm(fm, dummyDebugPrint);
  1131. cm.initLoggedIn();
  1132. /* tests the handling of server reply only,
  1133. * initial request assumed as correct (tested in other tests) */
  1134. std::string cmd;
  1135. std::vector<std::string> args;
  1136. Json::Value root;
  1137. CmdMan::CmdRet retvalHdl;
  1138. // prepare root
  1139. root["command"] = "get";
  1140. root["file"] = "filename.txt";
  1141. root["accept"] = false;
  1142. root["error"] = "foobar";
  1143. ON_CALL(fm, getGetName()).WillByDefault(testing::Return("filename.txt"));
  1144. EXPECT_CALL(fm, cancelGet());
  1145. retvalHdl = cm.handle(root);
  1146. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1147. EXPECT_EQ(retvalHdl.msg["command"].asString(), "get");
  1148. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1149. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  1150. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  1151. EXPECT_TRUE(cm.isLoggedIn());
  1152. }
  1153. TEST(testGet, Negative_ServerWrongFile) {
  1154. FileManMock fm;
  1155. CmdManForTest cm(fm, dummyDebugPrint);
  1156. cm.initLoggedIn();
  1157. /* tests the handling of server reply only,
  1158. * initial request assumed as correct (tested in other tests) */
  1159. std::string cmd;
  1160. std::vector<std::string> args;
  1161. Json::Value root;
  1162. CmdMan::CmdRet retvalHdl;
  1163. // prepare root
  1164. root["command"] = "get";
  1165. root["file"] = "completely_different_filename.txt";
  1166. root["accept"] = true;
  1167. ON_CALL(fm, getGetName()).WillByDefault(testing::Return("filename.txt"));
  1168. EXPECT_CALL(fm, isGetting()).WillOnce(testing::Return(true));
  1169. EXPECT_CALL(fm, cancelGet);
  1170. retvalHdl = cm.handle(root);
  1171. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1172. EXPECT_EQ(retvalHdl.msg["command"].asString(), "get");
  1173. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1174. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  1175. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  1176. EXPECT_TRUE(cm.isLoggedIn());
  1177. }
  1178. TEST(testGetdata, Negative_ServerError) {
  1179. FileManMock fm;
  1180. CmdManForTest cm(fm, dummyDebugPrint);
  1181. cm.initLoggedIn();
  1182. /* tests the handling of server reply only,
  1183. * initial request assumed as correct (tested in other tests) */
  1184. std::string cmd;
  1185. std::vector<std::string> args;
  1186. Json::Value root;
  1187. CmdMan::CmdRet retvalHdl;
  1188. // prepare root
  1189. root["command"] = "getdata";
  1190. root["file"] = "filename.txt";
  1191. root["remaining"] = 42;
  1192. root["cancel"] = true;
  1193. root["error"] = "fancy foobar";
  1194. ON_CALL(fm, getGetName()).WillByDefault(testing::Return("filename.txt"));
  1195. ON_CALL(fm, getGetRemainingChunks()).WillByDefault(testing::Return(42));
  1196. ON_CALL(fm, isGetting()).WillByDefault(testing::Return(true));
  1197. retvalHdl = cm.handle(root);
  1198. EXPECT_CALL(fm, cancelGet);
  1199. retvalHdl = cm.handle(root);
  1200. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1201. EXPECT_EQ(retvalHdl.msg["command"].asString(), "getdata");
  1202. EXPECT_TRUE(retvalHdl.msg["cancel"].asBool());
  1203. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  1204. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("fancy foobar"));
  1205. EXPECT_TRUE(cm.isLoggedIn());
  1206. }
  1207. TEST(testGetdata, Negative_ServerWrongFile) {
  1208. FileManMock fm;
  1209. CmdManForTest cm(fm, dummyDebugPrint);
  1210. cm.initLoggedIn();
  1211. /* tests the handling of server reply only,
  1212. * initial request assumed as correct (tested in other tests) */
  1213. std::string cmd;
  1214. std::vector<std::string> args;
  1215. Json::Value root;
  1216. CmdMan::CmdRet retvalHdl;
  1217. // prepare root
  1218. root["command"] = "getdata";
  1219. root["file"] = "completely_different_filename.txt";
  1220. root["remaining"] = 42;
  1221. root["cancel"] = false;
  1222. ON_CALL(fm, getGetName()).WillByDefault(testing::Return("filename.txt"));
  1223. ON_CALL(fm, getGetRemainingChunks()).WillByDefault(testing::Return(42));
  1224. ON_CALL(fm, isGetting()).WillByDefault(testing::Return(true));
  1225. retvalHdl = cm.handle(root);
  1226. EXPECT_CALL(fm, cancelGet);
  1227. retvalHdl = cm.handle(root);
  1228. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1229. EXPECT_EQ(retvalHdl.msg["command"].asString(), "getdata");
  1230. EXPECT_TRUE(retvalHdl.msg["cancel"].asBool());
  1231. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  1232. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  1233. EXPECT_TRUE(cm.isLoggedIn());
  1234. }
  1235. TEST(testGetdata, Negative_ServerWrongChunkNumber) {
  1236. FileManMock fm;
  1237. CmdManForTest cm(fm, dummyDebugPrint);
  1238. cm.initLoggedIn();
  1239. /* tests the handling of server reply only,
  1240. * initial request assumed as correct (tested in other tests) */
  1241. std::string cmd;
  1242. std::vector<std::string> args;
  1243. Json::Value root;
  1244. CmdMan::CmdRet retvalHdl;
  1245. // prepare root
  1246. root["command"] = "getdata";
  1247. root["file"] = "filename.txt";
  1248. root["remaining"] = 23;
  1249. root["cancel"] = false;
  1250. ON_CALL(fm, getGetName()).WillByDefault(testing::Return("filename.txt"));
  1251. ON_CALL(fm, getGetRemainingChunks()).WillByDefault(testing::Return(42));
  1252. ON_CALL(fm, isGetting()).WillByDefault(testing::Return(true));
  1253. retvalHdl = cm.handle(root);
  1254. EXPECT_CALL(fm, cancelGet);
  1255. retvalHdl = cm.handle(root);
  1256. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1257. EXPECT_EQ(retvalHdl.msg["command"].asString(), "getdata");
  1258. EXPECT_TRUE(retvalHdl.msg["cancel"].asBool());
  1259. EXPECT_EQ(retvalHdl.msg["file"].asString(), "filename.txt");
  1260. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  1261. EXPECT_TRUE(cm.isLoggedIn());
  1262. }
  1263. /* =====================================
  1264. * tests for list[data]
  1265. */
  1266. TEST(testList, Positive) {
  1267. FileManMock fm;
  1268. CmdManForTest cm(fm, dummyDebugPrint);
  1269. cm.initLoggedIn();
  1270. std::string cmd;
  1271. std::vector<std::string> args;
  1272. Json::Value root;
  1273. CmdMan::CmdRet retvalCmd;
  1274. CmdMan::CmdRet retvalHdl;
  1275. // prepare root/cmd/args
  1276. cmd = "list";
  1277. args = {};
  1278. root["command"] = "list";
  1279. root["accept"] = true;
  1280. root["items"] = 92;
  1281. root["chunks"] = 2;
  1282. EXPECT_CALL(fm, openList).WillOnce(testing::Return(true));
  1283. EXPECT_CALL(fm, cancelList).Times(0); // not called
  1284. // stick into cmdman and check things
  1285. retvalCmd = cm.execute(cmd, args);
  1286. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1287. EXPECT_EQ(retvalCmd.msg["command"].asString(), "list");
  1288. EXPECT_CALL(fm, isListing()).WillOnce(testing::Return(true));
  1289. EXPECT_CALL(fm, setListChunks(2));
  1290. retvalHdl = cm.handle(root);
  1291. EXPECT_EQ(retvalHdl.type, CmdMan::send);
  1292. EXPECT_EQ(retvalHdl.msg["command"].asString(), "list");
  1293. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1294. EXPECT_EQ(retvalHdl.nextcommand, "listdata");
  1295. EXPECT_TRUE(cm.isLoggedIn());
  1296. }
  1297. TEST(testListdata, Positive_ChunksRemaining) {
  1298. FileManMock fm;
  1299. CmdManForTest cm(fm, dummyDebugPrint);
  1300. cm.initLoggedIn();
  1301. std::string cmd;
  1302. std::vector<std::string> args;
  1303. Json::Value root, namesArr;
  1304. vector<string> namevec = {"blue.txt", "red.pdf", "green.sh", "yellow.tgz"};
  1305. CmdMan::CmdRet retvalCmd;
  1306. CmdMan::CmdRet retvalHdl;
  1307. // prepare root/cmd/args
  1308. cmd = "listdata";
  1309. args = {};
  1310. root["command"] = "listdata";
  1311. root["cancel"] = false;
  1312. root["remaining"] = 3;
  1313. for (string s : namevec)
  1314. namesArr.append(s);
  1315. root["names"] = namesArr;
  1316. EXPECT_CALL(fm, getListRemainingChunks).Times(2).WillRepeatedly(testing::Return(3));
  1317. EXPECT_CALL(fm, isListing).Times(2).WillRepeatedly(testing::Return(true));
  1318. EXPECT_CALL(fm, cancelList).Times(0); // not called
  1319. // stick into cmdman and check things
  1320. retvalCmd = cm.execute(cmd, args);
  1321. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1322. EXPECT_EQ(retvalCmd.msg["command"].asString(), "listdata");
  1323. EXPECT_EQ(retvalCmd.msg["chunk"].asInt(), 3);
  1324. EXPECT_EQ(retvalCmd.msg["cancel"].asBool(), false);
  1325. vector<string> vec{"blue.txt", "red.pdf", "green.sh", "yellow.tgz"};
  1326. EXPECT_CALL(fm, putListData(vec));
  1327. retvalHdl = cm.handle(root);
  1328. EXPECT_EQ(retvalHdl.type, CmdMan::send);
  1329. EXPECT_EQ(retvalHdl.nextcommand, "listdata");
  1330. EXPECT_TRUE(cm.isLoggedIn());
  1331. }
  1332. TEST(testListdata, Positive_LastChunk) {
  1333. FileManMock fm;
  1334. CmdManForTest cm(fm, dummyDebugPrint);
  1335. cm.initLoggedIn();
  1336. std::string cmd;
  1337. std::vector<std::string> args;
  1338. Json::Value root, namesArr;
  1339. CmdMan::CmdRet retvalCmd;
  1340. CmdMan::CmdRet retvalHdl;
  1341. // prepare root/cmd/args
  1342. cmd = "listdata";
  1343. args = {};
  1344. root["command"] = "listdata";
  1345. root["cancel"] = false;
  1346. root["remaining"] = 0;
  1347. namesArr.append("magenta.vcd");
  1348. root["names"] = namesArr;
  1349. EXPECT_CALL(fm, getListRemainingChunks).Times(2).WillRepeatedly(testing::Return(0));
  1350. EXPECT_CALL(fm, isListing).Times(2).WillRepeatedly(testing::Return(true));
  1351. EXPECT_CALL(fm, cancelList).Times(0); // not called
  1352. // stick into cmdman and check things
  1353. retvalCmd = cm.execute(cmd, args);
  1354. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1355. EXPECT_EQ(retvalCmd.msg["command"].asString(), "listdata");
  1356. EXPECT_EQ(retvalCmd.msg["chunk"].asInt(), 0);
  1357. EXPECT_EQ(retvalCmd.msg["cancel"].asBool(), false);
  1358. vector<string> vec{"magenta.vcd"};
  1359. EXPECT_CALL(fm, putListData(vec));
  1360. EXPECT_CALL(fm, closeList);
  1361. vector<string> allFiles{"blue.txt", "red.pdf", "green.sh", "yellow.tgz", "cyan.exe", "white", "black", "magenta.vcd"};
  1362. EXPECT_CALL(fm, getListData).WillOnce(testing::Return(allFiles));
  1363. retvalHdl = cm.handle(root);
  1364. // convert returned json value into vector
  1365. vector<string> retval_msg_names;
  1366. for (Json::Value i : retvalHdl.msg["names"])
  1367. retval_msg_names.push_back(i.asString());
  1368. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1369. EXPECT_EQ(retvalHdl.msg["command"].asString(), "list");
  1370. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1371. EXPECT_EQ(retval_msg_names, allFiles);
  1372. EXPECT_TRUE(cm.isLoggedIn());
  1373. }
  1374. TEST(testList, Negative_ListEmpty) {
  1375. FileManMock fm;
  1376. CmdManForTest cm(fm, dummyDebugPrint);
  1377. cm.initLoggedIn();
  1378. std::string cmd;
  1379. std::vector<std::string> args;
  1380. Json::Value root;
  1381. CmdMan::CmdRet retvalCmd;
  1382. CmdMan::CmdRet retvalHdl;
  1383. // prepare root/cmd/args
  1384. cmd = "list";
  1385. args = {};
  1386. root["command"] = "list";
  1387. root["accept"] = true;
  1388. root["items"] = 0;
  1389. // stick into cmdman and check things
  1390. EXPECT_CALL(fm, openList).WillOnce(testing::Return(true));
  1391. retvalCmd = cm.execute(cmd, args);
  1392. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1393. EXPECT_EQ(retvalCmd.msg["command"].asString(), "list");
  1394. EXPECT_CALL(fm, closeList);
  1395. retvalHdl = cm.handle(root);
  1396. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1397. EXPECT_EQ(retvalHdl.msg["command"].asString(), "list");
  1398. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1399. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("There are no files stored on the server."));
  1400. EXPECT_TRUE(cm.isLoggedIn());
  1401. }
  1402. TEST(testListdata, Negative) {
  1403. FileManMock fm;
  1404. CmdManForTest cm(fm, dummyDebugPrint);
  1405. cm.initLoggedIn();
  1406. /* tests the handling of server reply only,
  1407. * initial request assumed as correct (tested in other tests) */
  1408. std::string cmd;
  1409. std::vector<std::string> args;
  1410. Json::Value root;
  1411. CmdMan::CmdRet retvalHdl;
  1412. // prepare root/cmd/args
  1413. root["command"] = "listdata";
  1414. root["cancel"] = true;
  1415. root["error"] = "some foobar";
  1416. EXPECT_CALL(fm, cancelList);
  1417. retvalHdl = cm.handle(root);
  1418. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1419. EXPECT_EQ(retvalHdl.msg["command"].asString(), "list");
  1420. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1421. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("some foobar"));
  1422. EXPECT_TRUE(cm.isLoggedIn());
  1423. }
  1424. TEST(testList, Negative_AlreadyListing) {
  1425. FileManMock fm;
  1426. CmdManForTest cm(fm, dummyDebugPrint);
  1427. cm.initLoggedIn();
  1428. std::string cmd;
  1429. std::vector<std::string> args;
  1430. Json::Value root;
  1431. CmdMan::CmdRet retvalCmd;
  1432. // prepare cmd/args
  1433. cmd = "list";
  1434. args = {};
  1435. EXPECT_CALL(fm, openList).WillOnce(testing::Return(false));
  1436. EXPECT_CALL(fm, cancelList).Times(0); // not called
  1437. // stick into cmdman and check things
  1438. retvalCmd = cm.execute(cmd, args);
  1439. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1440. EXPECT_EQ(retvalCmd.msg["command"].asString(), "list");
  1441. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  1442. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1443. EXPECT_TRUE(cm.isLoggedIn());
  1444. }
  1445. TEST(testList, Negative_NotListing_ServerRequest) {
  1446. FileManMock fm;
  1447. CmdManForTest cm(fm, dummyDebugPrint);
  1448. cm.initLoggedIn();
  1449. std::string cmd;
  1450. std::vector<std::string> args;
  1451. Json::Value root, namesArr;
  1452. CmdMan::CmdRet retvalHdl;
  1453. // prepare root
  1454. root["command"] = "listdata";
  1455. root["cancel"] = false;
  1456. root["remaining"] = 0;
  1457. namesArr.append("magenta.vcd");
  1458. root["names"] = namesArr;
  1459. EXPECT_CALL(fm, cancelList).Times(0);
  1460. EXPECT_CALL(fm, putListData).Times(0);
  1461. EXPECT_CALL(fm, closeList).Times(0);
  1462. EXPECT_CALL(fm, getListData).Times(0);
  1463. EXPECT_CALL(fm, isListing).WillOnce(testing::Return(false));
  1464. retvalHdl = cm.handle(root);
  1465. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1466. EXPECT_EQ(retvalHdl.msg["command"].asString(), "list");
  1467. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1468. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  1469. EXPECT_TRUE(cm.isLoggedIn());
  1470. }
  1471. TEST(testListdata, Negative_NotListing_UserRequest) {
  1472. FileManMock fm;
  1473. CmdManForTest cm(fm, dummyDebugPrint);
  1474. cm.initLoggedIn();
  1475. std::string cmd;
  1476. std::vector<std::string> args;
  1477. CmdMan::CmdRet retvalCmd;
  1478. // prepare root/cmd/args
  1479. cmd = "listdata";
  1480. args = {};
  1481. EXPECT_CALL(fm, isListing).WillOnce(testing::Return(false));
  1482. // stick into cmdman and check things
  1483. retvalCmd = cm.execute(cmd, args);
  1484. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1485. EXPECT_EQ(retvalCmd.msg["command"].asString(), "list");
  1486. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1487. EXPECT_EQ(retvalCmd.msg["accept"].asBool(), false);
  1488. }
  1489. TEST(testListdata, Negative_NotListing_ServerRequest) {
  1490. FileManMock fm;
  1491. CmdManForTest cm(fm, dummyDebugPrint);
  1492. cm.initLoggedIn();
  1493. std::string cmd;
  1494. std::vector<std::string> args;
  1495. Json::Value root, namesArr;
  1496. CmdMan::CmdRet retvalHdl;
  1497. // prepare root/cmd/args
  1498. root["command"] = "listdata";
  1499. root["cancel"] = false;
  1500. root["remaining"] = 0;
  1501. namesArr.append("magenta.vcd");
  1502. root["names"] = namesArr;
  1503. EXPECT_CALL(fm, cancelList).Times(0);
  1504. retvalHdl = cm.handle(root);
  1505. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1506. EXPECT_EQ(retvalHdl.msg["command"].asString(), "list");
  1507. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1508. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  1509. EXPECT_TRUE(cm.isLoggedIn());
  1510. }
  1511. /* =====================================
  1512. * tests for deleteme
  1513. */
  1514. TEST(testDeleteme, TooFewArgs) {
  1515. FileManMock fm;
  1516. CmdManForTest cm(fm, dummyDebugPrint);
  1517. cm.initLoggedIn();
  1518. std::string cmd;
  1519. std::vector<std::string> args;
  1520. CmdMan::CmdRet retvalCmd;
  1521. // prepare cmd/args
  1522. cmd = "deleteme";
  1523. args = {};
  1524. // stick into cmdman
  1525. retvalCmd = cm.execute(cmd, args);
  1526. // check things
  1527. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1528. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deleteme");
  1529. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  1530. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1531. EXPECT_TRUE(cm.isLoggedIn());
  1532. }
  1533. TEST(testDeleteme, Positive) {
  1534. FileManMock fm;
  1535. CmdManForTest cm(fm, dummyDebugPrint);
  1536. cm.initLoggedIn();
  1537. std::string cmd;
  1538. std::vector<std::string> args;
  1539. Json::Value root;
  1540. CmdMan::CmdRet retvalCmd;
  1541. CmdMan::CmdRet retvalHdl;
  1542. // prepare cmd/args/root
  1543. cmd = "deleteme";
  1544. args = {"myFancyPasswd"};
  1545. root["command"] = "deleteme";
  1546. root["accept"] = true;
  1547. // stick into cmdman
  1548. retvalCmd = cm.execute(cmd, args);
  1549. retvalHdl = cm.handle(root);
  1550. // check things
  1551. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1552. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deleteme");
  1553. EXPECT_EQ(retvalCmd.msg["pass"].asString(), "myFancyPasswd");
  1554. EXPECT_EQ(retvalHdl.type, CmdMan::print | CmdMan::close);
  1555. EXPECT_EQ(retvalHdl.msg["command"].asString(), "deleteme");
  1556. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1557. EXPECT_TRUE(cm.isNotConnected());
  1558. }
  1559. TEST(testDeleteme, Negative) {
  1560. FileManMock fm;
  1561. CmdManForTest cm(fm, dummyDebugPrint);
  1562. cm.initLoggedIn();
  1563. std::string cmd;
  1564. std::vector<std::string> args;
  1565. Json::Value root;
  1566. CmdMan::CmdRet retvalCmd;
  1567. CmdMan::CmdRet retvalHdl;
  1568. // prepare cmd/args/root
  1569. cmd = "deleteme";
  1570. args = {"myFancyPasswd"};
  1571. root["command"] = "deleteme";
  1572. root["accept"] = false;
  1573. root["error"] = "password incorrect";
  1574. // stick into cmdman
  1575. retvalCmd = cm.execute(cmd, args);
  1576. retvalHdl = cm.handle(root);
  1577. // check things
  1578. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1579. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deleteme");
  1580. EXPECT_EQ(retvalCmd.msg["pass"].asString(), "myFancyPasswd");
  1581. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1582. EXPECT_EQ(retvalHdl.msg["command"].asString(), "deleteme");
  1583. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1584. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("password incorrect"));
  1585. EXPECT_TRUE(cm.isLoggedIn());
  1586. }
  1587. /* =====================================
  1588. * tests for deletefile
  1589. */
  1590. TEST(testDeletefile, TooFewArgs) {
  1591. FileManMock fm;
  1592. CmdManForTest cm(fm, dummyDebugPrint);
  1593. cm.initLoggedIn();
  1594. std::string cmd;
  1595. std::vector<std::string> args;
  1596. CmdMan::CmdRet retvalCmd;
  1597. // prepare cmd/args
  1598. cmd = "deletefile";
  1599. args = {};
  1600. // stick into cmdman
  1601. retvalCmd = cm.execute(cmd, args);
  1602. // check things
  1603. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1604. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deletefile");
  1605. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  1606. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1607. EXPECT_TRUE(cm.isLoggedIn());
  1608. }
  1609. TEST(testDeletefile, Positive) {
  1610. FileManMock fm;
  1611. CmdManForTest cm(fm, dummyDebugPrint);
  1612. cm.initLoggedIn();
  1613. std::string cmd;
  1614. std::vector<std::string> args;
  1615. Json::Value root;
  1616. CmdMan::CmdRet retvalCmd;
  1617. CmdMan::CmdRet retvalHdl;
  1618. // prepare cmd/args/root
  1619. cmd = "deletefile";
  1620. args = {"fancy_file.txt"};
  1621. root["command"] = "deletefile";
  1622. root["file"] = "fancy_file.txt";
  1623. root["accept"] = true;
  1624. // stick into cmdman
  1625. retvalCmd = cm.execute(cmd, args);
  1626. retvalHdl = cm.handle(root);
  1627. // check things
  1628. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1629. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deletefile");
  1630. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  1631. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1632. EXPECT_EQ(retvalHdl.msg["command"].asString(), "deletefile");
  1633. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1634. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  1635. EXPECT_TRUE(cm.isLoggedIn());
  1636. }
  1637. TEST(testDeletefile, Negative) {
  1638. FileManMock fm;
  1639. CmdManForTest cm(fm, dummyDebugPrint);
  1640. cm.initLoggedIn();
  1641. std::string cmd;
  1642. std::vector<std::string> args;
  1643. Json::Value root;
  1644. CmdMan::CmdRet retvalCmd;
  1645. CmdMan::CmdRet retvalHdl;
  1646. // prepare cmd/args/root
  1647. cmd = "deletefile";
  1648. args = {"fancy_file.txt"};
  1649. root["command"] = "deletefile";
  1650. root["file"] = "fancy_file.txt";
  1651. root["accept"] = false;
  1652. root["error"] = "file does not exist";
  1653. // stick into cmdman
  1654. retvalCmd = cm.execute(cmd, args);
  1655. retvalHdl = cm.handle(root);
  1656. // check things
  1657. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1658. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deletefile");
  1659. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  1660. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1661. EXPECT_EQ(retvalHdl.msg["command"].asString(), "deletefile");
  1662. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1663. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  1664. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("file does not exist"));
  1665. EXPECT_TRUE(cm.isLoggedIn());
  1666. }
  1667. /* =====================================
  1668. * test for status
  1669. */
  1670. TEST(testStatus, Test) {
  1671. FileManMock fm;
  1672. CmdManForTest cm(fm, dummyDebugPrint);
  1673. cm.initLoggedIn();
  1674. std::string cmd;
  1675. std::vector<std::string> args;
  1676. Json::Value root;
  1677. CmdMan::CmdRet retvalCmd;
  1678. CmdMan::CmdRet retvalHdl;
  1679. // prepare cmd/args/root
  1680. cmd = "status";
  1681. args = {};
  1682. root["command"] = "status";
  1683. root["response"] = "fancy response";
  1684. root["accept"] = true;
  1685. // stick into cmdman
  1686. retvalCmd = cm.execute(cmd, args);
  1687. retvalHdl = cm.handle(root);
  1688. // check things
  1689. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1690. EXPECT_EQ(retvalCmd.msg["command"].asString(), "status");
  1691. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1692. EXPECT_EQ(retvalHdl.msg["command"].asString(), "status");
  1693. EXPECT_THAT(retvalHdl.msg["response"].asString(), testing::HasSubstr("fancy response"));
  1694. EXPECT_TRUE(cm.isLoggedIn());
  1695. }
  1696. /* =====================================
  1697. * test for extendedstatus
  1698. */
  1699. TEST(testExtendedstatus, Negative) {
  1700. FileManMock fm;
  1701. CmdManForTest cm(fm, dummyDebugPrint);
  1702. cm.initLoggedIn();
  1703. std::string cmd;
  1704. std::vector<std::string> args;
  1705. Json::Value root;
  1706. CmdMan::CmdRet retvalCmd;
  1707. CmdMan::CmdRet retvalHdl;
  1708. // prepare cmd/args/root
  1709. cmd = "extendedstatus";
  1710. args = {};
  1711. root["command"] = "extendedstatus";
  1712. root["accept"] = false;
  1713. root["error"] = "some foobar went wrong";
  1714. // stick into cmdman
  1715. retvalCmd = cm.execute(cmd, args);
  1716. retvalHdl = cm.handle(root);
  1717. // check things
  1718. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1719. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedstatus");
  1720. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1721. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedstatus");
  1722. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1723. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("some foobar went wrong"));
  1724. EXPECT_TRUE(cm.isLoggedIn());
  1725. }
  1726. TEST(testExtendedstatus, Positive) {
  1727. FileManMock fm;
  1728. CmdManForTest cm(fm, dummyDebugPrint);
  1729. cm.initLoggedIn();
  1730. std::string cmd;
  1731. std::vector<std::string> args;
  1732. Json::Value root, transfersclientserver, tcs1, tcs2, transfersserverserver, tss1, tss2, tss3;
  1733. CmdMan::CmdRet retvalCmd;
  1734. CmdMan::CmdRet retvalHdl;
  1735. // prepare cmd/args/root
  1736. cmd = "extendedstatus";
  1737. args = {};
  1738. root["command"] = "extendedstatus";
  1739. root["accept"] = true;
  1740. tcs1["upload"] = true;
  1741. tcs1["file"] = "green.txt";
  1742. tcs1["progress"] = 99;
  1743. transfersclientserver.append(tcs1);
  1744. tcs2["upload"] = false;
  1745. tcs2["file"] = "red.txt";
  1746. tcs2["progress"] = 2;
  1747. transfersclientserver.append(tcs2);
  1748. root["transfersclientserver"] = transfersclientserver;
  1749. tss1["type"] = "upload";
  1750. tss1["file"] = "cyan.txt";
  1751. tss1["progress"] = 100;
  1752. tss1["method"] = "urg field";
  1753. tss1["speed"] = 42.0f;
  1754. transfersserverserver.append(tss1);
  1755. tss2["type"] = "download";
  1756. tss2["file"] = "magenta.txt";
  1757. tss2["progress"] = 42;
  1758. tss2["method"] = "fancy thing";
  1759. tss2["speed"] = 0.1f;
  1760. transfersserverserver.append(tss2);
  1761. tss3["type"] = "queued";
  1762. tss3["file"] = "yellow.txt";
  1763. tss3["progress"] = 42;
  1764. tss3["method"] = "cool timing stuff";
  1765. tss3["speed"] = 9000.001f;
  1766. transfersserverserver.append(tss3);
  1767. root["transfersserverserver"] = transfersserverserver;
  1768. // stick into cmdman
  1769. retvalCmd = cm.execute(cmd, args);
  1770. retvalHdl = cm.handle(root);
  1771. // check things
  1772. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1773. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedstatus");
  1774. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1775. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedstatus");
  1776. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1777. EXPECT_TRUE(retvalHdl.msg["transfersclientserver"][0]["upload"].asBool());
  1778. EXPECT_EQ(retvalHdl.msg["transfersclientserver"][0]["file"].asString(), "green.txt");
  1779. EXPECT_EQ(retvalHdl.msg["transfersclientserver"][0]["progress"].asInt(), 99);
  1780. EXPECT_FALSE(retvalHdl.msg["transfersclientserver"][1]["upload"].asBool());
  1781. EXPECT_EQ(retvalHdl.msg["transfersclientserver"][1]["file"].asString(), "red.txt");
  1782. EXPECT_EQ(retvalHdl.msg["transfersclientserver"][1]["progress"].asInt(), 2);
  1783. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][0]["type"].asString(), "upload");
  1784. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][0]["file"].asString(), "cyan.txt");
  1785. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][0]["progress"].asInt(), 100);
  1786. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][0]["method"].asString(), "urg field");
  1787. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][0]["speed"].asFloat(), 42.0f);
  1788. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][1]["type"].asString(), "download");
  1789. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][1]["file"].asString(), "magenta.txt");
  1790. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][1]["progress"].asInt(), 42);
  1791. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][1]["method"].asString(), "fancy thing");
  1792. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][1]["speed"].asFloat(), 0.1f);
  1793. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][2]["type"].asString(), "queued");
  1794. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][2]["file"].asString(), "yellow.txt");
  1795. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][2]["progress"].asInt(), 42);
  1796. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][2]["method"].asString(), "cool timing stuff");
  1797. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][2]["speed"].asFloat(), 9000.001f);
  1798. EXPECT_TRUE(cm.isLoggedIn());
  1799. }
  1800. TEST(testExtendedstatus, Positive_NoTransfersRunning) {
  1801. FileManMock fm;
  1802. CmdManForTest cm(fm, dummyDebugPrint);
  1803. cm.initLoggedIn();
  1804. std::string cmd;
  1805. std::vector<std::string> args;
  1806. Json::Value root, transfersclientserver, transfersserverserver;
  1807. CmdMan::CmdRet retvalCmd;
  1808. CmdMan::CmdRet retvalHdl;
  1809. // prepare cmd/args/root
  1810. cmd = "extendedstatus";
  1811. args = {};
  1812. root["command"] = "extendedstatus";
  1813. root["accept"] = true;
  1814. root["transfersclientserver"] = transfersclientserver;
  1815. root["transfersserverserver"] = transfersserverserver;
  1816. // stick into cmdman
  1817. retvalCmd = cm.execute(cmd, args);
  1818. retvalHdl = cm.handle(root);
  1819. // check things
  1820. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1821. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedstatus");
  1822. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1823. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedstatus");
  1824. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1825. EXPECT_TRUE(retvalHdl.msg["transfersclientserver"].empty());
  1826. EXPECT_TRUE(retvalHdl.msg["transfersserverserver"].empty());
  1827. EXPECT_TRUE(cm.isLoggedIn());
  1828. }
  1829. /* =====================================
  1830. * tests for help
  1831. */
  1832. TEST(testHelp, LoggedIn) {
  1833. FileManMock fm;
  1834. CmdManForTest cm(fm, dummyDebugPrint);
  1835. cm.initLoggedIn();
  1836. std::string cmd;
  1837. std::vector<std::string> args;
  1838. CmdMan::CmdRet retvalCmd;
  1839. // prepare cmd/args/root
  1840. cmd = "help";
  1841. args = {};
  1842. // stick into cmdman
  1843. retvalCmd = cm.execute(cmd, args);
  1844. // check things
  1845. EXPECT_EQ(retvalCmd.type, CmdMan::print);
  1846. EXPECT_EQ(retvalCmd.msg["command"].asString(), "help");
  1847. EXPECT_NE(retvalCmd.msg["names"], "");
  1848. EXPECT_TRUE(cm.isLoggedIn());
  1849. }
  1850. TEST(testHelp, NotConnected) {
  1851. FileManMock fm;
  1852. CmdManForTest cm(fm, dummyDebugPrint);
  1853. std::string cmd;
  1854. std::vector<std::string> args;
  1855. CmdMan::CmdRet retvalCmd;
  1856. // prepare cmd/args/root
  1857. cmd = "help";
  1858. args = {};
  1859. // stick into cmdman
  1860. retvalCmd = cm.execute(cmd, args);
  1861. // check things
  1862. EXPECT_EQ(retvalCmd.type, CmdMan::print);
  1863. EXPECT_EQ(retvalCmd.msg["command"].asString(), "help");
  1864. EXPECT_NE(retvalCmd.msg["names"], "");
  1865. EXPECT_TRUE(cm.isNotConnected());
  1866. }
  1867. /* =====================================
  1868. * tests for keyfile and closekey
  1869. */
  1870. TEST(testKeyfile, TooFewArgs) {
  1871. FileManMock fm;
  1872. CmdManForTest cm(fm, dummyDebugPrint);
  1873. cm.initLoggedIn();
  1874. std::string cmd;
  1875. std::vector<std::string> args;
  1876. CmdMan::CmdRet retvalCmd;
  1877. // prepare cmd/args
  1878. cmd = "keyfile";
  1879. args = {};
  1880. // stick into cmdman
  1881. EXPECT_CALL(fm, openKey).Times(0);
  1882. EXPECT_CALL(fm, getOpensslError).Times(0);
  1883. retvalCmd = cm.execute(cmd, args);
  1884. // check things
  1885. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1886. EXPECT_EQ(retvalCmd.msg["command"].asString(), "keyfile");
  1887. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  1888. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1889. EXPECT_TRUE(cm.isLoggedIn());
  1890. }
  1891. TEST(testKeyfile, Negative) {
  1892. FileManMock fm;
  1893. CmdManForTest cm(fm, dummyDebugPrint);
  1894. cm.initLoggedIn();
  1895. std::string cmd;
  1896. std::vector<std::string> args;
  1897. CmdMan::CmdRet retvalCmd;
  1898. // prepare cmd/args
  1899. cmd = "keyfile";
  1900. args = {"./some/path"};
  1901. // stick into cmdman
  1902. EXPECT_CALL(fm, openKey).WillOnce(testing::Return(false));
  1903. EXPECT_CALL(fm, getOpensslError).WillOnce(testing::Return("some openssl error"));
  1904. retvalCmd = cm.execute(cmd, args);
  1905. // check things
  1906. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1907. EXPECT_EQ(retvalCmd.msg["command"].asString(), "keyfile");
  1908. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  1909. EXPECT_EQ(retvalCmd.msg["file"].asString(), "./some/path");
  1910. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1911. EXPECT_TRUE(cm.isLoggedIn());
  1912. }
  1913. TEST(testKeyfile, Positive) {
  1914. FileManMock fm;
  1915. CmdManForTest cm(fm, dummyDebugPrint);
  1916. cm.initLoggedIn();
  1917. std::string cmd;
  1918. std::vector<std::string> args;
  1919. CmdMan::CmdRet retvalCmd;
  1920. // prepare cmd/args
  1921. cmd = "keyfile";
  1922. args = {"./some/path"};
  1923. // stick into cmdman
  1924. EXPECT_CALL(fm, openKey).WillOnce(testing::Return(true));
  1925. EXPECT_CALL(fm, getOpensslError).Times(0);
  1926. retvalCmd = cm.execute(cmd, args);
  1927. // check things
  1928. EXPECT_EQ(retvalCmd.type, CmdMan::print);
  1929. EXPECT_EQ(retvalCmd.msg["command"].asString(), "keyfile");
  1930. EXPECT_TRUE(retvalCmd.msg["accept"].asBool());
  1931. EXPECT_EQ(retvalCmd.msg["file"].asString(), "./some/path");
  1932. EXPECT_TRUE(cm.isLoggedIn());
  1933. }
  1934. TEST(testClosekey, Positive) {
  1935. FileManMock fm;
  1936. CmdManForTest cm(fm, dummyDebugPrint);
  1937. cm.initLoggedIn();
  1938. std::string cmd;
  1939. std::vector<std::string> args;
  1940. CmdMan::CmdRet retvalCmd;
  1941. // prepare cmd/args
  1942. cmd = "closekey";
  1943. args = {};
  1944. // stick into cmdman
  1945. EXPECT_CALL(fm, closeKey).WillOnce(testing::Return(true));
  1946. retvalCmd = cm.execute(cmd, args);
  1947. // check things
  1948. EXPECT_EQ(retvalCmd.type, CmdMan::print);
  1949. EXPECT_EQ(retvalCmd.msg["command"].asString(), "closekey");
  1950. EXPECT_TRUE(retvalCmd.msg["accept"].asBool());
  1951. EXPECT_TRUE(cm.isLoggedIn());
  1952. }
  1953. TEST(testClosekey, Negative) {
  1954. FileManMock fm;
  1955. CmdManForTest cm(fm, dummyDebugPrint);
  1956. cm.initLoggedIn();
  1957. std::string cmd;
  1958. std::vector<std::string> args;
  1959. CmdMan::CmdRet retvalCmd;
  1960. // prepare cmd/args
  1961. cmd = "closekey";
  1962. args = {};
  1963. // stick into cmdman
  1964. EXPECT_CALL(fm, closeKey).WillOnce(testing::Return(false));
  1965. retvalCmd = cm.execute(cmd, args);
  1966. // check things
  1967. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1968. EXPECT_EQ(retvalCmd.msg["command"].asString(), "closekey");
  1969. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  1970. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1971. EXPECT_TRUE(cm.isLoggedIn());
  1972. }
  1973. /* =====================================
  1974. * test for notifications
  1975. */
  1976. TEST(testNotifications, Positive) {
  1977. FileManMock fm;
  1978. CmdManForTest cm(fm, dummyDebugPrint);
  1979. cm.initLoggedIn();
  1980. std::string cmd;
  1981. std::vector<std::string> args;
  1982. Json::Value root, messagesArr;
  1983. CmdMan::CmdRet retvalCmd;
  1984. CmdMan::CmdRet retvalHdl;
  1985. vector<string> msgvec = {"msg1", "msg2", "msg3 foo", "msg4 hello world!"};
  1986. // prepare cmd/args/root
  1987. cmd = "notifications";
  1988. args = {};
  1989. root["command"] = "notifications";
  1990. for (string s : msgvec)
  1991. messagesArr.append(s);
  1992. root["messages"] = messagesArr;
  1993. root["accept"] = true;
  1994. // stick into cmdman
  1995. retvalCmd = cm.execute(cmd, args);
  1996. retvalHdl = cm.handle(root);
  1997. // check things
  1998. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1999. EXPECT_EQ(retvalCmd.msg["command"].asString(), "notifications");
  2000. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  2001. EXPECT_EQ(retvalHdl.msg["command"].asString(), "notifications");
  2002. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  2003. vector<string> retval_msgs;
  2004. for (Json::Value i : retvalHdl.msg["messages"])
  2005. retval_msgs.push_back(i.asString());
  2006. EXPECT_EQ(retval_msgs, msgvec);
  2007. EXPECT_TRUE(cm.isLoggedIn());
  2008. }
  2009. TEST(testNotifications, Negative) {
  2010. FileManMock fm;
  2011. CmdManForTest cm(fm, dummyDebugPrint);
  2012. cm.initLoggedIn();
  2013. std::string cmd;
  2014. std::vector<std::string> args;
  2015. Json::Value root, messagesArr;
  2016. CmdMan::CmdRet retvalCmd;
  2017. CmdMan::CmdRet retvalHdl;
  2018. // prepare cmd/args/root
  2019. cmd = "notifications";
  2020. args = {};
  2021. root["command"] = "notifications";
  2022. root["accept"] = false;
  2023. root["error"] = "some very descriptive string";
  2024. // stick into cmdman
  2025. retvalCmd = cm.execute(cmd, args);
  2026. retvalHdl = cm.handle(root);
  2027. // check things
  2028. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2029. EXPECT_EQ(retvalCmd.msg["command"].asString(), "notifications");
  2030. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  2031. EXPECT_EQ(retvalHdl.msg["command"].asString(), "notifications");
  2032. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  2033. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("some very descriptive string"));
  2034. EXPECT_TRUE(cm.isLoggedIn());
  2035. }
  2036. /* =====================================
  2037. * tests for queue and dequeue
  2038. */
  2039. TEST(testQueue, TooFewArgs) {
  2040. FileManMock fm;
  2041. CmdManForTest cm(fm, dummyDebugPrint);
  2042. cm.initLoggedIn();
  2043. std::string cmd;
  2044. std::vector<std::string> args;
  2045. CmdMan::CmdRet retvalCmd;
  2046. // prepare cmd/args
  2047. cmd = "queue";
  2048. args = {};
  2049. // stick into cmdman
  2050. retvalCmd = cm.execute(cmd, args);
  2051. // check things
  2052. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  2053. EXPECT_EQ(retvalCmd.msg["command"].asString(), "queue");
  2054. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  2055. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  2056. EXPECT_TRUE(cm.isLoggedIn());
  2057. }
  2058. TEST(testQueue, Positive) {
  2059. FileManMock fm;
  2060. CmdManForTest cm(fm, dummyDebugPrint);
  2061. cm.initLoggedIn();
  2062. std::string cmd;
  2063. std::vector<std::string> args;
  2064. Json::Value root;
  2065. CmdMan::CmdRet retvalCmd;
  2066. CmdMan::CmdRet retvalHdl;
  2067. // prepare cmd/args/root
  2068. cmd = "queue";
  2069. args = {"fancy_file.txt"};
  2070. root["command"] = "queue";
  2071. root["file"] = "fancy_file.txt";
  2072. root["accept"] = true;
  2073. // stick into cmdman
  2074. retvalCmd = cm.execute(cmd, args);
  2075. retvalHdl = cm.handle(root);
  2076. // check things
  2077. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2078. EXPECT_EQ(retvalCmd.msg["command"].asString(), "queue");
  2079. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  2080. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  2081. EXPECT_EQ(retvalHdl.msg["command"].asString(), "queue");
  2082. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  2083. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  2084. EXPECT_TRUE(cm.isLoggedIn());
  2085. }
  2086. TEST(testQueue, Negative) {
  2087. FileManMock fm;
  2088. CmdManForTest cm(fm, dummyDebugPrint);
  2089. cm.initLoggedIn();
  2090. std::string cmd;
  2091. std::vector<std::string> args;
  2092. Json::Value root;
  2093. CmdMan::CmdRet retvalCmd;
  2094. CmdMan::CmdRet retvalHdl;
  2095. // prepare cmd/args/root
  2096. cmd = "queue";
  2097. args = {"fancy_file.txt"};
  2098. root["command"] = "queue";
  2099. root["file"] = "fancy_file.txt";
  2100. root["accept"] = false;
  2101. root["error"] = "file does not exist";
  2102. // stick into cmdman
  2103. retvalCmd = cm.execute(cmd, args);
  2104. retvalHdl = cm.handle(root);
  2105. // check things
  2106. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2107. EXPECT_EQ(retvalCmd.msg["command"].asString(), "queue");
  2108. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  2109. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  2110. EXPECT_EQ(retvalHdl.msg["command"].asString(), "queue");
  2111. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  2112. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  2113. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("file does not exist"));
  2114. EXPECT_TRUE(cm.isLoggedIn());
  2115. }
  2116. TEST(testDequeue, TooFewArgs) {
  2117. FileManMock fm;
  2118. CmdManForTest cm(fm, dummyDebugPrint);
  2119. cm.initLoggedIn();
  2120. std::string cmd;
  2121. std::vector<std::string> args;
  2122. CmdMan::CmdRet retvalCmd;
  2123. // prepare cmd/args
  2124. cmd = "dequeue";
  2125. args = {};
  2126. // stick into cmdman
  2127. retvalCmd = cm.execute(cmd, args);
  2128. // check things
  2129. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  2130. EXPECT_EQ(retvalCmd.msg["command"].asString(), "dequeue");
  2131. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  2132. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  2133. EXPECT_TRUE(cm.isLoggedIn());
  2134. }
  2135. TEST(testDequeue, Positive) {
  2136. FileManMock fm;
  2137. CmdManForTest cm(fm, dummyDebugPrint);
  2138. cm.initLoggedIn();
  2139. std::string cmd;
  2140. std::vector<std::string> args;
  2141. Json::Value root;
  2142. CmdMan::CmdRet retvalCmd;
  2143. CmdMan::CmdRet retvalHdl;
  2144. // prepare cmd/args/root
  2145. cmd = "dequeue";
  2146. args = {"fancy_file.txt"};
  2147. root["command"] = "dequeue";
  2148. root["file"] = "fancy_file.txt";
  2149. root["accept"] = true;
  2150. // stick into cmdman
  2151. retvalCmd = cm.execute(cmd, args);
  2152. retvalHdl = cm.handle(root);
  2153. // check things
  2154. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2155. EXPECT_EQ(retvalCmd.msg["command"].asString(), "dequeue");
  2156. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  2157. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  2158. EXPECT_EQ(retvalHdl.msg["command"].asString(), "dequeue");
  2159. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  2160. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  2161. EXPECT_TRUE(cm.isLoggedIn());
  2162. }
  2163. TEST(testDequeue, Negative) {
  2164. FileManMock fm;
  2165. CmdManForTest cm(fm, dummyDebugPrint);
  2166. cm.initLoggedIn();
  2167. std::string cmd;
  2168. std::vector<std::string> args;
  2169. Json::Value root;
  2170. CmdMan::CmdRet retvalCmd;
  2171. CmdMan::CmdRet retvalHdl;
  2172. // prepare cmd/args/root
  2173. cmd = "dequeue";
  2174. args = {"fancy_file.txt"};
  2175. root["command"] = "dequeue";
  2176. root["file"] = "fancy_file.txt";
  2177. root["accept"] = false;
  2178. root["error"] = "file not in queue";
  2179. // stick into cmdman
  2180. retvalCmd = cm.execute(cmd, args);
  2181. retvalHdl = cm.handle(root);
  2182. // check things
  2183. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2184. EXPECT_EQ(retvalCmd.msg["command"].asString(), "dequeue");
  2185. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  2186. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  2187. EXPECT_EQ(retvalHdl.msg["command"].asString(), "dequeue");
  2188. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  2189. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  2190. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("file not in queue"));
  2191. EXPECT_TRUE(cm.isLoggedIn());
  2192. }
  2193. /* =====================================
  2194. * test for all commands that require a login
  2195. */
  2196. TEST(testCommandsWithRequiredLogin, NotLoggedInOrNotConnected) {
  2197. FileManMock fm;
  2198. CmdManForTest cm(fm, dummyDebugPrint);
  2199. cm.initLoggedIn();
  2200. CmdMan::CmdRet retval1, retval2, retval3;
  2201. // prepare cmd/args
  2202. std::vector<std::string> cmds = {"put", "get", "list", "putdata", "getdata", "listdata",
  2203. "head", "deletefile", "deleteme", "keyfile", "closekey", "extendedstatus"};
  2204. // as every command works fine with too many args, we will simply pass three to all of them
  2205. std::vector<std::string> args = {"arg1", "arg2", "arg3"};
  2206. for (std::string cmd : cmds) {
  2207. cm.initNotConnected();
  2208. retval1 = cm.execute(cmd, args);
  2209. cm.initConnected();
  2210. retval2 = cm.execute(cmd, args);
  2211. cm.initVersionChecked();
  2212. retval3 = cm.execute(cmd, args);
  2213. EXPECT_EQ(retval1.type, CmdMan::error);
  2214. EXPECT_EQ(retval1.msg["command"].asString(), "error");
  2215. EXPECT_NE(retval1.msg["error"].asString(), "");
  2216. EXPECT_EQ(retval2.type, CmdMan::error);
  2217. EXPECT_EQ(retval2.msg["command"].asString(), "error");
  2218. EXPECT_NE(retval2.msg["error"].asString(), "");
  2219. EXPECT_EQ(retval3.type, CmdMan::error);
  2220. EXPECT_EQ(retval3.msg["command"].asString(), "error");
  2221. EXPECT_NE(retval3.msg["error"].asString(), "");
  2222. }
  2223. }
  2224. } // namespace
  2225. /* end of namespace */