Bläddra i källkod

Create mock callback functions for unit testing.

These replace the functions that were linked from sample_application.c,
which are not suitable for the unit test executable. In addition,
these allow use of the Cpputest memory leak detection, and simulating
memory allocation failures.
Jason Valenzuela 4 år sedan
förälder
incheckning
cef2833fcf

+ 1 - 1
source/tests/CMakeLists.txt

@@ -28,7 +28,7 @@ add_subdirectory( ports )
 add_subdirectory( enet_encap )
 add_subdirectory( utils )
 
-add_executable( OpENer_Tests OpENerTests.cpp )
+add_executable( OpENer_Tests OpENerTests.cpp callback_mock.cpp)
 
 find_library ( CPPUTEST_LIBRARY CppUTest ${CPPUTEST_HOME}/cpputest_build/lib )
 find_library ( CPPUTESTEXT_LIBRARY CppUTestExt ${CPPUTEST_HOME}/cpputest_build/lib )

+ 7 - 1
source/tests/OpENerTests.cpp

@@ -4,6 +4,7 @@
 
 #include "OpENerTests.h"
 #include "CppUTest/TestRegistry.h"
+#include "CppUTestExt/MockSupportPlugin.h"
 
 extern "C" {
 #include "endianconv.h"
@@ -36,12 +37,17 @@ int main(int argc,
 
   DetermineEndianess();
 
+  TestRegistry* reg = TestRegistry::getCurrentRegistry();
+
+  MockSupportPlugin mockPlugin;
+  reg->installPlugin(&mockPlugin);
+
   /*
    * Enable the Cpputest SetPointerPlugin to automatically reset the
    * assert_jump_enabled pointer after each test.
    */
   SetPointerPlugin assert_restore("AssertJumpRestore");
-  TestRegistry::getCurrentRegistry()->installPlugin(&assert_restore);
+  reg->installPlugin(&assert_restore);
 
   return CommandLineTestRunner::RunAllTests(argc, argv);
 }

+ 82 - 0
source/tests/callback_mock.cpp

@@ -0,0 +1,82 @@
+/*******************************************************************************
+ * This module provides a set of Cpputest mock functions for the OpENer
+ * callback API.
+ ******************************************************************************/
+#include "CppUTestExt/MockSupport.h"
+#include <stdio.h>
+extern "C" {
+  #include "opener_api.h"
+}
+
+
+EipStatus ApplicationInitialization(void) {
+  return (EipStatus)mock()
+         .actualCall(__func__)
+         .returnIntValueOrDefault(kEipStatusOk);
+}
+
+
+void HandleApplication(void) {
+  mock().actualCall(__func__);
+}
+
+
+void CheckIoConnectionEvent(unsigned int output_assembly_id,
+                            unsigned int input_assembly_id,
+                            IoConnectionEvent io_connection_event) {
+  mock().actualCall(__func__);
+}
+
+
+EipStatus AfterAssemblyDataReceived(CipInstance *instance) {
+  return (EipStatus)mock()
+         .actualCall(__func__)
+         .returnIntValueOrDefault(kEipStatusOk);
+}
+
+
+EipBool8 BeforeAssemblyDataSend(CipInstance *instance) {
+  return mock()
+         .actualCall(__func__)
+         .returnIntValueOrDefault(false);
+}
+
+
+EipStatus ResetDevice(void) {
+  return (EipStatus)mock()
+         .actualCall(__func__)
+         .returnIntValueOrDefault(kEipStatusError);
+}
+
+
+EipStatus ResetDeviceToInitialConfiguration(void) {
+  return (EipStatus)mock()
+         .actualCall(__func__)
+         .returnIntValueOrDefault(kEipStatusError);
+}
+
+
+void *CipCalloc(size_t number_of_elements,
+                size_t size_of_element) {
+  MockActualCall& call = mock().actualCall(__func__);
+
+  /*
+   * The return value is dependent upon if a predetermined result has been
+   * configured via andReturnValue(), which should only be used to
+   * simulate an allocation failure by returning NULL. Otherwise, the
+   * default is to return a normally-allocated pointer.
+   */
+  return call.hasReturnValue() ? NULL : calloc(number_of_elements,
+                                               size_of_element);
+}
+
+
+void CipFree(void *data) {
+  mock().actualCall(__func__);
+  free(data);
+}
+
+
+void RunIdleChanged(EipUint32 run_idle_value) {
+  mock().actualCall(__func__);
+}

+ 1 - 1
source/tests/cip/CMakeLists.txt

@@ -14,6 +14,6 @@ include_directories( ${SRC_DIR}/cip )
 
 add_library( CipTest ${CipTestSrc} )
 
-target_link_libraries( CipTest CIP ENET_ENCAP PLATFORM_GENERIC SAMPLE_APP ${OpENer_PLATFORM}PLATFORM ${PLATFORM_SPEC_LIBS} rt)
+target_link_libraries( CipTest CIP ENET_ENCAP PLATFORM_GENERIC ${OpENer_PLATFORM}PLATFORM ${PLATFORM_SPEC_LIBS} rt)
 
 target_link_libraries( CipTest gcov ${CPPUTEST_LIBRARY} ${CPPUTESTEXT_LIBRARY} )

+ 5 - 1
source/tests/cip/cipstringtests.cpp

@@ -5,6 +5,7 @@
 *****************************************************I*************************/
 
 #include <CppUTest/TestHarness.h>
+#include <CppUTestExt/MockSupport.h>
 #include <stdint.h>
 #include <string.h>
 
@@ -16,7 +17,10 @@ extern "C" {
 }
 
 TEST_GROUP (CipString) {
-
+  void setup()
+  {
+    mock().disable();
+  }
 };
 
 TEST (CipString, CipStringNClearNullPointer) {