Refactor cynara_creds_get_[client|user] method 95/41595/4
authorRadoslaw Bartosiak <r.bartosiak@samsung.com>
Tue, 16 Jun 2015 12:10:16 +0000 (14:10 +0200)
committerRadoslaw Bartosiak <r.bartosiak@samsung.com>
Thu, 16 Jul 2015 05:55:04 +0000 (07:55 +0200)
Changes needed to successfully run UT when there is no configuration
file.

Change-Id: Ib4731e952e271ce65eeda58a4af81ee00ba51e6d
Signed-off-by: Radoslaw Bartosiak <r.bartosiak@samsung.com>
src/helpers/creds-commons/CredsCommonsInner.cpp
src/helpers/creds-commons/CredsCommonsInner.h
src/helpers/creds-commons/creds-commons.cpp
test/credsCommons/parser/CredsCommonsInner.h
test/credsCommons/parser/Parser.cpp

index e068709..51027f2 100644 (file)
@@ -16,7 +16,7 @@
  /**
  * @file        src/helpers/creds-commons/CredsCommonsInner.cpp
  * @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
- * @version     1.0
+ * @version     1.1
  * @brief       Implementation of internal credential commons functions
  */
 
@@ -24,6 +24,7 @@
 #include <cctype>
 #include <fstream>
 #include <functional>
