Fix options handling in ChecksumGenerator 59/233859/8
authorZofia Abramowska <z.abramowska@samsung.com>
Tue, 19 May 2020 15:05:10 +0000 (17:05 +0200)
committerZofia Abramowska <z.abramowska@samsung.com>
Wed, 8 Jul 2020 14:17:29 +0000 (16:17 +0200)
* do not output checksum when wrong algorithm option is passed
* do not fail on run() when help option is passed
* do not throw on constructor when wrong option is passed

Change-Id: Iaebd7eeb8b6dae0e3a6f7f51bce8554528973d88

src/chsgen/ChecksumGenerator.cpp
src/chsgen/ChecksumGenerator.h
src/chsgen/main.cpp
test/chsgen/checksumgenerator.cpp

index f1ffecce12cafd4d80aca6c691314a4c3a3eff0f..a01e36ec788aec5c793f8c9e4ab2a5e2109979e7 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2020 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.
 
 #include <algorithm>
 #include <cstring>
+#include <filesystem>
 #include <iostream>
 #include <memory>
 #include <new>
-#include <sstream>
 #include <stdexcept>
 #include <string>
+
 #include <unistd.h>
 
 #include <cynara-error.h>
@@ -51,19 +52,15 @@ const int CommandLineExtraParameter = 1;
 const std::string OPTION_MD5   = "md5";
 const std::string OPTION_CRYPT = "crypt";
 
