PageRenderTime 49ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/test/service/main/cmdlineparser.cpp

https://gitlab.com/github-cloud-corporation/cynara
C++ | 542 lines | 330 code | 79 blank | 133 comment | 15 complexity | df1655c89d0558dc5b9e3b5e9adb04d5 MD5 | raw file
  1. /*
  2. * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License
  15. */
  16. /**
  17. * @file test/service/main/cmdlineparser.cpp
  18. * @author Pawel Wieczorek <p.wieczorek2@samsung.com>
  19. * @version 1.0
  20. * @brief Tests of CmdlineParser
  21. */
  22. #include <string>
  23. #include <service/main/CmdlineParser.h>
  24. #include "CynaraCommandlineTest.h"
  25. namespace {
  26. const std::string execName("./cynara");
  27. const std::string helpMessage(
  28. "Usage: " + execName + " [OPTIONS]\n\n"
  29. "Information mode options [program exits after printing information]:\n"
  30. " -V, --version print version of " + execName + " and exit\n"
  31. " -h, --help print this help message and exit\n"
  32. "Normal work mode options:\n"
  33. " -d, --daemon daemonize "
  34. "[by default " + execName + " does not daemonize]\n"
  35. " -m, --mask=MASK set umask to MASK "
  36. "[by default no umask is set]\n"
  37. " -u, --user=USER change user to USER "
  38. "[by default uid is not changed]\n"
  39. " -g, --group=GROUP change group to GROUP "
  40. "[by default gid is not changed]\n");
  41. } // namespace
  42. namespace Parser = Cynara::CmdlineParser;
  43. /**
  44. * @brief Verify if passing "help" option to commandline handler returns help message
  45. * @test Expected result:
  46. * - call handler indicates success
  47. * - help message in output stream
  48. * - empty error stream
  49. */
  50. TEST_F(CynaraCommandlineTest, help) {
  51. std::string err;
  52. std::string out;
  53. for (const auto &opt : { "-h", "--help" }) {
  54. clearOutput();
  55. prepare_argv({ execName, opt });
  56. SCOPED_TRACE(opt);
  57. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  58. getOutput(out, err);
  59. ASSERT_FALSE(options.m_error);
  60. ASSERT_TRUE(options.m_exit);
  61. ASSERT_FALSE(options.m_daemon);
  62. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  63. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  64. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  65. ASSERT_EQ(helpMessage, out);
  66. ASSERT_TRUE(err.empty());
  67. }
  68. }
  69. /**
  70. * @brief Verify if passing "version" option to commandline handler returns version message
  71. * @test Expected result:
  72. * - call handler indicates success
  73. * - version message in output stream
  74. * - empty error stream
  75. */
  76. TEST_F(CynaraCommandlineTest, version) {
  77. std::string err;
  78. std::string out;
  79. for (const auto &opt : { "-V", "--version" }) {
  80. clearOutput();
  81. prepare_argv({ execName, opt });
  82. SCOPED_TRACE(opt);
  83. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  84. getOutput(out, err);
  85. ASSERT_FALSE(options.m_error);
  86. ASSERT_TRUE(options.m_exit);
  87. ASSERT_FALSE(options.m_daemon);
  88. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  89. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  90. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  91. ASSERT_EQ(std::string(CYNARA_VERSION) + "\n", out);
  92. ASSERT_TRUE(err.empty());
  93. }
  94. }
  95. /**
  96. * @brief Verify if passing unknown option to commandline handler returns error message
  97. * @test Expected result:
  98. * - call handler indicates failure
  99. * - help message in output stream
  100. * - error message in error stream
  101. */
  102. TEST_F(CynaraCommandlineTest, unknownOption) {
  103. std::string err;
  104. std::string out;
  105. for (const auto &badOpt : { "-b", "--badOption" }) {
  106. clearOutput();
  107. prepare_argv({ execName, badOpt });
  108. SCOPED_TRACE(badOpt);
  109. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  110. getOutput(out, err);
  111. ASSERT_TRUE(options.m_error);
  112. ASSERT_TRUE(options.m_exit);
  113. ASSERT_FALSE(options.m_daemon);
  114. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  115. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  116. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  117. ASSERT_EQ(helpMessage, out);
  118. ASSERT_EQ(std::string("Unknown option: ") + badOpt + "\n", err);
  119. }
  120. }
  121. /**
  122. * @brief Verify if passing no options to commandline handler succeeds
  123. * @test Expected result:
  124. * - call handler indicates success
  125. * - empty output stream
  126. * - empty error stream
  127. */
  128. TEST_F(CynaraCommandlineTest, noOption) {
  129. std::string err;
  130. std::string out;
  131. clearOutput();
  132. prepare_argv({ execName });
  133. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  134. getOutput(out, err);
  135. ASSERT_FALSE(options.m_error);
  136. ASSERT_FALSE(options.m_exit);
  137. ASSERT_FALSE(options.m_daemon);
  138. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  139. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  140. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  141. ASSERT_TRUE(out.empty());
  142. ASSERT_TRUE(err.empty());
  143. }
  144. /**
  145. * @brief Verify if passing daemon option to commandline succeeds
  146. * @test Expected result:
  147. * - call handler indicates success
  148. * - empty output stream
  149. * - empty error stream
  150. */
  151. TEST_F(CynaraCommandlineTest, daemonOption) {
  152. std::string err;
  153. std::string out;
  154. for (const auto &daemonOpt : { "-d", "--daemon" }) {
  155. clearOutput();
  156. prepare_argv({ execName, daemonOpt });
  157. SCOPED_TRACE(daemonOpt);
  158. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  159. getOutput(out, err);
  160. ASSERT_FALSE(options.m_error);
  161. ASSERT_FALSE(options.m_exit);
  162. ASSERT_TRUE(options.m_daemon);
  163. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  164. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  165. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  166. ASSERT_TRUE(out.empty());
  167. ASSERT_TRUE(err.empty());
  168. }
  169. }
  170. /**
  171. * @brief Verify if passing mask option to commandline succeeds
  172. * @test Expected result:
  173. * - call handler indicates success
  174. * - empty output stream
  175. * - empty error stream
  176. */
  177. TEST_F(CynaraCommandlineTest, maskOption) {
  178. std::string err;
  179. std::string out;
  180. std::string maskParam("0666");
  181. for (const auto &maskOpt : { "-m", "--mask" }) {
  182. clearOutput();
  183. prepare_argv({ execName, maskOpt, maskParam});
  184. SCOPED_TRACE(maskOpt);
  185. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  186. getOutput(out, err);
  187. ASSERT_FALSE(options.m_error);
  188. ASSERT_FALSE(options.m_exit);
  189. ASSERT_FALSE(options.m_daemon);
  190. ASSERT_EQ(options.m_mask, static_cast<mode_t>(0666));
  191. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  192. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  193. ASSERT_TRUE(out.empty());
  194. ASSERT_TRUE(err.empty());
  195. }
  196. }
  197. /**
  198. * @brief Verify if passing invalid mask option to commandline fails
  199. * @test Expected result:
  200. * - call handler indicates failure
  201. * - help message in output stream
  202. * - error message in error stream
  203. */
  204. TEST_F(CynaraCommandlineTest, maskOptionInvalid) {
  205. std::string err;
  206. std::string out;
  207. std::string maskParam("MASK");
  208. for (const auto &maskOpt : { "-m", "--mask" }) {
  209. clearOutput();
  210. prepare_argv({ execName, maskOpt, maskParam});
  211. SCOPED_TRACE(maskOpt);
  212. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  213. getOutput(out, err);
  214. ASSERT_TRUE(options.m_error);
  215. ASSERT_TRUE(options.m_exit);
  216. ASSERT_FALSE(options.m_daemon);
  217. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  218. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  219. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  220. ASSERT_EQ(helpMessage, out);
  221. ASSERT_EQ("Invalid param: MASK\n", err);
  222. }
  223. }
  224. /**
  225. * @brief Verify if passing no mask option to commandline fails
  226. * @test Expected result:
  227. * - call handler indicates failure
  228. * - help message in output stream
  229. * - error message in error stream
  230. */
  231. TEST_F(CynaraCommandlineTest, maskOptionNoParam) {
  232. std::string err;
  233. std::string out;
  234. for (const auto &maskOpt : { "-m", "--mask" }) {
  235. clearOutput();
  236. prepare_argv({ execName, maskOpt});
  237. SCOPED_TRACE(maskOpt);
  238. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  239. getOutput(out, err);
  240. ASSERT_TRUE(options.m_error);
  241. ASSERT_TRUE(options.m_exit);
  242. ASSERT_FALSE(options.m_daemon);
  243. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  244. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  245. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  246. ASSERT_EQ(helpMessage, out);
  247. ASSERT_EQ(std::string("Missing argument for option: ") + maskOpt + "\n", err);
  248. }
  249. }
  250. /**
  251. * @brief Verify if passing user option to commandline succeeds
  252. * @test Expected result:
  253. * - call handler indicates success
  254. * - empty output stream
  255. * - empty error stream
  256. */
  257. TEST_F(CynaraCommandlineTest, userOptionName) {
  258. std::string err;
  259. std::string out;
  260. std::string userParam("root");
  261. for (const auto &userOpt : { "-u", "--user" }) {
  262. clearOutput();
  263. prepare_argv({ execName, userOpt, userParam});
  264. SCOPED_TRACE(userOpt);
  265. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  266. getOutput(out, err);
  267. ASSERT_FALSE(options.m_error);
  268. ASSERT_FALSE(options.m_exit);
  269. ASSERT_FALSE(options.m_daemon);
  270. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  271. ASSERT_EQ(options.m_uid, static_cast<uid_t>(0));
  272. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  273. ASSERT_TRUE(out.empty());
  274. ASSERT_TRUE(err.empty());
  275. }
  276. }
  277. /**
  278. * @brief Verify if passing user option to commandline succeeds
  279. * @test Expected result:
  280. * - call handler indicates success
  281. * - empty output stream
  282. * - empty error stream
  283. */
  284. TEST_F(CynaraCommandlineTest, userOptionUid) {
  285. std::string err;
  286. std::string out;
  287. std::string userParam("1234");
  288. for (const auto &userOpt : { "-u", "--user" }) {
  289. clearOutput();
  290. prepare_argv({ execName, userOpt, userParam});
  291. SCOPED_TRACE(userOpt);
  292. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  293. getOutput(out, err);
  294. ASSERT_FALSE(options.m_error);
  295. ASSERT_FALSE(options.m_exit);
  296. ASSERT_FALSE(options.m_daemon);
  297. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  298. ASSERT_EQ(options.m_uid, static_cast<uid_t>(1234));
  299. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  300. ASSERT_TRUE(out.empty());
  301. ASSERT_TRUE(err.empty());
  302. }
  303. }
  304. /**
  305. * @brief Verify if passing invalid user option to commandline fails
  306. * @test Expected result:
  307. * - call handler indicates success
  308. * - help message in output stream
  309. * - error message in error stream
  310. */
  311. TEST_F(CynaraCommandlineTest, userOptionInvalid) {
  312. std::string err;
  313. std::string out;
  314. std::string userParam("UserThatDoNotExist");
  315. for (const auto &userOpt : { "-u", "--user" }) {
  316. clearOutput();
  317. prepare_argv({ execName, userOpt, userParam});
  318. SCOPED_TRACE(userOpt);
  319. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  320. getOutput(out, err);
  321. ASSERT_TRUE(options.m_error);
  322. ASSERT_TRUE(options.m_exit);
  323. ASSERT_FALSE(options.m_daemon);
  324. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  325. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  326. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  327. ASSERT_EQ(helpMessage, out);
  328. ASSERT_EQ("Invalid param: UserThatDoNotExist\n", err);
  329. }
  330. }
  331. /**
  332. * @brief Verify if passing no user option to commandline fails
  333. * @test Expected result:
  334. * - call handler indicates failure
  335. * - help message in output stream
  336. * - error message in error stream
  337. */
  338. TEST_F(CynaraCommandlineTest, userOptionNoParam) {
  339. std::string err;
  340. std::string out;
  341. for (const auto &userOpt : { "-u", "--user" }) {
  342. clearOutput();
  343. prepare_argv({ execName, userOpt});
  344. SCOPED_TRACE(userOpt);
  345. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  346. getOutput(out, err);
  347. ASSERT_TRUE(options.m_error);
  348. ASSERT_TRUE(options.m_exit);
  349. ASSERT_FALSE(options.m_daemon);
  350. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  351. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  352. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  353. ASSERT_EQ(helpMessage, out);
  354. ASSERT_EQ(std::string("Missing argument for option: ") + userOpt + "\n", err);
  355. }
  356. }
  357. /**
  358. * @brief Verify if passing group option to commandline succeeds
  359. * @test Expected result:
  360. * - call handler indicates success
  361. * - empty output stream
  362. * - empty error stream
  363. */
  364. TEST_F(CynaraCommandlineTest, groupOptionName) {
  365. std::string err;
  366. std::string out;
  367. std::string groupParam("root");
  368. for (const auto &groupOpt : { "-g", "--group" }) {
  369. clearOutput();
  370. prepare_argv({ execName, groupOpt, groupParam});
  371. SCOPED_TRACE(groupOpt);
  372. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  373. getOutput(out, err);
  374. ASSERT_FALSE(options.m_error);
  375. ASSERT_FALSE(options.m_exit);
  376. ASSERT_FALSE(options.m_daemon);
  377. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  378. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  379. ASSERT_EQ(options.m_gid, static_cast<gid_t>(0));
  380. ASSERT_TRUE(out.empty());
  381. ASSERT_TRUE(err.empty());
  382. }
  383. }
  384. /**
  385. * @brief Verify if passing group option to commandline succeeds
  386. * @test Expected result:
  387. * - call handler indicates success
  388. * - empty output stream
  389. * - empty error stream
  390. */
  391. TEST_F(CynaraCommandlineTest, groupOptionGid) {
  392. std::string err;
  393. std::string out;
  394. std::string groupParam("1234");
  395. for (const auto &groupOpt : { "-g", "--group" }) {
  396. clearOutput();
  397. prepare_argv({ execName, groupOpt, groupParam});
  398. SCOPED_TRACE(groupOpt);
  399. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  400. getOutput(out, err);
  401. ASSERT_FALSE(options.m_error);
  402. ASSERT_FALSE(options.m_exit);
  403. ASSERT_FALSE(options.m_daemon);
  404. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  405. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  406. ASSERT_EQ(options.m_gid, static_cast<gid_t>(1234));
  407. ASSERT_TRUE(out.empty());
  408. ASSERT_TRUE(err.empty());
  409. }
  410. }
  411. /**
  412. * @brief Verify if passing invalid group option to commandline fails
  413. * @test Expected result:
  414. * - call handler indicates success
  415. * - help message in output stream
  416. * - error message in error stream
  417. */
  418. TEST_F(CynaraCommandlineTest, groupOptionInvalid) {
  419. std::string err;
  420. std::string out;
  421. std::string groupParam("GroupThatDoNotExist");
  422. for (const auto &groupOpt : { "-g", "--group" }) {
  423. clearOutput();
  424. prepare_argv({ execName, groupOpt, groupParam});
  425. SCOPED_TRACE(groupOpt);
  426. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  427. getOutput(out, err);
  428. ASSERT_TRUE(options.m_error);
  429. ASSERT_TRUE(options.m_exit);
  430. ASSERT_FALSE(options.m_daemon);
  431. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  432. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  433. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  434. ASSERT_EQ(helpMessage, out);
  435. ASSERT_EQ("Invalid param: GroupThatDoNotExist\n", err);
  436. }
  437. }
  438. /**
  439. * @brief Verify if passing no group option to commandline fails
  440. * @test Expected result:
  441. * - call handler indicates failure
  442. * - help message in output stream
  443. * - error message in error stream
  444. */
  445. TEST_F(CynaraCommandlineTest, groupOptionNoParam) {
  446. std::string err;
  447. std::string out;
  448. for (const auto &groupOpt : { "-g", "--group" }) {
  449. clearOutput();
  450. prepare_argv({ execName, groupOpt});
  451. SCOPED_TRACE(groupOpt);
  452. const auto options = Parser::handleCmdlineOptions(this->argc(), this->argv());
  453. getOutput(out, err);
  454. ASSERT_TRUE(options.m_error);
  455. ASSERT_TRUE(options.m_exit);
  456. ASSERT_FALSE(options.m_daemon);
  457. ASSERT_EQ(options.m_mask, static_cast<mode_t>(-1));
  458. ASSERT_EQ(options.m_uid, static_cast<uid_t>(-1));
  459. ASSERT_EQ(options.m_gid, static_cast<gid_t>(-1));
  460. ASSERT_EQ(helpMessage, out);
  461. ASSERT_EQ(std::string("Missing argument for option: ") + groupOpt + "\n", err);
  462. }
  463. }