cmdman_test.cpp 70 KB

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