2727#include <fluent-bit/flb_gzip.h>
2828#include <monkey/mk_core.h>
2929#include "flb_tests_runtime.h"
30+ #include "../../plugins/in_splunk/splunk_prot.h"
3031
3132#define JSON_CONTENT_TYPE "application/json"
3233
@@ -922,6 +923,92 @@ void flb_test_splunk_collector_event_1_0()
922923 flb_test_splunk (8819 , "/services/collector/event/1.0" );
923924}
924925
926+ void flb_test_splunk_xff_extract ()
927+ {
928+ struct flb_lib_out_cb cb_data ;
929+ struct test_ctx * ctx ;
930+ struct flb_http_client * c ;
931+ int ret ;
932+ int num ;
933+ size_t b_sent ;
934+ char * buf = "{\"event\": \"Pony 1 has left the barn\"}" ;
935+ char * expected = "\"xff\":\"203.0.113.1\"" ;
936+ char * xff_value = " 203.0.113.1, 70.41.3.18, 150.172.238.178" ;
937+ char sport [16 ];
938+ int port = 8820 ;
939+
940+ snprintf (sport , 16 , "%d" , port );
941+
942+ clear_output_num ();
943+
944+ cb_data .cb = cb_check_result_json ;
945+ cb_data .data = expected ;
946+
947+ ctx = test_ctx_create (& cb_data );
948+ if (!TEST_CHECK (ctx != NULL )) {
949+ TEST_MSG ("test_ctx_create failed" );
950+ exit (EXIT_FAILURE );
951+ }
952+
953+ ret = flb_input_set (ctx -> flb , ctx -> i_ffd ,
954+ "port" , sport ,
955+ "add_remote_addr" , "true" ,
956+ "remote_addr_key" , "xff" ,
957+ NULL );
958+ TEST_CHECK (ret == 0 );
959+
960+ ret = flb_output_set (ctx -> flb , ctx -> o_ffd ,
961+ "match" , "*" ,
962+ "format" , "json" ,
963+ NULL );
964+ TEST_CHECK (ret == 0 );
965+
966+ /* Start the engine */
967+ ret = flb_start (ctx -> flb );
968+ TEST_CHECK (ret == 0 );
969+
970+ ctx -> httpc = splunk_client_ctx_create (port );
971+ TEST_CHECK (ctx -> httpc != NULL );
972+
973+ c = flb_http_client (ctx -> httpc -> u_conn , FLB_HTTP_POST , "/services/collector/event" ,
974+ buf , strlen (buf ), "127.0.0.1" , port , NULL , 0 );
975+ ret = flb_http_add_header (c , FLB_HTTP_HEADER_CONTENT_TYPE ,
976+ strlen (FLB_HTTP_HEADER_CONTENT_TYPE ),
977+ JSON_CONTENT_TYPE , strlen (JSON_CONTENT_TYPE ));
978+ TEST_CHECK (ret == 0 );
979+ ret = flb_http_add_header (c , SPLUNK_XFF_HEADER ,
980+ strlen (SPLUNK_XFF_HEADER ),
981+ xff_value , strlen (xff_value ));
982+ TEST_CHECK (ret == 0 );
983+ if (!TEST_CHECK (c != NULL )) {
984+ TEST_MSG ("splunk_client failed" );
985+ exit (EXIT_FAILURE );
986+ }
987+
988+ ret = flb_http_do (c , & b_sent );
989+ if (!TEST_CHECK (ret == 0 )) {
990+ TEST_MSG ("ret error. ret=%d\n" , ret );
991+ }
992+ else if (!TEST_CHECK (b_sent > 0 )){
993+ TEST_MSG ("b_sent size error. b_sent = %lu\n" , b_sent );
994+ }
995+ else if (!TEST_CHECK (c -> resp .status == 200 )) {
996+ TEST_MSG ("http response code error. expect: 200, got: %d\n" , c -> resp .status );
997+ }
998+
999+ /* waiting to flush */
1000+ flb_time_msleep (1500 );
1001+
1002+ num = get_output_num ();
1003+ if (!TEST_CHECK (num > 0 )) {
1004+ TEST_MSG ("no outputs" );
1005+ }
1006+
1007+ flb_http_client_destroy (c );
1008+ flb_upstream_conn_release (ctx -> httpc -> u_conn );
1009+ test_ctx_destroy (ctx );
1010+ }
1011+
9251012TEST_LIST = {
9261013 {"health" , flb_test_splunk_health },
9271014 {"collector" , flb_test_splunk_collector },
@@ -936,5 +1023,6 @@ TEST_LIST = {
9361023 {"tag_key" , flb_test_splunk_tag_key },
9371024 {"collector_event_with_auth_key" , flb_test_splunk_collector_event_hec_token_key },
9381025 {"collector_raw_with_auth_key" , flb_test_splunk_collector_raw_hec_token_key },
1026+ {"collector_xff_extract" , flb_test_splunk_xff_extract },
9391027 {NULL , NULL }
9401028};
0 commit comments