set(DECODER_NAME http)

set(TEST_RUN_DIR ${CMAKE_BINARY_DIR}/test/decoders/http)
set(SAPP_DEVEL_DIR ${TEST_RUN_DIR}/lib)
set(TEST_MAIN http_test_main)

include_directories(${CMAKE_SOURCE_DIR}/include)
include_directories(${CMAKE_SOURCE_DIR}/test)
include_directories(/usr/local/include/cjson)
include_directories(/opt/tsg/framework/include/stellar)
include_directories(/opt/MESA/include/MESA)
include_directories(/opt/tsg/stellar/include/)

#various ways to add -rdynamic for centos7, centos8, and different cmake version
add_definitions(-rdynamic)
link_directories(${SAPP_DEVEL_DIR})

# assemble test env
add_test(NAME HTTP_MKDIR_METRIC COMMAND sh -c "mkdir -p ${TEST_RUN_DIR}/metrics; mkdir -p ${TEST_RUN_DIR}/plugin; mkdir -p ${TEST_RUN_DIR}/log; mkdir -p ${TEST_RUN_DIR}/pcap")
add_test(NAME HTTP_COPY_SPEC COMMAND sh -c "mkdir -p ${TEST_RUN_DIR}/plugin/ && cp ${CMAKE_CURRENT_SOURCE_DIR}/env/spec.toml ${TEST_RUN_DIR}/plugin/spec.toml")
add_test(NAME HTTP_COPY_CONF COMMAND sh -c "mkdir -p ${TEST_RUN_DIR}/conf/ && cp ${CMAKE_CURRENT_SOURCE_DIR}/env/stellar.toml ${TEST_RUN_DIR}/conf/stellar.toml")
add_test(NAME HTTP_COPY_LOG_CONF COMMAND sh -c "mkdir -p ${TEST_RUN_DIR}/conf/ && cp ${CMAKE_CURRENT_SOURCE_DIR}/env/log.toml ${TEST_RUN_DIR}/conf/log.toml")
add_test(NAME HTTP_COPY_HTTP_DECODER_CONF COMMAND sh -c "mkdir -p ${TEST_RUN_DIR}/etc/http && cp ${CMAKE_CURRENT_SOURCE_DIR}/env/http_decoder.toml ${TEST_RUN_DIR}/etc/http/")
add_test(NAME HTTP_COPY_HTTP_GTEST_ENTRY_CONF COMMAND sh -c "mkdir -p ${TEST_RUN_DIR}/etc/http && cp ${CMAKE_CURRENT_SOURCE_DIR}/env/gtest_entry.toml ${TEST_RUN_DIR}/etc/http/")

# update config files
add_test(NAME HTTP_UPDATE_GTEST_PLUG_ENTRY COMMAND bash -c "sed -i 's/name=.*/name=\\x22http_decoder_test_entry\\x22/' ${TEST_RUN_DIR}/etc/http/gtest_entry.toml")
add_test(NAME HTTP_UPDATE_GTEST_PLUG_TOPIC COMMAND bash -c "sed -i 's/topic=.*/topic=\\x22HTTP_DECODER_MESSAGE\\x22/' ${TEST_RUN_DIR}/etc/http/gtest_entry.toml")

# update plugin to be tested
add_test(NAME HTTP_CP_DECODER_SO COMMAND sh -c "cp ${CMAKE_BINARY_DIR}/decoders/http/http_dyn.so ${TEST_RUN_DIR}/plugin/${DECODER_NAME}.so")
add_test(NAME HTTP_CP_DECODER_GTEST_SO COMMAND sh -c "cp ${CMAKE_BINARY_DIR}/test/decoders/http/${DECODER_NAME}_test.so ${TEST_RUN_DIR}/plugin/${DECODER_NAME}_test.so")

set_tests_properties(HTTP_MKDIR_METRIC HTTP_COPY_SPEC HTTP_COPY_HTTP_DECODER_CONF  HTTP_COPY_HTTP_GTEST_ENTRY_CONF  
                    HTTP_COPY_CONF HTTP_COPY_LOG_CONF 
                    HTTP_CP_DECODER_SO HTTP_CP_DECODER_GTEST_SO 
                    HTTP_UPDATE_GTEST_PLUG_ENTRY HTTP_UPDATE_GTEST_PLUG_TOPIC 
                    PROPERTIES FIXTURES_SETUP TestFixture)

set(TEST_JSON_DIR ${PROJECT_SOURCE_DIR}/benchmarks/json/http)        
set(TEST_PCAP_DIR ${PROJECT_SOURCE_DIR}/benchmarks/pcap/http)

