Parcourir la source

[1960] If help or version printed do not start the test.

Marcin Siodelski il y a 12 ans
Parent
commit
b1b7f7957f

+ 11 - 7
tests/tools/perfdhcp/command_options.cc

@@ -85,7 +85,7 @@ CommandOptions::reset() {
     generateDuidTemplate();
 }
 
-void
+bool
 CommandOptions::parse(int argc, char** const argv) {
     // Reset internal variables used by getopt
     // to eliminate undefined behavior when
@@ -114,11 +114,15 @@ CommandOptions::parse(int argc, char** const argv) {
     // Reset values of class members
     reset();
 
-    initialize(argc, argv);
-    validate();
+    // Informs if program has been run with 'h' or 'v' option.
+    bool help_or_version_mode = initialize(argc, argv);
+    if (!help_or_version_mode) {
+        validate();
+    }
+    return (help_or_version_mode);
 }
 
-void
+bool
 CommandOptions::initialize(int argc, char** argv) {
     char opt = 0;               // Subsequent options returned by getopt()
     std::string drop_arg;       // Value of -D<value>argument
@@ -144,7 +148,7 @@ CommandOptions::initialize(int argc, char** argv) {
         switch (opt) {
         case 'v':
             version();
-            return;
+            return (true);
 
         case '1':
             use_first_ = true;
@@ -229,7 +233,7 @@ CommandOptions::initialize(int argc, char** argv) {
 
         case 'h':
             usage();
-            return;
+            return (true);
 
         case 'i':
             exchange_mode_ = DO_SA;
@@ -424,6 +428,7 @@ CommandOptions::initialize(int argc, char** argv) {
     if (duid_template_.size() == 0) {
         generateDuidTemplate();
     }
+    return (false);
 }
 
 void
@@ -863,7 +868,6 @@ CommandOptions::usage() const {
         "   * 'a': print the decoded command line arguments\n"
         "   * 'e': print the exit reason\n"
         "   * 'i': print rate processing details\n"
-        "   * 'r': print randomization details\n"
         "   * 's': print first server-id\n"
         "   * 't': when finished, print timers of all successful exchanges\n"
         "   * 'T': when finished, print templates\n"

+ 4 - 2
tests/tools/perfdhcp/command_options.h

@@ -57,7 +57,8 @@ public:
     /// \param argc Argument count passed to main().
     /// \param argv Argument value array passed to main().
     /// \throws isc::InvalidParameter if parse fails.
-    void parse(int argc, char** const argv);
+    /// \return true if program has been run in help or version mode ('h' or 'v' flag).
+    bool parse(int argc, char** const argv);
 
     /// \brief Returns IP version.
     ///
@@ -261,7 +262,8 @@ private:
     /// \param argc Argument count passed to main().
     /// \param argv Argument value array passed to main().
     /// \throws isc::InvalidParameter if command line options initialization fails.
-    void initialize(int argc, char** argv);
+    /// \return true if program has been run in help or version mode ('h' or 'v' flag).
+    bool initialize(int argc, char** argv);
 
     /// \brief Validates initialized options.
     ///

+ 6 - 1
tests/tools/perfdhcp/main.cc

@@ -29,7 +29,12 @@ main(int argc, char* argv[]) {
     std::string diags(command_options.getDiags());
     int ret_code = 0;
     try {
-        command_options.parse(argc, argv);
+        // If parser returns true it means that user specified
+        // 'h' or 'v' command line option. Program shows the
+        // help or version message and exits here.
+        if (command_options.parse(argc, argv)) {
+            return (ret_code);
+        }
     } catch(isc::Exception& e) {
         ret_code = 1;
         std::cout << "Error parsing command line options: "

+ 3 - 2
tests/tools/perfdhcp/tests/command_options_helper.h

@@ -87,13 +87,14 @@ public:
     /// parsing.
     ///
     /// \param cmdline command line provided as single string.
-    static void process(const std::string& cmdline) {
+    /// \return true if program has been run in help or version mode ('h' or 'v' flag).
+    static bool process(const std::string& cmdline) {
         CommandOptions& opt = CommandOptions::instance();
         int argc = 0;
         char** argv = tokenizeString(cmdline, argc);
         ArgvPtr args(argv, argc);
         opt.reset();
-        opt.parse(args.getArgc(), args.getArgv());
+        return (opt.parse(args.getArgc(), args.getArgv()));
     }
 
 private:

+ 19 - 50
tests/tools/perfdhcp/tests/command_options_unittest.cc

@@ -21,7 +21,7 @@
 #include <dhcp/iface_mgr.h>
 #include <exceptions/exceptions.h>
 
-#include "../command_options.h"
+#include "command_options_helper.h"
 
 using namespace std;
 using namespace isc;
@@ -45,19 +45,11 @@ protected:
     /// parses arguments using CommandOptions class to set
     /// its data members and de-allocates array of C-strings.
     ///
-    /// \param cmdline Command line to parse
-    /// \throws std::bad allocation if tokenization failed
-    void process(const std::string& cmdline) {
-        CommandOptions& opt = CommandOptions::instance();
-        int argc = 0;
-        char** argv = tokenizeString(cmdline, &argc);
-        opt.reset();
-        opt.parse(argc, argv);
-        for(int i = 0; i < argc; ++i) {
-            free(argv[i]);
-            argv[i] = NULL;
-        }
-        free(argv);
+    /// \param cmdline Command line to parse.
+    /// \throws std::bad allocation if tokenization failed.
+    /// \return true if program has been run in help or version mode ('h' or 'v' flag).
+    bool process(const std::string& cmdline) {
+        CommandOptionsHelper::process(cmdline);
     }
 
     /// \brief Check default initialized values
@@ -143,42 +135,6 @@ protected:
         EXPECT_EQ("", opt.getWrapped());
         EXPECT_EQ("192.168.0.1", opt.getServerName());
     }
-
-    /// \brief Split string to array of C-strings
-    ///
-    /// \param s String to split (tokenize)
-    /// \param num Number of tokens returned
-    /// \return array of C-strings (tokens)
-    char** tokenizeString(const std::string& text_to_split, int* num) const {
-        char** results = NULL;
-        // Tokenization with std streams
-        std::stringstream text_stream(text_to_split);
-        // Iterators to be used for tokenization
-        std::istream_iterator<std::string> text_iterator(text_stream);
-        std::istream_iterator<std::string> text_end;
-        // Tokenize string (space is a separator) using begin and end iteratos
-        std::vector<std::string> tokens(text_iterator, text_end);
-
-        if (tokens.size() > 0) {
-            // Allocate array of C-strings where we will store tokens
-            results = static_cast<char**>(malloc(tokens.size() * sizeof(char*)));
-            if (results == NULL) {
-                throw std::bad_alloc();
-            }
-            // Store tokens in C-strings array
-            for (int i = 0; i < tokens.size(); ++i) {
-                char* cs = static_cast<char*>(malloc(tokens[i].length() + 1));
-                strcpy(cs, tokens[i].c_str());
-                results[i] = cs;
-            }
-            // Return number of tokens to calling function
-            if (num != NULL) {
-                *num = tokens.size();
-            }
-        }
-        return results;
-    }
-
 };
 
 TEST_F(CommandOptionsTest, Defaults) {
@@ -186,6 +142,19 @@ TEST_F(CommandOptionsTest, Defaults) {
     checkDefaults();
 }
 
+TEST_F(CommandOptionsTest, HelpVersion) {
+    // The parser is supposed to return true if 'h' or 'v' options
+    // are specified.
+    EXPECT_TRUE(process("perfdhcp -h"));
+    EXPECT_TRUE(process("perfdhcp -v"));
+    EXPECT_TRUE(process("perfdhcp -h -v"));
+    EXPECT_TRUE(process("perfdhcp -6 -l ethx -h all"));
+    EXPECT_TRUE(process("perfdhcp -l ethx -v all"));
+    // No 'h' or 'v' option specified. The false value
+    // should be returned.
+    EXPECT_FALSE(process("perfdhcp -l ethx all"));
+}
+
 TEST_F(CommandOptionsTest, UseFirst) {
     CommandOptions& opt = CommandOptions::instance();
     process("perfdhcp -1 -B -l ethx all");