Add tests for version reporting 52/35552/4
authorPawel Wieczorek <p.wieczorek2@samsung.com>
Tue, 17 Feb 2015 09:28:02 +0000 (10:28 +0100)
committerPawel Wieczorek <p.wieczorek2@samsung.com>
Tue, 24 Feb 2015 15:02:32 +0000 (16:02 +0100)
This patch adds tests for calling main Cynara executable with additional
options. Following call scenarios are checked (both long and short
options):
* print version,
* print help,
* unknown option.

Change-Id: Ibab5d7a081fd1da8b98a59c9a242fd17725cd400

test/CMakeLists.txt
test/service/main/CynaraCommandlineTest.h [new file with mode: 0644]
test/service/main/cmdlineparser.cpp [new file with mode: 0644]

index 119e2e7..8032ee3 100644 (file)
@@ -59,6 +59,7 @@ SET(CYNARA_SOURCES_FOR_TESTS
     ${CYNARA_SRC}/cyad/PolicyTypeTranslator.cpp
     ${CYNARA_SRC}/helpers/creds-commons/CredsCommonsInner.cpp
     ${CYNARA_SRC}/helpers/creds-commons/creds-commons.cpp
+    ${CYNARA_SRC}/service/main/CmdlineParser.cpp
     ${CYNARA_SRC}/storage/BucketDeserializer.cpp
     ${CYNARA_SRC}/storage/InMemoryStorageBackend.cpp
     ${CYNARA_SRC}/storage/Integrity.cpp
@@ -86,6 +87,7 @@ SET(CYNARA_TESTS_SOURCES
     cyad/policy_collection.cpp
     cyad/policy_parser.cpp
     helpers.cpp
+    service/main/cmdlineparser.cpp
     storage/performance/bucket.cpp
     storage/storage/policies.cpp
     storage/storage/check.cpp
diff --git a/test/service/main/CynaraCommandlineTest.h b/test/service/main/CynaraCommandlineTest.h
new file mode 100644 (file)
index 0000000..eaf208d
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *    Licensed under the Apache License, Version 2.0 (the "License");
+ *    you may not use this file except in compliance with the License.
+ *    You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *    Unless required by applicable law or agreed to in writing, software
+ *    distributed under the License is distributed on an "AS IS" BASIS,
+ *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *    See the License for the specific language governing permissions and
+ *    limitations under the License.
+ */
+/**
+ * @file        test/service/main/CynaraCommandlineTest.h
+ * @author      Pawel Wieczorek <p.wieczorek2@samsung.com>
+ * @version     1.0
+ * @brief       Fixture for CmdlineParser tests
+ */
+
+#ifndef TEST_SERVICE_MAIN_CYNARACOMMANDLINETEST_H_
+#define TEST_SERVICE_MAIN_CYNARACOMMANDLINETEST_H_
+
+#include <QuietCommandlineTest.h>
+
+class CynaraCommandlineTest : public QuietCommandlineTest {
+};
+
+#endif /* TEST_SERVICE_MAIN_CYNARACOMMANDLINETEST_H_ */
diff --git a/test/service/main/cmdlineparser.cpp b/test/service/main/cmdlineparser.cpp
new file mode 100644 (file)
index 0000000..3f6a43b
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License
+ */
+/**
+ * @file        test/service/main/cmdlineparser.cpp
+ * @author      Pawel Wieczorek <p.wieczorek2@samsung.com>
+ * @version     1.0
+ * @brief       Tests of CmdlineParser
+ */
+
+#include <string>
+
+#include <service/main/CmdlineParser.h>
+
+#include "CynaraCommandlineTest.h"
+
+namespace {
+
+const std::string execName("./cynara");
+const std::string helpMessage("Usage: " + execName + " [OPTIONS]\n\n"
+        "  -V, --version                  print version of ./cynara and exit\n"
+        "  -h, --help                     print this help message and exit\n");
+
+} // namespace
+
+namespace Parser = Cynara::CmdlineParser;
+
+/**
+ * @brief   Verify if passing "help" option to commandline handler returns help message
+ * @test    Expected result:
+ * - call handler indicates success
+ * - help message in output stream
+ * - empty error stream
+ */
+TEST_F(CynaraCommandlineTest, help) {
+    std::string err;
+    std::string out;
+
+    for (const auto &opt : { "-h", "--help" }) {
+        clearOutput();
+        prepare_argv({ execName, opt });
+
+        SCOPED_TRACE(opt);
+        const auto handlingSuccess = Parser::handleCmdlineOptions(this->argc(), this->argv());
+        getOutput(out, err);
+
+        ASSERT_TRUE(handlingSuccess);
+        ASSERT_EQ(helpMessage, out);
+        ASSERT_TRUE(err.empty());
+    }
+}
+
+/**
+ * @brief   Verify if passing "version" option to commandline handler returns version message
+ * @test    Expected result:
+ * - call handler indicates success
+ * - version message in output stream
+ * - empty error stream
+ */
+TEST_F(CynaraCommandlineTest, version) {
+    std::string err;
+    std::string out;
+
+    for (const auto &opt : { "-V", "--version" }) {
+        clearOutput();
+        prepare_argv({ execName, opt });
+
+        SCOPED_TRACE(opt);
+        const auto handlingSuccess = Parser::handleCmdlineOptions(this->argc(), this->argv());
+        getOutput(out, err);
+
+        ASSERT_TRUE(handlingSuccess);
+        ASSERT_EQ(std::string(CYNARA_VERSION) + "\n", out);
+        ASSERT_TRUE(err.empty());
+    }
+}
+
+/**
+ * @brief   Verify if passing unknown option to commandline handler returns error message
+ * @test    Expected result:
+ * - call handler indicates failure
+ * - help message in output stream
+ * - error message in error stream
+ */
+TEST_F(CynaraCommandlineTest, unknownOption) {
+    std::string err;
+    std::string out;
+
+    for (const auto &badOpt : { "-b", "--badOption" }) {
+        clearOutput();
+        prepare_argv({ execName, badOpt });
+
+        SCOPED_TRACE(badOpt);
+        const auto handlingSuccess = Parser::handleCmdlineOptions(this->argc(), this->argv());
+        getOutput(out, err);
+
+        ASSERT_FALSE(handlingSuccess);
+        ASSERT_EQ(helpMessage, out);
+        ASSERT_EQ("Unknown option\n", err);
+    }
+}
+
+/**
+ * @brief   Verify if passing no options to commandline handler returns error message
+ * @test    Expected result:
+ * - call handler indicates failure
+ * - help message in output stream
+ * - error message in error stream
+ */
+TEST_F(CynaraCommandlineTest, noOption) {
+    std::string err;
+    std::string out;
+
+    clearOutput();
+    prepare_argv({ execName });
+
+    const auto handlingSuccess = Parser::handleCmdlineOptions(this->argc(), this->argv());
+    getOutput(out, err);
+
+    ASSERT_FALSE(handlingSuccess);
+    ASSERT_EQ(helpMessage, out);
+    ASSERT_EQ("No options given\n", err);
+}