cmdman_test.cpp 89 KB

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