Separate ckm-test by the need of capability
[platform/core/test/security-tests.git] / src / ckm / ckm-common.cpp
index 47ac15d..a34b0d5 100644 (file)
 #include <ckmc/ckmc-type.h>
 #include <ckm-common.h>
 #include <tests_common.h>
-#include <access_provider2.h>
 #include <ckm/ckm-control.h>
 #include <ckm/ckm-manager.h>
 #include <ckmc/ckmc-control.h>
 #include <ckmc/ckmc-manager.h>
-#include <service_manager.h>
 #include <fcntl.h>
 #include <unistd.h>
 
 const std::string SMACK_USER_APP_PREFIX = "User::App::";
+const char *SYSTEM_LABEL = ckmc_owner_id_system;
+const char *TEST_LABEL = "test_label";
+const char *TEST_LABEL_2 = "test_label_2";
+const char *TEST_LABEL_3 = "test_label_3";
+const char *TEST_LABEL_4 = "test_label_4";
+const char *TEST_LABEL_5 = "test_label_5";
 
 void generate_random(size_t random_bytes, char *output)
 {
@@ -49,22 +53,6 @@ void generate_random(size_t random_bytes, char *output)
     }
 }
 
-const char* SERVICE[] = {
-        "central-key-manager-listener.service",
-        "central-key-manager.service" };
-
-void start_service(ServiceIdx idx)
-{
-    ServiceManager sm(SERVICE[idx]);
-    sm.startService();
-}
-
-void stop_service(ServiceIdx idx)
-{
-    ServiceManager sm(SERVICE[idx]);
-    sm.stopService();
-}
-
 std::string getLabel() {
     int ret;
     char* myLabel = NULL;
@@ -95,29 +83,16 @@ std::string aliasWithLabel(const char *label, const char *alias)
     return std::string(alias);
 }
 
-// changes process label
-void change_label(const char* label)
+std::string aliasWithLabelFromSelf(const char *alias)
 {
-    int ret = smack_set_label_for_self(label);
-    RUNNER_ASSERT_MSG(0 == ret, "Error in smack_set_label_for_self("<<label<<"). Error: " << ret);
-}
+    std::ostringstream oss;
+    oss << getOwnerIdFromSelf() << ckmc_label_name_separator << alias;
 
-ScopedLabel::ScopedLabel(const char* label) : m_original_label(getLabel())
-{
-    change_label(label);
+    return oss.str();
 }
 
-ScopedLabel::~ScopedLabel()
-{
-    /*
-     * Let it throw. If we can't restore label then remaining tests results will be
-     * unreliable anyway.
-     */
-    change_label(m_original_label.c_str());
-}
-
-const char * CKMCErrorToString(int error) {
 #define ERRORDESCRIBE(name) case name: return #name
+const char * CKMCErrorToString(int error) {
     switch(error) {
         ERRORDESCRIBE(CKMC_ERROR_NONE);
         ERRORDESCRIBE(CKMC_ERROR_INVALID_PARAMETER);
@@ -144,9 +119,38 @@ const char * CKMCErrorToString(int error) {
         ERRORDESCRIBE(CKMC_ERROR_UNKNOWN);
         default: return "Error not defined";
     }
-#undef ERRORDESCRIBE
 }
 
+const char * CKMErrorToString(int error) {
+    switch (error) {
+        ERRORDESCRIBE(CKM_API_SUCCESS);
+        ERRORDESCRIBE(CKM_API_ERROR_SOCKET);
+        ERRORDESCRIBE(CKM_API_ERROR_BAD_REQUEST);
+        ERRORDESCRIBE(CKM_API_ERROR_BAD_RESPONSE);
+        ERRORDESCRIBE(CKM_API_ERROR_SEND_FAILED);
+        ERRORDESCRIBE(CKM_API_ERROR_RECV_FAILED);
+        ERRORDESCRIBE(CKM_API_ERROR_AUTHENTICATION_FAILED);
+        ERRORDESCRIBE(CKM_API_ERROR_INPUT_PARAM);
+        ERRORDESCRIBE(CKM_API_ERROR_BUFFER_TOO_SMALL);
+        ERRORDESCRIBE(CKM_API_ERROR_OUT_OF_MEMORY);
+        ERRORDESCRIBE(CKM_API_ERROR_ACCESS_DENIED);
+        ERRORDESCRIBE(CKM_API_ERROR_SERVER_ERROR);
+        ERRORDESCRIBE(CKM_API_ERROR_DB_LOCKED);
+        ERRORDESCRIBE(CKM_API_ERROR_DB_ERROR);
+        ERRORDESCRIBE(CKM_API_ERROR_DB_ALIAS_EXISTS);
+        ERRORDESCRIBE(CKM_API_ERROR_DB_ALIAS_UNKNOWN);
+        ERRORDESCRIBE(CKM_API_ERROR_VERIFICATION_FAILED);
+        ERRORDESCRIBE(CKM_API_ERROR_INVALID_FORMAT);
+        ERRORDESCRIBE(CKM_API_ERROR_FILE_ACCESS_DENIED);
+        ERRORDESCRIBE(CKM_API_ERROR_NOT_EXPORTABLE);
+        ERRORDESCRIBE(CKM_API_ERROR_FILE_SYSTEM);
+        ERRORDESCRIBE(CKM_API_ERROR_NOT_SUPPORTED);
+        ERRORDESCRIBE(CKM_API_ERROR_UNKNOWN);
+        default: return "Error not defined";
+    }
+}
+#undef ERRORDESCRIBE
+
 std::string CKMCReadableError(int error) {
     std::string output("Error: ");
     output += std::to_string(error);
@@ -359,7 +363,7 @@ void unlock_user_data(uid_t user_id, const char *passwd)
     int ret;
     auto control = CKM::Control::create();
     RUNNER_ASSERT_MSG(CKM_API_SUCCESS == (ret = control->unlockUserKey(user_id, passwd)),
-                      "Error=" << CKM::ErrorToString(ret));
+                      "Error=" << CKMErrorToString(ret));
 }
 
 void remove_user_data(uid_t user_id)