Browse Source

Add securechannel tests file

Mark 7 years ago
parent
commit
b9f4ea92cf
3 changed files with 121 additions and 9 deletions
  1. 16 9
      src/ua_securechannel.c
  2. 4 0
      tests/CMakeLists.txt
  3. 101 0
      tests/check_securechannel.c

+ 16 - 9
src/ua_securechannel.c

@@ -48,14 +48,17 @@ UA_StatusCode
 UA_SecureChannel_init(UA_SecureChannel *channel,
                       const UA_SecurityPolicy *securityPolicy,
                       const UA_ByteString *remoteCertificate) {
-    UA_StatusCode retval = UA_STATUSCODE_GOOD;
+
+    if(channel == NULL || securityPolicy == NULL || remoteCertificate == NULL) {
+        return UA_STATUSCODE_BADINTERNALERROR;
+    }
 
     memset(channel, 0, sizeof(UA_SecureChannel));
     channel->state = UA_SECURECHANNELSTATE_FRESH;
     channel->securityPolicy = securityPolicy;
 
-    retval = securityPolicy->channelModule.newContext(securityPolicy, remoteCertificate,
-                                                      &channel->channelContext);
+    UA_StatusCode retval = securityPolicy->channelModule.newContext(securityPolicy, remoteCertificate,
+                                                                    &channel->channelContext);
     if(retval != UA_STATUSCODE_GOOD)
         return retval;
 
@@ -76,6 +79,10 @@ UA_SecureChannel_init(UA_SecureChannel *channel,
 
 void
 UA_SecureChannel_deleteMembersCleanup(UA_SecureChannel *channel) {
+
+    if(channel == NULL)
+        return;
+
     /* Delete members */
     UA_ByteString_deleteMembers(&channel->remoteCertificate);
     UA_ByteString_deleteMembers(&channel->localNonce);
@@ -270,8 +277,8 @@ hideBytesAsym(UA_SecureChannel *const channel, UA_Byte **const buf_start,
 
         /* Add some overhead length due to RSA implementations adding a signature themselves */
         *buf_end -= securityPolicy->channelModule
-            .getRemoteAsymEncryptionBufferLengthOverhead(channel->channelContext,
-                                                         potentialEncryptionMaxSize);
+                                  .getRemoteAsymEncryptionBufferLengthOverhead(channel->channelContext,
+                                                                               potentialEncryptionMaxSize);
     }
 }
 
@@ -393,7 +400,7 @@ UA_SecureChannel_sendAsymmetricOPNMessage(UA_SecureChannel *channel, UA_UInt32 r
             connection->releaseSendBuffer(connection, &buf);
             return retval;
         }
-
+        
         /* Specification part 6, 6.7.4: The OpenSecureChannel Messages are
          * signed and encrypted if the SecurityMode is not None (even if the
          * SecurityMode is SignOnly). */
@@ -629,7 +636,7 @@ UA_SecureChannel_sendSymmetricMessage(UA_SecureChannel *channel, UA_UInt32 reque
 
     /* Encode with the chunking callback */
     retval |= UA_encodeBinary(content, contentType, &buf_start, &buf_end,
-                              (UA_exchangeEncodeBuffer)sendChunkSymmetric, &ci);
+                              (UA_exchangeEncodeBuffer) sendChunkSymmetric, &ci);
 
     /* TODO: Error handling. Send out an abort chunk if this is not the first chunk.
      * If this is the first chunk of the message:
@@ -764,7 +771,7 @@ decryptChunk(UA_SecureChannel *channel, const UA_SecurityPolicyCryptoModule *cry
        messageType == UA_MESSAGETYPE_OPN) {
         /* Compute the padding size */
         sigsize = cryptoModule->
-            getRemoteSignatureSize(securityPolicy, channel->channelContext);
+                                  getRemoteSignatureSize(securityPolicy, channel->channelContext);
 
         if(channel->securityMode == UA_MESSAGESECURITYMODE_SIGNANDENCRYPT ||
            (messageType == UA_MESSAGETYPE_OPN &&
@@ -772,7 +779,7 @@ decryptChunk(UA_SecureChannel *channel, const UA_SecurityPolicyCryptoModule *cry
             paddingSize = chunk->data[chunkSizeAfterDecryption - sigsize - 1];
 
             size_t keyLength = cryptoModule->
-                getRemoteEncryptionKeyLength(securityPolicy, channel->channelContext);
+                                               getRemoteEncryptionKeyLength(securityPolicy, channel->channelContext);
             if(keyLength > 2048) {
                 paddingSize <<= 8; /* Extra padding size */
                 paddingSize += chunk->data[chunkSizeAfterDecryption - sigsize - 2];

+ 4 - 0
tests/CMakeLists.txt

@@ -103,6 +103,10 @@ add_executable(check_utils check_utils.c $<TARGET_OBJECTS:open62541-object> $<TA
 target_link_libraries(check_utils ${LIBS})
 add_test_valgrind(utils ${TESTS_BINARY_DIR}/check_utils)
 
+add_executable(check_securechannel check_securechannel.c $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-testplugins>)
+target_link_libraries(check_securechannel ${LIBS})
+add_test_valgrind(check_securechannel ${TESTS_BINARY_DIR}/check_securechannel)
+
 # Test Server
 
 add_executable(check_accesscontrol server/check_accesscontrol.c $<TARGET_OBJECTS:open62541-object> $<TARGET_OBJECTS:open62541-testplugins>)

+ 101 - 0
tests/check_securechannel.c

@@ -0,0 +1,101 @@
+/* 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/. */
+
+#include <stdio.h>
+#include <stdlib.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}
+
+UA_SecureChannel testChannel;
+UA_ByteString dummyCertificate = UA_BYTESTRING_STATIC("DUMMY CERTIFICATE DUMMY CERTIFICATE DUMMY CERTIFICATE");
+UA_SecurityPolicy dummyPolicy;
+
+/*
+static void
+setup(void) {
+    UA_SecureChannel_init(&testChannel, &dummyPolicy, &dummyCertificate);
+}
+
+static void
+teardown(void) {
+    UA_SecureChannel_deleteMembersCleanup(&testChannel);
+}*/
+
+static void
+setup_dummyPolicy(void) {
+    UA_SecurityPolicy_Dummy(&dummyPolicy);
+}
+
+static void
+teardown_dummyPolicy(void) {
+    UA_SecurityPolicy_Dummy_deleteMembers(&dummyPolicy);
+}
+
+START_TEST(SecureChannel_initAndDelete)
+    {
+        UA_SecurityPolicy_None(&dummyPolicy, dummyCertificate, UA_Log_Stdout);
+        UA_StatusCode retval;
+
+        UA_SecureChannel channel;
+        retval = UA_SecureChannel_init(&channel, &dummyPolicy, &dummyCertificate);
+
+        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");
+    }
+END_TEST
+
+START_TEST(SecureChannel_initAndDelete_invalidParameters)
+    {
+        UA_StatusCode retval = UA_SecureChannel_init(NULL, NULL, NULL);
+        ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected init to fail");
+
+        UA_SecureChannel channel;
+        retval = UA_SecureChannel_init(&channel, &dummyPolicy, NULL);
+        ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected init to fail");
+
+        retval = UA_SecureChannel_init(&channel, NULL, &dummyCertificate);
+        ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected init to fail");
+
+        retval = UA_SecureChannel_init(NULL, &dummyPolicy, &dummyCertificate);
+        ck_assert_msg(retval != UA_STATUSCODE_GOOD, "Expected init to fail");
+
+        UA_SecureChannel_deleteMembersCleanup(NULL);
+    }
+END_TEST
+
+/*
+START_TEST(SecureChannel_generateNewKeys)
+    {
+
+    }
+END_TEST*/
+
+static Suite *
+testSuite_SecureChannel(void) {
+    Suite *s = suite_create("SecureChannel");
+
+    TCase *tc_initAndDelete = tcase_create("Initialize and delete Securechannel");
+    tcase_add_test(tc_initAndDelete, SecureChannel_initAndDelete);
+    tcase_add_test(tc_initAndDelete, SecureChannel_initAndDelete_invalidParameters);
+    suite_add_tcase(s, tc_initAndDelete);
+    return s;
+}
+
+int
+main(void) {
+    Suite *s = testSuite_SecureChannel();
+    SRunner *sr = srunner_create(s);
+    srunner_set_fork_status(sr, CK_NOFORK);
+    srunner_run_all(sr, CK_NORMAL);
+    int number_failed = srunner_ntests_failed(sr);
+    srunner_free(sr);
+    return (number_failed == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}