+#include <fstream>
 #include <sstream>
 #include <string>
 
 
 namespace Cynara {
 
-std::string CredsCommonsInnerBackend::credsConfigurationFile(void) {
-    return Cynara::PathConfig::confPath + "/creds.conf";
+int CredsCommonsInnerBackend::credsConfigurationFile(std::ifstream &f) {
+    f.open(Cynara::PathConfig::confPath + "/creds.conf", std::fstream::in);
+    if (!f.is_open())
+        return CYNARA_API_CONFIGURATION_ERROR;
+    return CYNARA_API_SUCCESS;
 }
 // parses stream with configuration skipping comments (from # till the end of line)
 // untill a line of form <non empty key>=<empty or no empty value>
@@ -78,13 +82,10 @@ bool CredsCommonsInnerBackend::interpretValue(const CredentialsMap &methodCodeMa
     return true;
 }
 
-int CredsCommonsInnerBackend::getMethodFromConfigurationFile(const CredentialsMap &methodCodeMap,
+int CredsCommonsInnerBackend::getMethodFromConfigurationFile(std::istream &f,
+                                                             const CredentialsMap &methodCodeMap,
                                                              const std::string &methodName,
                                                              int &method) {
-    std::ifstream f(credsConfigurationFile());
-    if (!f.is_open())
-        return CYNARA_API_CONFIGURATION_ERROR;
-
     std::locale loc = f.getloc();
     bool occurred = false;
     std::string key, value;
index afa3b0b..b526ebc 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-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.
 /**
  * @file        src/helpers/creds-commons/CredsCommonsInner.h
  * @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
- * @version     1.0
+ * @version     1.1
  * @brief       Declaration of internal credential commons functions
  */
 
 #ifndef SRC_HELPERS_CREDS_COMMONS_CREDSCOMMONSINNER_H_
 #define SRC_HELPERS_CREDS_COMMONS_CREDSCOMMONSINNER_H_
 
+#include <istream>
+#include <fstream>
 #include <map>
 #include <string>
 
@@ -32,12 +34,13 @@ typedef std::map<std::string, int> CredentialsMap;
 
 class CredsCommonsInnerBackend {
     public:
-        static std::string credsConfigurationFile(void);
+        static int credsConfigurationFile(std::ifstream &f);
         static bool getKeyAndValue(std::istream &f, const std::locale &loc, std::string &key,
                                    std::string &value);
         static bool interpretValue(const CredentialsMap &methodCodeMap, int &method,
                                    const std::string &value, bool &occurred);
-        static int getMethodFromConfigurationFile(const CredentialsMap &methodCodeMap,
+        static int getMethodFromConfigurationFile(std::istream &f,
+                                                  const CredentialsMap &methodCodeMap,
                                                   const std::string &methodName, int &method);
     private:
         // trim from the start
index 30a24e1..478c968 100644 (file)
  * @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
  * @author      Aleksander Zdyb <a.zdyb@samsung.com>
  * @author      Jacek Bukarewicz <j.bukarewicz@samsung.com>
- * @version     1.0
+ * @version     1.1
  * @brief       Implementation of external libcynara-creds-commons API
  */
 
+#include <fstream>
+
 #include <attributes/attributes.h>
 #include <exceptions/TryCatch.h>
 
@@ -39,9 +41,13 @@ int cynara_creds_get_default_client_method(enum cynara_client_creds *method) {
 
     if (cachedMethodCode == -1) {
         int ret = Cynara::tryCatch([&] () {
-            return Cynara::CredsCommonsInnerBackend::
-                      getMethodFromConfigurationFile(clientCredsMap, "client_default",
-                                                     cachedMethodCode);
+            std::ifstream f;
+            int r = Cynara::CredsCommonsInnerBackend::credsConfigurationFile(f);
+            if (r != CYNARA_API_SUCCESS)
+                return r;
+
+            return Cynara::CredsCommonsInnerBackend::getMethodFromConfigurationFile(
+                     f, clientCredsMap, "client_default", cachedMethodCode);
         });
         if (ret != CYNARA_API_SUCCESS)
             return ret;
@@ -59,10 +65,12 @@ int cynara_creds_get_default_user_method(enum cynara_user_creds *method) {
 
     if (cachedMethodCode == -1) {
         int ret = Cynara::tryCatch([&] () {
-            return Cynara::CredsCommonsInnerBackend::
-                                       getMethodFromConfigurationFile(userCredsMap,
-                                                                      "user_default",
-                                                                      cachedMethodCode);
+            std::ifstream f;
+            int r = Cynara::CredsCommonsInnerBackend::credsConfigurationFile(f);
+            if (r != CYNARA_API_SUCCESS)
+                return r;
+            return Cynara::CredsCommonsInnerBackend::getMethodFromConfigurationFile(
+                     f, userCredsMap, "user_default", cachedMethodCode);
         });
         if (ret != CYNARA_API_SUCCESS)
             return ret;
index b7c8bf4..2f5e0be 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2014-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.
 /**
  * @file        test/credsCommons/parser/CredsCommonsInner.h
  * @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
- * @version     1.0
+ * @version     1.1
  * @brief       Declaration of internal credential commons functions for gmock tests
  */
 
 #ifndef TEST_CREDSCOMMONS_PARSER_CREDSCOMMONSINNER_H_
 #define TEST_CREDSCOMMONS_PARSER_CREDSCOMMONSINNER_H_
 
+#include <istream>
+#include <fstream>
 #include <map>
 #include <string>
 
@@ -32,13 +34,14 @@ typedef std::map<std::string, int> CredentialsMap;
 
 class CredsCommonsInnerBackend {
     public:
-        static std::string credsConfigurationFile(void);
+        static int credsConfigurationFile(std::ifstream &f);
         virtual ~CredsCommonsInnerBackend() {};
         virtual bool getKeyAndValue(std::istream &f, const std::locale &loc, std::string &key,
                                     std::string &value);
         virtual bool interpretValue(const CredentialsMap &methodCodeMap, int &method,
                                     const std::string &value, bool &occurred);
-        virtual int getMethodFromConfigurationFile(const CredentialsMap &methodCodeMap,
+        virtual int getMethodFromConfigurationFile(std::istream &f,
+                                                   const CredentialsMap &methodCodeMap,
                                                    const std::string &methodName, int &method);
     private:
         // trim from the start
index 7418dd6..b3697af 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
+ * Copyright (c) 2014-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.
@@ -16,7 +16,7 @@
 /**
  * @file        test/credsCommons/parser/Parser.cpp
  * @author      Radoslaw Bartosiak <r.bartosiak@samsung.com>
- * @version     1.0
+ * @version     1.1
  * @brief       Tests of internal implementation of credential commons functions
  */
 
@@ -25,6 +25,7 @@
 #include <gmock/gmock.h>
 #include <gtest/gtest.h>
 #include <iostream>
+#include <sstream>
 
 #include "FakeCredsCommonsInnerBackend.h"
 
@@ -221,9 +222,9 @@ TEST(parser, getMethodFromConfigurationFileOK2Lines) {
     EXPECT_CALL(fakeBackend, interpretValue(_, _,"smack", Eq(false)))
     .WillOnce(DoAll(SetArgReferee<1>(CLIENT_METHOD_SMACK), SetArgReferee<3>(true),
               Return(true)));
-
+    std::istringstream iss("unimportant as the configuration file content is mocked above");
     EXPECT_EQ(CYNARA_API_SUCCESS,
-              fakeBackend.getMethodFromConfigurationFile(clientCredsMap, "client_default",
+              fakeBackend.getMethodFromConfigurationFile(iss, clientCredsMap, "client_default",
                                                          method));
     EXPECT_EQ(CLIENT_METHOD_SMACK, method);
 }
@@ -240,9 +241,9 @@ TEST(parser, getMethodFromConfigurationFileOK3Lines) {
     EXPECT_CALL(fakeBackend, interpretValue(_, _,"smack", Eq(false)))
     .WillOnce(DoAll(SetArgReferee<1>(CLIENT_METHOD_SMACK), SetArgReferee<3>(true),
               Return(true)));
-
+    std::istringstream iss("unimportant as the configuration file content is mocked above");
     EXPECT_EQ(CYNARA_API_SUCCESS,
-              fakeBackend.getMethodFromConfigurationFile(clientCredsMap, "client_default",
+              fakeBackend.getMethodFromConfigurationFile(iss, clientCredsMap, "client_default",
                                                          method));
     EXPECT_EQ(CLIENT_METHOD_SMACK, method);
 }
@@ -259,8 +260,9 @@ TEST(parser, getMethodFromConfigurationFileBadNoEntry) {
     .WillOnce(DoAll(SetArgReferee<2>("client_default"), SetArgReferee<3>("someWrongValue"), Return(true)))
     .WillOnce(Return(false));
     //user default is not in the file (contrary to A,B, client_default)
+    std::istringstream iss("unimportant as the configuration file content is mocked above");
     EXPECT_EQ(CYNARA_API_CONFIGURATION_ERROR,
-              fakeBackend.getMethodFromConfigurationFile(clientCredsMap, "user_default",
+              fakeBackend.getMethodFromConfigurationFile(iss, clientCredsMap, "user_default",
                                                          method));
     EXPECT_EQ(NOT_A_METHOD_CODE, method);
 }
@@ -278,9 +280,9 @@ TEST(parser, getMethodFromConfigurationFileBadValueInEntry) {
     .WillOnce(DoAll(SetArgReferee<2>("client_default"), SetArgReferee<3>("someWrongValue"), Return(true)));
     EXPECT_CALL(fakeBackend, interpretValue(clientCredsMap, _, "someWrongValue", Eq(false)))
     .WillOnce(Return(false));
-
+    std::istringstream iss("unimportant as the configuration file content is mocked above");
     EXPECT_EQ(CYNARA_API_CONFIGURATION_ERROR,
-              fakeBackend.getMethodFromConfigurationFile(clientCredsMap, "client_default",
+              fakeBackend.getMethodFromConfigurationFile(iss, clientCredsMap, "client_default",
                                                          method));
     EXPECT_EQ(NOT_A_METHOD_CODE, method);
 }
@@ -301,9 +303,9 @@ TEST(parser, getMethodFromConfigurationFileBadTwoEntries) {
                           Return(true)));
     EXPECT_CALL(fakeBackend, interpretValue(clientCredsMap, _, "someWrongValue", Eq(true)))
     .WillOnce(Return(false));
-
+    std::istringstream iss("unimportant as the configuration file content is mocked above");
     EXPECT_EQ(CYNARA_API_CONFIGURATION_ERROR,
-              fakeBackend.getMethodFromConfigurationFile(clientCredsMap, "client_default",
+              fakeBackend.getMethodFromConfigurationFile(iss, clientCredsMap, "client_default",
                                                          method));
     EXPECT_EQ(NOT_A_METHOD_CODE, method);
 }
@@ -323,8 +325,9 @@ TEST(parser, getMethodFromConfigurationFileManyStrangeKeyAndValues) {
     .WillOnce(DoAll(SetArgReferee<2>(""), SetArgReferee<3>(""), Return(true)))
     .WillOnce(DoAll(SetArgReferee<2>("key5"), SetArgReferee<3>("value5"), Return(true)))
     .WillOnce(Return(false));
+    std::istringstream iss("unimportant as the configuration file content is mocked above");
     EXPECT_EQ(CYNARA_API_SUCCESS,
-              fakeBackend.getMethodFromConfigurationFile(clientCredsMap, "client_default",
+              fakeBackend.getMethodFromConfigurationFile(iss, clientCredsMap, "client_default",
                                                          method));
     EXPECT_EQ(CLIENT_METHOD_PID, method);
 }
@@ -341,9 +344,9 @@ TEST(parser, getMethodFromConfigurationFileFoundTheKeyAgain) {
                     Return(true)))
     .WillOnce(DoAll(SetArgReferee<2>("client_default"), SetArgReferee<3>("smack"),
                     Return(true)));
-
+    std::istringstream iss("unimportant as the configuration file content is mocked above");
     EXPECT_EQ(CYNARA_API_CONFIGURATION_ERROR,
-              fakeBackend.getMethodFromConfigurationFile(clientCredsMap, "client_default",
+              fakeBackend.getMethodFromConfigurationFile(iss, clientCredsMap, "client_default",
                                                          method));
     EXPECT_EQ(NOT_A_METHOD_CODE, method);
 }
@@ -361,9 +364,9 @@ TEST(parser, getMethodFromConfigurationFileTheKeyNotFound) {
     .WillOnce(DoAll(SetArgReferee<2>("other_default"), SetArgReferee<3>("smack"),
                     Return(true)))
     .WillOnce(Return(false));
-
+    std::istringstream iss("unimportant as the configuration file content is mocked above");
     EXPECT_EQ(CYNARA_API_CONFIGURATION_ERROR,
-              fakeBackend.getMethodFromConfigurationFile(clientCredsMap, "client_default",
+              fakeBackend.getMethodFromConfigurationFile(iss, clientCredsMap, "client_default",
                                                          method));
     EXPECT_EQ(NOT_A_METHOD_CODE, method);
 }
@@ -378,9 +381,22 @@ TEST(parser, getMethodFromConfigurationFileFoundTheKeyWithWrongValue) {
     .WillOnce(DoAll(SetArgReferee<2>("user_default"), SetArgReferee<3>("gid"), Return(true)))
     .WillOnce(DoAll(SetArgReferee<2>("client_default"), SetArgReferee<3>("noSmackNoPid"),
                     Return(true)));
-
+    std::istringstream iss("unimportant as the configuration file content is mocked above");
     EXPECT_EQ(CYNARA_API_CONFIGURATION_ERROR,
-              fakeBackend.getMethodFromConfigurationFile(clientCredsMap, "client_default",
+              fakeBackend.getMethodFromConfigurationFile(iss, clientCredsMap, "client_default",
                                                          method));
     EXPECT_EQ(NOT_A_METHOD_CODE, method);
 }
+
+
+TEST(parser, getMethodFromConfigurationFileWithoutMockupFileOK) {
+    static const CredentialsMap clientCredsMap{{"smack", CLIENT_METHOD_SMACK},
+                                               {"pid", CLIENT_METHOD_PID}};
+    int method = NOT_A_METHOD_CODE;
+    CredsCommonsInnerBackend backend;
+    std::istringstream iss("client_default=smack\nuser_default=uid");
+    EXPECT_EQ(CYNARA_API_SUCCESS,
+              backend.getMethodFromConfigurationFile(iss, clientCredsMap, "client_default",
+                                                     method));
+    EXPECT_EQ(CLIENT_METHOD_SMACK, method);
+}