CKM: CC mode uses user label to read & write keys.
[platform/core/test/security-tests.git] / tests / ckm / cc-mode.cpp
index 1b8fd33..e164917 100644 (file)
@@ -31,8 +31,7 @@
 
 #include <dpl/test/test_runner.h>
 #include <tests_common.h>
-#include <dbus_access.h>
-
+#include <ckm-common.h>
 #include <ckm/ckm-manager.h>
 #include <ckm/ckm-control.h>
 
@@ -52,19 +51,12 @@ const size_t MAX_RETRIES = 50;
 const char* const ENABLED = "Enabled";
 const char* const ENFORCING = "Enforcing";
 const char* const DISABLED = "Disabled";
+const char* const READY = "Ready";
+const char* const UNSET = "Unset"; // Meaningless value for unset.
 
+const char* const USER_LABEL = "User";
 const char* const CKM_LOCK = "/var/run/key-manager.pid";
-const char* SERVICE[] = {
-        "/org/freedesktop/systemd1/unit/central_2dkey_2dmanager_2dlistener_2eservice",
-        "/org/freedesktop/systemd1/unit/central_2dkey_2dmanager_2eservice" };
-
-enum ServiceIdx {
-    LISTENER,
-    MANAGER
-};
 
-// disable CC
-int _unset_mdpp_key = vconf_unset(VCONFKEY_SECURITY_MDPP_STATE);
 
 // Wrapper for mdpp state that restores the original value upon destruction
 class MdppState
@@ -82,38 +74,31 @@ private:
 
 MdppState::MdppState()
 {
+    ScopedLabel sl(USER_LABEL);
     m_original = vconf_get_str(VCONFKEY_SECURITY_MDPP_STATE);
 }
 
 MdppState::~MdppState()
 {
+    ScopedLabel sl(USER_LABEL);
     if (!m_original)
-        vconf_unset(VCONFKEY_SECURITY_MDPP_STATE);
+        vconf_set_str(VCONFKEY_SECURITY_MDPP_STATE, UNSET);
     else {
         vconf_set_str(VCONFKEY_SECURITY_MDPP_STATE, m_original);
-        free(m_original);
     }
 }
 
 void MdppState::set(const char* const value)
 {
+    ScopedLabel sl(USER_LABEL);
     if (value)
-        RUNNER_ASSERT_MSG(0 == vconf_set_str(VCONFKEY_SECURITY_MDPP_STATE, value),
-                             "vconf_set() failed");
+    {
+        int ret = vconf_set_str(VCONFKEY_SECURITY_MDPP_STATE, value);
+        RUNNER_ASSERT_MSG(0 == ret,
+                             "vconf_set() failed, ec: " << ret);
+    }
     else
-        vconf_unset(VCONFKEY_SECURITY_MDPP_STATE);
-}
-
-void start_service(ServiceIdx idx)
-{
-    DBusAccess dbus(SERVICE[idx]);
-    dbus.start();
-}
-
-void stop_service(ServiceIdx idx)
-{
-    DBusAccess dbus(SERVICE[idx]);
-    dbus.stop();
+        vconf_set_str(VCONFKEY_SECURITY_MDPP_STATE, UNSET);
 }
 
 void remove_user_data()
@@ -164,6 +149,10 @@ void read_key(ManagerShPtr& manager, const Alias& alias, int expected) {
 
 void read_keys(int expected)
 {
+// if mdpp is disabled at compilation time we expect that read_key always succeeds
+#ifndef DSECURITY_MDFPP_STATE_ENABLE
+    expected = CKM_API_SUCCESS;
+#endif
     auto manager = Manager::create();
 
     read_key(manager, rsa_pri_alias, expected);
@@ -218,6 +207,9 @@ RUNNER_TEST(TCC_0010_updateCCMode)
     mdpp.set(DISABLED);
     update_cc_mode();
 
+    mdpp.set(READY);
+    update_cc_mode();
+
     mdpp.set("whatever");
     update_cc_mode();
 }
@@ -237,7 +229,10 @@ RUNNER_TEST(TCC_0020_noListener)
 
     mdpp.set(DISABLED);
     update_cc_mode();
-    read_keys(CKM_API_SUCCESS);
+    // MJK, it's counter-intuitive: Disabled does not mean
+    // that CC mode is disabled, but that device
+    // self-test failed "device DISABLED"
+    read_keys(CKM_API_ERROR_BAD_REQUEST);
 
     mdpp.set("whatever");
     update_cc_mode();
@@ -251,9 +246,17 @@ RUNNER_TEST(TCC_0020_noListener)
     update_cc_mode();
     read_keys(CKM_API_ERROR_BAD_REQUEST);
 
-    mdpp.set(DISABLED);
+    mdpp.set(READY);
+    update_cc_mode();
+    read_keys(CKM_API_SUCCESS);
+
+    mdpp.set("whatever");
     update_cc_mode();
     read_keys(CKM_API_SUCCESS);
+
+    mdpp.set(DISABLED);
+    update_cc_mode();
+    read_keys(CKM_API_ERROR_BAD_REQUEST);
 }
 
 // when listener is started with mdpp key unset it should not update mdpp status in ckm
@@ -289,7 +292,7 @@ RUNNER_TEST(TCC_0040_callbackRegistered)
 
     remove_user_data();
     save_keys();
-    read_keys(CKM_API_SUCCESS);
+    read_keys(CKM_API_ERROR_BAD_REQUEST);
 
     mdpp.set("whatever");
     usleep(SLEEP); // give some time for notification to reach ckm
@@ -301,6 +304,10 @@ RUNNER_TEST(TCC_0040_callbackRegistered)
 
     mdpp.set(DISABLED);
     usleep(SLEEP); // give some time for notification to reach ckm
+    read_keys(CKM_API_ERROR_BAD_REQUEST);
+
+    mdpp.set(READY);
+    usleep(SLEEP); // give some time for notification to reach ckm
     read_keys(CKM_API_SUCCESS);
 
     mdpp.set(ENFORCING);
@@ -319,7 +326,7 @@ RUNNER_TEST(TCC_0050_manualCkmDisabled)
 
     remove_user_data();
     save_keys();
-    read_keys(CKM_API_SUCCESS);
+    read_keys(CKM_API_ERROR_BAD_REQUEST);
 }
 
 // run ckm manually and see if it properly loads mdpp setting
@@ -343,6 +350,16 @@ RUNNER_TEST(TCC_0070_manualCkmEnforcing)
 }
 
 // run ckm manually and see if it properly loads mdpp setting
+RUNNER_TEST(TCC_0075_manualCkmReady)
+{
+    restart_ckm(READY);
+
+    remove_user_data();
+    save_keys();
+    read_keys(CKM_API_SUCCESS);
+}
+
+// run ckm manually and see if it properly loads mdpp setting
 RUNNER_TEST(TCC_0080_manualCkmWhatever)
 {
     restart_ckm("whatever");