Browse Source

-Added testing SecurityPolicy
-Added generateNewKeys test

Mark 7 years ago
parent
commit
e7f80f9779

+ 2 - 1
tests/CMakeLists.txt

@@ -39,7 +39,8 @@ set(test_plugin_sources ${PROJECT_SOURCE_DIR}/plugins/ua_network_tcp.c
                         ${PROJECT_SOURCE_DIR}/plugins/ua_accesscontrol_default.c
                         ${PROJECT_SOURCE_DIR}/plugins/ua_nodestore_default.c
                         ${PROJECT_SOURCE_DIR}/tests/testing-plugins/testing_clock.c
-                        ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicy_none.c)
+                        ${PROJECT_SOURCE_DIR}/plugins/ua_securitypolicy_none.c
+                        ${PROJECT_SOURCE_DIR}/tests/testing-plugins/testing_policy.c)
 
 add_library(open62541-testplugins OBJECT ${test_plugin_sources})
 add_dependencies(open62541-testplugins open62541)

+ 50 - 15
tests/check_securechannel.c

@@ -5,12 +5,10 @@
 #include <stdio.h>
 #include <stdlib.h>
 
+#include "testing_policy.h"
 #include "ua_securechannel.h"
-#include "ua_securitypolicy_none.h"
 
 #include "check.h"
-#include "ua_types_generated_handling.h"
-#include "ua_log_stdout.h"
 
 #define UA_BYTESTRING_STATIC(s) {sizeof(s)-1, (UA_Byte*)s}
 
@@ -18,30 +16,45 @@ UA_SecureChannel testChannel;
 UA_ByteString dummyCertificate = UA_BYTESTRING_STATIC("DUMMY CERTIFICATE DUMMY CERTIFICATE DUMMY CERTIFICATE");
 UA_SecurityPolicy dummyPolicy;
 
-/*
+
+funcs_called fCalled;
+
 static void
-setup(void) {
+setup_secureChannel(void) {
+    TestingPolicy(&dummyPolicy, dummyCertificate, &fCalled);
     UA_SecureChannel_init(&testChannel, &dummyPolicy, &dummyCertificate);
 }
 
 static void
-teardown(void) {
+teardown_secureChannel(void) {
     UA_SecureChannel_deleteMembersCleanup(&testChannel);
-}*/
+    dummyPolicy.deleteMembers(&dummyPolicy);
+}
+
+static void
+setup_funcs_called(void) {
+    memset(&fCalled, 0, sizeof(struct funcs_called));
+}
+
+static void
+teardown_funcs_called(void) {
+    memset(&fCalled, 0, sizeof(struct funcs_called));
+}
 
+/*
 static void
 setup_dummyPolicy(void) {
-    UA_SecurityPolicy_Dummy(&dummyPolicy);
+    TestingPolicy(&dummyPolicy, dummyCertificate, &fCalled);
 }
 
 static void
 teardown_dummyPolicy(void) {
-    UA_SecurityPolicy_Dummy_deleteMembers(&dummyPolicy);
-}
+    dummyPolicy.deleteMembers(&dummyPolicy);
+}*/
 
 START_TEST(SecureChannel_initAndDelete)
     {
-        UA_SecurityPolicy_None(&dummyPolicy, dummyCertificate, UA_Log_Stdout);
+        TestingPolicy(&dummyPolicy, dummyCertificate, &fCalled);
         UA_StatusCode retval;
 
         UA_SecureChannel channel;
@@ -49,6 +62,12 @@ START_TEST(SecureChannel_initAndDelete)
 
         ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected StatusCode to be good");
         ck_assert_msg(channel.state == UA_SECURECHANNELSTATE_FRESH, "Expected state to be fresh");
+        ck_assert_msg(fCalled.newContext, "Expected newContext to have been called");
+        ck_assert_msg(fCalled.makeCertificateThumbprint, "Expected makeCertificateThumbprint to have been called");
+        ck_assert_msg(channel.securityPolicy == &dummyPolicy, "SecurityPolicy not set correctly");
+
+        UA_SecureChannel_deleteMembersCleanup(&channel);
+        ck_assert_msg(fCalled.deleteContext, "Expected deleteContext to have been called");
     }
 END_TEST
 
@@ -71,21 +90,37 @@ START_TEST(SecureChannel_initAndDelete_invalidParameters)
     }
 END_TEST
 