-void printHelp(const char *exeName) {
-    static int done = 0;
-    if (!done) {
-        std::cout << "Usage: " << exeName << " FILE [OPTION]" << std::endl << std::endl <<
-               "Options:\n"
-               "\t-" << static_cast<char>(CommandLineOption::Algorithm) <<
-               " ALGORITHM   use selected algorith to count hash. Currently this "
-               "tool supports 'crypt' and 'md5'. 'crypt' is used as the default value " <<
-               "for backward compatibility." << std::endl <<
-               "\t-" << static_cast<char>(CommandLineOption::Help) <<
-               "             print this help" << std::endl;
-        done = 1;
-    }
+void printHelp(const std::string &exeName) {
+    std::cout << "Usage: " << exeName << " FILE [OPTION]" << std::endl << std::endl <<
+        "Options:\n"
+        "\t-" << static_cast<char>(CommandLineOption::Algorithm) <<
+        " ALGORITHM   use selected algorith to count hash. Currently this "
+        "tool supports 'crypt' and 'md5'. 'crypt' is used as the default value " <<
+        "for backward compatibility." << std::endl <<
+        "\t-" << static_cast<char>(CommandLineOption::Help) <<
+        "             print this help" << std::endl;
 }
 
 std::string generateCrypt(const std::string &data) {
@@ -94,14 +91,15 @@ const char ChecksumGenerator::m_fieldSeparator(';');
 const char ChecksumGenerator::m_recordSeparator('\n');
 const std::string ChecksumGenerator::m_backupFilenameSuffix("~");
 
-ChecksumGenerator::ChecksumGenerator(int argc, char * const *argv)
-    : m_algorithm(OPTION_CRYPT)
-{
+ChecksumGenerator::ChecksumGenerator()
+    : m_showHelp(false), m_exeName("cynara-db-chsgen"), m_algorithm(OPTION_CRYPT)
+{}
+
+void ChecksumGenerator::parseOptions(int argc, char * const *argv) {
     optind = 0;
 
-    const char *exeName = "cynara-db-chsgen";
-    if (argc > 0 && argv[0])
-        exeName = argv[0];
+    if (argc > 0)
+        m_exeName = std::filesystem::path(argv[0]).filename();
 
     int option;
     std::stringstream shortOptions;
@@ -120,14 +118,18 @@ ChecksumGenerator::ChecksumGenerator(int argc, char * const *argv)
             m_algorithm = optarg;
             break;
         case CommandLineOption::Help:
-            printHelp(exeName);
+            m_showHelp = true;
             break;
         }
     }
 }
-
-int ChecksumGenerator::run(void) {
+int ChecksumGenerator::run(int argc, char * const *argv) {
     try {
+        parseOptions(argc, argv);
+        if (m_showHelp) {
+            printHelp(m_exeName);
+            return CYNARA_API_SUCCESS;
+        }
         openFileStream();
         copyFileStream();
         printRecord();
@@ -170,11 +172,12 @@ void ChecksumGenerator::printRecord(void) const {
         throw std::bad_alloc();
     }
 
-    std::string basename(::basename(pathnameDuplicate.get()));
-    removeBackupSuffix(basename);
+    std::string filename = std::filesystem::path(m_pathname).filename();
+    removeBackupSuffix(filename);
 
-    std::cout << basename << m_fieldSeparator << generate(m_copyStream.str())
-              << m_recordSeparator;
+    auto checksum = generate(m_copyStream.str());
+    std::cout << filename << m_fieldSeparator << generate(m_copyStream.str())
+           << m_recordSeparator;
 }
 
 void ChecksumGenerator::removeBackupSuffix(std::string &filename) const {
index edc1558b1ec3c9fb89ce9d70b7a0a71d36d49fe0..962ad2e262e507ab8cab5c5a2dea47c83e4a333d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2020 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.
@@ -31,19 +31,21 @@ namespace Cynara {
 
 class ChecksumGenerator {
 public:
-    ChecksumGenerator(int argc, char * const *argv);
-    ~ChecksumGenerator() {}
+    ChecksumGenerator();
 
-    int run(void);
+    int run(int argc, char * const *argv);
 
 private:
     const std::string generate(const std::string &data) const;
 
+    void parseOptions(int argc, char * const *argv);
     void openFileStream(void);
     void copyFileStream(void);
     void printRecord(void) const;
     void removeBackupSuffix(std::string &filename) const;
 
+    bool m_showHelp;
+    std::string m_exeName;
     std::ifstream m_inputStream;
     std::stringstream m_copyStream;
     std::string m_pathname;
index d8104a6aff81021cec88dc15fd031330f54d7112..6e38d494d4674f0d8d213a17eb2e53c2b2616b50 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015-2016 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2015-2020 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.
@@ -30,8 +30,8 @@
 
 int main(int argc, char **argv) {
     try {
-        Cynara::ChecksumGenerator chsgen(argc, argv);
-        return chsgen.run();
+        Cynara::ChecksumGenerator chsgen;
+        return chsgen.run(argc, argv);
     } catch (const std::bad_alloc &) {
         std::cerr << "Chsgen could not allocate memory" << std::endl;
         return CYNARA_API_OUT_OF_MEMORY;
index 9ecfc2488df39a71843105a17c383e7251be7392..51ef48fca3febf9ae722d717b23932baec0f07e5 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2015-2020 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.
@@ -54,8 +54,8 @@ TEST_F(ChsgenCommandlineTest, noFile) {
     clearOutput();
     prepare_argv({ execName });
 
-    Cynara::ChecksumGenerator chsgen(this->argc(), this->argv());
-    const auto ret = chsgen.run();
+    Cynara::ChecksumGenerator chsgen;
+    const auto ret = chsgen.run(this->argc(), this->argv());
     getOutput(out, err);
 
     ASSERT_EQ(CYNARA_API_UNKNOWN_ERROR, ret);
@@ -81,8 +81,8 @@ TEST_F(ChsgenCommandlineTest, recordGeneration) {
         prepare_argv({ execName, Cynara::PathConfig::testsPath + "/db3/" + file });
         SCOPED_TRACE(file);
 
-        Cynara::ChecksumGenerator chsgen(this->argc(), this->argv());
-        const auto ret = chsgen.run();
+        Cynara::ChecksumGenerator chsgen;
+        const auto ret = chsgen.run(this->argc(), this->argv());
         getOutput(out, err);
 
         ASSERT_EQ(CYNARA_API_SUCCESS, ret);
@@ -110,8 +110,8 @@ TEST_F(ChsgenCommandlineTest, suffixErasing) {
                        Cynara::PathConfig::testsPath + "/db6/" + file + backupFilenameSuffix });
         SCOPED_TRACE(file);
 
-        Cynara::ChecksumGenerator chsgen(this->argc(), this->argv());
-        const auto ret = chsgen.run();
+        Cynara::ChecksumGenerator chsgen;
+        const auto ret = chsgen.run(this->argc(), this->argv());
         getOutput(out, err);
 
         ASSERT_EQ(CYNARA_API_SUCCESS, ret);