blob: e84e67fe3958fec3ec1f6b0879edbe41c249e245 [file] [log] [blame]
Filip Tehlarb15a0002022-11-10 12:34:17 +01001package main
2
Filip Tehlarc204c872022-12-21 08:59:16 +01003import (
Matus Fabian82ad9662024-06-04 19:00:00 +02004 "bytes"
Filip Tehlar8df3de42023-01-27 13:14:34 +01005 "fmt"
Matus Fabian2d1f0e62024-06-06 11:24:36 +02006 "io"
Matus Fabian9bb07622024-09-04 18:04:54 +02007 "math/rand"
Matus Fabiand086a362024-06-27 13:20:10 +02008 "net"
Matus Fabianb7a9ed72024-05-10 16:20:40 +02009 "net/http"
Adrian Villind74e4402024-11-04 13:16:24 +010010 "net/http/httptest"
Matus Fabiand086a362024-06-27 13:20:10 +020011 "net/http/httptrace"
12 "os"
Matus Fabiand46e6742024-07-31 16:08:40 +020013 "strconv"
Adrian Villin5a612a42024-08-28 09:34:14 +020014 "strings"
Matus Fabian5c4c1b62024-06-28 16:11:04 +020015 "sync"
Adrian Villincee15aa2024-03-14 11:42:55 -040016 "time"
17
Adrian Villin5a4c7a92024-09-26 11:24:34 +020018 "github.com/onsi/gomega/ghttp"
19 "github.com/onsi/gomega/gmeasure"
20
Adrian Villin4677d922024-06-14 09:32:39 +020021 . "fd.io/hs-test/infra"
Matus Fabian5c4c1b62024-06-28 16:11:04 +020022 . "github.com/onsi/ginkgo/v2"
Filip Tehlarc204c872022-12-21 08:59:16 +010023)
24
Adrian Villincee15aa2024-03-14 11:42:55 -040025func init() {
Adrian Villin4677d922024-06-14 09:32:39 +020026 RegisterVethTests(HttpCliTest, HttpCliConnectErrorTest)
Matus Fabianba9ea132024-07-12 11:07:17 +020027 RegisterSoloVethTests(HttpClientGetMemLeakTest)
Matus Fabiand086a362024-06-27 13:20:10 +020028 RegisterNoTopoTests(HeaderServerTest, HttpPersistentConnectionTest, HttpPipeliningTest,
Matus Fabianb7a9ed72024-05-10 16:20:40 +020029 HttpStaticMovedTest, HttpStaticNotFoundTest, HttpCliMethodNotAllowedTest,
Matus Fabian82ad9662024-06-04 19:00:00 +020030 HttpCliBadRequestTest, HttpStaticBuildInUrlGetIfStatsTest, HttpStaticBuildInUrlPostIfStatsTest,
31 HttpInvalidRequestLineTest, HttpMethodNotImplementedTest, HttpInvalidHeadersTest,
32 HttpContentLengthTest, HttpStaticBuildInUrlGetIfListTest, HttpStaticBuildInUrlGetVersionTest,
33 HttpStaticMacTimeTest, HttpStaticBuildInUrlGetVersionVerboseTest, HttpVersionNotSupportedTest,
34 HttpInvalidContentLengthTest, HttpInvalidTargetSyntaxTest, HttpStaticPathTraversalTest, HttpUriDecodeTest,
Adrian Villin5a612a42024-08-28 09:34:14 +020035 HttpHeadersTest, HttpStaticFileHandlerTest, HttpStaticFileHandlerDefaultMaxAgeTest, HttpClientTest,
36 HttpClientErrRespTest, HttpClientPostFormTest, HttpClientGet128kbResponseTest, HttpClientGetResponseBodyTest,
37 HttpClientGetNoResponseBodyTest, HttpClientPostFileTest, HttpClientPostFilePtrTest, HttpUnitTest,
Adrian Villind74e4402024-11-04 13:16:24 +010038 HttpRequestLineTest, HttpClientGetTimeout, HttpStaticFileHandlerWrkTest, HttpStaticUrlHandlerWrkTest, HttpConnTimeoutTest,
Matus Fabian82b3cc12024-11-21 17:01:45 +010039 HttpClientGetRepeat, HttpClientPostRepeat, HttpIgnoreH2UpgradeTest)
Matus Fabian9bb07622024-09-04 18:04:54 +020040 RegisterNoTopoSoloTests(HttpStaticPromTest, HttpGetTpsTest, HttpGetTpsInterruptModeTest, PromConcurrentConnectionsTest,
Matus Fabianfa5defd2024-10-02 09:00:19 +020041 PromMemLeakTest, HttpClientPostMemLeakTest, HttpInvalidClientRequestMemLeakTest, HttpPostTpsTest, HttpPostTpsInterruptModeTest,
42 PromConsecutiveConnectionsTest)
Adrian Villincee15aa2024-03-14 11:42:55 -040043}
44
Matus Fabian5409d332024-05-28 13:39:13 +020045const wwwRootPath = "/tmp/www_root"
Matus Fabianfa5defd2024-10-02 09:00:19 +020046const defaultHttpTimeout = time.Second * 10
Matus Fabian5409d332024-05-28 13:39:13 +020047
Matus Fabian2d1f0e62024-06-06 11:24:36 +020048func httpDownloadBenchmark(s *HstSuite, experiment *gmeasure.Experiment, data interface{}) {
49 url, isValid := data.(string)
Adrian Villin4677d922024-06-14 09:32:39 +020050 s.AssertEqual(true, isValid)
Matus Fabianfa5defd2024-10-02 09:00:19 +020051 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian2d1f0e62024-06-06 11:24:36 +020052 req, err := http.NewRequest("GET", url, nil)
Adrian Villin4677d922024-06-14 09:32:39 +020053 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian2d1f0e62024-06-06 11:24:36 +020054 t := time.Now()
55 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +020056 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian2d1f0e62024-06-06 11:24:36 +020057 defer resp.Body.Close()
Matus Fabiana647a832024-08-26 18:01:14 +020058 s.AssertHttpStatus(resp, 200)
Matus Fabian2d1f0e62024-06-06 11:24:36 +020059 _, err = io.ReadAll(resp.Body)
Matus Fabiand46e6742024-07-31 16:08:40 +020060 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian2d1f0e62024-06-06 11:24:36 +020061 duration := time.Since(t)
62 experiment.RecordValue("Download Speed", (float64(resp.ContentLength)/1024/1024)/duration.Seconds(), gmeasure.Units("MB/s"), gmeasure.Precision(2))
Matus Fabian2d1f0e62024-06-06 11:24:36 +020063}
64
Matus Fabian9bb07622024-09-04 18:04:54 +020065func HttpGetTpsInterruptModeTest(s *NoTopoSuite) {
66 HttpGetTpsTest(s)
Adrian Villin1fde9992024-06-24 08:14:05 -040067}
68
Matus Fabian9bb07622024-09-04 18:04:54 +020069func HttpGetTpsTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +020070 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +020071 serverAddress := s.VppAddr()
Matus Fabian2d1f0e62024-06-06 11:24:36 +020072 url := "http://" + serverAddress + ":8080/test_file_10M"
73
Adrian Villin4677d922024-06-14 09:32:39 +020074 vpp.Vppctl("http tps uri tcp://0.0.0.0/8080")
Matus Fabian2d1f0e62024-06-06 11:24:36 +020075
Matus Fabian9bb07622024-09-04 18:04:54 +020076 s.RunBenchmark("HTTP tps download 10M", 10, 0, httpDownloadBenchmark, url)
77}
78
79func httpUploadBenchmark(s *HstSuite, experiment *gmeasure.Experiment, data interface{}) {
80 url, isValid := data.(string)
81 s.AssertEqual(true, isValid)
82 body := make([]byte, 10485760)
83 _, err := rand.Read(body)
Matus Fabianfa5defd2024-10-02 09:00:19 +020084 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian9bb07622024-09-04 18:04:54 +020085 req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
86 s.AssertNil(err, fmt.Sprint(err))
87 t := time.Now()
88 resp, err := client.Do(req)
89 s.AssertNil(err, fmt.Sprint(err))
90 defer resp.Body.Close()
91 s.AssertHttpStatus(resp, 200)
92 _, err = io.ReadAll(resp.Body)
93 s.AssertNil(err, fmt.Sprint(err))
94 duration := time.Since(t)
95 experiment.RecordValue("Upload Speed", (float64(req.ContentLength)/1024/1024)/duration.Seconds(), gmeasure.Units("MB/s"), gmeasure.Precision(2))
96}
97
98func HttpPostTpsInterruptModeTest(s *NoTopoSuite) {
99 HttpPostTpsTest(s)
100}
101
102func HttpPostTpsTest(s *NoTopoSuite) {
103 vpp := s.GetContainerByName("vpp").VppInstance
104 serverAddress := s.VppAddr()
105 url := "http://" + serverAddress + ":8080/test_file_10M"
106
107 vpp.Vppctl("http tps uri tcp://0.0.0.0/8080")
108
109 s.RunBenchmark("HTTP tps upload 10M", 10, 0, httpUploadBenchmark, url)
Filip Tehlarb15a0002022-11-10 12:34:17 +0100110}
111
Matus Fabiand086a362024-06-27 13:20:10 +0200112func HttpPersistentConnectionTest(s *NoTopoSuite) {
113 // testing url handler app do not support multi-thread
114 s.SkipIfMultiWorker()
115 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200116 serverAddress := s.VppAddr()
Matus Fabiand086a362024-06-27 13:20:10 +0200117 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
118 s.Log(vpp.Vppctl("test-url-handler enable"))
119
120 transport := http.DefaultTransport
121 transport.(*http.Transport).Proxy = nil
122 transport.(*http.Transport).DisableKeepAlives = false
123 client := &http.Client{
124 Transport: transport,
125 Timeout: time.Second * 30,
126 CheckRedirect: func(req *http.Request, via []*http.Request) error {
127 return http.ErrUseLastResponse
128 }}
129
Matus Fabian56cefc82024-08-26 18:26:58 +0200130 body := []byte("{\"sandwich\": {\"spam\": 2, \"eggs\": 1}}")
131 req, err := http.NewRequest("POST", "http://"+serverAddress+":80/test3", bytes.NewBuffer(body))
Matus Fabiand086a362024-06-27 13:20:10 +0200132 s.AssertNil(err, fmt.Sprint(err))
133 resp, err := client.Do(req)
134 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiand086a362024-06-27 13:20:10 +0200135 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200136 s.AssertHttpStatus(resp, 200)
Matus Fabiand086a362024-06-27 13:20:10 +0200137 s.AssertEqual(false, resp.Close)
Matus Fabian56cefc82024-08-26 18:26:58 +0200138 s.AssertHttpContentLength(resp, int64(0))
Matus Fabiand086a362024-06-27 13:20:10 +0200139 o1 := vpp.Vppctl("show session verbose proto http state ready")
140 s.Log(o1)
Matus Fabian1c9d1de2024-11-13 16:31:53 +0100141 s.AssertContains(o1, "established")
Matus Fabiand086a362024-06-27 13:20:10 +0200142
Matus Fabian56cefc82024-08-26 18:26:58 +0200143 req, err = http.NewRequest("GET", "http://"+serverAddress+":80/test1", nil)
144 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiand086a362024-06-27 13:20:10 +0200145 clientTrace := &httptrace.ClientTrace{
146 GotConn: func(info httptrace.GotConnInfo) {
147 s.AssertEqual(true, info.Reused, "connection not reused")
148 },
149 }
Matus Fabian56cefc82024-08-26 18:26:58 +0200150 req = req.WithContext(httptrace.WithClientTrace(req.Context(), clientTrace))
151 resp, err = client.Do(req)
152 s.AssertNil(err, fmt.Sprint(err))
153 s.Log(DumpHttpResp(resp, true))
154 s.AssertHttpStatus(resp, 200)
155 s.AssertEqual(false, resp.Close)
156 s.AssertHttpBody(resp, "hello")
157 o2 := vpp.Vppctl("show session verbose proto http state ready")
158 s.Log(o2)
Matus Fabian1c9d1de2024-11-13 16:31:53 +0100159 s.AssertContains(o2, "established")
Matus Fabian56cefc82024-08-26 18:26:58 +0200160 s.AssertEqual(o1, o2)
161
162 req, err = http.NewRequest("GET", "http://"+serverAddress+":80/test2", nil)
Matus Fabiana647a832024-08-26 18:01:14 +0200163 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiand086a362024-06-27 13:20:10 +0200164 req = req.WithContext(httptrace.WithClientTrace(req.Context(), clientTrace))
165 resp, err = client.Do(req)
166 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiand086a362024-06-27 13:20:10 +0200167 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200168 s.AssertHttpStatus(resp, 200)
Matus Fabiand086a362024-06-27 13:20:10 +0200169 s.AssertEqual(false, resp.Close)
Matus Fabiana647a832024-08-26 18:01:14 +0200170 s.AssertHttpBody(resp, "some data")
Matus Fabian56cefc82024-08-26 18:26:58 +0200171 o2 = vpp.Vppctl("show session verbose proto http state ready")
Matus Fabiand086a362024-06-27 13:20:10 +0200172 s.Log(o2)
Matus Fabian1c9d1de2024-11-13 16:31:53 +0100173 s.AssertContains(o2, "established")
Matus Fabiand086a362024-06-27 13:20:10 +0200174 s.AssertEqual(o1, o2)
175}
176
177func HttpPipeliningTest(s *NoTopoSuite) {
178 // testing url handler app do not support multi-thread
179 s.SkipIfMultiWorker()
180 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200181 serverAddress := s.VppAddr()
Matus Fabiand086a362024-06-27 13:20:10 +0200182 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
183 s.Log(vpp.Vppctl("test-url-handler enable"))
184
185 req1 := "GET /test_delayed HTTP/1.1\r\nHost:" + serverAddress + ":80\r\nUser-Agent:test\r\n\r\n"
186 req2 := "GET /test1 HTTP/1.1\r\nHost:" + serverAddress + ":80\r\nUser-Agent:test\r\n\r\n"
187
188 conn, err := net.DialTimeout("tcp", serverAddress+":80", time.Second*30)
189 s.AssertNil(err, fmt.Sprint(err))
190 defer conn.Close()
191 err = conn.SetDeadline(time.Now().Add(time.Second * 15))
192 s.AssertNil(err, fmt.Sprint(err))
193 n, err := conn.Write([]byte(req1))
194 s.AssertNil(err, fmt.Sprint(err))
195 s.AssertEqual(n, len([]rune(req1)))
196 // send second request a bit later so first is already in progress
197 time.Sleep(500 * time.Millisecond)
198 n, err = conn.Write([]byte(req2))
199 s.AssertNil(err, fmt.Sprint(err))
200 s.AssertEqual(n, len([]rune(req2)))
201 reply := make([]byte, 1024)
Adrian Villin5a612a42024-08-28 09:34:14 +0200202 _, err = conn.Read(reply)
Matus Fabiand086a362024-06-27 13:20:10 +0200203 s.AssertNil(err, fmt.Sprint(err))
204 s.Log(string(reply))
205 s.AssertContains(string(reply), "delayed data", "first request response not received")
206 s.AssertNotContains(string(reply), "hello", "second request response received")
207 // make sure response for second request is not received later
208 _, err = conn.Read(reply)
209 s.AssertMatchError(err, os.ErrDeadlineExceeded, "second request response received")
210}
211
Adrian Villincee15aa2024-03-14 11:42:55 -0400212func HttpCliTest(s *VethsSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200213 serverContainer := s.GetContainerByName("server-vpp")
214 clientContainer := s.GetContainerByName("client-vpp")
Filip Tehlarb15a0002022-11-10 12:34:17 +0100215
Adrian Villin4677d922024-06-14 09:32:39 +0200216 serverVeth := s.GetInterfaceByName(ServerInterfaceName)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100217
Adrian Villin4677d922024-06-14 09:32:39 +0200218 serverContainer.VppInstance.Vppctl("http cli server")
Filip Tehlarb15a0002022-11-10 12:34:17 +0100219
Adrian Villin4677d922024-06-14 09:32:39 +0200220 uri := "http://" + serverVeth.Ip4AddressString() + "/80"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100221
Adrian Villin4677d922024-06-14 09:32:39 +0200222 o := clientContainer.VppInstance.Vppctl("http cli client" +
Filip Tehlard8944382023-11-27 13:28:36 +0100223 " uri " + uri + " query /show/vlib/graph")
Filip Tehlarb15a0002022-11-10 12:34:17 +0100224
Adrian Villin4677d922024-06-14 09:32:39 +0200225 s.Log(o)
226 s.AssertContains(o, "<html>", "<html> not found in the result!")
Matus Fabianba9ea132024-07-12 11:07:17 +0200227 s.AssertContains(o, "</html>", "</html> not found in the result!")
Filip Tehlarb15a0002022-11-10 12:34:17 +0100228}
Filip Tehlarc204c872022-12-21 08:59:16 +0100229
Matus Fabian3d008932024-05-13 10:29:11 +0200230func HttpCliConnectErrorTest(s *VethsSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200231 clientContainer := s.GetContainerByName("client-vpp")
Adrian Villin4677d922024-06-14 09:32:39 +0200232 serverVeth := s.GetInterfaceByName(ServerInterfaceName)
Matus Fabian3d008932024-05-13 10:29:11 +0200233
Adrian Villin4677d922024-06-14 09:32:39 +0200234 uri := "http://" + serverVeth.Ip4AddressString() + "/80"
Matus Fabian3d008932024-05-13 10:29:11 +0200235
Adrian Villin4677d922024-06-14 09:32:39 +0200236 o := clientContainer.VppInstance.Vppctl("http cli client" +
Matus Fabian3d008932024-05-13 10:29:11 +0200237 " uri " + uri + " query /show/vlib/graph")
238
Adrian Villin4677d922024-06-14 09:32:39 +0200239 s.Log(o)
240 s.AssertContains(o, "failed to connect")
Matus Fabian3d008932024-05-13 10:29:11 +0200241}
242
Matus Fabianba9ea132024-07-12 11:07:17 +0200243func HttpClientTest(s *NoTopoSuite) {
Matus Fabiana647a832024-08-26 18:01:14 +0200244 serverAddress := s.HostAddr()
Matus Fabianba9ea132024-07-12 11:07:17 +0200245 server := ghttp.NewUnstartedServer()
246 l, err := net.Listen("tcp", serverAddress+":80")
247 s.AssertNil(err, fmt.Sprint(err))
248 server.HTTPTestServer.Listener = l
249 server.AppendHandlers(
250 ghttp.CombineHandlers(
Matus Fabiand46e6742024-07-31 16:08:40 +0200251 s.LogHttpReq(true),
Matus Fabianba9ea132024-07-12 11:07:17 +0200252 ghttp.VerifyRequest("GET", "/test"),
253 ghttp.VerifyHeader(http.Header{"User-Agent": []string{"http_cli_client"}}),
Adrian Villin3601b322024-08-19 14:41:35 +0200254 ghttp.VerifyHeader(http.Header{"Accept": []string{"text/html"}}),
Matus Fabianba9ea132024-07-12 11:07:17 +0200255 ghttp.RespondWith(http.StatusOK, "<html><body><p>Hello</p></body></html>"),
256 ))
257 server.Start()
258 defer server.Close()
259 uri := "http://" + serverAddress + "/80"
260 vpp := s.GetContainerByName("vpp").VppInstance
261 o := vpp.Vppctl("http cli client uri " + uri + " query /test")
262
263 s.Log(o)
264 s.AssertContains(o, "<html>", "<html> not found in the result!")
265 s.AssertContains(o, "</html>", "</html> not found in the result!")
266}
267
268func HttpClientErrRespTest(s *NoTopoSuite) {
Matus Fabiana647a832024-08-26 18:01:14 +0200269 serverAddress := s.HostAddr()
Matus Fabianba9ea132024-07-12 11:07:17 +0200270 server := ghttp.NewUnstartedServer()
271 l, err := net.Listen("tcp", serverAddress+":80")
272 s.AssertNil(err, fmt.Sprint(err))
273 server.HTTPTestServer.Listener = l
274 server.AppendHandlers(
275 ghttp.CombineHandlers(
Matus Fabiand46e6742024-07-31 16:08:40 +0200276 s.LogHttpReq(true),
Matus Fabianba9ea132024-07-12 11:07:17 +0200277 ghttp.VerifyRequest("GET", "/test"),
278 ghttp.RespondWith(http.StatusNotFound, "404: Not Found"),
279 ))
280 server.Start()
281 defer server.Close()
282 uri := "http://" + serverAddress + "/80"
283 vpp := s.GetContainerByName("vpp").VppInstance
284 o := vpp.Vppctl("http cli client uri " + uri + " query /test")
285
286 s.Log(o)
287 s.AssertContains(o, "404: Not Found", "error not found in the result!")
288}
289
Matus Fabiand46e6742024-07-31 16:08:40 +0200290func HttpClientPostFormTest(s *NoTopoSuite) {
Matus Fabiana647a832024-08-26 18:01:14 +0200291 serverAddress := s.HostAddr()
Matus Fabiand46e6742024-07-31 16:08:40 +0200292 body := "field1=value1&field2=value2"
293
294 server := ghttp.NewUnstartedServer()
295 l, err := net.Listen("tcp", serverAddress+":80")
296 s.AssertNil(err, fmt.Sprint(err))
297 server.HTTPTestServer.Listener = l
298 server.AppendHandlers(
299 ghttp.CombineHandlers(
300 s.LogHttpReq(true),
301 ghttp.VerifyRequest("POST", "/test"),
Adrian Villin3601b322024-08-19 14:41:35 +0200302 ghttp.VerifyContentType("application/x-www-form-urlencoded"),
Adrian Villin5a612a42024-08-28 09:34:14 +0200303 ghttp.VerifyHeaderKV("Hello", "World"),
Matus Fabiand46e6742024-07-31 16:08:40 +0200304 ghttp.VerifyBody([]byte(body)),
305 ghttp.RespondWith(http.StatusOK, nil),
306 ))
307 server.Start()
308 defer server.Close()
309
310 uri := "http://" + serverAddress + "/80"
311 vpp := s.GetContainerByName("vpp").VppInstance
Adrian Villin5a612a42024-08-28 09:34:14 +0200312 o := vpp.Vppctl("http client post verbose header Hello:World uri " + uri + " target /test data " + body)
Matus Fabiand46e6742024-07-31 16:08:40 +0200313
314 s.Log(o)
Adrian Villin5a612a42024-08-28 09:34:14 +0200315 s.AssertContains(o, "200 OK")
316}
317
318func HttpClientGetResponseBodyTest(s *NoTopoSuite) {
319 response := "<body>hello world</body>"
320 size := len(response)
321 httpClientGet(s, response, size)
322}
323
324func HttpClientGet128kbResponseTest(s *NoTopoSuite) {
325 response := strings.Repeat("a", 128*1024)
326 size := len(response)
327 httpClientGet(s, response, size)
328}
329
330func HttpClientGetNoResponseBodyTest(s *NoTopoSuite) {
331 response := ""
332 httpClientGet(s, response, 0)
333}
334
335func httpClientGet(s *NoTopoSuite, response string, size int) {
336 serverAddress := s.HostAddr()
337 vpp := s.GetContainerByName("vpp").VppInstance
338
339 server := ghttp.NewUnstartedServer()
340 l, err := net.Listen("tcp", serverAddress+":80")
341 s.AssertNil(err, fmt.Sprint(err))
342 server.HTTPTestServer.Listener = l
343 server.AppendHandlers(
344 ghttp.CombineHandlers(
345 s.LogHttpReq(false),
346 ghttp.VerifyRequest("GET", "/test"),
347 ghttp.VerifyHeaderKV("Hello", "World"),
348 ghttp.VerifyHeaderKV("Test-H2", "Test-K2"),
349 ghttp.RespondWith(http.StatusOK, string(response), http.Header{"Content-Length": {strconv.Itoa(size)}}),
350 ))
351 server.Start()
352 defer server.Close()
353
354 uri := "http://" + serverAddress + "/80"
355 cmd := "http client use-ptr verbose header Hello:World header Test-H2:Test-K2 save-to response.txt uri " + uri + " target /test"
356
357 o := vpp.Vppctl(cmd)
358 outputLen := len(o)
359 if outputLen > 500 {
360 s.Log(o[:500])
361 s.Log("* HST Framework: output limited to 500 chars to avoid flooding the console. Output length: " + fmt.Sprint(outputLen))
362 } else {
363 s.Log(o)
364 }
365 s.AssertContains(o, "200 OK")
366 s.AssertContains(o, response)
367 s.AssertContains(o, "Content-Length: "+strconv.Itoa(size))
368
369 file_contents := vpp.Container.Exec(false, "cat /tmp/response.txt")
370 s.AssertContains(file_contents, response)
371}
372
Adrian Villind74e4402024-11-04 13:16:24 +0100373func startSimpleServer(s *NoTopoSuite, replyCount *int, serverAddress string) (server *httptest.Server) {
374 var err error
375 server = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
376 fmt.Fprintf(w, "Hello")
377 *replyCount++
378 }))
379 server.Listener, err = net.Listen("tcp", serverAddress+":80")
380 s.AssertNil(err, "Error while creating listener.")
381
382 server.Start()
383
384 return server
385}
386
387func HttpClientGetRepeat(s *NoTopoSuite) {
388 httpClientRepeat(s, "")
389}
390
391func HttpClientPostRepeat(s *NoTopoSuite) {
392 httpClientRepeat(s, "post")
393}
394
395func httpClientRepeat(s *NoTopoSuite, requestMethod string) {
396 replyCount := 0
397 vpp := s.GetContainerByName("vpp").VppInstance
398 serverAddress := s.HostAddr()
399 repeatAmount := 10000
400 server := startSimpleServer(s, &replyCount, serverAddress)
401 defer server.Close()
402
403 if requestMethod == "post" {
404 fileName := "/tmp/test_file.txt"
405 s.Log(vpp.Container.Exec(false, "fallocate -l 64 "+fileName))
406 s.Log(vpp.Container.Exec(false, "ls -la "+fileName))
407 requestMethod += " file /tmp/test_file.txt"
408 }
409
410 uri := "http://" + serverAddress + "/80"
411 cmd := fmt.Sprintf("http client %s use-ptr duration 10 header Hello:World uri %s target /index.html",
412 requestMethod, uri)
413
414 s.Log("Duration 10s")
415 o := vpp.Vppctl(cmd)
416 outputLen := len(o)
417 if outputLen > 500 {
418 s.Log(o[:500])
419 s.Log("* HST Framework: output limited to 500 chars to avoid flooding the console. Output length: " + fmt.Sprint(outputLen))
420 } else {
421 s.Log(o)
422 }
423 s.Log("Server response count: %d", replyCount)
424 s.AssertNotNil(o)
425 s.AssertNotContains(o, "error")
426 s.AssertGreaterThan(replyCount, 15000)
427
428 cmd = fmt.Sprintf("http client %s use-ptr repeat %d header Hello:World uri %s target /index.html",
429 requestMethod, repeatAmount, uri)
430
431 replyCount = 0
432 s.Log("Repeat %d", repeatAmount)
433 o = vpp.Vppctl(cmd)
434 outputLen = len(o)
435 if outputLen > 500 {
436 s.Log(o[:500])
437 s.Log("* HST Framework: output limited to 500 chars to avoid flooding the console. Output length: " + fmt.Sprint(outputLen))
438 } else {
439 s.Log(o)
440 }
441 s.Log("Server response count: %d", replyCount)
442 s.AssertNotNil(o)
443 s.AssertNotContains(o, "error")
444 s.AssertEqual(repeatAmount, replyCount)
445}
446
Adrian Villin5a612a42024-08-28 09:34:14 +0200447func HttpClientGetTimeout(s *NoTopoSuite) {
448 serverAddress := s.HostAddr()
449 vpp := s.GetContainerByName("vpp").VppInstance
450
451 server := ghttp.NewUnstartedServer()
452 l, err := net.Listen("tcp", serverAddress+":"+s.GetPortFromPpid())
453 s.AssertNil(err, fmt.Sprint(err))
454 server.HTTPTestServer.Listener = l
455 server.AppendHandlers(
456 ghttp.CombineHandlers(
457 s.LogHttpReq(false),
458 ghttp.VerifyRequest("GET", "/timeout"),
459 http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
460 time.Sleep(5 * time.Second)
461 }),
462 ghttp.RespondWith(http.StatusOK, nil),
463 ))
464 server.Start()
465 defer server.Close()
466 uri := "http://" + serverAddress + "/" + s.GetPortFromPpid()
467 cmd := "http client verbose timeout 1 uri " + uri + " target /timeout"
468
469 o := vpp.Vppctl(cmd)
470 s.Log(o)
471 s.AssertContains(o, "error: timeout")
Matus Fabiand46e6742024-07-31 16:08:40 +0200472}
473
474func httpClientPostFile(s *NoTopoSuite, usePtr bool, fileSize int) {
Matus Fabiana647a832024-08-26 18:01:14 +0200475 serverAddress := s.HostAddr()
Matus Fabiand46e6742024-07-31 16:08:40 +0200476 vpp := s.GetContainerByName("vpp").VppInstance
477 fileName := "/tmp/test_file.txt"
Adrian Villin2acdf1e2024-09-25 14:49:11 +0200478 s.Log(vpp.Container.Exec(false, "fallocate -l "+strconv.Itoa(fileSize)+" "+fileName))
479 s.Log(vpp.Container.Exec(false, "ls -la "+fileName))
Matus Fabiand46e6742024-07-31 16:08:40 +0200480
481 server := ghttp.NewUnstartedServer()
482 l, err := net.Listen("tcp", serverAddress+":80")
483 s.AssertNil(err, fmt.Sprint(err))
484 server.HTTPTestServer.Listener = l
485 server.AppendHandlers(
486 ghttp.CombineHandlers(
487 s.LogHttpReq(false),
488 ghttp.VerifyRequest("POST", "/test"),
489 ghttp.VerifyHeader(http.Header{"Content-Length": []string{strconv.Itoa(fileSize)}}),
Adrian Villin3601b322024-08-19 14:41:35 +0200490 ghttp.VerifyContentType("application/octet-stream"),
Matus Fabiand46e6742024-07-31 16:08:40 +0200491 ghttp.RespondWith(http.StatusOK, nil),
492 ))
493 server.Start()
494 defer server.Close()
495
496 uri := "http://" + serverAddress + "/80"
Adrian Villin5a612a42024-08-28 09:34:14 +0200497 cmd := "http client post verbose uri " + uri + " target /test file " + fileName
Matus Fabiand46e6742024-07-31 16:08:40 +0200498 if usePtr {
499 cmd += " use-ptr"
500 }
501 o := vpp.Vppctl(cmd)
502
503 s.Log(o)
Adrian Villin5a612a42024-08-28 09:34:14 +0200504 s.AssertContains(o, "200 OK")
Matus Fabiand46e6742024-07-31 16:08:40 +0200505}
506
507func HttpClientPostFileTest(s *NoTopoSuite) {
508 httpClientPostFile(s, false, 32768)
509}
510
511func HttpClientPostFilePtrTest(s *NoTopoSuite) {
512 httpClientPostFile(s, true, 131072)
513}
514
Matus Fabian769a3b72024-10-29 15:06:49 +0100515func HttpUnitTest(s *NoTopoSuite) {
516 vpp := s.GetContainerByName("vpp").VppInstance
517 o := vpp.Vppctl("test http all")
518 s.Log(o)
Matus Fabian3d0b4ab2024-12-06 17:45:53 +0100519 s.AssertContains(o, "SUCCESS")
Matus Fabiand58177c2024-08-08 12:50:32 +0200520}
521
Adrian Villincee15aa2024-03-14 11:42:55 -0400522func HttpStaticPromTest(s *NoTopoSuite) {
Filip Tehlarcc1475c2023-11-29 12:59:05 +0100523 query := "stats.prom"
Adrian Villin4677d922024-06-14 09:32:39 +0200524 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200525 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200526 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
527 s.Log(vpp.Vppctl("prom enable"))
Adrian Villincee15aa2024-03-14 11:42:55 -0400528 time.Sleep(time.Second * 5)
Matus Fabianfa5defd2024-10-02 09:00:19 +0200529 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200530 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/"+query, nil)
531 s.AssertNil(err, fmt.Sprint(err))
532 resp, err := client.Do(req)
533 s.AssertNil(err, fmt.Sprint(err))
534 defer resp.Body.Close()
535 s.Log(DumpHttpResp(resp, false))
Matus Fabiana647a832024-08-26 18:01:14 +0200536 s.AssertHttpStatus(resp, 200)
537 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/plain")
538 s.AssertGreaterThan(resp.ContentLength, 0)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200539 _, err = io.ReadAll(resp.Body)
Matus Fabiand46e6742024-07-31 16:08:40 +0200540 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200541}
542
Matus Fabianfa5defd2024-10-02 09:00:19 +0200543func promReq(s *NoTopoSuite, url string, timeout time.Duration) {
544 client := NewHttpClient(timeout)
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200545 req, err := http.NewRequest("GET", url, nil)
546 s.AssertNil(err, fmt.Sprint(err))
547 resp, err := client.Do(req)
548 s.AssertNil(err, fmt.Sprint(err))
549 defer resp.Body.Close()
Matus Fabiana647a832024-08-26 18:01:14 +0200550 s.AssertHttpStatus(resp, 200)
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200551 _, err = io.ReadAll(resp.Body)
Matus Fabiand46e6742024-07-31 16:08:40 +0200552 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200553}
554
Matus Fabianc712de52024-07-24 12:56:14 +0200555func promReqWg(s *NoTopoSuite, url string, wg *sync.WaitGroup) {
556 defer GinkgoRecover()
557 defer wg.Done()
Matus Fabianfa5defd2024-10-02 09:00:19 +0200558 promReq(s, url, defaultHttpTimeout)
Matus Fabianc712de52024-07-24 12:56:14 +0200559}
560
Matus Fabiand46e6742024-07-31 16:08:40 +0200561func PromConcurrentConnectionsTest(s *NoTopoSuite) {
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200562 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200563 serverAddress := s.VppAddr()
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200564 url := "http://" + serverAddress + ":80/stats.prom"
565
566 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
567 s.Log(vpp.Vppctl("prom enable"))
568 time.Sleep(time.Second * 5)
569
570 var wg sync.WaitGroup
571 for i := 0; i < 20; i++ {
572 wg.Add(1)
Matus Fabianc712de52024-07-24 12:56:14 +0200573 go promReqWg(s, url, &wg)
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200574 }
575 wg.Wait()
576 s.Log(vpp.Vppctl("show session verbose proto http"))
577}
578
Matus Fabianfa5defd2024-10-02 09:00:19 +0200579func PromConsecutiveConnectionsTest(s *NoTopoSuite) {
580 vpp := s.GetContainerByName("vpp").VppInstance
581 serverAddress := s.VppAddr()
582 url := "http://" + serverAddress + ":80/stats.prom"
583
584 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
585 s.Log(vpp.Vppctl("prom enable"))
586 time.Sleep(time.Second * 5)
587
588 for i := 0; i < 1000; i++ {
589 promReq(s, url, time.Millisecond*500)
590 }
591}
592
Matus Fabianc712de52024-07-24 12:56:14 +0200593func PromMemLeakTest(s *NoTopoSuite) {
594 s.SkipUnlessLeakCheck()
595
596 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200597 serverAddress := s.VppAddr()
Matus Fabianc712de52024-07-24 12:56:14 +0200598 url := "http://" + serverAddress + ":80/stats.prom"
599
600 /* no goVPP less noise */
601 vpp.Disconnect()
602
603 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
604 s.Log(vpp.Vppctl("prom enable"))
605 time.Sleep(time.Second * 3)
606
607 /* warmup request (FIB) */
Matus Fabianfa5defd2024-10-02 09:00:19 +0200608 promReq(s, url, defaultHttpTimeout)
Matus Fabianc712de52024-07-24 12:56:14 +0200609
610 vpp.EnableMemoryTrace()
611 traces1, err := vpp.GetMemoryTrace()
612 s.AssertNil(err, fmt.Sprint(err))
613
614 /* collect stats couple of times */
615 for i := 0; i < 5; i++ {
616 time.Sleep(time.Second * 1)
Matus Fabianfa5defd2024-10-02 09:00:19 +0200617 promReq(s, url, defaultHttpTimeout)
Matus Fabianc712de52024-07-24 12:56:14 +0200618 }
619
620 /* let's give it some time to clean up sessions */
621 time.Sleep(time.Second * 5)
622
623 traces2, err := vpp.GetMemoryTrace()
624 s.AssertNil(err, fmt.Sprint(err))
625 vpp.MemLeakCheck(traces1, traces2)
626}
627
Matus Fabianba9ea132024-07-12 11:07:17 +0200628func HttpClientGetMemLeakTest(s *VethsSuite) {
629 s.SkipUnlessLeakCheck()
630
631 serverContainer := s.GetContainerByName("server-vpp").VppInstance
632 clientContainer := s.GetContainerByName("client-vpp").VppInstance
633 serverVeth := s.GetInterfaceByName(ServerInterfaceName)
634
635 /* no goVPP less noise */
636 clientContainer.Disconnect()
637
638 serverContainer.Vppctl("http cli server")
639
640 uri := "http://" + serverVeth.Ip4AddressString() + "/80"
641
642 /* warmup request (FIB) */
643 clientContainer.Vppctl("http cli client uri " + uri + " query /show/version")
644
Matus Fabiand46e6742024-07-31 16:08:40 +0200645 /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */
646 time.Sleep(time.Second * 12)
647
Matus Fabianba9ea132024-07-12 11:07:17 +0200648 clientContainer.EnableMemoryTrace()
649 traces1, err := clientContainer.GetMemoryTrace()
650 s.AssertNil(err, fmt.Sprint(err))
651
652 clientContainer.Vppctl("http cli client uri " + uri + " query /show/vlib/graph")
653
654 /* let's give it some time to clean up sessions */
655 time.Sleep(time.Second * 12)
656
657 traces2, err := clientContainer.GetMemoryTrace()
658 s.AssertNil(err, fmt.Sprint(err))
659 clientContainer.MemLeakCheck(traces1, traces2)
660}
661
Matus Fabiand46e6742024-07-31 16:08:40 +0200662func HttpClientPostMemLeakTest(s *NoTopoSuite) {
663 s.SkipUnlessLeakCheck()
664
Matus Fabiana647a832024-08-26 18:01:14 +0200665 serverAddress := s.HostAddr()
Matus Fabiand46e6742024-07-31 16:08:40 +0200666 body := "field1=value1&field2=value2"
667
668 uri := "http://" + serverAddress + "/80"
669 vpp := s.GetContainerByName("vpp").VppInstance
670
671 /* no goVPP less noise */
672 vpp.Disconnect()
673
674 server := ghttp.NewUnstartedServer()
675 l, err := net.Listen("tcp", serverAddress+":80")
676 s.AssertNil(err, fmt.Sprint(err))
677 server.HTTPTestServer.Listener = l
678 server.AppendHandlers(
679 ghttp.CombineHandlers(
680 ghttp.VerifyRequest("POST", "/test"),
681 ghttp.RespondWith(http.StatusOK, nil),
682 ),
683 ghttp.CombineHandlers(
684 ghttp.VerifyRequest("POST", "/test"),
685 ghttp.RespondWith(http.StatusOK, nil),
686 ),
687 )
688 server.Start()
689 defer server.Close()
690
691 /* warmup request (FIB) */
692 vpp.Vppctl("http post uri " + uri + " target /test data " + body)
693
694 /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */
695 time.Sleep(time.Second * 12)
696
697 vpp.EnableMemoryTrace()
698 traces1, err := vpp.GetMemoryTrace()
699 s.AssertNil(err, fmt.Sprint(err))
700
701 vpp.Vppctl("http post uri " + uri + " target /test data " + body)
702
703 /* let's give it some time to clean up sessions */
704 time.Sleep(time.Second * 12)
705
706 traces2, err := vpp.GetMemoryTrace()
707 s.AssertNil(err, fmt.Sprint(err))
708 vpp.MemLeakCheck(traces1, traces2)
709}
710
Matus Fabian55467552024-08-06 15:55:26 +0200711func HttpInvalidClientRequestMemLeakTest(s *NoTopoSuite) {
712 s.SkipUnlessLeakCheck()
713
714 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200715 serverAddress := s.VppAddr()
Matus Fabian55467552024-08-06 15:55:26 +0200716
717 /* no goVPP less noise */
718 vpp.Disconnect()
719
720 vpp.Vppctl("http cli server")
721
722 /* warmup request (FIB) */
723 _, err := TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n")
724 s.AssertNil(err, fmt.Sprint(err))
725
726 /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */
727 time.Sleep(time.Second * 12)
728
729 vpp.EnableMemoryTrace()
730 traces1, err := vpp.GetMemoryTrace()
731 s.AssertNil(err, fmt.Sprint(err))
732
733 _, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n")
734 s.AssertNil(err, fmt.Sprint(err))
735
736 /* let's give it some time to clean up sessions */
737 time.Sleep(time.Second * 12)
738
739 traces2, err := vpp.GetMemoryTrace()
740 s.AssertNil(err, fmt.Sprint(err))
741 vpp.MemLeakCheck(traces1, traces2)
742
743}
744
Matus Fabian6885d5f2024-10-08 13:58:37 +0200745func runWrkPerf(s *NoTopoSuite) {
746 nConnections := 1000
747 serverAddress := s.VppAddr()
748
749 wrkCont := s.GetContainerByName("wrk")
750 args := fmt.Sprintf("-c %d -t 2 -d 30s http://%s:80/64B", nConnections, serverAddress)
751 wrkCont.ExtraRunningArgs = args
752 wrkCont.Run()
753 s.Log("Please wait for 30s, test is running.")
754 o, err := wrkCont.GetOutput()
755 s.Log(o)
756 s.AssertEmpty(err, "err: '%s'", err)
757}
758
759func HttpStaticFileHandlerWrkTest(s *NoTopoSuite) {
760 vpp := s.GetContainerByName("vpp").VppInstance
761 serverAddress := s.VppAddr()
Matus Fabian15106be2024-10-08 20:06:32 +0200762 vpp.Container.Exec(false, "mkdir -p "+wwwRootPath)
Matus Fabian6885d5f2024-10-08 13:58:37 +0200763 content := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
764 err := vpp.Container.CreateFile(wwwRootPath+"/64B", content)
765 s.AssertNil(err, fmt.Sprint(err))
766 s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 private-segment-size 256m"))
767 runWrkPerf(s)
768}
769
770func HttpStaticUrlHandlerWrkTest(s *NoTopoSuite) {
771 vpp := s.GetContainerByName("vpp").VppInstance
772 serverAddress := s.VppAddr()
773 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers private-segment-size 256m"))
774 s.Log(vpp.Vppctl("test-url-handler enable"))
775 runWrkPerf(s)
776}
777
Adrian Villin86fa9432024-08-08 08:56:34 +0200778func HttpStaticFileHandlerDefaultMaxAgeTest(s *NoTopoSuite) {
779 HttpStaticFileHandlerTestFunction(s, "default")
780}
781
Matus Fabiand46e6742024-07-31 16:08:40 +0200782func HttpStaticFileHandlerTest(s *NoTopoSuite) {
Adrian Villin86fa9432024-08-08 08:56:34 +0200783 HttpStaticFileHandlerTestFunction(s, "123")
784}
785
786func HttpStaticFileHandlerTestFunction(s *NoTopoSuite, max_age string) {
Matus Fabiana647a832024-08-26 18:01:14 +0200787 var maxAgeFormatted string
Adrian Villin86fa9432024-08-08 08:56:34 +0200788 if max_age == "default" {
Matus Fabiana647a832024-08-26 18:01:14 +0200789 maxAgeFormatted = ""
Adrian Villin86fa9432024-08-08 08:56:34 +0200790 max_age = "600"
791 } else {
Matus Fabiana647a832024-08-26 18:01:14 +0200792 maxAgeFormatted = "max-age " + max_age
Adrian Villin86fa9432024-08-08 08:56:34 +0200793 }
794
Matus Fabianba9ea132024-07-12 11:07:17 +0200795 content := "<html><body><p>Hello</p></body></html>"
796 content2 := "<html><body><p>Page</p></body></html>"
Adrian Villin7e6606a2024-08-16 15:23:28 +0200797
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200798 vpp := s.GetContainerByName("vpp").VppInstance
Adrian Villin2acdf1e2024-09-25 14:49:11 +0200799 vpp.Container.Exec(false, "mkdir -p "+wwwRootPath)
Matus Fabiand46e6742024-07-31 16:08:40 +0200800 err := vpp.Container.CreateFile(wwwRootPath+"/index.html", content)
801 s.AssertNil(err, fmt.Sprint(err))
802 err = vpp.Container.CreateFile(wwwRootPath+"/page.html", content2)
803 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiana647a832024-08-26 18:01:14 +0200804 serverAddress := s.VppAddr()
805 s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug cache-size 2m " + maxAgeFormatted))
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200806
Matus Fabianfa5defd2024-10-02 09:00:19 +0200807 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200808 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/index.html", nil)
809 s.AssertNil(err, fmt.Sprint(err))
810 resp, err := client.Do(req)
811 s.AssertNil(err, fmt.Sprint(err))
Adrian Villin7e6606a2024-08-16 15:23:28 +0200812
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200813 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200814 s.AssertHttpStatus(resp, 200)
815 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html")
816 s.AssertHttpHeaderWithValue(resp, "Cache-Control", "max-age="+max_age)
817 parsedTime, err := time.Parse(time.RFC1123, resp.Header.Get("Last-Modified"))
Matus Fabiand46e6742024-07-31 16:08:40 +0200818 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiana647a832024-08-26 18:01:14 +0200819 s.AssertTimeEqualWithinThreshold(parsedTime, time.Now(), time.Minute*5)
820 s.AssertEqual(len(resp.Header.Get("Last-Modified")), 29)
821 s.AssertHttpContentLength(resp, int64(len([]rune(content))))
822 s.AssertHttpBody(resp, content)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200823 o := vpp.Vppctl("show http static server cache verbose")
824 s.Log(o)
825 s.AssertContains(o, "index.html")
826 s.AssertNotContains(o, "page.html")
827
828 resp, err = client.Do(req)
829 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200830 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200831 s.AssertHttpStatus(resp, 200)
832 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html")
833 s.AssertHttpHeaderWithValue(resp, "Cache-Control", "max-age="+max_age)
834 s.AssertHttpContentLength(resp, int64(len([]rune(content))))
835 s.AssertHttpBody(resp, content)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200836
837 req, err = http.NewRequest("GET", "http://"+serverAddress+":80/page.html", nil)
838 s.AssertNil(err, fmt.Sprint(err))
839 resp, err = client.Do(req)
840 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200841 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200842 s.AssertHttpStatus(resp, 200)
843 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html")
844 s.AssertHttpHeaderWithValue(resp, "Cache-Control", "max-age="+max_age)
845 s.AssertHttpContentLength(resp, int64(len([]rune(content2))))
846 s.AssertHttpBody(resp, content2)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200847 o = vpp.Vppctl("show http static server cache verbose")
848 s.Log(o)
849 s.AssertContains(o, "index.html")
850 s.AssertContains(o, "page.html")
Filip Tehlarcc1475c2023-11-29 12:59:05 +0100851}
852
Matus Fabian5409d332024-05-28 13:39:13 +0200853func HttpStaticPathTraversalTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200854 vpp := s.GetContainerByName("vpp").VppInstance
Adrian Villin2acdf1e2024-09-25 14:49:11 +0200855 vpp.Container.Exec(false, "mkdir -p "+wwwRootPath)
856 vpp.Container.Exec(false, "mkdir -p "+"/tmp/secret_folder")
Matus Fabiand46e6742024-07-31 16:08:40 +0200857 err := vpp.Container.CreateFile("/tmp/secret_folder/secret_file.txt", "secret")
858 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiana647a832024-08-26 18:01:14 +0200859 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200860 s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug"))
Matus Fabian5409d332024-05-28 13:39:13 +0200861
Matus Fabianfa5defd2024-10-02 09:00:19 +0200862 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian5409d332024-05-28 13:39:13 +0200863 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/../secret_folder/secret_file.txt", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200864 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian5409d332024-05-28 13:39:13 +0200865 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200866 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian5409d332024-05-28 13:39:13 +0200867 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200868 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200869 s.AssertHttpStatus(resp, 404)
870 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
871 s.AssertHttpHeaderNotPresent(resp, "Cache-Control")
872 s.AssertHttpContentLength(resp, int64(0))
Matus Fabian5409d332024-05-28 13:39:13 +0200873}
874
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200875func HttpStaticMovedTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200876 vpp := s.GetContainerByName("vpp").VppInstance
Adrian Villin2acdf1e2024-09-25 14:49:11 +0200877 vpp.Container.Exec(false, "mkdir -p "+wwwRootPath+"/tmp.aaa")
Matus Fabiand46e6742024-07-31 16:08:40 +0200878 err := vpp.Container.CreateFile(wwwRootPath+"/tmp.aaa/index.html", "<html><body><p>Hello</p></body></html>")
879 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiana647a832024-08-26 18:01:14 +0200880 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200881 s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug"))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200882
Matus Fabianfa5defd2024-10-02 09:00:19 +0200883 client := NewHttpClient(defaultHttpTimeout)
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200884 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/tmp.aaa", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200885 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200886 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200887 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200888 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200889 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200890 s.AssertHttpStatus(resp, 301)
891 s.AssertHttpHeaderWithValue(resp, "Location", "http://"+serverAddress+"/tmp.aaa/index.html")
892 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
893 s.AssertHttpHeaderNotPresent(resp, "Cache-Control")
894 s.AssertHttpContentLength(resp, int64(0))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200895}
896
897func HttpStaticNotFoundTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200898 vpp := s.GetContainerByName("vpp").VppInstance
Adrian Villin2acdf1e2024-09-25 14:49:11 +0200899 vpp.Container.Exec(false, "mkdir -p "+wwwRootPath)
Matus Fabiana647a832024-08-26 18:01:14 +0200900 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200901 s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug"))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200902
Matus Fabianfa5defd2024-10-02 09:00:19 +0200903 client := NewHttpClient(defaultHttpTimeout)
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200904 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/notfound.html", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200905 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian595d8cb2024-05-17 11:28:43 +0200906 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200907 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200908 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200909 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200910 s.AssertHttpStatus(resp, 404)
911 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
912 s.AssertHttpHeaderNotPresent(resp, "Cache-Control")
913 s.AssertHttpContentLength(resp, int64(0))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200914}
915
916func HttpCliMethodNotAllowedTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200917 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200918 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200919 vpp.Vppctl("http cli server")
Matus Fabian616201a2024-05-02 11:17:15 +0200920
Matus Fabianfa5defd2024-10-02 09:00:19 +0200921 client := NewHttpClient(defaultHttpTimeout)
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200922 req, err := http.NewRequest("POST", "http://"+serverAddress+":80/test", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200923 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian595d8cb2024-05-17 11:28:43 +0200924 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200925 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200926 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200927 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200928 s.AssertHttpStatus(resp, 405)
929 s.AssertHttpHeaderWithValue(resp, "Allow", "GET", "server MUST generate an Allow header")
930 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
931 s.AssertHttpContentLength(resp, int64(0))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200932}
933
934func HttpCliBadRequestTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200935 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200936 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200937 vpp.Vppctl("http cli server")
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200938
Matus Fabianfa5defd2024-10-02 09:00:19 +0200939 client := NewHttpClient(defaultHttpTimeout)
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200940 req, err := http.NewRequest("GET", "http://"+serverAddress+":80", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200941 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian595d8cb2024-05-17 11:28:43 +0200942 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200943 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200944 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200945 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200946 s.AssertHttpStatus(resp, 400)
947 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
948 s.AssertHttpContentLength(resp, int64(0))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200949}
950
Matus Fabian82ad9662024-06-04 19:00:00 +0200951func HttpStaticBuildInUrlGetVersionTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200952 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200953 serverAddress := s.VppAddr()
Matus Fabian84167222024-10-17 13:41:51 +0200954 s.Log(vpp.Vppctl("http static server uri tls://" + serverAddress + "/80 url-handlers debug"))
Matus Fabian82ad9662024-06-04 19:00:00 +0200955
Matus Fabianfa5defd2024-10-02 09:00:19 +0200956 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian84167222024-10-17 13:41:51 +0200957 req, err := http.NewRequest("GET", "https://"+serverAddress+":80/version.json", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200958 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200959 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200960 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200961 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200962 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200963 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +0200964 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +0200965 s.AssertNil(err, fmt.Sprint(err))
966 s.AssertContains(string(data), "vpp_details")
967 s.AssertContains(string(data), "version")
968 s.AssertContains(string(data), "build_date")
969 s.AssertNotContains(string(data), "build_by")
970 s.AssertNotContains(string(data), "build_host")
971 s.AssertNotContains(string(data), "build_dir")
Matus Fabiana647a832024-08-26 18:01:14 +0200972 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
Matus Fabian82ad9662024-06-04 19:00:00 +0200973}
974
975func HttpStaticBuildInUrlGetVersionVerboseTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200976 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200977 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200978 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabian82ad9662024-06-04 19:00:00 +0200979
Matus Fabianfa5defd2024-10-02 09:00:19 +0200980 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian82ad9662024-06-04 19:00:00 +0200981 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/version.json?verbose=true", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200982 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200983 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200984 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200985 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200986 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200987 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +0200988 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +0200989 s.AssertNil(err, fmt.Sprint(err))
990 s.AssertContains(string(data), "vpp_details")
991 s.AssertContains(string(data), "version")
992 s.AssertContains(string(data), "build_date")
993 s.AssertContains(string(data), "build_by")
994 s.AssertContains(string(data), "build_host")
995 s.AssertContains(string(data), "build_dir")
Matus Fabiana647a832024-08-26 18:01:14 +0200996 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
Matus Fabian82ad9662024-06-04 19:00:00 +0200997}
998
999func HttpStaticBuildInUrlGetIfListTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001000 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001001 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001002 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabian82ad9662024-06-04 19:00:00 +02001003
Matus Fabianfa5defd2024-10-02 09:00:19 +02001004 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian82ad9662024-06-04 19:00:00 +02001005 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/interface_list.json", nil)
Adrian Villin4677d922024-06-14 09:32:39 +02001006 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001007 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +02001008 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001009 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +02001010 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +02001011 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +02001012 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +02001013 s.AssertNil(err, fmt.Sprint(err))
1014 s.AssertContains(string(data), "interface_list")
Matus Fabiana647a832024-08-26 18:01:14 +02001015 s.AssertContains(string(data), s.VppIfName())
1016 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
Matus Fabian82ad9662024-06-04 19:00:00 +02001017}
1018
1019func HttpStaticBuildInUrlGetIfStatsTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001020 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001021 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001022 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabian82ad9662024-06-04 19:00:00 +02001023
Matus Fabianfa5defd2024-10-02 09:00:19 +02001024 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian82ad9662024-06-04 19:00:00 +02001025 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/interface_stats.json", nil)
Adrian Villin4677d922024-06-14 09:32:39 +02001026 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001027 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +02001028 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001029 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +02001030 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +02001031 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +02001032 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +02001033 s.AssertNil(err, fmt.Sprint(err))
1034 s.AssertContains(string(data), "interface_stats")
1035 s.AssertContains(string(data), "local0")
Matus Fabiana647a832024-08-26 18:01:14 +02001036 s.AssertContains(string(data), s.VppIfName())
1037 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
Matus Fabian82ad9662024-06-04 19:00:00 +02001038}
1039
1040func validatePostInterfaceStats(s *NoTopoSuite, data string) {
Adrian Villin4677d922024-06-14 09:32:39 +02001041 s.AssertContains(data, "interface_stats")
Matus Fabiana647a832024-08-26 18:01:14 +02001042 s.AssertContains(data, s.VppIfName())
Adrian Villin4677d922024-06-14 09:32:39 +02001043 s.AssertNotContains(data, "error")
1044 s.AssertNotContains(data, "local0")
Matus Fabian82ad9662024-06-04 19:00:00 +02001045}
1046
1047func HttpStaticBuildInUrlPostIfStatsTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001048 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001049 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001050 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabiana647a832024-08-26 18:01:14 +02001051 body := []byte(s.VppIfName())
Matus Fabian82ad9662024-06-04 19:00:00 +02001052
Matus Fabianfa5defd2024-10-02 09:00:19 +02001053 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian82ad9662024-06-04 19:00:00 +02001054 req, err := http.NewRequest("POST",
1055 "http://"+serverAddress+":80/interface_stats.json", bytes.NewBuffer(body))
Adrian Villin4677d922024-06-14 09:32:39 +02001056 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001057 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +02001058 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001059 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +02001060 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +02001061 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +02001062 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +02001063 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001064 validatePostInterfaceStats(s, string(data))
Matus Fabiana647a832024-08-26 18:01:14 +02001065 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
Matus Fabian82ad9662024-06-04 19:00:00 +02001066}
1067
1068func HttpStaticMacTimeTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001069 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001070 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001071 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabiana647a832024-08-26 18:01:14 +02001072 s.Log(vpp.Vppctl("mactime enable-disable " + s.VppIfName()))
Matus Fabian82ad9662024-06-04 19:00:00 +02001073
Matus Fabianfa5defd2024-10-02 09:00:19 +02001074 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian82ad9662024-06-04 19:00:00 +02001075 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/mactime.json", nil)
Adrian Villin4677d922024-06-14 09:32:39 +02001076 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001077 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +02001078 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001079 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +02001080 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +02001081 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +02001082 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +02001083 s.AssertNil(err, fmt.Sprint(err))
1084 s.AssertContains(string(data), "mactime")
Matus Fabiana647a832024-08-26 18:01:14 +02001085 s.AssertContains(string(data), s.HostAddr())
Adrian Villin4677d922024-06-14 09:32:39 +02001086 s.AssertContains(string(data), s.GetInterfaceByName(TapInterfaceName).HwAddress.String())
Matus Fabiana647a832024-08-26 18:01:14 +02001087 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
1088 parsedTime, err := time.Parse(time.RFC1123, resp.Header.Get("Date"))
1089 s.AssertNil(err, fmt.Sprint(err))
1090 s.AssertTimeEqualWithinThreshold(parsedTime, time.Now(), time.Minute*5)
Adrian Villin7e6606a2024-08-16 15:23:28 +02001091 s.AssertEqual(len(resp.Header.Get("Date")), 29)
Matus Fabian82ad9662024-06-04 19:00:00 +02001092}
1093
1094func HttpInvalidRequestLineTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001095 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001096 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001097 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001098
Matus Fabian69123a32024-08-23 17:35:50 +02001099 resp, err := TcpSendReceive(serverAddress+":80", " GET / HTTP/1.1")
1100 s.AssertNil(err, fmt.Sprint(err))
1101 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid request line start not allowed")
1102
1103 resp, err = TcpSendReceive(serverAddress+":80", "\rGET / HTTP/1.1")
1104 s.AssertNil(err, fmt.Sprint(err))
1105 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid request line start not allowed")
1106
1107 resp, err = TcpSendReceive(serverAddress+":80", "\nGET / HTTP/1.1")
1108 s.AssertNil(err, fmt.Sprint(err))
1109 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid request line start not allowed")
1110
1111 resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1")
Adrian Villin4677d922024-06-14 09:32:39 +02001112 s.AssertNil(err, fmt.Sprint(err))
1113 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid framing not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001114
Adrian Villin4677d922024-06-14 09:32:39 +02001115 resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n")
1116 s.AssertNil(err, fmt.Sprint(err))
1117 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid framing not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001118
Adrian Villin4677d922024-06-14 09:32:39 +02001119 resp, err = TcpSendReceive(serverAddress+":80", "GET /\r\n\r\n")
1120 s.AssertNil(err, fmt.Sprint(err))
1121 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "HTTP-version must be present")
Matus Fabian82ad9662024-06-04 19:00:00 +02001122
Adrian Villin4677d922024-06-14 09:32:39 +02001123 resp, err = TcpSendReceive(serverAddress+":80", "GET HTTP/1.1\r\n\r\n")
1124 s.AssertNil(err, fmt.Sprint(err))
1125 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "request-target must be present")
Matus Fabian82ad9662024-06-04 19:00:00 +02001126
Adrian Villin4677d922024-06-14 09:32:39 +02001127 resp, err = TcpSendReceive(serverAddress+":80", "GET HTTP/1.1\r\n\r\n")
1128 s.AssertNil(err, fmt.Sprint(err))
1129 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "request-target must be present")
Matus Fabian82ad9662024-06-04 19:00:00 +02001130
Adrian Villin4677d922024-06-14 09:32:39 +02001131 resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/x\r\n\r\n")
1132 s.AssertNil(err, fmt.Sprint(err))
1133 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'HTTP/x' invalid http version not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001134
Adrian Villin4677d922024-06-14 09:32:39 +02001135 resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP1.1\r\n\r\n")
1136 s.AssertNil(err, fmt.Sprint(err))
1137 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'HTTP1.1' invalid http version not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001138}
1139
Matus Fabian69123a32024-08-23 17:35:50 +02001140func HttpRequestLineTest(s *NoTopoSuite) {
1141 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001142 serverAddress := s.VppAddr()
Matus Fabian69123a32024-08-23 17:35:50 +02001143 vpp.Vppctl("http cli server")
1144
1145 resp, err := TcpSendReceive(serverAddress+":80", "\r\nGET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent:test\r\n\r\n")
1146 s.AssertNil(err, fmt.Sprint(err))
1147 s.AssertContains(resp, "HTTP/1.1 200 OK")
1148 s.AssertContains(resp, "<html>", "html content not found")
1149}
1150
Matus Fabian82ad9662024-06-04 19:00:00 +02001151func HttpInvalidTargetSyntaxTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001152 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001153 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001154 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabian82ad9662024-06-04 19:00:00 +02001155
Adrian Villin4677d922024-06-14 09:32:39 +02001156 resp, err := TcpSendReceive(serverAddress+":80", "GET /interface|stats.json HTTP/1.1\r\n\r\n")
1157 s.AssertNil(err, fmt.Sprint(err))
1158 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'|' not allowed in target path")
Matus Fabian82ad9662024-06-04 19:00:00 +02001159
Adrian Villin4677d922024-06-14 09:32:39 +02001160 resp, err = TcpSendReceive(serverAddress+":80", "GET /interface#stats.json HTTP/1.1\r\n\r\n")
1161 s.AssertNil(err, fmt.Sprint(err))
1162 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'#' not allowed in target path")
Matus Fabian82ad9662024-06-04 19:00:00 +02001163
Adrian Villin4677d922024-06-14 09:32:39 +02001164 resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%stats.json HTTP/1.1\r\n\r\n")
1165 s.AssertNil(err, fmt.Sprint(err))
1166 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +02001167 "after '%' there must be two hex-digit characters in target path")
1168
Adrian Villin4677d922024-06-14 09:32:39 +02001169 resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%1stats.json HTTP/1.1\r\n\r\n")
1170 s.AssertNil(err, fmt.Sprint(err))
1171 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +02001172 "after '%' there must be two hex-digit characters in target path")
1173
Adrian Villin4677d922024-06-14 09:32:39 +02001174 resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%Bstats.json HTTP/1.1\r\n\r\n")
1175 s.AssertNil(err, fmt.Sprint(err))
1176 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +02001177 "after '%' there must be two hex-digit characters in target path")
1178
Adrian Villin4677d922024-06-14 09:32:39 +02001179 resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%stats.json%B HTTP/1.1\r\n\r\n")
1180 s.AssertNil(err, fmt.Sprint(err))
1181 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +02001182 "after '%' there must be two hex-digit characters in target path")
1183
Adrian Villin4677d922024-06-14 09:32:39 +02001184 resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose>true HTTP/1.1\r\n\r\n")
1185 s.AssertNil(err, fmt.Sprint(err))
1186 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'>' not allowed in target query")
Matus Fabian82ad9662024-06-04 19:00:00 +02001187
Adrian Villin4677d922024-06-14 09:32:39 +02001188 resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose%true HTTP/1.1\r\n\r\n")
1189 s.AssertNil(err, fmt.Sprint(err))
1190 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +02001191 "after '%' there must be two hex-digit characters in target query")
1192
Adrian Villin4677d922024-06-14 09:32:39 +02001193 resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose=%1 HTTP/1.1\r\n\r\n")
1194 s.AssertNil(err, fmt.Sprint(err))
1195 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +02001196 "after '%' there must be two hex-digit characters in target query")
1197}
1198
1199func HttpInvalidContentLengthTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001200 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001201 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001202 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001203
Adrian Villin4677d922024-06-14 09:32:39 +02001204 resp, err := TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length:\r\n\r\n")
1205 s.AssertNil(err, fmt.Sprint(err))
1206 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Content-Length value must be present")
Matus Fabian82ad9662024-06-04 19:00:00 +02001207
Adrian Villin4677d922024-06-14 09:32:39 +02001208 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length: \r\n\r\n")
1209 s.AssertNil(err, fmt.Sprint(err))
1210 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Content-Length value must be present")
Matus Fabian82ad9662024-06-04 19:00:00 +02001211
Adrian Villin4677d922024-06-14 09:32:39 +02001212 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length: a\r\n\r\n")
1213 s.AssertNil(err, fmt.Sprint(err))
1214 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +02001215 "Content-Length value other than digit not allowed")
1216}
1217
1218func HttpContentLengthTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001219 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001220 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001221 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabiana647a832024-08-26 18:01:14 +02001222 ifName := s.VppIfName()
Matus Fabian82ad9662024-06-04 19:00:00 +02001223
Adrian Villin4677d922024-06-14 09:32:39 +02001224 resp, err := TcpSendReceive(serverAddress+":80",
Matus Fabian82ad9662024-06-04 19:00:00 +02001225 "POST /interface_stats.json HTTP/1.1\r\nContent-Length:4\r\n\r\n"+ifName)
Adrian Villin4677d922024-06-14 09:32:39 +02001226 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001227 validatePostInterfaceStats(s, resp)
1228
Adrian Villin4677d922024-06-14 09:32:39 +02001229 resp, err = TcpSendReceive(serverAddress+":80",
Matus Fabian82b3cc12024-11-21 17:01:45 +01001230 "POST /interface_stats.json HTTP/1.1\r\nContent-Length: 4 \r\n\r\n"+ifName)
Adrian Villin4677d922024-06-14 09:32:39 +02001231 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001232 validatePostInterfaceStats(s, resp)
1233
Adrian Villin4677d922024-06-14 09:32:39 +02001234 resp, err = TcpSendReceive(serverAddress+":80",
Matus Fabian82b3cc12024-11-21 17:01:45 +01001235 "POST /interface_stats.json HTTP/1.1\r\nContent-Length:\t\t4\r\n\r\n"+ifName)
Adrian Villin4677d922024-06-14 09:32:39 +02001236 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001237 validatePostInterfaceStats(s, resp)
1238}
1239
1240func HttpMethodNotImplementedTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001241 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001242 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001243 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001244
Matus Fabianfa5defd2024-10-02 09:00:19 +02001245 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian82ad9662024-06-04 19:00:00 +02001246 req, err := http.NewRequest("OPTIONS", "http://"+serverAddress+":80/show/version", nil)
Adrian Villin4677d922024-06-14 09:32:39 +02001247 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001248 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +02001249 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001250 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +02001251 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +02001252 s.AssertHttpStatus(resp, 501)
1253 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
1254 s.AssertHttpContentLength(resp, int64(0))
Matus Fabian82ad9662024-06-04 19:00:00 +02001255}
1256
1257func HttpVersionNotSupportedTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001258 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001259 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001260 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001261
Adrian Villin4677d922024-06-14 09:32:39 +02001262 resp, err := TcpSendReceive(serverAddress+":80", "GET / HTTP/2\r\n\r\n")
1263 s.AssertNil(err, fmt.Sprint(err))
1264 s.AssertContains(resp, "HTTP/1.1 505 HTTP Version Not Supported")
Matus Fabian82ad9662024-06-04 19:00:00 +02001265}
1266
1267func HttpUriDecodeTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001268 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001269 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001270 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001271
Matus Fabianfa5defd2024-10-02 09:00:19 +02001272 client := NewHttpClient(defaultHttpTimeout)
Matus Fabian82ad9662024-06-04 19:00:00 +02001273 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/sh%6fw%20versio%6E%20verbose", nil)
Adrian Villin4677d922024-06-14 09:32:39 +02001274 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001275 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +02001276 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001277 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +02001278 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +02001279 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +02001280 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +02001281 s.AssertNil(err, fmt.Sprint(err))
Adrian Villin4677d922024-06-14 09:32:39 +02001282 s.AssertNotContains(string(data), "unknown input")
1283 s.AssertContains(string(data), "Compiler")
Matus Fabiana647a832024-08-26 18:01:14 +02001284 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html")
Matus Fabian82ad9662024-06-04 19:00:00 +02001285}
1286
1287func HttpHeadersTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001288 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001289 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001290 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001291
Matus Fabian82b3cc12024-11-21 17:01:45 +01001292 transport := http.DefaultTransport
1293 transport.(*http.Transport).Proxy = nil
1294 transport.(*http.Transport).DisableKeepAlives = false
1295 client := &http.Client{
1296 Transport: transport,
1297 Timeout: time.Second * 30,
1298 }
1299
1300 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/show/version", nil)
Adrian Villin4677d922024-06-14 09:32:39 +02001301 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82b3cc12024-11-21 17:01:45 +01001302 req.Header.Add("Accept", "text/xml")
1303 req.Header.Add("Accept-Language", "*")
1304 req.Header.Add("Accept", "text/plain")
1305 req.Header.Add("Accept", "text/html")
1306 resp, err := client.Do(req)
1307 s.AssertNil(err, fmt.Sprint(err))
1308 defer resp.Body.Close()
1309 s.Log(DumpHttpResp(resp, true))
1310 s.AssertHttpStatus(resp, 200)
1311 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/plain")
1312 data, err := io.ReadAll(resp.Body)
1313 s.AssertNil(err, fmt.Sprint(err))
1314 s.AssertNotContains(string(data), "<html>", "html content received instead of plain text")
1315
1316 req2, err := http.NewRequest("GET", "http://"+serverAddress+":80/show/version", nil)
1317 s.AssertNil(err, fmt.Sprint(err))
1318 req2.Header.Add("Accept", "text/html")
1319 resp2, err := client.Do(req2)
1320 s.AssertNil(err, fmt.Sprint(err))
1321 defer resp2.Body.Close()
1322 s.Log(DumpHttpResp(resp2, true))
1323 s.AssertHttpStatus(resp2, 200)
1324 s.AssertHttpHeaderWithValue(resp2, "Content-Type", "text/html")
1325 data2, err := io.ReadAll(resp2.Body)
1326 s.AssertNil(err, fmt.Sprint(err))
1327 s.AssertContains(string(data2), "<html>", "html content not received")
Matus Fabian82ad9662024-06-04 19:00:00 +02001328}
1329
1330func HttpInvalidHeadersTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001331 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001332 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001333 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001334
Adrian Villin4677d922024-06-14 09:32:39 +02001335 resp, err := TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nUser-Agent: test\r\n")
1336 s.AssertNil(err, fmt.Sprint(err))
1337 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Header section must end with CRLF CRLF")
Matus Fabian82ad9662024-06-04 19:00:00 +02001338
Adrian Villin4677d922024-06-14 09:32:39 +02001339 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser@Agent:test\r\n\r\n")
1340 s.AssertNil(err, fmt.Sprint(err))
1341 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'@' not allowed in field name")
Matus Fabian82ad9662024-06-04 19:00:00 +02001342
Adrian Villin4677d922024-06-14 09:32:39 +02001343 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent\r\n\r\n")
1344 s.AssertNil(err, fmt.Sprint(err))
1345 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "incomplete field line not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001346
Adrian Villin4677d922024-06-14 09:32:39 +02001347 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\n: test\r\n\r\n")
1348 s.AssertNil(err, fmt.Sprint(err))
1349 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field name not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001350
Adrian Villin4677d922024-06-14 09:32:39 +02001351 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\rUser-Agent:test\r\n\r\n")
1352 s.AssertNil(err, fmt.Sprint(err))
1353 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid field line end not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001354
Adrian Villin4677d922024-06-14 09:32:39 +02001355 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\nUser-Agent:test\r\n\r\n")
1356 s.AssertNil(err, fmt.Sprint(err))
1357 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid field line end not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001358
Adrian Villin4677d922024-06-14 09:32:39 +02001359 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent:\r\n\r\n")
1360 s.AssertNil(err, fmt.Sprint(err))
1361 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field value not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001362
Adrian Villin4677d922024-06-14 09:32:39 +02001363 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent: \r\n\r\n")
1364 s.AssertNil(err, fmt.Sprint(err))
1365 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field value not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001366}
1367
Matus Fabianb7a9ed72024-05-10 16:20:40 +02001368func HeaderServerTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001369 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001370 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001371 vpp.Vppctl("http cli server")
Matus Fabianb7a9ed72024-05-10 16:20:40 +02001372
Matus Fabianfa5defd2024-10-02 09:00:19 +02001373 client := NewHttpClient(defaultHttpTimeout)
Matus Fabianb7a9ed72024-05-10 16:20:40 +02001374 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/show/version", nil)
Adrian Villin4677d922024-06-14 09:32:39 +02001375 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian595d8cb2024-05-17 11:28:43 +02001376 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +02001377 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +02001378 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +02001379 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +02001380 s.AssertHttpStatus(resp, 200)
1381 s.AssertHttpHeaderWithValue(resp, "Server", "http_cli_server")
1382 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html")
Matus Fabian616201a2024-05-02 11:17:15 +02001383}
Matus Fabian84167222024-10-17 13:41:51 +02001384
1385func HttpConnTimeoutTest(s *NoTopoSuite) {
1386 vpp := s.GetContainerByName("vpp").VppInstance
1387 serverAddress := s.VppAddr()
1388 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug keepalive-timeout 2"))
1389
1390 req := "GET /version.json HTTP/1.1\r\nHost:" + serverAddress + ":80\r\nUser-Agent:test\r\n\r\n"
1391 conn, err := net.DialTimeout("tcp", serverAddress+":80", time.Second*30)
1392 s.AssertNil(err, fmt.Sprint(err))
1393 defer conn.Close()
1394 err = conn.SetDeadline(time.Now().Add(time.Second * 30))
1395 s.AssertNil(err, fmt.Sprint(err))
1396 _, err = conn.Write([]byte(req))
1397 s.AssertNil(err, fmt.Sprint(err))
1398 reply := make([]byte, 1024)
1399 _, err = conn.Read(reply)
1400 s.AssertNil(err, fmt.Sprint(err))
1401 s.AssertContains(string(reply), "HTTP/1.1 200 OK")
1402 s.Log(vpp.Vppctl("show session verbose 2"))
1403
1404 s.Log("waiting for close on the server side")
1405 time.Sleep(time.Second * 5)
1406 s.Log(vpp.Vppctl("show session verbose 2"))
1407
1408 _, err = conn.Write([]byte(req))
1409 s.AssertNil(err, fmt.Sprint(err))
1410 reply = make([]byte, 1024)
1411 _, err = conn.Read(reply)
1412 s.AssertMatchError(err, io.EOF, "connection not closed by server")
1413}
Matus Fabian82b3cc12024-11-21 17:01:45 +01001414
1415func HttpIgnoreH2UpgradeTest(s *NoTopoSuite) {
1416 vpp := s.GetContainerByName("vpp").VppInstance
1417 serverAddress := s.VppAddr()
1418 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
1419
1420 transport := http.DefaultTransport
1421 transport.(*http.Transport).Proxy = nil
1422 transport.(*http.Transport).DisableKeepAlives = false
1423 client := &http.Client{
1424 Transport: transport,
1425 Timeout: time.Second * 30,
1426 }
1427
1428 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/version.json", nil)
1429 s.AssertNil(err, fmt.Sprint(err))
1430 req.Header.Add("Connection", "Upgrade")
1431 req.Header.Add("Upgrade", "HTTP/2.0")
1432 resp, err := client.Do(req)
1433 s.AssertNil(err, fmt.Sprint(err))
1434 defer resp.Body.Close()
1435 s.Log(DumpHttpResp(resp, true))
1436 s.AssertHttpStatus(resp, 200)
1437 s.AssertHttpHeaderNotPresent(resp, "Upgrade")
1438}