-/*
+
 START_TEST(SecureChannel_generateNewKeys)
     {
-
+        UA_StatusCode retval = UA_SecureChannel_generateNewKeys(&testChannel);
+
+        ck_assert_msg(retval == UA_STATUSCODE_GOOD, "Expected Statuscode to be good");
+        ck_assert_msg(fCalled.generateKey, "Expected generateKey to have been called");
+        ck_assert_msg(fCalled.setLocalSymEncryptingKey, "Expected setLocalSymEncryptingKey to have been called");
+        ck_assert_msg(fCalled.setLocalSymSigningKey, "Expected setLocalSymSigningKey to have been called");
+        ck_assert_msg(fCalled.setLocalSymIv, "Expected setLocalSymIv to have been called");
+        ck_assert_msg(fCalled.setRemoteSymEncryptingKey, "Expected setRemoteSymEncryptingKey to have been called");
+        ck_assert_msg(fCalled.setRemoteSymSigningKey, "Expected setRemoteSymSigningKey to have been called");
+        ck_assert_msg(fCalled.setRemoteSymIv, "Expected setRemoteSymIv to have been called");
     }
-END_TEST*/
+END_TEST
 
 static Suite *
 testSuite_SecureChannel(void) {
     Suite *s = suite_create("SecureChannel");
 
     TCase *tc_initAndDelete = tcase_create("Initialize and delete Securechannel");
+    tcase_add_checked_fixture(tc_initAndDelete, setup_funcs_called, teardown_funcs_called);
     tcase_add_test(tc_initAndDelete, SecureChannel_initAndDelete);
     tcase_add_test(tc_initAndDelete, SecureChannel_initAndDelete_invalidParameters);
     suite_add_tcase(s, tc_initAndDelete);
+
+    TCase *tc_generateNewKeys = tcase_create("Test generateNewKeys function");
+    tcase_add_checked_fixture(tc_generateNewKeys, setup_funcs_called, teardown_funcs_called);
+    tcase_add_checked_fixture(tc_generateNewKeys, setup_secureChannel, teardown_secureChannel);
+    tcase_add_test(tc_generateNewKeys, SecureChannel_generateNewKeys);
+    suite_add_tcase(s, tc_generateNewKeys);
     return s;
 }
 
