StrictCompile()

add_library(bongoconnio SHARED
	sockets.c
	connio.c
	trace.c
	addrpool.c
	unix-ip.c
	proxy-protocol.c
)

target_link_libraries(bongoconnio PRIVATE
	bongoxpl
	GnuTLS::GnuTLS
	GLib2::GLib2
)

install(TARGETS bongoconnio DESTINATION ${LIB_INSTALL_DIR})

if(BUILD_TESTING)
	add_executable(connio-newline-test tests/newline-test.c)
	target_link_libraries(connio-newline-test PRIVATE bongoconnio)
	add_test(NAME connio-newline COMMAND connio-newline-test)

	add_executable(connio-default-timeout-test tests/default-timeout-test.c)
	target_link_libraries(connio-default-timeout-test PRIVATE bongoconnio)
	add_test(NAME connio-default-timeout COMMAND connio-default-timeout-test)

	add_executable(connio-data-test tests/data-test.c)
	target_link_libraries(connio-data-test PRIVATE
		bongoconnio
		Threads::Threads)
	add_test(NAME connio-data COMMAND connio-data-test)

	add_executable(connio-proxy-protocol-test tests/proxy-protocol-test.c)
	target_link_libraries(connio-proxy-protocol-test PRIVATE
		bongoconnio
		Threads::Threads)
	add_test(NAME connio-proxy-protocol COMMAND connio-proxy-protocol-test)

	add_executable(connio-interface-list-test tests/interface-list-test.c)
	target_link_libraries(connio-interface-list-test PRIVATE
		bongoconnio
		bongoxpl
		Threads::Threads)
	add_test(NAME connio-interface-list COMMAND connio-interface-list-test)
	set_tests_properties(connio-interface-list PROPERTIES TIMEOUT 10)

	add_executable(connio-queue-registration-cancel-test
		tests/queue-registration-cancel-test.c)
	target_link_libraries(connio-queue-registration-cancel-test PRIVATE
		bongomsgapi
		Threads::Threads)
	add_test(NAME connio-queue-registration-cancel
		COMMAND connio-queue-registration-cancel-test)
	set_tests_properties(connio-queue-registration-cancel PROPERTIES TIMEOUT 5)

	find_program(OPENSSL_EXECUTABLE openssl)
	if(OPENSSL_EXECUTABLE)
		set(TLS_TEST_KEY "${CMAKE_CURRENT_BINARY_DIR}/tls-peer-test.key")
		set(TLS_TEST_CERT "${CMAKE_CURRENT_BINARY_DIR}/tls-peer-test.crt")
		add_test(NAME connio-tls-test-certificate
			COMMAND ${OPENSSL_EXECUTABLE} req -x509 -newkey rsa:2048 -nodes
				-keyout ${TLS_TEST_KEY} -out ${TLS_TEST_CERT} -days 1
				-subj /CN=localhost -addext subjectAltName=DNS:localhost)
		set_tests_properties(connio-tls-test-certificate PROPERTIES
			FIXTURES_SETUP connio_tls_certificate)

		add_executable(connio-tls-peer-test tests/tls-peer-test.c)
		target_link_libraries(connio-tls-peer-test PRIVATE
			bongoconnio
			GnuTLS::GnuTLS
			Threads::Threads)
		add_test(NAME connio-tls-peer
			COMMAND connio-tls-peer-test ${TLS_TEST_CERT} ${TLS_TEST_KEY})
		set_tests_properties(connio-tls-peer PROPERTIES
			FIXTURES_REQUIRED connio_tls_certificate)
	endif()
endif()
