DCM API tests refactoring 57/180957/2
authorDariusz Michaluk <d.michaluk@samsung.com>
Wed, 13 Jun 2018 13:13:00 +0000 (15:13 +0200)
committerDariusz Michaluk <d.michaluk@samsung.com>
Wed, 13 Jun 2018 13:38:08 +0000 (15:38 +0200)
Rename dcm_hw_api_test to dcm_api_test.
Remove RSA tests (not supported by device).
Fix warnings.

Change-Id: Id0143b229c3c371aadb7a30eef8d3bc209fdc430

packaging/device-certificate-manager.spec
tests/CMakeLists.txt
tests/api_test.cpp [new file with mode: 0644]
tests/example_capi.c
tests/hw_api_test.cpp [deleted file]

index 279445a99f7ec4be2eba8fed7d00195bb09a687e..01e5559aa1b664168b914490c468577067dee8ea 100644 (file)
@@ -120,5 +120,5 @@ fi
 %license LICENSE
 %{_bindir}/dcm_example_client
 %{_bindir}/dcm_example_capi
-%{_bindir}/dcm_hw_api_test
+%{_bindir}/dcm_api_test
 %{_bindir}/dcm_test_cert_rewriter
index a4fa1d67a5d9109ff75e1fcda3b8d45a39111be0..022e861f0b83880d3995dcea05ab66bc4e710468 100644 (file)
@@ -45,12 +45,12 @@ IF(Boost_UNIT_TEST_FRAMEWORK_FOUND)
                        ${MBEDCRYPTO_LIB}
                        ${MBEDX509_LIB})
 
-       add_executable(dcm_hw_api_test hw_api_test.cpp)
-       target_link_libraries(dcm_hw_api_test
+       add_executable(dcm_api_test api_test.cpp)
+       target_link_libraries(dcm_api_test
                        device-certificate-manager
                        ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES})
 