@@ -94,7 +129,7 @@ main(void) {
     Suite *s = testSuite_SecureChannel();
     SRunner *sr = srunner_create(s);
     srunner_set_fork_status(sr, CK_NOFORK);
-    srunner_run_all(sr, CK_NORMAL);
+    srunner_run_all(sr, CK_VERBOSE);
     int number_failed = srunner_ntests_failed(sr);
     srunner_free(sr);
     return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;

+ 232 - 0
tests/testing-plugins/testing_policy.c

@@ -0,0 +1,232 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
+ * See http://creativecommons.org/publicdomain/zero/1.0/ for more information. */
+
+#include <ua_types.h>
+#include "ua_securitypolicy_none.h"
+#include "ua_types_generated_handling.h"
+#include "testing_policy.h"
+#include "check.h"
+
+#define SET_CALLED(func) funcsCalled->func = true
+
+#define SYM_ENCRYPTION_BLOCK_SIZE 3
+#define SYM_SIGNING_KEY_LENGTH 5
+#define SYM_ENCRYPTION_KEY_LENGTH 7
+
+funcs_called *funcsCalled;
+
+static UA_StatusCode
+verify_testing(const UA_SecurityPolicy *securityPolicy,
+               const void *channelContext,
+               const UA_ByteString *message,
+               const UA_ByteString *signature) {
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+sign_testing(const UA_SecurityPolicy *securityPolicy,
+             const void *channelContext,
+             const UA_ByteString *message,
+             UA_ByteString *signature) {
+    return UA_STATUSCODE_GOOD;
+}
+
+static size_t
+length_testing(const UA_SecurityPolicy *securityPolicy,
+               const void *channelContext) {
+    return ENCRYPTION_KEY_LENGTH;
+}
+
+static UA_StatusCode
+encrypt_testing(const UA_SecurityPolicy *securityPolicy,
+                const void *channelContext,
+                UA_ByteString *data) {
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+decrypt_testing(const UA_SecurityPolicy *securityPolicy,
+                const void *channelContext,
+                UA_ByteString *data) {
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+makeThumbprint_testing(const UA_SecurityPolicy *securityPolicy,
+                       const UA_ByteString *certificate,
+                       UA_ByteString *thumbprint) {
+    SET_CALLED(makeCertificateThumbprint);
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+compareThumbprint_testing(const UA_SecurityPolicy *securityPolicy,
+                          const UA_ByteString *certificateThumbprint) {
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+generateKey_testing(const UA_SecurityPolicy *securityPolicy,
+                    const UA_ByteString *secret,
+                    const UA_ByteString *seed,
+                    UA_ByteString *out) {
+    SET_CALLED(generateKey);
+    ck_assert(securityPolicy);
+    ck_assert(secret);
+    ck_assert(seed);
+    ck_assert(out);
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+generateNonce_testing(const UA_SecurityPolicy *securityPolicy,
+                      UA_ByteString *out) {
+    out->data = UA_Byte_new();
+    if(!out->data)
+        return UA_STATUSCODE_BADOUTOFMEMORY;
+    out->length = 1;
+    out->data[0] = 'a';
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+newContext_testing(const UA_SecurityPolicy *securityPolicy,
+                   const UA_ByteString *remoteCertificate,
+                   void **channelContext) {
+    SET_CALLED(newContext);
+    ck_assert(securityPolicy);
+    ck_assert(remoteCertificate);
+    ck_assert(channelContext);
+
+    *channelContext = (void *) funcsCalled;
+    return UA_STATUSCODE_GOOD;
+}
+
+static void
+deleteContext_testing(void *channelContext) {
+    SET_CALLED(deleteContext);
+    ck_assert(channelContext);
+}
+
+static UA_StatusCode
+setLocalSymEncryptingKey_testing(void *channelContext,
+                                 const UA_ByteString *val) {
+    SET_CALLED(setLocalSymEncryptingKey);
+    ck_assert(channelContext);
+    ck_assert(val);
+    ck_assert(val->length == SYM_ENCRYPTION_KEY_LENGTH);
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+setLocalSymSigningKey_testing(void *channelContext,
+                              const UA_ByteString *val) {
+    SET_CALLED(setLocalSymSigningKey);
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+setLocalSymIv_testing(void *channelContext,
+                      const UA_ByteString *val) {
+    SET_CALLED(setLocalSymIv);
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+setRemoteSymEncryptingKey_testing(void *channelContext,
+                                  const UA_ByteString *val) {
+    SET_CALLED(setRemoteSymEncryptingKey);
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+setRemoteSymSigningKey_testing(void *channelContext,
+                               const UA_ByteString *val) {
+    SET_CALLED(setRemoteSymSigningKey);
+    return UA_STATUSCODE_GOOD;
+}
+
+static UA_StatusCode
+setRemoteSymIv_testing(void *channelContext,
+                       const UA_ByteString *val) {
+    SET_CALLED(setRemoteSymIv);
+    return UA_STATUSCODE_GOOD;
+}
+
+static size_t
+getRemoteAsymPlainTextBlockSize_testing(const void *channelContext) {
+    return 5;
+}
+
+static size_t
+getRemoteAsymEncryptionBufferLengthOverhead_testing(const void *channelContext,
+                                                    size_t maxEncryptionLength) {
+    return 0;
+}
+
+static UA_StatusCode
+compareCertificate_testing(const void *channelContext,
+                           const UA_ByteString *certificate) {
+    return UA_STATUSCODE_GOOD;
+}
+
+static void
+policy_deletemembers_testing(UA_SecurityPolicy *policy) {
+    UA_ByteString_deleteMembers(&policy->localCertificate);
+}
+
+UA_StatusCode
+TestingPolicy(UA_SecurityPolicy *policy, const UA_ByteString localCertificate,
+              funcs_called *fCalled) {
+    funcsCalled = fCalled;
+    policy->policyContext = (void *) funcsCalled;
+    policy->policyUri = UA_STRING("http://opcfoundation.org/UA/SecurityPolicy#Testing");
+    policy->logger = NULL;
+    UA_ByteString_copy(&localCertificate, &policy->localCertificate);
+
+    policy->asymmetricModule.makeCertificateThumbprint = makeThumbprint_testing;
+    policy->asymmetricModule.compareCertificateThumbprint = compareThumbprint_testing;
+    policy->asymmetricModule.cryptoModule.signatureAlgorithmUri = UA_STRING_NULL;
+    policy->asymmetricModule.cryptoModule.verify = verify_testing;
+    policy->asymmetricModule.cryptoModule.sign = sign_testing;
+    policy->asymmetricModule.cryptoModule.getLocalSignatureSize = length_testing;
+    policy->asymmetricModule.cryptoModule.getRemoteSignatureSize = length_testing;
+    policy->asymmetricModule.cryptoModule.encrypt = encrypt_testing;
+    policy->asymmetricModule.cryptoModule.decrypt = decrypt_testing;
+    policy->asymmetricModule.cryptoModule.getLocalEncryptionKeyLength = length_testing;
+    policy->asymmetricModule.cryptoModule.getRemoteEncryptionKeyLength = length_testing;
+
+    policy->symmetricModule.generateKey = generateKey_testing;
+    policy->symmetricModule.generateNonce = generateNonce_testing;
+    policy->symmetricModule.cryptoModule.signatureAlgorithmUri = UA_STRING_NULL;
+    policy->symmetricModule.cryptoModule.verify = verify_testing;
+    policy->symmetricModule.cryptoModule.sign = sign_testing;
+    policy->symmetricModule.cryptoModule.getLocalSignatureSize = length_testing;
+    policy->symmetricModule.cryptoModule.getRemoteSignatureSize = length_testing;
+    policy->symmetricModule.cryptoModule.encrypt = encrypt_testing;
+    policy->symmetricModule.cryptoModule.decrypt = decrypt_testing;
+    policy->symmetricModule.cryptoModule.getLocalEncryptionKeyLength = length_testing;
+    policy->symmetricModule.cryptoModule.getRemoteEncryptionKeyLength = length_testing;
+    policy->symmetricModule.encryptionBlockSize = SYM_ENCRYPTION_BLOCK_SIZE;
+    policy->symmetricModule.signingKeyLength = SYM_SIGNING_KEY_LENGTH;
+
+    policy->channelModule.newContext = newContext_testing;
+    policy->channelModule.deleteContext = deleteContext_testing;
+    policy->channelModule.setLocalSymEncryptingKey = setLocalSymEncryptingKey_testing;
+    policy->channelModule.setLocalSymSigningKey = setLocalSymSigningKey_testing;
+    policy->channelModule.setLocalSymIv = setLocalSymIv_testing;
+    policy->channelModule.setRemoteSymEncryptingKey = setRemoteSymEncryptingKey_testing;
+    policy->channelModule.setRemoteSymSigningKey = setRemoteSymSigningKey_testing;
+    policy->channelModule.setRemoteSymIv = setRemoteSymIv_testing;
+    policy->channelModule.compareCertificate = compareCertificate_testing;
+    policy->channelModule.getRemoteAsymPlainTextBlockSize = getRemoteAsymPlainTextBlockSize_testing;
+    policy->channelModule.getRemoteAsymEncryptionBufferLengthOverhead =
+        getRemoteAsymEncryptionBufferLengthOverhead_testing;
+    policy->deleteMembers = policy_deletemembers_testing;
+
+    return UA_STATUSCODE_GOOD;
+}

+ 50 - 0
tests/testing-plugins/testing_policy.h

@@ -0,0 +1,50 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef OPEN62541_TESTING_POLICY_H
+#define OPEN62541_TESTING_POLICY_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "ua_plugin_securitypolicy.h"
+#include "ua_plugin_log.h"
+
+typedef struct funcs_called {
+    bool asym_enc;
+    bool asym_dec;
+
+    bool sym_enc;
+    bool sym_dec;
+
+    bool asym_sign;
+    bool asym_verify;
+
+    bool sym_sign;
+    bool sym_verify;
+
+    bool newContext;
+    bool deleteContext;
+
+    bool makeCertificateThumbprint;
+    bool generateKey;
+
+    bool setLocalSymEncryptingKey;
+    bool setLocalSymSigningKey;
+    bool setLocalSymIv;
+    bool setRemoteSymEncryptingKey;
+    bool setRemoteSymSigningKey;
+    bool setRemoteSymIv;
+} funcs_called;
+
+UA_StatusCode UA_EXPORT
+TestingPolicy(UA_SecurityPolicy *policy, const UA_ByteString localCertificate,
+              funcs_called *fCalled);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif //OPEN62541_TESTING_POLICY_H