Code refactoring:
authorBartlomiej Grzelewski <b.grzelewski@samsung.com>
Wed, 6 Aug 2014 13:26:23 +0000 (15:26 +0200)
committerBartlomiej Grzelewski <b.grzelewski@samsung.com>
Fri, 12 Sep 2014 12:59:27 +0000 (14:59 +0200)
* Replace "throw 1" with "Throw(Exception::BrokenProtocol)".
* Remove const in virtual method in Control class.
* Simplified ControlImpl implementation.

Change-Id: I1236585980a0748283e0415ab815fdbb7a16b88c

src/include/ckm/ckm-control.h
src/manager/client/client-control.cpp
src/manager/service/ckm-service.cpp
src/manager/service/ckm-service.h

index f812e24..828d8f1 100644 (file)
@@ -39,22 +39,22 @@ class Control
 {
 public:
     // decrypt user key with password
-    virtual int unlockUserKey(uid_t user, const Password &password) const = 0;
+    virtual int unlockUserKey(uid_t user, const Password &password) = 0;
 
     // remove user key from memory
-    virtual int lockUserKey(uid_t user) const = 0;
+    virtual int lockUserKey(uid_t user) = 0;
 
     // remove user data from Store and erase key used for encryption
-    virtual int removeUserData(uid_t user) const = 0;
+    virtual int removeUserData(uid_t user) = 0;
 
     // change password for user
-    virtual int changeUserPassword(uid_t user, const Password &oldPassword, const Password &newPassword) const = 0;
+    virtual int changeUserPassword(uid_t user, const Password &oldPassword, const Password &newPassword) = 0;
 
     // This is work around for security-server api - resetPassword that may be called without passing oldPassword.
     // This api should not be supported on tizen 3.0
     // User must be already logged in and his DKEK is already loaded into memory in plain text form.
     // The service will use DKEK in plain text and encrypt it in encrypted form (using new password).
-    virtual int resetUserPassword(uid_t user, const Password &newPassword) const = 0;
+    virtual int resetUserPassword(uid_t user, const Password &newPassword) = 0;
 
     virtual ~Control(){}
 
index 982a4c1..21ebdd6 100644 (file)
@@ -36,20 +36,18 @@ public:
     ControlImpl& operator=(const ControlImpl &) = delete;
     ControlImpl& operator=(ControlImpl &&) = delete;
 
-    virtual int unlockUserKey(uid_t user, const Password &password) const {
+    virtual int unlockUserKey(uid_t user, const Password &password) {
         return try_catch([&] {
+            if((int)user < 0) {
+                return CKM_API_ERROR_INPUT_PARAM;
+            }
+
             MessageBuffer send, recv;
             Serialization::Serialize(send, static_cast<int>(ControlCommand::UNLOCK_USER_KEY));
             Serialization::Serialize(send, user);
             Serialization::Serialize(send, password);
-            int retCode;
 
-            if((int)user < 0) {
-                retCode = CKM_API_ERROR_INPUT_PARAM;
-                return retCode;
-            }
-
-            retCode = sendToServer(
+            int retCode = sendToServer(
                 SERVICE_SOCKET_CKM_CONTROL,
                 send.Pop(),
                 recv);
@@ -64,19 +62,17 @@ public:
         });
     }
 
-    virtual int lockUserKey(uid_t user) const {
+    virtual int lockUserKey(uid_t user) {
         return try_catch([&] {
+            if((int)user < 0) {
+                return CKM_API_ERROR_INPUT_PARAM;
+            }
+
             MessageBuffer send, recv;
             Serialization::Serialize(send, static_cast<int>(ControlCommand::LOCK_USER_KEY));
             Serialization::Serialize(send, user);
-            int retCode;
 
-            if((int)user < 0) {
-                retCode = CKM_API_ERROR_INPUT_PARAM;
-                return retCode;
-            }
-
-            retCode = sendToServer(
+            int retCode = sendToServer(
                 SERVICE_SOCKET_CKM_CONTROL,
                 send.Pop(),
                 recv);
@@ -91,19 +87,17 @@ public:
         });
     }
 
-    virtual int removeUserData(uid_t user) const {
+    virtual int removeUserData(uid_t user) {
         return try_catch([&] {
+            if((int)user < 0) {
+                return CKM_API_ERROR_INPUT_PARAM;
+            }
+
             MessageBuffer send, recv;
             Serialization::Serialize(send, static_cast<int>(ControlCommand::REMOVE_USER_DATA));
             Serialization::Serialize(send, user);
-            int retCode;
 
-            if((int)user < 0) {
-                retCode = CKM_API_ERROR_INPUT_PARAM;
-                return retCode;
-            }
-
-            retCode = sendToServer(
+            int retCode = sendToServer(
                 SERVICE_SOCKET_CKM_CONTROL,
                 send.Pop(),
                 recv);
@@ -118,21 +112,19 @@ public:
         });
     }
 
-    virtual int changeUserPassword(uid_t user, const Password &oldPassword, const Password &newPassword) const {
+    virtual int changeUserPassword(uid_t user, const Password &oldPassword, const Password &newPassword) {
         return try_catch([&] {
+            if((int)user < 0) {
+                return CKM_API_ERROR_INPUT_PARAM;
+            }
+
             MessageBuffer send, recv;
             Serialization::Serialize(send, static_cast<int>(ControlCommand::CHANGE_USER_PASSWORD));
             Serialization::Serialize(send, user);
             Serialization::Serialize(send, oldPassword);
             Serialization::Serialize(send, newPassword);
-            int retCode;
-
-            if((int)user < 0) {
-                retCode = CKM_API_ERROR_INPUT_PARAM;
-                return retCode;
-            }
 
-            retCode = sendToServer(
+            int retCode = sendToServer(
                 SERVICE_SOCKET_CKM_CONTROL,
                 send.Pop(),
                 recv);
@@ -147,16 +139,23 @@ public:
         });
     }
 
-    virtual int resetUserPassword(uid_t user, const Password &newPassword) const {
+    virtual int resetUserPassword(uid_t user, const Password &newPassword) {
         return try_catch([&] {
+            if((int)user < 0) {
+                return CKM_API_ERROR_INPUT_PARAM;
+            }
+
             MessageBuffer send, recv;
             Serialization::Serialize(send, static_cast<int>(ControlCommand::RESET_USER_PASSWORD));
             Serialization::Serialize(send, user);
             Serialization::Serialize(send, newPassword);
-            int retCode;
 
-            if((int)user < 0) {
-                retCode = CKM_API_ERROR_INPUT_PARAM;
+            int retCode = sendToServer(
+                SERVICE_SOCKET_CKM_CONTROL,
+                send.Pop(),
+                recv);
+
+            if (CKM_API_SUCCESS != retCode) {
                 return retCode;
             }
 
index 8297cd2..020df35 100644 (file)
@@ -93,6 +93,8 @@ bool CKMService::processOne(
         return true;
     } Catch (MessageBuffer::Exception::Base) {
         LogError("Broken protocol. Closing socket.");
+    } Catch (Exception::BrokenProtocol) {
+        LogError("Broken protocol. Closing socket.");
     } catch (const std::string &e) {
         LogError("String exception(" << e << "). Closing socket");
     } catch (...) {
@@ -132,8 +134,7 @@ RawBuffer CKMService::processControl(MessageBuffer &buffer) {
         Deserialization::Deserialize(buffer, newPass);
         return m_logic->resetUserPassword(user, newPass);
     default:
-        // TODO
-        throw 1; // broken protocol
+        Throw(Exception::BrokenProtocol);
     }
 }
 
@@ -310,8 +311,7 @@ RawBuffer CKMService::processStorage(Credentials &cred, MessageBuffer &buffer){
                 static_cast<const RSAPaddingAlgorithm>(padding));
         }
         default:
-        // TODO
-            throw 1; // broken protocol
+            Throw(Exception::BrokenProtocol);
     }
 }
 
index f305a05..053749c 100644 (file)
@@ -25,6 +25,7 @@
 #include <generic-socket-manager.h>
 #include <connection-info.h>
 #include <message-buffer.h>
+#include <dpl/exception.h>
 
 namespace CKM {
 
@@ -54,6 +55,12 @@ public:
     void process(const ReadEvent &event);
     void close(const CloseEvent &event);
 private:
+    class Exception {
+    public:
+        DECLARE_EXCEPTION_TYPE(CKM::Exception, Base)
+        DECLARE_EXCEPTION_TYPE(Base, BrokenProtocol)
+    };
+
     bool processOne(
         const ConnectionID &conn,
         ConnectionInfo &info);