project(lws-minimal-ss-ws-echo C)
cmake_minimum_required(VERSION 3.10)
find_package(libwebsockets CONFIG REQUIRED)

require_lws_config(LWS_ROLE_H1 1 requirements)
require_lws_config(LWS_ROLE_WS 1 requirements)
require_lws_config(LWS_WITHOUT_CLIENT 0 requirements)
require_lws_config(LWS_WITH_SECURE_STREAMS 1 requirements)
require_lws_config(LWS_WITH_SECURE_STREAMS_STATIC_POLICY_ONLY 0 requirements)
require_lws_config(LWS_WITH_TLS 1 requirements)

require_lws_config(LWS_WITH_SYS_FAULT_INJECTION 1 has_fault_injection)
require_lws_config(LWS_WITH_SECURE_STREAMS_PROXY_API 1 has_ss_proxy)
require_lws_config(LWS_WITH_SYS_STATE 1 has_sys_state)

CHECK_C_SOURCE_COMPILES("#include <libwebsockets.h>\nint main(void) {\ni#if defined(LWS_WITH_SECURE_STREAMS_PROXY_API)\n return 0;\n #else\n fail\n #endif\n return 0;\n}\n" HAS_LWS_WITH_SECURE_STREAMS_PROXY_API)

if (requirements)

	set(SRCS main.c ws-echo-ss.c)

	add_executable(${PROJECT_NAME} ${SRCS})

	if (websockets_shared)
		target_link_libraries(${PROJECT_NAME}
			websockets_shared
			${LIBWEBSOCKETS_DEP_LIBS})
		add_dependencies(${PROJECT_NAME}
			websockets_shared)
	else()
		target_link_libraries(${PROJECT_NAME}
			websockets
			${LIBWEBSOCKETS_DEP_LIBS})
	endif()

	### --- this section related to also building example with SSPC / Proxy --->

	if (HAS_LWS_WITH_SECURE_STREAMS_PROXY_API OR has_ss_proxy OR
	    LWS_WITH_SECURE_STREAMS_PROXY_API)
		add_compile_options(-DLWS_SS_USE_SSPC)

		add_executable(${PROJECT_NAME}-client ${SRCS})

		if (websockets_shared)
			target_link_libraries(${PROJECT_NAME}-client
				websockets_shared
				${LIBWEBSOCKETS_DEP_LIBS})
			add_dependencies(${PROJECT_NAME}-client
				websockets_shared)
		else()
			target_link_libraries(${PROJECT_NAME}-client
				websockets
				${LIBWEBSOCKETS_DEP_LIBS})
		endif()
	endif()

	### <--- this section related to building with SSPC / Proxy END



	### ---everything else related to ctest / CI ----->

	find_program(VALGRIND "valgrind")

	if (LWS_CTEST_INTERNET_AVAILABLE AND NOT WIN32)
	
		#
		# External-egress variant: the example's default policy points at
		# libwebsockets.org, which needs real internet routing.  This is
		# only registered for builds that can actually reach it
		# (dual-stack / IPv4-capable); in an IPv6-only / no-egress CI
		# build the test would hang on DNS / connect until the ctest
		# timeout.  The -local variant below provides the IPv6 coverage
		# instead.
		#
		if (LWS_IPV4)

			#
			# When running in CI, wait for a lease on the resources
			# before starting this test, so the server does not get
			# thousands of simultaneous tls connection attempts
			#
			# sai-resource holds the lease on the resources until
			# the time given in seconds or the sai-resource instance
			# exits, whichever happens first
			#
			# If running under Sai, creates a lock test called "res_sspcmin"
			#

			sai_resource(warmcat_conns 1 40 sspcmin-ws-echo)

			#
			# simple test not via proxy
			#

			if (VALGRIND)
				message("testing via valgrind")
				add_test(NAME mssws_echo-warmcat COMMAND
					${VALGRIND} --tool=memcheck --leak-check=yes --num-callers=20
					$<TARGET_FILE:${PROJECT_NAME}>)
			else()
				add_test(NAME mssws_echo-warmcat COMMAND ${PROJECT_NAME})
			endif()

			set_tests_properties(mssws_echo-warmcat
					     PROPERTIES
					     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/minimal-examples/client/ws-echo
					     TIMEOUT 40)
			if (DEFINED ENV{SAI_OVN})
				set_tests_properties(mssws_echo-warmcat PROPERTIES FIXTURES_REQUIRED "res_msspcmin")
			endif()

			if (has_fault_injection)
				if (VALGRIND)
					add_test(NAME mssws_echo-warmcat-fi1 COMMAND
						${VALGRIND} --tool=memcheck --leak-check=yes --num-callers=20
						$<TARGET_FILE:${PROJECT_NAME}>
						--fault-injection "ss/ss_create_destroy_me"
						--expected-exit 1)
					add_test(NAME mssws_echo-warmcat-fi2 COMMAND
						${VALGRIND} --tool=memcheck --leak-check=yes --num-callers=20
						$<TARGET_FILE:${PROJECT_NAME}>
						--fault-injection "ss/ss_no_streamtype_policy"
						--expected-exit 1)
				else()
					add_test(NAME mssws_echo-warmcat-fi1 COMMAND lws-minimal-secure-streams
						 --fault-injection "ss/ss_create_destroy_me"
						 --expected-exit 1)
					add_test(NAME mssws_echo-warmcat-fi2 COMMAND lws-minimal-secure-streams
						 --fault-injection "ss/ss_no_streamtype_policy"
						 --expected-exit 1)
	 			 endif()

				set_tests_properties(mssws_echo-warmcat-fi1
						     mssws_echo-warmcat-fi2
					     PROPERTIES
					     WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/minimal-examples/client/ws-echo
					     TIMEOUT 5)

			endif()

		endif() # LWS_IPV4 (external-egress variant)


		#
		# Local-server variant: runs against a local test-server over TLS
		# (serving the lws-mirror-protocol ws echo), so it does not need
		# any external network egress.  Suitable for IPv6-only / no-egress CI.
		# test-server is given the localhost-100y cert/key with -C/-K so the
		# client can trust it via the same local_test trust store as the
		# httpbin-based local tests.
		#

		if (LWS_WITH_SERVER AND NOT LWS_WITHOUT_TEST_SERVER)
			lws_get_free_port(PORT_WS_SRV_TLS)

			configure_file(policy-local.json.in policy-local.json @ONLY)

			set(LOCAL_CERT_DIR ${CMAKE_SOURCE_DIR}/minimal-examples-lowlevel/http-server/minimal-http-server-tls)

			add_test(NAME st_ws_srv
				COMMAND ${CMAKE_SOURCE_DIR}/scripts/ctest-background.sh
					ws_srv $<TARGET_FILE:test-server>
					-s -p ${PORT_WS_SRV_TLS}
					-C ${LOCAL_CERT_DIR}/localhost-100y.cert
					-K ${LOCAL_CERT_DIR}/localhost-100y.key
					-r ${CMAKE_BINARY_DIR}/share/libwebsockets-test-server)
			add_test(NAME ki_ws_srv
				COMMAND ${CMAKE_SOURCE_DIR}/scripts/ctest-background-kill.sh
					ws_srv $<TARGET_FILE_NAME:test-server> -p ${PORT_WS_SRV_TLS})
			set_tests_properties(st_ws_srv PROPERTIES
					WORKING_DIRECTORY .
					FIXTURES_SETUP ws_srv
					TIMEOUT 800
					ENVIRONMENT "SAI_LIST_PORT=${PORT_WS_SRV_TLS}")
			set_tests_properties(ki_ws_srv PROPERTIES FIXTURES_CLEANUP ws_srv)

			add_test(NAME mssws_echo-local
				COMMAND ${PROJECT_NAME}
					-c ${CMAKE_CURRENT_BINARY_DIR}/policy-local.json)
			set_tests_properties(mssws_echo-local PROPERTIES
					WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/minimal-examples/client/ws-echo
					FIXTURES_REQUIRED "ws_srv"
					TIMEOUT 40)
		endif()


		if (has_sys_state AND (HAS_LWS_WITH_SECURE_STREAMS_PROXY_API OR has_ss_proxy OR
		    LWS_WITH_SECURE_STREAMS_PROXY_API))

			#
			# Define test dep to bring up and take down the test
			# proxy
			#

			if (${CMAKE_SYSTEM_NAME} MATCHES "Linux")
				# uds abstract namespace for linux
				set(CTEST_SOCKET_PATH "@ctest-msspws_echo--$ENV{SAI_PROJECT}-$ENV{SAI_OVN}")
			else()
				# filesystem socket for others
				string(SUBSTRING "$ENV{SAI_OVN}" 32 16 SHORT_HASH)
				set(CTEST_SOCKET_PATH "/tmp/ctest-msspws_echo-$ENV{SAI_PROJECT}-${SHORT_HASH}")
			endif()
			add_test(NAME st_mssws_echo-proxy COMMAND
				${CMAKE_SOURCE_DIR}/scripts/ctest-background.sh
				mssws_echo-proxy $<TARGET_FILE:lws-minimal-secure-streams-proxy>
				-i ${CTEST_SOCKET_PATH} )
			set_tests_properties(st_mssws_echo-proxy PROPERTIES WORKING_DIRECTORY . FIXTURES_SETUP mssws_echo-proxy TIMEOUT 800)

			add_test(NAME ki_mssws_echo-proxy COMMAND
				${CMAKE_SOURCE_DIR}/scripts/ctest-background-kill.sh
				mssws_echo-proxy $<TARGET_FILE:lws-minimal-secure-streams-proxy>
				-i ${CTEST_SOCKET_PATH})
			set_tests_properties(ki_mssws_echo-proxy PROPERTIES FIXTURES_CLEANUP mssws_echo-proxy)

			#
			# the client part that will connect to the proxy
			#

			if (VALGRIND)
				message("testing via valgrind")
				add_test(NAME msspcws-echo-minimal COMMAND
					${VALGRIND} --tool=memcheck --leak-check=yes --num-callers=20
					$<TARGET_FILE:${PROJECT_NAME}-client>
					--ssproxy-iface +${CTEST_SOCKET_PATH})
			else()
				add_test(NAME msspcws-echo-minimal COMMAND
					  ${PROJECT_NAME}-client
					  --ssproxy-iface +${CTEST_SOCKET_PATH})
			endif()
			
			set(fixlist "mssws_echo-proxy")
			if (DEFINED ENV{SAI_OVN})
				list(APPEND fixlist "res_mssws_echo-proxy")
			endif()
			
			set_tests_properties(msspcws-echo-minimal PROPERTIES
				WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/minimal-examples/client/ws-echo
				FIXTURES_REQUIRED "${fixlist}"
				TIMEOUT 40)

		endif()

	endif()

	### <--- related to ctest / CI END

endif()

