cmdman_test.cpp 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198
  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(false)).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, isListingSimple()).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, isListingSimple).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<Json::Value> vec{Json::Value("blue.txt"), Json::Value("red.pdf"), Json::Value("green.sh"), Json::Value("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, isListingSimple).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<Json::Value> vec{Json::Value("magenta.vcd")};
  1359. EXPECT_CALL(fm, putListData(vec));
  1360. EXPECT_CALL(fm, closeList);
  1361. vector<Json::Value> 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. vector<string> allFilesStringVec{"blue.txt", "red.pdf", "green.sh", "yellow.tgz", "cyan.exe", "white", "black", "magenta.vcd"};
  1372. EXPECT_EQ(retval_msg_names, allFilesStringVec);
  1373. EXPECT_TRUE(cm.isLoggedIn());
  1374. }
  1375. TEST(testList, Negative_ListEmpty) {
  1376. FileManMock fm;
  1377. CmdManForTest cm(fm, dummyDebugPrint);
  1378. cm.initLoggedIn();
  1379. std::string cmd;
  1380. std::vector<std::string> args;
  1381. Json::Value root;
  1382. CmdMan::CmdRet retvalCmd;
  1383. CmdMan::CmdRet retvalHdl;
  1384. // prepare root/cmd/args
  1385. cmd = "list";
  1386. args = {};
  1387. root["command"] = "list";
  1388. root["accept"] = true;
  1389. root["items"] = 0;
  1390. // stick into cmdman and check things
  1391. EXPECT_CALL(fm, openList(false)).WillOnce(testing::Return(true));
  1392. retvalCmd = cm.execute(cmd, args);
  1393. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1394. EXPECT_EQ(retvalCmd.msg["command"].asString(), "list");
  1395. EXPECT_CALL(fm, closeList);
  1396. retvalHdl = cm.handle(root);
  1397. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1398. EXPECT_EQ(retvalHdl.msg["command"].asString(), "list");
  1399. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1400. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("There are no files stored on the server."));
  1401. EXPECT_TRUE(cm.isLoggedIn());
  1402. }
  1403. TEST(testListdata, Negative) {
  1404. FileManMock fm;
  1405. CmdManForTest cm(fm, dummyDebugPrint);
  1406. cm.initLoggedIn();
  1407. /* tests the handling of server reply only,
  1408. * initial request assumed as correct (tested in other tests) */
  1409. Json::Value root;
  1410. CmdMan::CmdRet retvalHdl;
  1411. // prepare root/cmd/args
  1412. root["command"] = "listdata";
  1413. root["cancel"] = true;
  1414. root["error"] = "some foobar";
  1415. EXPECT_CALL(fm, cancelList);
  1416. retvalHdl = cm.handle(root);
  1417. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1418. EXPECT_EQ(retvalHdl.msg["command"].asString(), "list");
  1419. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1420. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("some foobar"));
  1421. EXPECT_TRUE(cm.isLoggedIn());
  1422. }
  1423. TEST(testList, Negative_AlreadyListing) {
  1424. FileManMock fm;
  1425. CmdManForTest cm(fm, dummyDebugPrint);
  1426. cm.initLoggedIn();
  1427. std::string cmd;
  1428. std::vector<std::string> args;
  1429. CmdMan::CmdRet retvalCmd;
  1430. // prepare cmd/args
  1431. cmd = "list";
  1432. args = {};
  1433. EXPECT_CALL(fm, openList(false)).WillOnce(testing::Return(false));
  1434. EXPECT_CALL(fm, cancelList).Times(0); // not called
  1435. // stick into cmdman and check things
  1436. retvalCmd = cm.execute(cmd, args);
  1437. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1438. EXPECT_EQ(retvalCmd.msg["command"].asString(), "list");
  1439. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  1440. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1441. EXPECT_TRUE(cm.isLoggedIn());
  1442. }
  1443. TEST(testList, Negative_NotListing_ServerRequest) {
  1444. FileManMock fm;
  1445. CmdManForTest cm(fm, dummyDebugPrint);
  1446. cm.initLoggedIn();
  1447. Json::Value root, namesArr;
  1448. CmdMan::CmdRet retvalHdl;
  1449. // prepare root
  1450. root["command"] = "listdata";
  1451. root["cancel"] = false;
  1452. root["remaining"] = 0;
  1453. namesArr.append("magenta.vcd");
  1454. root["names"] = namesArr;
  1455. EXPECT_CALL(fm, cancelList).Times(0);
  1456. EXPECT_CALL(fm, putListData).Times(0);
  1457. EXPECT_CALL(fm, closeList).Times(0);
  1458. EXPECT_CALL(fm, getListData).Times(0);
  1459. EXPECT_CALL(fm, isListingSimple).WillOnce(testing::Return(false));
  1460. retvalHdl = cm.handle(root);
  1461. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1462. EXPECT_EQ(retvalHdl.msg["command"].asString(), "list");
  1463. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1464. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  1465. EXPECT_TRUE(cm.isLoggedIn());
  1466. }
  1467. TEST(testListdata, Negative_NotListing_UserRequest) {
  1468. FileManMock fm;
  1469. CmdManForTest cm(fm, dummyDebugPrint);
  1470. cm.initLoggedIn();
  1471. std::string cmd;
  1472. std::vector<std::string> args;
  1473. CmdMan::CmdRet retvalCmd;
  1474. // prepare root/cmd/args
  1475. cmd = "listdata";
  1476. args = {};
  1477. EXPECT_CALL(fm, isListingSimple).WillOnce(testing::Return(false));
  1478. // stick into cmdman and check things
  1479. retvalCmd = cm.execute(cmd, args);
  1480. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1481. EXPECT_EQ(retvalCmd.msg["command"].asString(), "list");
  1482. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1483. EXPECT_EQ(retvalCmd.msg["accept"].asBool(), false);
  1484. }
  1485. TEST(testListdata, Negative_NotListing_ServerRequest) {
  1486. FileManMock fm;
  1487. CmdManForTest cm(fm, dummyDebugPrint);
  1488. cm.initLoggedIn();
  1489. std::string cmd;
  1490. std::vector<std::string> args;
  1491. Json::Value root, namesArr;
  1492. CmdMan::CmdRet retvalHdl;
  1493. // prepare root/cmd/args
  1494. root["command"] = "listdata";
  1495. root["cancel"] = false;
  1496. root["remaining"] = 0;
  1497. namesArr.append("magenta.vcd");
  1498. root["names"] = namesArr;
  1499. EXPECT_CALL(fm, cancelList).Times(0);
  1500. EXPECT_CALL(fm, isListingSimple).WillOnce(testing::Return(false));
  1501. retvalHdl = cm.handle(root);
  1502. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1503. EXPECT_EQ(retvalHdl.msg["command"].asString(), "list");
  1504. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1505. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  1506. EXPECT_TRUE(cm.isLoggedIn());
  1507. }
  1508. /* =====================================
  1509. * tests for extendedlist[data]
  1510. */
  1511. TEST(testExtendedlist, Positive) {
  1512. FileManMock fm;
  1513. CmdManForTest cm(fm, dummyDebugPrint);
  1514. cm.initLoggedIn();
  1515. std::string cmd;
  1516. std::vector<std::string> args;
  1517. Json::Value root;
  1518. CmdMan::CmdRet retvalCmd;
  1519. CmdMan::CmdRet retvalHdl;
  1520. // prepare root/cmd/args
  1521. cmd = "extendedlist";
  1522. args = {};
  1523. root["command"] = "extendedlist";
  1524. root["accept"] = true;
  1525. root["items"] = 92;
  1526. root["chunks"] = 2;
  1527. EXPECT_CALL(fm, openList(true)).WillOnce(testing::Return(true));
  1528. EXPECT_CALL(fm, cancelList).Times(0); // not called
  1529. // stick into cmdman and check things
  1530. retvalCmd = cm.execute(cmd, args);
  1531. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1532. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedlist");
  1533. EXPECT_CALL(fm, isListingExtended()).WillOnce(testing::Return(true));
  1534. EXPECT_CALL(fm, setListChunks(2));
  1535. retvalHdl = cm.handle(root);
  1536. EXPECT_EQ(retvalHdl.type, CmdMan::send);
  1537. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedlist");
  1538. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1539. EXPECT_EQ(retvalHdl.nextcommand, "extendedlistdata");
  1540. EXPECT_TRUE(cm.isLoggedIn());
  1541. }
  1542. TEST(testExtendedlistdata, Positive_ChunksRemaining) {
  1543. FileManMock fm;
  1544. CmdManForTest cm(fm, dummyDebugPrint);
  1545. cm.initLoggedIn();
  1546. std::string cmd;
  1547. std::vector<std::string> args;
  1548. Json::Value root, files, f1, f2, namesArr;
  1549. CmdMan::CmdRet retvalCmd;
  1550. CmdMan::CmdRet retvalHdl;
  1551. // prepare root/cmd/args
  1552. f1["name"] = "blue.txt";
  1553. f1["size"] = 42.0f;
  1554. f1["head"] = "some head 1";
  1555. files.append(f1);
  1556. f2["name"] = "green.pdf";
  1557. f2["size"] = 9.0f;
  1558. f2["head"] = "some head 2";
  1559. files.append(f2);
  1560. cmd = "extendedlistdata";
  1561. args = {};
  1562. root["command"] = "extendedlistdata";
  1563. root["cancel"] = false;
  1564. root["remaining"] = 3;
  1565. root["files"] = files;
  1566. EXPECT_CALL(fm, getListRemainingChunks).Times(2).WillRepeatedly(testing::Return(3));
  1567. EXPECT_CALL(fm, isListingExtended).Times(2).WillRepeatedly(testing::Return(true));
  1568. EXPECT_CALL(fm, cancelList).Times(0); // not called
  1569. // stick into cmdman and check things
  1570. retvalCmd = cm.execute(cmd, args);
  1571. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1572. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedlistdata");
  1573. EXPECT_EQ(retvalCmd.msg["chunk"].asInt(), 3);
  1574. EXPECT_EQ(retvalCmd.msg["cancel"].asBool(), false);
  1575. vector<Json::Value> vec{f1, f2};
  1576. EXPECT_CALL(fm, putListData(vec));
  1577. retvalHdl = cm.handle(root);
  1578. EXPECT_EQ(retvalHdl.type, CmdMan::send);
  1579. EXPECT_EQ(retvalHdl.nextcommand, "extendedlistdata");
  1580. EXPECT_TRUE(cm.isLoggedIn());
  1581. }
  1582. TEST(testExtendedlistdata, Positive_LastChunk) {
  1583. FileManMock fm;
  1584. CmdManForTest cm(fm, dummyDebugPrint);
  1585. cm.initLoggedIn();
  1586. std::string cmd;
  1587. std::vector<std::string> args;
  1588. Json::Value root, files, f1, f2, f3, inputfile;
  1589. CmdMan::CmdRet retvalCmd;
  1590. CmdMan::CmdRet retvalHdl;
  1591. // prepare file json/root/cmd/args
  1592. inputfile["name"] = "magenta.vcd";
  1593. inputfile["size"] = 9000.001f;
  1594. inputfile["head"] = "some head 3";
  1595. files.append(inputfile);
  1596. cmd = "extendedlistdata";
  1597. args = {};
  1598. root["command"] = "extendedlistdata";
  1599. root["cancel"] = false;
  1600. root["remaining"] = 0;
  1601. root["files"] = files;
  1602. EXPECT_CALL(fm, getListRemainingChunks).Times(2).WillRepeatedly(testing::Return(0));
  1603. EXPECT_CALL(fm, isListingExtended).Times(2).WillRepeatedly(testing::Return(true));
  1604. EXPECT_CALL(fm, cancelList).Times(0); // not called
  1605. // stick into cmdman and check things
  1606. retvalCmd = cm.execute(cmd, args);
  1607. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1608. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedlistdata");
  1609. EXPECT_EQ(retvalCmd.msg["chunk"].asInt(), 0);
  1610. EXPECT_EQ(retvalCmd.msg["cancel"].asBool(), false);
  1611. vector<Json::Value> vec{inputfile};
  1612. EXPECT_CALL(fm, putListData(vec));
  1613. EXPECT_CALL(fm, closeList);
  1614. // output of fileman
  1615. f1["name"] = "blue.txt";
  1616. f1["size"] = 42.0f;
  1617. f1["encrypted"] = "unencrypted";
  1618. f2["name"] = "green.pdf";
  1619. f2["size"] = 9.0f;
  1620. f2["encrypted"] = "decryptable";
  1621. f3["name"] = "magenta.vcd";
  1622. f3["size"] = 9000.001f;
  1623. f3["encrypted"] = "undecryptable";
  1624. vector<Json::Value> allFiles{f1, f2, f3};
  1625. EXPECT_CALL(fm, getListData).WillOnce(testing::Return(allFiles));
  1626. retvalHdl = cm.handle(root);
  1627. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1628. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedlist");
  1629. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1630. EXPECT_EQ(retvalHdl.msg["files"][0]["name"].asString(), "blue.txt");
  1631. EXPECT_EQ(retvalHdl.msg["files"][0]["encrypted"].asString(), "unencrypted");
  1632. EXPECT_EQ(retvalHdl.msg["files"][0]["size"].asFloat(), 42.0f);
  1633. EXPECT_EQ(retvalHdl.msg["files"][1]["name"].asString(), "green.pdf");
  1634. EXPECT_EQ(retvalHdl.msg["files"][1]["encrypted"].asString(), "decryptable");
  1635. EXPECT_EQ(retvalHdl.msg["files"][1]["size"].asFloat(), 9.0f);
  1636. EXPECT_EQ(retvalHdl.msg["files"][2]["name"].asString(), "magenta.vcd");
  1637. EXPECT_EQ(retvalHdl.msg["files"][2]["encrypted"].asString(), "undecryptable");
  1638. EXPECT_EQ(retvalHdl.msg["files"][2]["size"].asFloat(), 9000.001f);
  1639. EXPECT_TRUE(cm.isLoggedIn());
  1640. }
  1641. TEST(testExtendedlist, Negative_ListEmpty) {
  1642. FileManMock fm;
  1643. CmdManForTest cm(fm, dummyDebugPrint);
  1644. cm.initLoggedIn();
  1645. std::string cmd;
  1646. std::vector<std::string> args;
  1647. Json::Value root;
  1648. CmdMan::CmdRet retvalCmd;
  1649. CmdMan::CmdRet retvalHdl;
  1650. // prepare root/cmd/args
  1651. cmd = "extendedlist";
  1652. args = {};
  1653. root["command"] = "extendedlist";
  1654. root["accept"] = true;
  1655. root["items"] = 0;
  1656. // stick into cmdman and check things
  1657. EXPECT_CALL(fm, openList(true)).WillOnce(testing::Return(true));
  1658. retvalCmd = cm.execute(cmd, args);
  1659. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1660. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedlist");
  1661. EXPECT_CALL(fm, closeList);
  1662. retvalHdl = cm.handle(root);
  1663. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1664. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedlist");
  1665. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1666. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("There are no files stored on the server."));
  1667. EXPECT_TRUE(cm.isLoggedIn());
  1668. }
  1669. TEST(testExtendedlistdata, Negative) {
  1670. FileManMock fm;
  1671. CmdManForTest cm(fm, dummyDebugPrint);
  1672. cm.initLoggedIn();
  1673. /* tests the handling of server reply only,
  1674. * initial request assumed as correct (tested in other tests) */
  1675. Json::Value root;
  1676. CmdMan::CmdRet retvalHdl;
  1677. // prepare root/cmd/args
  1678. root["command"] = "extendedlistdata";
  1679. root["cancel"] = true;
  1680. root["error"] = "some foobar";
  1681. EXPECT_CALL(fm, cancelList);
  1682. retvalHdl = cm.handle(root);
  1683. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1684. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedlist");
  1685. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1686. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("some foobar"));
  1687. EXPECT_TRUE(cm.isLoggedIn());
  1688. }
  1689. TEST(testExtendedlist, Negative_AlreadyListing) {
  1690. FileManMock fm;
  1691. CmdManForTest cm(fm, dummyDebugPrint);
  1692. cm.initLoggedIn();
  1693. std::string cmd;
  1694. std::vector<std::string> args;
  1695. Json::Value root;
  1696. CmdMan::CmdRet retvalCmd;
  1697. // prepare cmd/args
  1698. cmd = "extendedlist";
  1699. args = {};
  1700. EXPECT_CALL(fm, openList(true)).WillOnce(testing::Return(false));
  1701. EXPECT_CALL(fm, cancelList).Times(0); // not called
  1702. // stick into cmdman and check things
  1703. retvalCmd = cm.execute(cmd, args);
  1704. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1705. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedlist");
  1706. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  1707. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1708. EXPECT_TRUE(cm.isLoggedIn());
  1709. }
  1710. TEST(testExtendedlist, Negative_NotListing_ServerRequest) {
  1711. FileManMock fm;
  1712. CmdManForTest cm(fm, dummyDebugPrint);
  1713. cm.initLoggedIn();
  1714. std::string cmd;
  1715. std::vector<std::string> args;
  1716. Json::Value root, files, inputfile;
  1717. CmdMan::CmdRet retvalHdl;
  1718. // prepare root
  1719. inputfile["name"] = "magenta.vcd";
  1720. inputfile["size"] = 9000.001f;
  1721. inputfile["head"] = "some head 3";
  1722. files.append(inputfile);
  1723. root["command"] = "extendedlistdata";
  1724. root["cancel"] = false;
  1725. root["remaining"] = 0;
  1726. root["files"] = files;
  1727. EXPECT_CALL(fm, cancelList).Times(0);
  1728. EXPECT_CALL(fm, putListData).Times(0);
  1729. EXPECT_CALL(fm, closeList).Times(0);
  1730. EXPECT_CALL(fm, getListData).Times(0);
  1731. EXPECT_CALL(fm, isListingExtended).WillOnce(testing::Return(false));
  1732. retvalHdl = cm.handle(root);
  1733. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1734. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedlist");
  1735. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1736. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  1737. EXPECT_TRUE(cm.isLoggedIn());
  1738. }
  1739. TEST(testExtendedistdata, Negative_NotListing_UserRequest) {
  1740. FileManMock fm;
  1741. CmdManForTest cm(fm, dummyDebugPrint);
  1742. cm.initLoggedIn();
  1743. std::string cmd;
  1744. std::vector<std::string> args;
  1745. CmdMan::CmdRet retvalCmd;
  1746. // prepare root/cmd/args
  1747. cmd = "extendedlistdata";
  1748. args = {};
  1749. EXPECT_CALL(fm, isListingExtended).WillOnce(testing::Return(false));
  1750. // stick into cmdman and check things
  1751. retvalCmd = cm.execute(cmd, args);
  1752. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1753. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedlist");
  1754. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1755. EXPECT_EQ(retvalCmd.msg["accept"].asBool(), false);
  1756. }
  1757. TEST(testExtendedlistdata, Negative_NotListing_ServerRequest) {
  1758. FileManMock fm;
  1759. CmdManForTest cm(fm, dummyDebugPrint);
  1760. cm.initLoggedIn();
  1761. std::string cmd;
  1762. std::vector<std::string> args;
  1763. Json::Value root, files, inputfile;
  1764. CmdMan::CmdRet retvalHdl;
  1765. // prepare root
  1766. inputfile["name"] = "magenta.vcd";
  1767. inputfile["size"] = 9000.001f;
  1768. inputfile["head"] = "some head 3";
  1769. files.append(inputfile);
  1770. root["command"] = "extendedlistdata";
  1771. root["cancel"] = false;
  1772. root["remaining"] = 0;
  1773. root["files"] = files;
  1774. EXPECT_CALL(fm, cancelList).Times(0);
  1775. EXPECT_CALL(fm, isListingExtended).WillOnce(testing::Return(false));
  1776. retvalHdl = cm.handle(root);
  1777. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1778. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedlist");
  1779. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1780. EXPECT_NE(retvalHdl.msg["error"].asString(), "");
  1781. EXPECT_TRUE(cm.isLoggedIn());
  1782. }
  1783. /* =====================================
  1784. * tests for deleteme
  1785. */
  1786. TEST(testDeleteme, TooFewArgs) {
  1787. FileManMock fm;
  1788. CmdManForTest cm(fm, dummyDebugPrint);
  1789. cm.initLoggedIn();
  1790. std::string cmd;
  1791. std::vector<std::string> args;
  1792. CmdMan::CmdRet retvalCmd;
  1793. // prepare cmd/args
  1794. cmd = "deleteme";
  1795. args = {};
  1796. // stick into cmdman
  1797. retvalCmd = cm.execute(cmd, args);
  1798. // check things
  1799. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1800. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deleteme");
  1801. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  1802. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1803. EXPECT_TRUE(cm.isLoggedIn());
  1804. }
  1805. TEST(testDeleteme, Positive) {
  1806. FileManMock fm;
  1807. CmdManForTest cm(fm, dummyDebugPrint);
  1808. cm.initLoggedIn();
  1809. std::string cmd;
  1810. std::vector<std::string> args;
  1811. Json::Value root;
  1812. CmdMan::CmdRet retvalCmd;
  1813. CmdMan::CmdRet retvalHdl;
  1814. // prepare cmd/args/root
  1815. cmd = "deleteme";
  1816. args = {"myFancyPasswd"};
  1817. root["command"] = "deleteme";
  1818. root["accept"] = true;
  1819. // stick into cmdman
  1820. retvalCmd = cm.execute(cmd, args);
  1821. retvalHdl = cm.handle(root);
  1822. // check things
  1823. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1824. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deleteme");
  1825. EXPECT_EQ(retvalCmd.msg["pass"].asString(), "myFancyPasswd");
  1826. EXPECT_EQ(retvalHdl.type, CmdMan::print | CmdMan::close);
  1827. EXPECT_EQ(retvalHdl.msg["command"].asString(), "deleteme");
  1828. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1829. EXPECT_TRUE(cm.isNotConnected());
  1830. }
  1831. TEST(testDeleteme, Negative) {
  1832. FileManMock fm;
  1833. CmdManForTest cm(fm, dummyDebugPrint);
  1834. cm.initLoggedIn();
  1835. std::string cmd;
  1836. std::vector<std::string> args;
  1837. Json::Value root;
  1838. CmdMan::CmdRet retvalCmd;
  1839. CmdMan::CmdRet retvalHdl;
  1840. // prepare cmd/args/root
  1841. cmd = "deleteme";
  1842. args = {"myFancyPasswd"};
  1843. root["command"] = "deleteme";
  1844. root["accept"] = false;
  1845. root["error"] = "password incorrect";
  1846. // stick into cmdman
  1847. retvalCmd = cm.execute(cmd, args);
  1848. retvalHdl = cm.handle(root);
  1849. // check things
  1850. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1851. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deleteme");
  1852. EXPECT_EQ(retvalCmd.msg["pass"].asString(), "myFancyPasswd");
  1853. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1854. EXPECT_EQ(retvalHdl.msg["command"].asString(), "deleteme");
  1855. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1856. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("password incorrect"));
  1857. EXPECT_TRUE(cm.isLoggedIn());
  1858. }
  1859. /* =====================================
  1860. * tests for deletefile
  1861. */
  1862. TEST(testDeletefile, TooFewArgs) {
  1863. FileManMock fm;
  1864. CmdManForTest cm(fm, dummyDebugPrint);
  1865. cm.initLoggedIn();
  1866. std::string cmd;
  1867. std::vector<std::string> args;
  1868. CmdMan::CmdRet retvalCmd;
  1869. // prepare cmd/args
  1870. cmd = "deletefile";
  1871. args = {};
  1872. // stick into cmdman
  1873. retvalCmd = cm.execute(cmd, args);
  1874. // check things
  1875. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  1876. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deletefile");
  1877. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  1878. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  1879. EXPECT_TRUE(cm.isLoggedIn());
  1880. }
  1881. TEST(testDeletefile, Positive) {
  1882. FileManMock fm;
  1883. CmdManForTest cm(fm, dummyDebugPrint);
  1884. cm.initLoggedIn();
  1885. std::string cmd;
  1886. std::vector<std::string> args;
  1887. Json::Value root;
  1888. CmdMan::CmdRet retvalCmd;
  1889. CmdMan::CmdRet retvalHdl;
  1890. // prepare cmd/args/root
  1891. cmd = "deletefile";
  1892. args = {"fancy_file.txt"};
  1893. root["command"] = "deletefile";
  1894. root["file"] = "fancy_file.txt";
  1895. root["accept"] = true;
  1896. // stick into cmdman
  1897. retvalCmd = cm.execute(cmd, args);
  1898. retvalHdl = cm.handle(root);
  1899. // check things
  1900. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1901. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deletefile");
  1902. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  1903. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1904. EXPECT_EQ(retvalHdl.msg["command"].asString(), "deletefile");
  1905. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  1906. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  1907. EXPECT_TRUE(cm.isLoggedIn());
  1908. }
  1909. TEST(testDeletefile, Negative) {
  1910. FileManMock fm;
  1911. CmdManForTest cm(fm, dummyDebugPrint);
  1912. cm.initLoggedIn();
  1913. std::string cmd;
  1914. std::vector<std::string> args;
  1915. Json::Value root;
  1916. CmdMan::CmdRet retvalCmd;
  1917. CmdMan::CmdRet retvalHdl;
  1918. // prepare cmd/args/root
  1919. cmd = "deletefile";
  1920. args = {"fancy_file.txt"};
  1921. root["command"] = "deletefile";
  1922. root["file"] = "fancy_file.txt";
  1923. root["accept"] = false;
  1924. root["error"] = "file does not exist";
  1925. // stick into cmdman
  1926. retvalCmd = cm.execute(cmd, args);
  1927. retvalHdl = cm.handle(root);
  1928. // check things
  1929. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1930. EXPECT_EQ(retvalCmd.msg["command"].asString(), "deletefile");
  1931. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  1932. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1933. EXPECT_EQ(retvalHdl.msg["command"].asString(), "deletefile");
  1934. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1935. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  1936. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("file does not exist"));
  1937. EXPECT_TRUE(cm.isLoggedIn());
  1938. }
  1939. /* =====================================
  1940. * test for status
  1941. */
  1942. TEST(testStatus, Test) {
  1943. FileManMock fm;
  1944. CmdManForTest cm(fm, dummyDebugPrint);
  1945. cm.initLoggedIn();
  1946. std::string cmd;
  1947. std::vector<std::string> args;
  1948. Json::Value root;
  1949. CmdMan::CmdRet retvalCmd;
  1950. CmdMan::CmdRet retvalHdl;
  1951. // prepare cmd/args/root
  1952. cmd = "status";
  1953. args = {};
  1954. root["command"] = "status";
  1955. root["response"] = "fancy response";
  1956. root["accept"] = true;
  1957. // stick into cmdman
  1958. retvalCmd = cm.execute(cmd, args);
  1959. retvalHdl = cm.handle(root);
  1960. // check things
  1961. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1962. EXPECT_EQ(retvalCmd.msg["command"].asString(), "status");
  1963. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  1964. EXPECT_EQ(retvalHdl.msg["command"].asString(), "status");
  1965. EXPECT_THAT(retvalHdl.msg["response"].asString(), testing::HasSubstr("fancy response"));
  1966. EXPECT_TRUE(cm.isLoggedIn());
  1967. }
  1968. /* =====================================
  1969. * test for extendedstatus
  1970. */
  1971. TEST(testExtendedstatus, Negative) {
  1972. FileManMock fm;
  1973. CmdManForTest cm(fm, dummyDebugPrint);
  1974. cm.initLoggedIn();
  1975. std::string cmd;
  1976. std::vector<std::string> args;
  1977. Json::Value root;
  1978. CmdMan::CmdRet retvalCmd;
  1979. CmdMan::CmdRet retvalHdl;
  1980. // prepare cmd/args/root
  1981. cmd = "extendedstatus";
  1982. args = {};
  1983. root["command"] = "extendedstatus";
  1984. root["accept"] = false;
  1985. root["error"] = "some foobar went wrong";
  1986. // stick into cmdman
  1987. retvalCmd = cm.execute(cmd, args);
  1988. retvalHdl = cm.handle(root);
  1989. // check things
  1990. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  1991. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedstatus");
  1992. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  1993. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedstatus");
  1994. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  1995. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("some foobar went wrong"));
  1996. EXPECT_TRUE(cm.isLoggedIn());
  1997. }
  1998. TEST(testExtendedstatus, Positive) {
  1999. FileManMock fm;
  2000. CmdManForTest cm(fm, dummyDebugPrint);
  2001. cm.initLoggedIn();
  2002. std::string cmd;
  2003. std::vector<std::string> args;
  2004. Json::Value root, transfersclientserver, tcs1, tcs2, transfersserverserver, tss1, tss2, tss3;
  2005. CmdMan::CmdRet retvalCmd;
  2006. CmdMan::CmdRet retvalHdl;
  2007. // prepare cmd/args/root
  2008. cmd = "extendedstatus";
  2009. args = {};
  2010. root["command"] = "extendedstatus";
  2011. root["accept"] = true;
  2012. tcs1["upload"] = true;
  2013. tcs1["file"] = "green.txt";
  2014. tcs1["progress"] = 99;
  2015. transfersclientserver.append(tcs1);
  2016. tcs2["upload"] = false;
  2017. tcs2["file"] = "red.txt";
  2018. tcs2["progress"] = 2;
  2019. transfersclientserver.append(tcs2);
  2020. root["transfersclientserver"] = transfersclientserver;
  2021. tss1["type"] = "upload";
  2022. tss1["file"] = "cyan.txt";
  2023. tss1["progress"] = 100;
  2024. tss1["method"] = "urg field";
  2025. tss1["speed"] = 42.0f;
  2026. transfersserverserver.append(tss1);
  2027. tss2["type"] = "download";
  2028. tss2["file"] = "magenta.txt";
  2029. tss2["progress"] = 42;
  2030. tss2["method"] = "fancy thing";
  2031. tss2["speed"] = 0.1f;
  2032. transfersserverserver.append(tss2);
  2033. tss3["type"] = "queued";
  2034. tss3["file"] = "yellow.txt";
  2035. tss3["progress"] = 42;
  2036. tss3["method"] = "cool timing stuff";
  2037. tss3["speed"] = 9000.001f;
  2038. transfersserverserver.append(tss3);
  2039. root["transfersserverserver"] = transfersserverserver;
  2040. // stick into cmdman
  2041. retvalCmd = cm.execute(cmd, args);
  2042. retvalHdl = cm.handle(root);
  2043. // check things
  2044. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2045. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedstatus");
  2046. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  2047. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedstatus");
  2048. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  2049. EXPECT_TRUE(retvalHdl.msg["transfersclientserver"][0]["upload"].asBool());
  2050. EXPECT_EQ(retvalHdl.msg["transfersclientserver"][0]["file"].asString(), "green.txt");
  2051. EXPECT_EQ(retvalHdl.msg["transfersclientserver"][0]["progress"].asInt(), 99);
  2052. EXPECT_FALSE(retvalHdl.msg["transfersclientserver"][1]["upload"].asBool());
  2053. EXPECT_EQ(retvalHdl.msg["transfersclientserver"][1]["file"].asString(), "red.txt");
  2054. EXPECT_EQ(retvalHdl.msg["transfersclientserver"][1]["progress"].asInt(), 2);
  2055. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][0]["type"].asString(), "upload");
  2056. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][0]["file"].asString(), "cyan.txt");
  2057. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][0]["progress"].asInt(), 100);
  2058. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][0]["method"].asString(), "urg field");
  2059. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][0]["speed"].asFloat(), 42.0f);
  2060. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][1]["type"].asString(), "download");
  2061. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][1]["file"].asString(), "magenta.txt");
  2062. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][1]["progress"].asInt(), 42);
  2063. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][1]["method"].asString(), "fancy thing");
  2064. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][1]["speed"].asFloat(), 0.1f);
  2065. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][2]["type"].asString(), "queued");
  2066. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][2]["file"].asString(), "yellow.txt");
  2067. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][2]["progress"].asInt(), 42);
  2068. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][2]["method"].asString(), "cool timing stuff");
  2069. EXPECT_EQ(retvalHdl.msg["transfersserverserver"][2]["speed"].asFloat(), 9000.001f);
  2070. EXPECT_TRUE(cm.isLoggedIn());
  2071. }
  2072. TEST(testExtendedstatus, Positive_NoTransfersRunning) {
  2073. FileManMock fm;
  2074. CmdManForTest cm(fm, dummyDebugPrint);
  2075. cm.initLoggedIn();
  2076. std::string cmd;
  2077. std::vector<std::string> args;
  2078. Json::Value root, transfersclientserver, transfersserverserver;
  2079. CmdMan::CmdRet retvalCmd;
  2080. CmdMan::CmdRet retvalHdl;
  2081. // prepare cmd/args/root
  2082. cmd = "extendedstatus";
  2083. args = {};
  2084. root["command"] = "extendedstatus";
  2085. root["accept"] = true;
  2086. root["transfersclientserver"] = transfersclientserver;
  2087. root["transfersserverserver"] = transfersserverserver;
  2088. // stick into cmdman
  2089. retvalCmd = cm.execute(cmd, args);
  2090. retvalHdl = cm.handle(root);
  2091. // check things
  2092. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2093. EXPECT_EQ(retvalCmd.msg["command"].asString(), "extendedstatus");
  2094. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  2095. EXPECT_EQ(retvalHdl.msg["command"].asString(), "extendedstatus");
  2096. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  2097. EXPECT_TRUE(retvalHdl.msg["transfersclientserver"].empty());
  2098. EXPECT_TRUE(retvalHdl.msg["transfersserverserver"].empty());
  2099. EXPECT_TRUE(cm.isLoggedIn());
  2100. }
  2101. /* =====================================
  2102. * tests for help
  2103. */
  2104. TEST(testHelp, LoggedIn) {
  2105. FileManMock fm;
  2106. CmdManForTest cm(fm, dummyDebugPrint);
  2107. cm.initLoggedIn();
  2108. std::string cmd;
  2109. std::vector<std::string> args;
  2110. CmdMan::CmdRet retvalCmd;
  2111. // prepare cmd/args/root
  2112. cmd = "help";
  2113. args = {};
  2114. // stick into cmdman
  2115. retvalCmd = cm.execute(cmd, args);
  2116. // check things
  2117. EXPECT_EQ(retvalCmd.type, CmdMan::print);
  2118. EXPECT_EQ(retvalCmd.msg["command"].asString(), "help");
  2119. EXPECT_NE(retvalCmd.msg["names"], "");
  2120. EXPECT_TRUE(cm.isLoggedIn());
  2121. }
  2122. TEST(testHelp, NotConnected) {
  2123. FileManMock fm;
  2124. CmdManForTest cm(fm, dummyDebugPrint);
  2125. std::string cmd;
  2126. std::vector<std::string> args;
  2127. CmdMan::CmdRet retvalCmd;
  2128. // prepare cmd/args/root
  2129. cmd = "help";
  2130. args = {};
  2131. // stick into cmdman
  2132. retvalCmd = cm.execute(cmd, args);
  2133. // check things
  2134. EXPECT_EQ(retvalCmd.type, CmdMan::print);
  2135. EXPECT_EQ(retvalCmd.msg["command"].asString(), "help");
  2136. EXPECT_NE(retvalCmd.msg["names"], "");
  2137. EXPECT_TRUE(cm.isNotConnected());
  2138. }
  2139. /* =====================================
  2140. * tests for keyfile and closekey
  2141. */
  2142. TEST(testKeyfile, TooFewArgs) {
  2143. FileManMock fm;
  2144. CmdManForTest cm(fm, dummyDebugPrint);
  2145. cm.initLoggedIn();
  2146. std::string cmd;
  2147. std::vector<std::string> args;
  2148. CmdMan::CmdRet retvalCmd;
  2149. // prepare cmd/args
  2150. cmd = "keyfile";
  2151. args = {};
  2152. // stick into cmdman
  2153. EXPECT_CALL(fm, openKey).Times(0);
  2154. EXPECT_CALL(fm, getOpensslError).Times(0);
  2155. retvalCmd = cm.execute(cmd, args);
  2156. // check things
  2157. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  2158. EXPECT_EQ(retvalCmd.msg["command"].asString(), "keyfile");
  2159. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  2160. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  2161. EXPECT_TRUE(cm.isLoggedIn());
  2162. }
  2163. TEST(testKeyfile, Negative) {
  2164. FileManMock fm;
  2165. CmdManForTest cm(fm, dummyDebugPrint);
  2166. cm.initLoggedIn();
  2167. std::string cmd;
  2168. std::vector<std::string> args;
  2169. CmdMan::CmdRet retvalCmd;
  2170. // prepare cmd/args
  2171. cmd = "keyfile";
  2172. args = {"./some/path"};
  2173. // stick into cmdman
  2174. EXPECT_CALL(fm, openKey).WillOnce(testing::Return(false));
  2175. EXPECT_CALL(fm, getOpensslError).WillOnce(testing::Return("some openssl error"));
  2176. retvalCmd = cm.execute(cmd, args);
  2177. // check things
  2178. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  2179. EXPECT_EQ(retvalCmd.msg["command"].asString(), "keyfile");
  2180. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  2181. EXPECT_EQ(retvalCmd.msg["file"].asString(), "./some/path");
  2182. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  2183. EXPECT_TRUE(cm.isLoggedIn());
  2184. }
  2185. TEST(testKeyfile, Positive) {
  2186. FileManMock fm;
  2187. CmdManForTest cm(fm, dummyDebugPrint);
  2188. cm.initLoggedIn();
  2189. std::string cmd;
  2190. std::vector<std::string> args;
  2191. CmdMan::CmdRet retvalCmd;
  2192. // prepare cmd/args
  2193. cmd = "keyfile";
  2194. args = {"./some/path"};
  2195. // stick into cmdman
  2196. EXPECT_CALL(fm, openKey).WillOnce(testing::Return(true));
  2197. EXPECT_CALL(fm, getOpensslError).Times(0);
  2198. retvalCmd = cm.execute(cmd, args);
  2199. // check things
  2200. EXPECT_EQ(retvalCmd.type, CmdMan::print);
  2201. EXPECT_EQ(retvalCmd.msg["command"].asString(), "keyfile");
  2202. EXPECT_TRUE(retvalCmd.msg["accept"].asBool());
  2203. EXPECT_EQ(retvalCmd.msg["file"].asString(), "./some/path");
  2204. EXPECT_TRUE(cm.isLoggedIn());
  2205. }
  2206. TEST(testClosekey, Positive) {
  2207. FileManMock fm;
  2208. CmdManForTest cm(fm, dummyDebugPrint);
  2209. cm.initLoggedIn();
  2210. std::string cmd;
  2211. std::vector<std::string> args;
  2212. CmdMan::CmdRet retvalCmd;
  2213. // prepare cmd/args
  2214. cmd = "closekey";
  2215. args = {};
  2216. // stick into cmdman
  2217. EXPECT_CALL(fm, closeKey).WillOnce(testing::Return(true));
  2218. retvalCmd = cm.execute(cmd, args);
  2219. // check things
  2220. EXPECT_EQ(retvalCmd.type, CmdMan::print);
  2221. EXPECT_EQ(retvalCmd.msg["command"].asString(), "closekey");
  2222. EXPECT_TRUE(retvalCmd.msg["accept"].asBool());
  2223. EXPECT_TRUE(cm.isLoggedIn());
  2224. }
  2225. TEST(testClosekey, Negative) {
  2226. FileManMock fm;
  2227. CmdManForTest cm(fm, dummyDebugPrint);
  2228. cm.initLoggedIn();
  2229. std::string cmd;
  2230. std::vector<std::string> args;
  2231. CmdMan::CmdRet retvalCmd;
  2232. // prepare cmd/args
  2233. cmd = "closekey";
  2234. args = {};
  2235. // stick into cmdman
  2236. EXPECT_CALL(fm, closeKey).WillOnce(testing::Return(false));
  2237. retvalCmd = cm.execute(cmd, args);
  2238. // check things
  2239. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  2240. EXPECT_EQ(retvalCmd.msg["command"].asString(), "closekey");
  2241. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  2242. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  2243. EXPECT_TRUE(cm.isLoggedIn());
  2244. }
  2245. /* =====================================
  2246. * test for notifications
  2247. */
  2248. TEST(testNotifications, Positive) {
  2249. FileManMock fm;
  2250. CmdManForTest cm(fm, dummyDebugPrint);
  2251. cm.initLoggedIn();
  2252. std::string cmd;
  2253. std::vector<std::string> args;
  2254. Json::Value root, messagesArr;
  2255. CmdMan::CmdRet retvalCmd;
  2256. CmdMan::CmdRet retvalHdl;
  2257. vector<string> msgvec = {"msg1", "msg2", "msg3 foo", "msg4 hello world!"};
  2258. // prepare cmd/args/root
  2259. cmd = "notifications";
  2260. args = {};
  2261. root["command"] = "notifications";
  2262. for (string s : msgvec)
  2263. messagesArr.append(s);
  2264. root["messages"] = messagesArr;
  2265. root["accept"] = true;
  2266. // stick into cmdman
  2267. retvalCmd = cm.execute(cmd, args);
  2268. retvalHdl = cm.handle(root);
  2269. // check things
  2270. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2271. EXPECT_EQ(retvalCmd.msg["command"].asString(), "notifications");
  2272. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  2273. EXPECT_EQ(retvalHdl.msg["command"].asString(), "notifications");
  2274. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  2275. vector<string> retval_msgs;
  2276. for (Json::Value i : retvalHdl.msg["messages"])
  2277. retval_msgs.push_back(i.asString());
  2278. EXPECT_EQ(retval_msgs, msgvec);
  2279. EXPECT_TRUE(cm.isLoggedIn());
  2280. }
  2281. TEST(testNotifications, Negative) {
  2282. FileManMock fm;
  2283. CmdManForTest cm(fm, dummyDebugPrint);
  2284. cm.initLoggedIn();
  2285. std::string cmd;
  2286. std::vector<std::string> args;
  2287. Json::Value root, messagesArr;
  2288. CmdMan::CmdRet retvalCmd;
  2289. CmdMan::CmdRet retvalHdl;
  2290. // prepare cmd/args/root
  2291. cmd = "notifications";
  2292. args = {};
  2293. root["command"] = "notifications";
  2294. root["accept"] = false;
  2295. root["error"] = "some very descriptive string";
  2296. // stick into cmdman
  2297. retvalCmd = cm.execute(cmd, args);
  2298. retvalHdl = cm.handle(root);
  2299. // check things
  2300. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2301. EXPECT_EQ(retvalCmd.msg["command"].asString(), "notifications");
  2302. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  2303. EXPECT_EQ(retvalHdl.msg["command"].asString(), "notifications");
  2304. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  2305. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("some very descriptive string"));
  2306. EXPECT_TRUE(cm.isLoggedIn());
  2307. }
  2308. /* =====================================
  2309. * tests for queue and dequeue
  2310. */
  2311. TEST(testQueue, TooFewArgs) {
  2312. FileManMock fm;
  2313. CmdManForTest cm(fm, dummyDebugPrint);
  2314. cm.initLoggedIn();
  2315. std::string cmd;
  2316. std::vector<std::string> args;
  2317. CmdMan::CmdRet retvalCmd;
  2318. // prepare cmd/args
  2319. cmd = "queue";
  2320. args = {};
  2321. // stick into cmdman
  2322. retvalCmd = cm.execute(cmd, args);
  2323. // check things
  2324. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  2325. EXPECT_EQ(retvalCmd.msg["command"].asString(), "queue");
  2326. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  2327. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  2328. EXPECT_TRUE(cm.isLoggedIn());
  2329. }
  2330. TEST(testQueue, Positive) {
  2331. FileManMock fm;
  2332. CmdManForTest cm(fm, dummyDebugPrint);
  2333. cm.initLoggedIn();
  2334. std::string cmd;
  2335. std::vector<std::string> args;
  2336. Json::Value root;
  2337. CmdMan::CmdRet retvalCmd;
  2338. CmdMan::CmdRet retvalHdl;
  2339. // prepare cmd/args/root
  2340. cmd = "queue";
  2341. args = {"fancy_file.txt"};
  2342. root["command"] = "queue";
  2343. root["file"] = "fancy_file.txt";
  2344. root["accept"] = true;
  2345. // stick into cmdman
  2346. retvalCmd = cm.execute(cmd, args);
  2347. retvalHdl = cm.handle(root);
  2348. // check things
  2349. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2350. EXPECT_EQ(retvalCmd.msg["command"].asString(), "queue");
  2351. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  2352. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  2353. EXPECT_EQ(retvalHdl.msg["command"].asString(), "queue");
  2354. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  2355. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  2356. EXPECT_TRUE(cm.isLoggedIn());
  2357. }
  2358. TEST(testQueue, Negative) {
  2359. FileManMock fm;
  2360. CmdManForTest cm(fm, dummyDebugPrint);
  2361. cm.initLoggedIn();
  2362. std::string cmd;
  2363. std::vector<std::string> args;
  2364. Json::Value root;
  2365. CmdMan::CmdRet retvalCmd;
  2366. CmdMan::CmdRet retvalHdl;
  2367. // prepare cmd/args/root
  2368. cmd = "queue";
  2369. args = {"fancy_file.txt"};
  2370. root["command"] = "queue";
  2371. root["file"] = "fancy_file.txt";
  2372. root["accept"] = false;
  2373. root["error"] = "file does not exist";
  2374. // stick into cmdman
  2375. retvalCmd = cm.execute(cmd, args);
  2376. retvalHdl = cm.handle(root);
  2377. // check things
  2378. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2379. EXPECT_EQ(retvalCmd.msg["command"].asString(), "queue");
  2380. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  2381. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  2382. EXPECT_EQ(retvalHdl.msg["command"].asString(), "queue");
  2383. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  2384. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  2385. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("file does not exist"));
  2386. EXPECT_TRUE(cm.isLoggedIn());
  2387. }
  2388. TEST(testDequeue, TooFewArgs) {
  2389. FileManMock fm;
  2390. CmdManForTest cm(fm, dummyDebugPrint);
  2391. cm.initLoggedIn();
  2392. std::string cmd;
  2393. std::vector<std::string> args;
  2394. CmdMan::CmdRet retvalCmd;
  2395. // prepare cmd/args
  2396. cmd = "dequeue";
  2397. args = {};
  2398. // stick into cmdman
  2399. retvalCmd = cm.execute(cmd, args);
  2400. // check things
  2401. EXPECT_EQ(retvalCmd.type, CmdMan::error);
  2402. EXPECT_EQ(retvalCmd.msg["command"].asString(), "dequeue");
  2403. EXPECT_FALSE(retvalCmd.msg["accept"].asBool());
  2404. EXPECT_NE(retvalCmd.msg["error"].asString(), "");
  2405. EXPECT_TRUE(cm.isLoggedIn());
  2406. }
  2407. TEST(testDequeue, Positive) {
  2408. FileManMock fm;
  2409. CmdManForTest cm(fm, dummyDebugPrint);
  2410. cm.initLoggedIn();
  2411. std::string cmd;
  2412. std::vector<std::string> args;
  2413. Json::Value root;
  2414. CmdMan::CmdRet retvalCmd;
  2415. CmdMan::CmdRet retvalHdl;
  2416. // prepare cmd/args/root
  2417. cmd = "dequeue";
  2418. args = {"fancy_file.txt"};
  2419. root["command"] = "dequeue";
  2420. root["file"] = "fancy_file.txt";
  2421. root["accept"] = true;
  2422. // stick into cmdman
  2423. retvalCmd = cm.execute(cmd, args);
  2424. retvalHdl = cm.handle(root);
  2425. // check things
  2426. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2427. EXPECT_EQ(retvalCmd.msg["command"].asString(), "dequeue");
  2428. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  2429. EXPECT_EQ(retvalHdl.type, CmdMan::print);
  2430. EXPECT_EQ(retvalHdl.msg["command"].asString(), "dequeue");
  2431. EXPECT_TRUE(retvalHdl.msg["accept"].asBool());
  2432. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  2433. EXPECT_TRUE(cm.isLoggedIn());
  2434. }
  2435. TEST(testDequeue, Negative) {
  2436. FileManMock fm;
  2437. CmdManForTest cm(fm, dummyDebugPrint);
  2438. cm.initLoggedIn();
  2439. std::string cmd;
  2440. std::vector<std::string> args;
  2441. Json::Value root;
  2442. CmdMan::CmdRet retvalCmd;
  2443. CmdMan::CmdRet retvalHdl;
  2444. // prepare cmd/args/root
  2445. cmd = "dequeue";
  2446. args = {"fancy_file.txt"};
  2447. root["command"] = "dequeue";
  2448. root["file"] = "fancy_file.txt";
  2449. root["accept"] = false;
  2450. root["error"] = "file not in queue";
  2451. // stick into cmdman
  2452. retvalCmd = cm.execute(cmd, args);
  2453. retvalHdl = cm.handle(root);
  2454. // check things
  2455. EXPECT_EQ(retvalCmd.type, CmdMan::send);
  2456. EXPECT_EQ(retvalCmd.msg["command"].asString(), "dequeue");
  2457. EXPECT_EQ(retvalCmd.msg["file"].asString(), "fancy_file.txt");
  2458. EXPECT_EQ(retvalHdl.type, CmdMan::error);
  2459. EXPECT_EQ(retvalHdl.msg["command"].asString(), "dequeue");
  2460. EXPECT_FALSE(retvalHdl.msg["accept"].asBool());
  2461. EXPECT_EQ(retvalHdl.msg["file"].asString(), "fancy_file.txt");
  2462. EXPECT_THAT(retvalHdl.msg["error"].asString(), testing::HasSubstr("file not in queue"));
  2463. EXPECT_TRUE(cm.isLoggedIn());
  2464. }
  2465. /* =====================================
  2466. * test for all commands that require a login
  2467. */
  2468. TEST(testCommandsWithRequiredLogin, NotLoggedInOrNotConnected) {
  2469. FileManMock fm;
  2470. CmdManForTest cm(fm, dummyDebugPrint);
  2471. cm.initLoggedIn();
  2472. CmdMan::CmdRet retval1, retval2, retval3;
  2473. // prepare cmd/args
  2474. std::vector<std::string> cmds = {"put", "get", "list", "extendedlist", "putdata", "getdata", "listdata", "extendedlistdata",
  2475. "head", "deletefile", "deleteme", "keyfile", "closekey", "extendedstatus"};
  2476. // as every command works fine with too many args, we will simply pass three to all of them
  2477. std::vector<std::string> args = {"arg1", "arg2", "arg3"};
  2478. for (std::string cmd : cmds) {
  2479. cm.initNotConnected();
  2480. retval1 = cm.execute(cmd, args);
  2481. cm.initConnected();
  2482. retval2 = cm.execute(cmd, args);
  2483. cm.initVersionChecked();
  2484. retval3 = cm.execute(cmd, args);
  2485. EXPECT_EQ(retval1.type, CmdMan::error);
  2486. EXPECT_EQ(retval1.msg["command"].asString(), "error");
  2487. EXPECT_NE(retval1.msg["error"].asString(), "");
  2488. EXPECT_EQ(retval2.type, CmdMan::error);
  2489. EXPECT_EQ(retval2.msg["command"].asString(), "error");
  2490. EXPECT_NE(retval2.msg["error"].asString(), "");
  2491. EXPECT_EQ(retval3.type, CmdMan::error);
  2492. EXPECT_EQ(retval3.msg["command"].asString(), "error");
  2493. EXPECT_NE(retval3.msg["error"].asString(), "");
  2494. }
  2495. }
  2496. } // namespace
  2497. /* end of namespace */