# run tests
add_test(NAME HTTP_GET_SINGLE_TRANS_TEST COMMAND sh -c "ln -sf ${TEST_PCAP_DIR}/http_get_single_trans.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_get_single_trans.json"  WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_GET_MULTI_TRANS_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_get_multi_trans.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_get_multi_trans.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_GET_LONG_COOKIE_TEST COMMAND sh -c "ln -sf ${TEST_PCAP_DIR}/http_get_long_cookie.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_get_long_cookie.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_GET_ENCODED_URI_TEST COMMAND sh -c "ln -sf ${TEST_PCAP_DIR}/http_get_encoded_uri.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_get_encoded_uri.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_RES_GZIP_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_res_gzip.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_res_gzip.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_CHUNKED_RES_GZIP_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_chunked_res_gzip.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_chunked_res_gzip.json"WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_OVER_TCP_KEEPALIVE_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_over_tcp_keepalive.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_over_tcp_keepalive.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_OVER_PPPOE_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_over_pppoe.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_over_pppoe.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_OVER_TLS_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_over_tls.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_over_tls.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME NON_HTTP_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/non_http.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/non_http.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_REQ_1BYTE_SLIDING_WINDOW_TEST COMMAND sh -c "ln -sf ${TEST_PCAP_DIR}/http_req_1byte_sliding_window.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_req_1byte_sliding_window.json"  WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_RES_1BYTE_SLIDING_WINDOW_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_res_1byte_sliding_window.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_res_1byte_sliding_window.json"  WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_NO_CONTENT_LENGTH_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_no_content_length.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_no_content_length.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_POST_MULTIPART_FORM_DATA_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_post_multipart_form_data.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_post_multipart_form_data.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_HEADERS_EXCEED_MAXIMUM_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_hdrs_exceed_maximum.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_hdrs_exceed_maximum.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_GET_MALFORMED_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_get_malformed.pcap ${TEST_RUN_DIR}/pcap/test.pcap;  ./${TEST_MAIN} ${TEST_JSON_DIR}/http_get_malformed.json"  WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_URL_WITHOUT_HOST_V4_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_url_test_without_host.pcap ${TEST_RUN_DIR}/pcap/test.pcap;  ./${TEST_MAIN} ${TEST_JSON_DIR}/http_url_test_without_host.json"  WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_URL_WITHOUT_HOST_V6_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_url_test_without_host_v6.pcap ${TEST_RUN_DIR}/pcap/test.pcap;  ./${TEST_MAIN} ${TEST_JSON_DIR}/http_url_test_without_host_v6.json"  WORKING_DIRECTORY ${TEST_RUN_DIR})
#no SYN, steallar not support !
# add_test(NAME HTTP_HEADER_VALUE_EMPTY_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_hdr_value_empty.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_hdr_value_empty.json"  WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_UPGRADE_WEBSOCKET_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_upgrade_websocket.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_upgrade_websocket.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_MULTI_PARSE_ERROR_TEST COMMAND sh -c "ln -sf ${TEST_PCAP_DIR}/http_multi_parse_error.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_multi_parse_error.json"  WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_GET_REQ_PIPELINE_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_get_req_pipeline.pcap ${TEST_RUN_DIR}/pcap/test.pcap;./${TEST_MAIN} ${TEST_JSON_DIR}/http_get_req_pipeline.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_TRANS_PIPELINE_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_trans_pipeline.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_trans_pipeline.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
#no SYN, steallar not support !
# add_test(NAME HTTP_HEADER_TRUNCATED_IN_KV_TEST COMMAND sh -c "ln -sf ${TEST_PCAP_DIR}/http_hdr_truncated_in_kv.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_hdr_truncated_in_kv.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
# add_test(NAME HTTP_HEADER_TRUNCATED_AFTER_KV_TEST COMMAND  ./${TEST_MAIN} ${TEST_JSON_DIR}/http_hdr_truncated_after_kv.json -r ${TEST_PCAP_DIR}/http_hdr_truncated_after_kv.pcap  WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_FIN_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_fin.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/non_http.json"  WORKING_DIRECTORY ${TEST_RUN_DIR})
# add_test(NAME HTTP_TUNNEL_ONLY_HDR_TEST COMMAND  ./${TEST_MAIN} ${TEST_JSON_DIR}/http_tunnel_s2c_only_hdr.json
#          -r ${TEST_PCAP_DIR}/http_tunnel_s2c_only_hdr.pcap WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_CHN_ENCODE_URL COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_chn_encode_url.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_chn_encode_url.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_ZLIB_DEADLOCK COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_zlib_deadlock.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_zlib_deadlock.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_OUT_OF_ORDER COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_out_of_order.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_out_of_order.json" WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_GZIP_OUT_OF_ORDER COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_gzip_out_of_order.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_gzip_out_of_order.json" WORKING_DIRECTORY ${TEST_RUN_DIR})

set_tests_properties(HTTP_GET_SINGLE_TRANS_TEST
                    HTTP_GET_MULTI_TRANS_TEST
                    HTTP_GET_LONG_COOKIE_TEST
                    HTTP_GET_ENCODED_URI_TEST
                    HTTP_RES_GZIP_TEST
                    HTTP_CHUNKED_RES_GZIP_TEST
                    HTTP_OVER_TCP_KEEPALIVE_TEST
                    HTTP_OVER_PPPOE_TEST
                    HTTP_OVER_TLS_TEST
                    NON_HTTP_TEST
                    HTTP_REQ_1BYTE_SLIDING_WINDOW_TEST
                    HTTP_RES_1BYTE_SLIDING_WINDOW_TEST
                    HTTP_NO_CONTENT_LENGTH_TEST
                    HTTP_POST_MULTIPART_FORM_DATA_TEST
                    HTTP_HEADERS_EXCEED_MAXIMUM_TEST
                    HTTP_GET_MALFORMED_TEST
                    HTTP_URL_WITHOUT_HOST_V4_TEST
                    HTTP_URL_WITHOUT_HOST_V6_TEST
                    HTTP_MULTI_PARSE_ERROR_TEST
                    HTTP_UPGRADE_WEBSOCKET_TEST
                    HTTP_GET_REQ_PIPELINE_TEST
                    HTTP_TRANS_PIPELINE_TEST
                    HTTP_FIN_TEST
                    HTTP_CHN_ENCODE_URL
                    HTTP_ZLIB_DEADLOCK
                    HTTP_OUT_OF_ORDER
                    HTTP_GZIP_OUT_OF_ORDER
                    PROPERTIES FIXTURES_REQUIRED TestFixture)

add_test(NAME HTTP_UPDATE_STATE_PLUG_ENTRY COMMAND bash -c "sed -i 's/name=.*/name=\\x22http_decoder_test_state_entry\\x22/' ${TEST_RUN_DIR}/etc/http/gtest_entry.toml")
add_test(NAME HTTP_UPDATE_STATE_PLUG_TOPIC COMMAND bash -c "sed -i 's/topic=.*/topic=\\x22HTTP_DECODER_MESSAGE\\x22/' ${TEST_RUN_DIR}/etc/http/gtest_entry.toml")

set_tests_properties(HTTP_UPDATE_STATE_PLUG_ENTRY HTTP_UPDATE_STATE_PLUG_TOPIC HTTP_CP_DECODER_SO HTTP_CP_DECODER_GTEST_SO HTTP_MKDIR_METRIC 
                     PROPERTIES FIXTURES_SETUP TestState)

add_test(NAME HTTP_MSG_TYPE_STATE_TEST COMMAND sh -c "ln -sf ${TEST_PCAP_DIR}/http_post.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_msg_type_state.json"    WORKING_DIRECTORY ${TEST_RUN_DIR})
add_test(NAME HTTP_MSG_TYPE_STATE_C2S_TEST COMMAND sh -c "ln -sf ${TEST_PCAP_DIR}/http_post_c2s.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_msg_type_state_c2s.json"  WORKING_DIRECTORY ${TEST_RUN_DIR})         
add_test(NAME HTTP_MSG_TYPE_STATE_S2C_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_post_s2c.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_msg_type_state_s2c.json"   WORKING_DIRECTORY ${TEST_RUN_DIR})      
add_test(NAME HTTP_MSG_TYPE_STATE_PIPELINE_TEST COMMAND  sh -c "ln -sf ${TEST_PCAP_DIR}/http_get_multi_trans.pcap ${TEST_RUN_DIR}/pcap/test.pcap;  ./${TEST_MAIN} ${TEST_JSON_DIR}/http_msg_type_state_pipeline.json"   WORKING_DIRECTORY ${TEST_RUN_DIR})    
add_test(NAME HTTP_MSG_TYPE_STATE_SES_EXCEPTION_C2S_TEST COMMAND sh -c "ln -sf ${TEST_PCAP_DIR}/http_session_exception_c2s.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_msg_type_state_exception_c2s.json" WORKING_DIRECTORY ${TEST_RUN_DIR})    
add_test(NAME HTTP_MSG_TYPE_STATE_SES_EXCEPTION_S2C_TEST COMMAND sh -c "ln -sf ${TEST_PCAP_DIR}/http_session_exception_s2c.pcap ${TEST_RUN_DIR}/pcap/test.pcap; ./${TEST_MAIN} ${TEST_JSON_DIR}/http_msg_type_state_exception_s2c.json" WORKING_DIRECTORY ${TEST_RUN_DIR})    

set_tests_properties(HTTP_MSG_TYPE_STATE_TEST
                    HTTP_MSG_TYPE_STATE_C2S_TEST
                    HTTP_MSG_TYPE_STATE_S2C_TEST
                    HTTP_MSG_TYPE_STATE_PIPELINE_TEST
                    HTTP_MSG_TYPE_STATE_SES_EXCEPTION_C2S_TEST
                    HTTP_MSG_TYPE_STATE_SES_EXCEPTION_S2C_TEST
                    PROPERTIES FIXTURES_REQUIRED TestState)

