tests UPDATE get ports from env
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index b5af1ab..7349994 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -12,14 +12,31 @@
 endforeach()
 add_library(testobj OBJECT ${test_srcs} ${compatsrc})
 
-set(NEXT_TEST_PORT 10005)
+set(TEST_SRC "ln2_test.c")
+set(NEXT_TEST_PORT 10050)
+
+macro(get_test_ports PORT_COUNT PORT_DEFINITIONS)
+    if (NOT ${PORT_COUNT})
+        set(${PORT_COUNT} 1)
+    endif()
+
+    SET(PORT_INDEX 0)
+    while(PORT_INDEX LESS ${${PORT_COUNT}})
+        list(APPEND ${PORT_DEFINITIONS} "TEST_PORT_${PORT_INDEX}=${NEXT_TEST_PORT}")
+        math(EXPR PORT_INDEX "${PORT_INDEX} + 1")
+        math(EXPR NEXT_TEST_PORT "${NEXT_TEST_PORT} + 1")
+    endwhile()
+    set(NEXT_TEST_PORT ${NEXT_TEST_PORT} PARENT_SCOPE)
+endmacro()
 
 function(libnetconf2_test)
     cmake_parse_arguments(TEST "" "NAME;PORT_COUNT" "WRAP_FUNCS" ${ARGN})
 
-    add_executable(${TEST_NAME} $<TARGET_OBJECTS:testobj> ${TEST_NAME}.c)
+    add_executable(${TEST_NAME} $<TARGET_OBJECTS:testobj> ${TEST_SRC} ${TEST_NAME}.c)
     target_link_libraries(${TEST_NAME} ${CMOCKA_LIBRARIES} ${LIBYANG_LIBRARIES} netconf2)
     target_include_directories(${TEST_NAME} PRIVATE ${CMOCKA_INCLUDE_DIR})
+
+    # wrap functions
     if(TEST_WRAP_FUNCS)
         set(wrap_link_flags "-Wl")
         foreach(mock_func IN LISTS TEST_WRAP_FUNCS)
@@ -27,23 +44,18 @@
         endforeach()
         set_target_properties(${TEST_NAME} PROPERTIES LINK_FLAGS "${wrap_link_flags}")
     endif()
-    target_compile_definitions(${TEST_NAME} PRIVATE "TEST_PORT=${NEXT_TEST_PORT}" "TEST_PORT_STR=\"${NEXT_TEST_PORT}\"")
-    if(NOT TEST_PORT_COUNT)
-        set(TEST_PORT_COUNT 1)
-    endif()
-    SET(PORT_INDEX 2)
-    while(PORT_INDEX LESS_EQUAL TEST_PORT_COUNT)
-        math(EXPR ANOTHER_PORT "${NEXT_TEST_PORT} + ${PORT_INDEX} - 1")
-        target_compile_definitions(${TEST_NAME} PRIVATE "TEST_PORT_${PORT_INDEX}=${ANOTHER_PORT}" "TEST_PORT_${PORT_INDEX}_STR=\"${ANOTHER_PORT}\"")
-        math(EXPR PORT_INDEX "${PORT_INDEX} + 1")
-    endwhile()
-    math(EXPR NEXT_TEST_PORT "${NEXT_TEST_PORT} + ${TEST_PORT_COUNT}")
-    set(NEXT_TEST_PORT ${NEXT_TEST_PORT} PARENT_SCOPE)
-    add_test(NAME ${TEST_NAME} COMMAND $<TARGET_FILE:${TEST_NAME}>)
 
+    # create a test, generate port numbers and set them as env vars for the test
+    add_test(NAME ${TEST_NAME} COMMAND $<TARGET_FILE:${TEST_NAME}>)
+    get_test_ports(TEST_PORT_COUNT TEST_PORT_DEFINITIONS)
+    set_tests_properties(${TEST_NAME} PROPERTIES ENVIRONMENT "${TEST_PORT_DEFINITIONS}")
+
+    # do the same for valgrind tests
     if(ENABLE_VALGRIND_TESTS)
         add_test(${TEST_NAME}_valgrind valgrind --leak-check=full --show-leak-kinds=all --error-exitcode=1
             --suppressions=${PROJECT_SOURCE_DIR}/tests/library_valgrind.supp ${CMAKE_BINARY_DIR}/tests/${TEST_NAME})
+        get_test_ports(TEST_PORT_COUNT VALGRIND_TEST_PORT_DEFINITIONS)
+        set_tests_properties(${TEST_NAME}_valgrind PROPERTIES ENVIRONMENT "${VALGRIND_TEST_PORT_DEFINITIONS}")
     endif()
 endfunction()