1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #include "tscore/Regression.h"
25 #include "HttpTransact.h"
26 #include "HttpSM.h"
27 
28 void
forceLinkRegressionHttpTransact()29 forceLinkRegressionHttpTransact()
30 {
31 }
32 
33 static void
init_sm(HttpSM * sm)34 init_sm(HttpSM *sm)
35 {
36   sm->init();
37   sm->t_state.hdr_info.client_request.create(HTTP_TYPE_REQUEST, nullptr);
38 }
39 
40 static void
setup_client_request(HttpSM * sm,const char * scheme,const char * request)41 setup_client_request(HttpSM *sm, const char *scheme, const char *request)
42 {
43   init_sm(sm);
44 
45   MIOBuffer *read_buffer        = new_MIOBuffer(HTTP_HEADER_BUFFER_SIZE_INDEX);
46   IOBufferReader *buffer_reader = read_buffer->alloc_reader();
47   read_buffer->write(request, strlen(request));
48 
49   HTTPParser httpParser;
50   http_parser_init(&httpParser);
51   int bytes_used = 0;
52   sm->t_state.hdr_info.client_request.parse_req(&httpParser, buffer_reader, &bytes_used, true /* eos */);
53   sm->t_state.hdr_info.client_request.url_get()->scheme_set(scheme, strlen(scheme));
54   sm->t_state.method = sm->t_state.hdr_info.client_request.method_get_wksidx();
55   free_MIOBuffer(read_buffer);
56 }
57 
58 /*
59 pstatus return values
60 REGRESSION_TEST_PASSED
61 REGRESSION_TEST_INPROGRESS
62 REGRESSION_TEST_FAILED
63 REGRESSION_TEST_NOT_RUN
64  */
REGRESSION_TEST(HttpTransact_is_request_valid)65 REGRESSION_TEST(HttpTransact_is_request_valid)(RegressionTest *t, int /* level */, int *pstatus)
66 {
67   HttpTransact transaction;
68   HttpSM sm;
69   *pstatus = REGRESSION_TEST_PASSED;
70 
71   struct {
72     const char *scheme;
73     const char *req;
74     bool result;
75   } requests[] = {// missing host header
76                   {"http", "GET / HTTP/1.1\r\n\r\n", false},
77                   // good get request
78                   {"http", "GET / HTTP/1.1\r\nHost: abc.com\r\n\r\n", true},
79                   // good trace request
80                   {"http", "TRACE / HTTP/1.1\r\nHost: abc.com\r\n\r\n", true},
81                   // content len < 0
82                   {"http", "POST / HTTP/1.1\r\nHost: abc.com\r\nContent-Length: -1\r\n\r\n", false},
83                   {"http", "PUSH / HTTP/1.1\r\nHost: abc.com\r\nContent-Length: -1\r\n\r\n", false},
84                   {"http", "PUT / HTTP/1.1\r\nHost: abc.com\r\nContent-Length: -1\r\n\r\n", false},
85                   // valid content len
86                   {"http", "POST / HTTP/1.1\r\nHost: abc.com\r\nContent-Length: 10\r\n\r\n", true},
87                   {"http", "PUSH / HTTP/1.1\r\nHost: abc.com\r\nContent-Length: 10\r\n\r\n", true},
88                   {"http", "PUT / HTTP/1.1\r\nHost: abc.com\r\nContent-Length: 10\r\n\r\n", true},
89                   // Content Length missing
90                   {"http", "POST / HTTP/1.1\r\nHost: abc.com\r\n\r\n", false},
91                   {"http", "PUSH / HTTP/1.1\r\nHost: abc.com\r\n\r\n", false},
92                   {"http", "PUT / HTTP/1.1\r\nHost: abc.com\r\n\r\n", false},
93                   {nullptr, nullptr, false}};
94   for (int i = 0; requests[i].req; i++) {
95     setup_client_request(&sm, requests[i].scheme, requests[i].req);
96 
97     if (requests[i].result != transaction.is_request_valid(&sm.t_state, &sm.t_state.hdr_info.client_request)) {
98       rprintf(t, "HttpTransact::is_request_valid - failed for request = '%s'.  Expected result was %s request\n", requests[i].req,
99               (requests[i].result ? "valid" : "invalid"));
100       *pstatus = REGRESSION_TEST_FAILED;
101     }
102   }
103 }
104 
REGRESSION_TEST(HttpTransact_handle_trace_and_options_requests)105 REGRESSION_TEST(HttpTransact_handle_trace_and_options_requests)(RegressionTest *t, int /* level */, int *pstatus)
106 {
107   HttpTransact transaction;
108   HttpSM sm;
109   *pstatus = REGRESSION_TEST_PASSED;
110 
111   struct {
112     const char *scheme;
113     const char *req;
114     bool result;
115   } requests[] = {// good trace request
116                   {"http", "TRACE www.abc.com/ HTTP/1.1\r\nHost: abc.com\r\nMax-Forwards: 0\r\n\r\n", true},
117                   {nullptr, nullptr, false}};
118   for (int i = 0; requests[i].req; i++) {
119     setup_client_request(&sm, requests[i].scheme, requests[i].req);
120 
121     if (requests[i].result != transaction.is_request_valid(&sm.t_state, &sm.t_state.hdr_info.client_request)) {
122       rprintf(t, "HttpTransact::is_request_valid - failed for request = '%s'.  Expected result was %s request\n", requests[i].req,
123               (requests[i].result ? "valid" : "invalid"));
124       *pstatus = REGRESSION_TEST_FAILED;
125     }
126     if (requests[i].result != transaction.handle_trace_and_options_requests(&sm.t_state, &sm.t_state.hdr_info.client_request)) {
127       rprintf(t, "HttpTransact::handle_trace_and_options - failed for request = '%s'.  Expected result was %s request\n",
128               requests[i].req, (requests[i].result ? "true" : "false"));
129       *pstatus = REGRESSION_TEST_FAILED;
130     }
131   }
132 }
133 
REGRESSION_TEST(HttpTransact_handle_request)134 REGRESSION_TEST(HttpTransact_handle_request)(RegressionTest * /* t */, int /* level */, int *pstatus)
135 {
136   // To be added..
137   *pstatus = REGRESSION_TEST_PASSED;
138 }
139