CKM: Adjust to C++ API changes
[platform/core/test/security-tests.git] / src / ckm / ckm-common.cpp
index c50f2d6..5479eb6 100644 (file)
  * @author     Krzysztof Jackiewicz (k.jackiewicz@samsung.com)
  * @version    1.0
  */
+
+#include <unistd.h>
+#include <sys/types.h>
+#include <cstdlib>
 #include <string>
 #include <fstream>
 #include <sys/smack.h>
@@ -32,6 +36,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <unordered_map>
+#include <tzplatform_config.h>
 
 const std::string SMACK_USER_APP_PREFIX = "User::Pkg::";
 const char *SYSTEM_LABEL = ckmc_owner_id_system;
@@ -382,8 +387,9 @@ void check_alias_list(const CKM::AliasVector& expected)
     RUNNER_ASSERT_MSG(expected == actual, "Actual list of aliases differ from expected list.");
 }
 
-void check_alias_info_list_helper(const CKM::AliasPwdVector& expected, const CKM::AliasPwdVector& actual,
-        const std::string &userSmackLabel)
+void check_alias_info_list_helper(const CKM::AliasInfoVector& expected,
+                                  const CKM::AliasInfoVector& actual,
+                                  const std::string &userSmackLabel)
 {
     std::string errorLogMsg;
     std::unordered_map<std::string, bool> aliasPwdMap;
@@ -393,7 +399,7 @@ void check_alias_info_list_helper(const CKM::AliasPwdVector& expected, const CKM
 
     for (const auto &it : actual)
     {
-        aliasPwdMap[std::get<0>(it)] = std::get<1>(it);
+        aliasPwdMap[std::get<0>(it)] = std::get<1>(it).passwordProtected;
     }
 
 
@@ -401,9 +407,9 @@ void check_alias_info_list_helper(const CKM::AliasPwdVector& expected, const CKM
     {
         auto aliasPwd = aliasPwdMap.find(userSmackLabel + std::get<0>(it));
         if (aliasPwd != aliasPwdMap.end()) {
-            if (aliasPwd->second != std::get<1>(it)) {
+            if (aliasPwd->second != std::get<1>(it).passwordProtected) {
                 errorLogMsg += "Alias: " + std::get<0>(it) + " has wrong encryption status: "
-                                + std::to_string(std::get<1>(it)) + "\n";
+                                + std::to_string(std::get<1>(it).passwordProtected) + "\n";
             }
         }
         else {
@@ -415,20 +421,25 @@ void check_alias_info_list_helper(const CKM::AliasPwdVector& expected, const CKM
         for (const auto &it : actual)
         {
             errorLogMsg += "Actual alias: " + std::get<0>(it) + " status: "
-                + std::to_string(std::get<1>(it)) + "\n";
+                + std::to_string(std::get<1>(it).passwordProtected) + "\n";
         }
         RUNNER_FAIL_MSG("Actual list of aliases differ from expected list.\n" + errorLogMsg);
     }
 }
 
-void check_alias_info_list(const CKM::AliasPwdVector& expected)
+std::pair<std::string, CKM::AliasInfo> make_alias_info(const std::string& alias, bool password)
+{
+    return std::make_pair(alias, CKM::AliasInfo({password, CKM::BackendId::SW}));
+}
+
+void check_alias_info_list(const CKM::AliasInfoVector& expected)
 {
     ckmc_alias_info_list_s *aliasInfoList = NULL;
     int ret = ckmc_get_data_alias_info_list(&aliasInfoList);
     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get the list of data aliases. " << ret << " / "
                       << CKMCErrorToString(ret));
 
-    CKM::AliasPwdVector actual;
+    CKM::AliasInfoVector actual;
     ckmc_alias_info_list_s *plist = aliasInfoList;
     char* alias;
     bool isPasswordProtected;
@@ -442,7 +453,7 @@ void check_alias_info_list(const CKM::AliasPwdVector& expected)
         RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "Failed to get password protection status" << ret << " / "
                           << CKMCErrorToString(ret));
         RUNNER_ASSERT_MSG(alias != nullptr, "Got null alias. Iterator: " << it);
-        actual.push_back(std::make_pair(alias, isPasswordProtected));
+        actual.push_back(make_alias_info(alias, isPasswordProtected));
         plist = plist->next;
         it++;
     }
@@ -564,13 +575,28 @@ ParamListPtr createParamListPtr()
     return ParamListPtr(list, ckmc_param_list_free);
 }
 
-void assert_buffers_equal(const ckmc_raw_buffer_s b1, const ckmc_raw_buffer_s b2, bool equal)
+void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer)
+{
+    int ret = ckmc_param_list_set_buffer(params.get(), name, buffer);
+    RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
+                      "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
+}
+
+void setParam(ParamListPtr& params, ckmc_param_name_e name, uint64_t integer)
+{
+    int ret = ckmc_param_list_set_integer(params.get(), name, integer);
+    RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE,
+                      "Failed to set param " << name << " error: " << CKMCErrorToString(ret));
+}
+
+void assert_buffers_equal(const ckmc_raw_buffer_s* b1, const ckmc_raw_buffer_s* b2, bool equal)
 {
     if(equal) {
-        RUNNER_ASSERT_MSG(b1.size == b2.size, "Buffer size differs: " << b1.size << "!=" << b2.size);
-        RUNNER_ASSERT_MSG(0 == memcmp(b1.data, b2.data, b1.size), "Buffer contents differ");
+        RUNNER_ASSERT_MSG(b1->size == b2->size,
+                          "Buffer size differs: " << b1->size << "!=" << b2->size);
+        RUNNER_ASSERT_MSG(0 == memcmp(b1->data, b2->data, b1->size), "Buffer contents differ");
     } else {
-        RUNNER_ASSERT_MSG(b1.size != b2.size || 0 != memcmp(b1.data, b2.data, b1.size),
+        RUNNER_ASSERT_MSG(b1->size != b2->size || 0 != memcmp(b1->data, b2->data, b1->size),
                           "Buffers should be different");
     }
 }
@@ -586,3 +612,17 @@ CKM::Policy generate_ckm_policy(int iterator_nr) {
     }
     return CKM::Policy();
 }
+
+void require_default_user(char *argv[])
+{
+    uid_t expected_uid = tzplatform_getuid(TZ_SYS_DEFAULT_USER);
+    if (expected_uid != geteuid()) {
+        std::string userStr("owner");
+        const char* user = tzplatform_getenv(TZ_SYS_DEFAULT_USER);
+        if (user)
+            userStr = user;
+
+        std::cerr << argv[0] << " should be executed as " << userStr << ". Aborting" << std::endl;
+        exit(-1);
+    }
+}