-       install(TARGETS dcm_test_cert_rewriter dcm_hw_api_test
+       install(TARGETS dcm_test_cert_rewriter dcm_api_test
                RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
 ELSE()
        message(STATUS "Boost::test library not found. Disabling unit test build")
diff --git a/tests/api_test.cpp b/tests/api_test.cpp
new file mode 100644 (file)
index 0000000..3b7a552
--- /dev/null
@@ -0,0 +1,168 @@
+#define BOOST_TEST_MODULE HW API test
+
+#include "device_certificate_manager.h"
+#include <iostream>
+#include <iomanip>
+#include <cstring>
+
+#include <boost/algorithm/hex.hpp>
+#include <boost/test/unit_test.hpp>
+
+BOOST_AUTO_TEST_CASE(test01_dcm_create_key_context)
+{
+    void *ctx;
+    int ret = dcm_create_key_context(NULL, NULL, NULL, &ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(ctx);
+    ret = dcm_free_key_context(ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+
+    ctx = NULL;
+    ret = dcm_create_key_context("", "", NULL, &ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(ctx);
+    ret = dcm_free_key_context(ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+
+    ctx = NULL;
+    ret = dcm_create_key_context("a", NULL, NULL, &ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(ctx);
+    ret = dcm_free_key_context(ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+
+    ctx = NULL;
+    ret = dcm_create_key_context(NULL, "a", NULL, &ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(ctx);
+    ret = dcm_free_key_context(ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+
+    ctx = NULL;
+    ret = dcm_create_key_context("a", "a", NULL, &ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(ctx);
+    ret = dcm_free_key_context(ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+
+    ctx = NULL;
+    ret = dcm_create_key_context("a", "b", "UNEXISTING", &ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_SOCKET);
+    BOOST_CHECK(!ctx);
+    ret = dcm_free_key_context(ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+}
+
+BOOST_AUTO_TEST_CASE(test02_dcm_get_certificate_chain)
+{
+    void *ctx;
+    char *cert;
+    size_t cert_len;
+
+    int ret = dcm_create_key_context("a", "b", NULL, &ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(ctx);
+
+    ret = dcm_get_certificate_chain(ctx, NULL, &cert_len);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
+
+    ret = dcm_get_certificate_chain(ctx, &cert, NULL);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
+
+    ret = dcm_get_certificate_chain(NULL, &cert, &cert_len);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
+
+    ret = dcm_get_certificate_chain(ctx, &cert, &cert_len);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(cert);
+    BOOST_REQUIRE(cert_len > 0);
+
+    ret = dcm_free_key_context(ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+
+    free(cert);
+}
+
+BOOST_AUTO_TEST_CASE(test03_dcm_get_key_bit_length)
+{
+    int ret;
+    void *ctx;
+    size_t key_len = 0;
+
+    ret = dcm_get_key_bit_length(NULL, &key_len);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
+
+    ret = dcm_create_key_context("a", "b", "ECDSA", &ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(ctx);
+
+    ret = dcm_get_key_bit_length(ctx, &key_len);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_REQUIRE(key_len > 0);
+
+    ret = dcm_free_key_context(ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+}
+
+BOOST_AUTO_TEST_CASE(test04_dcm_get_key_type)
+{
+    int ret;
+    void *ctx;
+    char *key_type;
+
+    ret = dcm_get_key_type(NULL, &key_type);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
+
+    ret = dcm_create_key_context("a", "b", "ECDSA", &ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(ctx);
+
+    ret = dcm_get_key_type(ctx, &key_type);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(key_type);
+    BOOST_CHECK(strcmp(key_type, "ECDSA") == 0);
+
+    ret = dcm_free_key_context(ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    free(key_type);
+}
+
+BOOST_AUTO_TEST_CASE(test05_dcm_create_signature)
+{
+    int ret;
+    void *ctx;
+    char *signature;
+    size_t signature_len;
+    char data[32];
+
+    ret = dcm_create_signature(NULL, DCM_DIGEST_SHA256, data, sizeof(data), &signature, &signature_len);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
+
+    ret = dcm_create_key_context("a", "b", "ECDSA", &ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(ctx);
+
+    ret = dcm_create_signature(ctx, DCM_DIGEST_SHA256, data, sizeof(data), NULL, &signature_len);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
+
+    ret = dcm_create_signature(ctx, DCM_DIGEST_SHA256, data, sizeof(data), &signature, NULL);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
+
+    std::cout << "Data to sign = ";
+    boost::algorithm::hex(std::begin(data), std::end(data), std::ostream_iterator<char>(std::cout));
+    std::cout << std::endl;
+
+    ret = dcm_create_signature(ctx, DCM_DIGEST_SHA256, data, sizeof(data), &signature, &signature_len);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+    BOOST_CHECK(signature);
+    BOOST_REQUIRE(signature_len > 0);
+
+    std::cout << "Signature = ";
+    boost::algorithm::hex(signature, signature + signature_len, std::ostream_iterator<char>(std::cout));
+    std::cout << std::endl;
+
+    ret = dcm_free_key_context(ctx);
+    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
+
+    free(signature);
+}
index 10a13178549513c10616080958c7d2a78bc8b94f..a38aa9721444d021e229dfbb2a806d78e1f065c4 100644 (file)
@@ -31,7 +31,7 @@ int main(int argc, char ** argv)
                printf("Can't get cert chain\n");
                goto exit;
        }
-       printf("Cert is %d bytes\n", cert_chain_len);
+       printf("Cert is %ld bytes\n", cert_chain_len);
        printf("Received cert %s\n", cert_chain);
 
        result = dcm_get_key_bit_length(key_ctx, &key_len);
@@ -39,7 +39,7 @@ int main(int argc, char ** argv)
                printf("Can't get key length\n");
                goto exit;
        }
-       printf("Private key is %d bits\n", key_len);
+       printf("Private key is %ld bits\n", key_len);
        printf("Private key is %s\n", key_type);
 
        result = dcm_create_signature(key_ctx, DCM_DIGEST_SHA256, "12345678901234567890123456789012", 32, &signature, &signature_len);
diff --git a/tests/hw_api_test.cpp b/tests/hw_api_test.cpp
deleted file mode 100644 (file)
index 2be7bd4..0000000
+++ /dev/null
@@ -1,201 +0,0 @@
-#define BOOST_TEST_MODULE HW API test
-
-#include "device_certificate_manager.h"
-#include <iostream>
-#include <iomanip>
-#include <cstring>
-
-#include <boost/algorithm/hex.hpp>
-#include <boost/test/unit_test.hpp>
-
-BOOST_AUTO_TEST_CASE(test01_dcm_create_key_context)
-{
-    void *ctx;
-    int ret = dcm_create_key_context(NULL, NULL, NULL, &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-
-    ctx = NULL;
-    ret = dcm_create_key_context("", "", NULL, &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-
-    ctx = NULL;
-    ret = dcm_create_key_context("a", NULL, NULL, &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-
-    ctx = NULL;
-    ret = dcm_create_key_context(NULL, "a", NULL, &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-
-    ctx = NULL;
-    ret = dcm_create_key_context("a", "a", NULL, &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-
-    ctx = NULL;
-    ret = dcm_create_key_context("a", "b", "UNEXISTING", &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_SOCKET);
-    BOOST_CHECK(!ctx);
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-}
-
-BOOST_AUTO_TEST_CASE(test02_dcm_get_certificate_chain)
-{
-    void *ctx;
-    char *cert;
-    size_t cert_len;
-
-    int ret = dcm_create_key_context("a", "b", NULL, &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-
-    ret = dcm_get_certificate_chain(ctx, NULL, &cert_len);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
-
-    ret = dcm_get_certificate_chain(ctx, &cert, NULL);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
-
-    ret = dcm_get_certificate_chain(NULL, &cert, &cert_len);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
-
-    ret = dcm_get_certificate_chain(ctx, &cert, &cert_len);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(cert);
-    BOOST_REQUIRE(cert_len > 0);
-
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-
-    free(cert);
-}
-
-BOOST_AUTO_TEST_CASE(test03_dcm_get_key_bit_length)
-{
-    int ret;
-    void *ctx;
-    size_t key_len = 0;
-
-    ret = dcm_get_key_bit_length(NULL, &key_len);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
-
-    ret = dcm_create_key_context("a", "b", "RSA", &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-
-    ret = dcm_get_key_bit_length(ctx, NULL);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
-
-    ret = dcm_get_key_bit_length(ctx, &key_len);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_REQUIRE(key_len > 0);
-
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-
-    ctx = NULL;
-    key_len = 0;
-    ret = dcm_create_key_context("a", "b", "ECDSA", &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-
-    ret = dcm_get_key_bit_length(ctx, &key_len);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_REQUIRE(key_len > 0);
-
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-}
-
-BOOST_AUTO_TEST_CASE(test04_dcm_get_key_type)
-{
-    int ret;
-    void *ctx;
-    char *key_type;
-
-    ret = dcm_get_key_type(NULL, &key_type);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
-
-    ret = dcm_create_key_context("a", "b", "RSA", &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-
-    ret = dcm_get_key_type(ctx, NULL);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
-
-    ret = dcm_get_key_type(ctx, &key_type);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(key_type);
-    BOOST_CHECK(strcmp(key_type, "RSA") == 0);
-
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    free(key_type);
-
-    ctx = NULL;
-    ret = dcm_create_key_context("a", "b", "ECDSA", &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-
-    ret = dcm_get_key_type(ctx, &key_type);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(key_type);
-    BOOST_CHECK(strcmp(key_type, "ECDSA") == 0);
-
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    free(key_type);
-}
-
-BOOST_AUTO_TEST_CASE(test05_dcm_create_signature)
-{
-    int ret;
-    void *ctx;
-    char *signature;
-    size_t signature_len;
-    char data[32];
-
-    ret = dcm_create_signature(NULL, DCM_DIGEST_SHA256, data, sizeof(data), &signature, &signature_len);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
-
-    ret = dcm_create_key_context("a", "b", "RSA", &ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(ctx);
-
-    ret = dcm_create_signature(ctx, DCM_DIGEST_SHA256, data, sizeof(data), NULL, &signature_len);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
-
-    ret = dcm_create_signature(ctx, DCM_DIGEST_SHA256, data, sizeof(data), &signature, NULL);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_INVALID_PARAMETER);
-
-    std::cout << "Data to sign = ";
-    boost::algorithm::hex(std::begin(data), std::end(data), std::ostream_iterator<char>(std::cout));
-    std::cout << std::endl;
-
-    ret = dcm_create_signature(ctx, DCM_DIGEST_SHA256, data, sizeof(data), &signature, &signature_len);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-    BOOST_CHECK(signature);
-    BOOST_REQUIRE(signature_len > 0);
-
-    std::cout << "Signature = ";
-    boost::algorithm::hex(signature, signature + signature_len, std::ostream_iterator<char>(std::cout));
-    std::cout << std::endl;
-
-    ret = dcm_free_key_context(ctx);
-    BOOST_REQUIRE_EQUAL(ret, DCM_ERROR_NONE);
-
-    free(signature);
-}