瀏覽代碼

use a fixed send buffer for the testing connection

Julius Pfrommer 6 年之前
父節點
當前提交
97c90048f7

+ 3 - 4
tests/check_securechannel.c

@@ -5,7 +5,6 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <src_generated/ua_types_generated.h>
-#include <testing_networklayers.h>
 #include <ua_types_encoding_binary.h>
 #include <src_generated/ua_transport_generated_encoding_binary.h>
 #include <src_generated/ua_transport_generated.h>
@@ -14,6 +13,7 @@
 #include <ua_plugin_securitypolicy.h>
 #include <src_generated/ua_transport_generated_handling.h>
 
+#include "testing_networklayers.h"
 #include "testing_policy.h"
 #include "ua_securechannel.h"
 
@@ -44,7 +44,7 @@ setup_secureChannel(void) {
     TestingPolicy(&dummyPolicy, dummyCertificate, &fCalled, &keySizes);
     UA_SecureChannel_init(&testChannel, &dummyPolicy, &dummyCertificate);
 
-    testingConnection = createDummyConnection(&sentData);
+    testingConnection = createDummyConnection(65535, &sentData);
     UA_Connection_attachSecureChannel(&testingConnection, &testChannel);
     testChannel.connection = &testingConnection;
 }
@@ -53,8 +53,7 @@ static void
 teardown_secureChannel(void) {
     UA_SecureChannel_deleteMembersCleanup(&testChannel);
     dummyPolicy.deleteMembers(&dummyPolicy);
-
-    memset(&testingConnection, 0, sizeof(UA_Connection));
+    testingConnection.close(&testingConnection);
 }
 
 static void

+ 1 - 2
tests/fuzz/fuzz_binary_message.cc

@@ -14,8 +14,7 @@
 */
 extern "C" int
 LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
-    UA_ByteString sentData = UA_BYTESTRING_NULL;
-    UA_Connection c = createDummyConnection(&sentData);
+    UA_Connection c = createDummyConnection(65535, NULL);
     UA_ServerConfig *config = UA_ServerConfig_new_default();
     UA_Server *server = UA_Server_new(config);
 

+ 16 - 10
tests/testing-plugins/testing_networklayers.c

@@ -8,20 +8,22 @@
 #include "testing_clock.h"
 #include "ua_config_default.h"
 
-UA_ByteString *vBuffer;
+static UA_ByteString *vBuffer;
+static UA_ByteString sendBuffer;
 
 UA_StatusCode UA_Client_recvTesting_result = UA_STATUSCODE_GOOD;
 
 static UA_StatusCode
 dummyGetSendBuffer(UA_Connection *connection, size_t length, UA_ByteString *buf) {
-    buf->data = (length == 0) ? NULL : (UA_Byte*)UA_malloc(length);
+    if(length > sendBuffer.length)
+        return UA_STATUSCODE_BADCOMMUNICATIONERROR;
+    *buf = sendBuffer;
     buf->length = length;
     return UA_STATUSCODE_GOOD;
 }
 
 static void
 dummyReleaseSendBuffer(UA_Connection *connection, UA_ByteString *buf) {
-    UA_ByteString_deleteMembers(buf);
 }
 
 static UA_StatusCode
@@ -29,9 +31,10 @@ dummySend(UA_Connection *connection, UA_ByteString *buf) {
     assert(connection != NULL);
     assert(buf != NULL);
 
-    UA_ByteString_deleteMembers(vBuffer);
-    UA_ByteString_copy(buf, vBuffer);
-    UA_ByteString_deleteMembers(buf);
+    if(vBuffer) {
+        UA_ByteString_deleteMembers(vBuffer);
+        UA_ByteString_copy(buf, vBuffer);
+    }
     return UA_STATUSCODE_GOOD;
 }
 
@@ -41,13 +44,16 @@ dummyReleaseRecvBuffer(UA_Connection *connection, UA_ByteString *buf) {
 
 static void
 dummyClose(UA_Connection *connection) {
-    UA_ByteString_deleteMembers(vBuffer);
+    if(vBuffer)
+        UA_ByteString_deleteMembers(vBuffer);
+    UA_ByteString_deleteMembers(&sendBuffer);
 }
 
-UA_Connection createDummyConnection(UA_ByteString *verificationBuffer) {
-    assert(verificationBuffer != NULL);
-
+UA_Connection createDummyConnection(size_t sendBufferSize,
+                                    UA_ByteString *verificationBuffer) {
     vBuffer = verificationBuffer;
+    UA_ByteString_allocBuffer(&sendBuffer, sendBufferSize);
+
     UA_Connection c;
     c.state = UA_CONNECTION_ESTABLISHED;
     c.localConf = UA_ConnectionConfig_default;

+ 8 - 5
tests/testing-plugins/testing_networklayers.h

@@ -11,12 +11,15 @@
 extern "C" {
 #endif
 
-/** @brief Create the TCP networklayer and listen to the specified port
+/**
+ * Create the TCP networklayer and listen to the specified port
  *
- * @param verificationBuffer the send function will write the data that is sent to this buffer, so that it is
- *                           possible to check what the send function received.
- */
-UA_Connection createDummyConnection(UA_ByteString *verificationBuffer);
+ * @param sendBufferSize The send buffer is reused. This is the max chunk size
+ * @param verificationBuffer the send function will copy the data that is sent
+ *        to this buffer, so that it is possible to check what the send function
+ *        received. */
+UA_Connection createDummyConnection(size_t sendBufferSize,
+                                    UA_ByteString *verificationBuffer);
 
 /**
  * Simulate network timing conditions