CKM: Helper functions refactored 33/288533/5
authorKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 17 Feb 2023 17:03:28 +0000 (18:03 +0100)
committerKrzysztof Jackiewicz <k.jackiewicz@samsung.com>
Fri, 3 Mar 2023 17:36:04 +0000 (18:36 +0100)
Functions moved to ckm-common.h to be accessible by other test cases.
Use unsigned integer in param lists.
Use pointers to buffers in buffer comparison.

Change-Id: I6d094cc4fc202be2a047861548f157775fe17a60

src/ckm/ckm-common.cpp
src/ckm/ckm-common.h
src/ckm/unprivileged/algo-params.cpp
src/ckm/unprivileged/encryption-decryption.cpp

index c50f2d6..59c9fc8 100644 (file)
@@ -564,13 +564,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");
     }
 }
index a61ffcf..516de56 100644 (file)
@@ -198,8 +198,10 @@ typedef std::shared_ptr<ckmc_raw_buffer_s> RawBufferPtr;
 typedef std::shared_ptr<struct __ckmc_param_list> ParamListPtr;
 
 ParamListPtr createParamListPtr();
+void setParam(ParamListPtr& params, ckmc_param_name_e name, ckmc_raw_buffer_s* buffer);
+void setParam(ParamListPtr& params, ckmc_param_name_e name, uint64_t integer);
 
-void assert_buffers_equal(const ckmc_raw_buffer_s b1, const ckmc_raw_buffer_s b2, bool equal=true);
+void assert_buffers_equal(const ckmc_raw_buffer_s* b1, const ckmc_raw_buffer_s* b2, bool equal=true);
 
 RawBufferPtr create_raw_buffer(ckmc_raw_buffer_s* buffer);
 
index 77919e0..cc4b004 100644 (file)
@@ -71,7 +71,7 @@ void check_buffer_param(ckmc_param_list_h list,
     ckmc_raw_buffer_s* got = NULL;
     int ret = ckmc_param_list_get_buffer(list, name, &got);
     RUNNER_ASSERT_MSG(ret == CKMC_ERROR_NONE, "No such buffer param: " << name);
-    assert_buffers_equal(expected, *got);
+    assert_buffers_equal(&expected, got);
 }
 
 void assert_param_count(ckmc_param_list_h list, size_t expected)
index 9030803..a29c452 100644 (file)
@@ -288,21 +288,6 @@ private:
     PolicyBackend m_backend;
 };
 
-
-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, int 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));
-}
-
 struct EncryptionResult
 {
     RawBufferPtr encrypted;
@@ -491,7 +476,7 @@ void encryptionWithCustomData(const Algo& algo, ckmc_param_name_e name)
     RawBufferPtr tmpDec = create_raw_buffer(decrypted);
 
     // check
-    assert_buffers_equal(*PLAIN_DATA.get(), *tmpDec.get());
+    assert_buffers_equal(PLAIN_DATA.get(), tmpDec.get());
     tmpDec.reset();
     decrypted = nullptr;
 
@@ -547,7 +532,7 @@ void testGcmIvSize(ckmc_raw_buffer_s* iv,
                            &decryptedTmp);
     decrypted = create_raw_buffer(decryptedTmp);
 
-    assert_buffers_equal(*PLAIN_DATA.get(), *decrypted.get());
+    assert_buffers_equal(PLAIN_DATA.get(), decrypted.get());
 }
 
 void testIntegrity(const Algo& algo)
@@ -570,7 +555,7 @@ void testIntegrity(const Algo& algo)
                            &decrypted);
 
     RawBufferPtr tmp = create_raw_buffer(decrypted);
-    assert_buffers_equal(*PLAIN_DATA.get(), *decrypted, false);
+    assert_buffers_equal(PLAIN_DATA.get(), decrypted, false);
 }
 
 void testCtrEncryptionInvalidLength(const Algo& algo)
@@ -599,9 +584,7 @@ void testCtrEncryptionInvalidLength(const Algo& algo)
         encryptedTmp = nullptr;
     };
     // invalid counter size
-    setParam(params, CKMC_PARAM_ED_CTR_LEN, -1);
-    test();
-    setParam(params, CKMC_PARAM_ED_CTR_LEN, 0);
+    setParam(params, CKMC_PARAM_ED_CTR_LEN, 0ULL);
     test();
     setParam(params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
     test();
@@ -663,9 +646,7 @@ void testCtrDecryptionInvalidLength(const Algo& algo)
         decrypted = nullptr;
     };
     // invalid counter size
-    setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, -1);
-    test();
-    setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0);
+    setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 0ULL);
     test();
     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, CTR_DEFAULT_LEN+1);
     test();
@@ -689,7 +670,7 @@ void testCtrDecryptionValidLength(const Algo& algo)
                                &decrypted);
         ckmc_buffer_free(decrypted);
         RawBufferPtr tmp = create_raw_buffer(decrypted);
-        assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
+        assert_buffers_equal(PLAIN_DATA.get(), decrypted);
     };
     // invalid counter size
     setParam(ret.params, CKMC_PARAM_ED_CTR_LEN, 1);
@@ -856,7 +837,7 @@ void testEncryptDecryptBigData(const Algo& algo)
                     &decrypted);
     RawBufferPtr tmp = create_raw_buffer(decrypted);
 
-    assert_buffers_equal(*BIG_DATA.get(), *decrypted);
+    assert_buffers_equal(BIG_DATA.get(), decrypted);
 }
 
 void testEncryptDecryptDifferentKeys(const Algo& algo, bool success)
@@ -880,7 +861,7 @@ void testEncryptDecryptDifferentKeys(const Algo& algo, bool success)
                                &decrypted);
         RawBufferPtr tmp = create_raw_buffer(decrypted);
 
-        assert_buffers_equal(*PLAIN_DATA.get(), *decrypted, false);
+        assert_buffers_equal(PLAIN_DATA.get(), decrypted, false);
     } else {
         assert_crypto_result(EncryptionError::INVALID_PARAM,
                              apiDecrypt,
@@ -909,7 +890,7 @@ void testRsaLongestData(const Algo& algo, size_t dataSize)
                            &decrypted);
     RawBufferPtr tmp = create_raw_buffer(decrypted);
 
-    assert_buffers_equal(*plain.get(), *decrypted);
+    assert_buffers_equal(plain.get(), decrypted);
 }
 
 void testRsaDataTooLong(const Algo& algo, size_t dataSize)
@@ -1165,7 +1146,7 @@ RUNNER_TEST_MULTIPLE(TED_0300_encrypt_decrypt, SyncEnv, AsyncEnv)
                                &decrypted);
         RawBufferPtr tmp = create_raw_buffer(decrypted);
 
-        assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
+        assert_buffers_equal(PLAIN_DATA.get(), decrypted);
     });
 }
 
@@ -1196,7 +1177,7 @@ RUNNER_TEST_MULTIPLE(TED_0310_encrypt_decrypt_password, SyncEnv, AsyncEnv)
                                &decrypted);
         RawBufferPtr tmp = create_raw_buffer(decrypted); // guarantees deletion
 
-        assert_buffers_equal(*PLAIN_DATA.get(), *decrypted);
+        assert_buffers_equal(PLAIN_DATA.get(), decrypted);
     });
 }