blob: bfd2d34483c2cee22a5f9a068949788ac45c10d9 [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 Fabianba9ea132024-07-12 11:07:17 +02006 "github.com/onsi/gomega/ghttp"
Matus Fabian2d1f0e62024-06-06 11:24:36 +02007 "github.com/onsi/gomega/gmeasure"
8 "io"
Matus Fabian9bb07622024-09-04 18:04:54 +02009 "math/rand"
Matus Fabiand086a362024-06-27 13:20:10 +020010 "net"
Matus Fabianb7a9ed72024-05-10 16:20:40 +020011 "net/http"
Matus Fabiand086a362024-06-27 13:20:10 +020012 "net/http/httptrace"
13 "os"
Matus Fabiand46e6742024-07-31 16:08:40 +020014 "strconv"
Matus Fabian5c4c1b62024-06-28 16:11:04 +020015 "sync"
Adrian Villincee15aa2024-03-14 11:42:55 -040016 "time"
17
Adrian Villin4677d922024-06-14 09:32:39 +020018 . "fd.io/hs-test/infra"
Matus Fabian5c4c1b62024-06-28 16:11:04 +020019 . "github.com/onsi/ginkgo/v2"
Filip Tehlarc204c872022-12-21 08:59:16 +010020)
21
Adrian Villincee15aa2024-03-14 11:42:55 -040022func init() {
Adrian Villin4677d922024-06-14 09:32:39 +020023 RegisterVethTests(HttpCliTest, HttpCliConnectErrorTest)
Matus Fabianba9ea132024-07-12 11:07:17 +020024 RegisterSoloVethTests(HttpClientGetMemLeakTest)
Matus Fabiand086a362024-06-27 13:20:10 +020025 RegisterNoTopoTests(HeaderServerTest, HttpPersistentConnectionTest, HttpPipeliningTest,
Matus Fabianb7a9ed72024-05-10 16:20:40 +020026 HttpStaticMovedTest, HttpStaticNotFoundTest, HttpCliMethodNotAllowedTest,
Matus Fabian82ad9662024-06-04 19:00:00 +020027 HttpCliBadRequestTest, HttpStaticBuildInUrlGetIfStatsTest, HttpStaticBuildInUrlPostIfStatsTest,
28 HttpInvalidRequestLineTest, HttpMethodNotImplementedTest, HttpInvalidHeadersTest,
29 HttpContentLengthTest, HttpStaticBuildInUrlGetIfListTest, HttpStaticBuildInUrlGetVersionTest,
30 HttpStaticMacTimeTest, HttpStaticBuildInUrlGetVersionVerboseTest, HttpVersionNotSupportedTest,
31 HttpInvalidContentLengthTest, HttpInvalidTargetSyntaxTest, HttpStaticPathTraversalTest, HttpUriDecodeTest,
Adrian Villin86fa9432024-08-08 08:56:34 +020032 HttpHeadersTest, HttpStaticFileHandlerTest, HttpStaticFileHandlerDefaultMaxAgeTest, HttpClientTest, HttpClientErrRespTest, HttpClientPostFormTest,
Matus Fabian69123a32024-08-23 17:35:50 +020033 HttpClientPostFileTest, HttpClientPostFilePtrTest, AuthorityFormTargetTest, HttpRequestLineTest)
Matus Fabian9bb07622024-09-04 18:04:54 +020034 RegisterNoTopoSoloTests(HttpStaticPromTest, HttpGetTpsTest, HttpGetTpsInterruptModeTest, PromConcurrentConnectionsTest,
35 PromMemLeakTest, HttpClientPostMemLeakTest, HttpInvalidClientRequestMemLeakTest, HttpPostTpsTest, HttpPostTpsInterruptModeTest)
Adrian Villincee15aa2024-03-14 11:42:55 -040036}
37
Matus Fabian5409d332024-05-28 13:39:13 +020038const wwwRootPath = "/tmp/www_root"
39
Matus Fabian2d1f0e62024-06-06 11:24:36 +020040func httpDownloadBenchmark(s *HstSuite, experiment *gmeasure.Experiment, data interface{}) {
41 url, isValid := data.(string)
Adrian Villin4677d922024-06-14 09:32:39 +020042 s.AssertEqual(true, isValid)
43 client := NewHttpClient()
Matus Fabian2d1f0e62024-06-06 11:24:36 +020044 req, err := http.NewRequest("GET", url, nil)
Adrian Villin4677d922024-06-14 09:32:39 +020045 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian2d1f0e62024-06-06 11:24:36 +020046 t := time.Now()
47 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +020048 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian2d1f0e62024-06-06 11:24:36 +020049 defer resp.Body.Close()
Matus Fabiana647a832024-08-26 18:01:14 +020050 s.AssertHttpStatus(resp, 200)
Matus Fabian2d1f0e62024-06-06 11:24:36 +020051 _, err = io.ReadAll(resp.Body)
Matus Fabiand46e6742024-07-31 16:08:40 +020052 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian2d1f0e62024-06-06 11:24:36 +020053 duration := time.Since(t)
54 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 +020055}
56
Matus Fabian9bb07622024-09-04 18:04:54 +020057func HttpGetTpsInterruptModeTest(s *NoTopoSuite) {
58 HttpGetTpsTest(s)
Adrian Villin1fde9992024-06-24 08:14:05 -040059}
60
Matus Fabian9bb07622024-09-04 18:04:54 +020061func HttpGetTpsTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +020062 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +020063 serverAddress := s.VppAddr()
Matus Fabian2d1f0e62024-06-06 11:24:36 +020064 url := "http://" + serverAddress + ":8080/test_file_10M"
65
Adrian Villin4677d922024-06-14 09:32:39 +020066 vpp.Vppctl("http tps uri tcp://0.0.0.0/8080")
Matus Fabian2d1f0e62024-06-06 11:24:36 +020067
Matus Fabian9bb07622024-09-04 18:04:54 +020068 s.RunBenchmark("HTTP tps download 10M", 10, 0, httpDownloadBenchmark, url)
69}
70
71func httpUploadBenchmark(s *HstSuite, experiment *gmeasure.Experiment, data interface{}) {
72 url, isValid := data.(string)
73 s.AssertEqual(true, isValid)
74 body := make([]byte, 10485760)
75 _, err := rand.Read(body)
76 client := NewHttpClient()
77 req, err := http.NewRequest("POST", url, bytes.NewBuffer(body))
78 s.AssertNil(err, fmt.Sprint(err))
79 t := time.Now()
80 resp, err := client.Do(req)
81 s.AssertNil(err, fmt.Sprint(err))
82 defer resp.Body.Close()
83 s.AssertHttpStatus(resp, 200)
84 _, err = io.ReadAll(resp.Body)
85 s.AssertNil(err, fmt.Sprint(err))
86 duration := time.Since(t)
87 experiment.RecordValue("Upload Speed", (float64(req.ContentLength)/1024/1024)/duration.Seconds(), gmeasure.Units("MB/s"), gmeasure.Precision(2))
88}
89
90func HttpPostTpsInterruptModeTest(s *NoTopoSuite) {
91 HttpPostTpsTest(s)
92}
93
94func HttpPostTpsTest(s *NoTopoSuite) {
95 vpp := s.GetContainerByName("vpp").VppInstance
96 serverAddress := s.VppAddr()
97 url := "http://" + serverAddress + ":8080/test_file_10M"
98
99 vpp.Vppctl("http tps uri tcp://0.0.0.0/8080")
100
101 s.RunBenchmark("HTTP tps upload 10M", 10, 0, httpUploadBenchmark, url)
Filip Tehlarb15a0002022-11-10 12:34:17 +0100102}
103
Matus Fabiand086a362024-06-27 13:20:10 +0200104func HttpPersistentConnectionTest(s *NoTopoSuite) {
105 // testing url handler app do not support multi-thread
106 s.SkipIfMultiWorker()
107 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200108 serverAddress := s.VppAddr()
Matus Fabiand086a362024-06-27 13:20:10 +0200109 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
110 s.Log(vpp.Vppctl("test-url-handler enable"))
111
112 transport := http.DefaultTransport
113 transport.(*http.Transport).Proxy = nil
114 transport.(*http.Transport).DisableKeepAlives = false
115 client := &http.Client{
116 Transport: transport,
117 Timeout: time.Second * 30,
118 CheckRedirect: func(req *http.Request, via []*http.Request) error {
119 return http.ErrUseLastResponse
120 }}
121
Matus Fabian56cefc82024-08-26 18:26:58 +0200122 body := []byte("{\"sandwich\": {\"spam\": 2, \"eggs\": 1}}")
123 req, err := http.NewRequest("POST", "http://"+serverAddress+":80/test3", bytes.NewBuffer(body))
Matus Fabiand086a362024-06-27 13:20:10 +0200124 s.AssertNil(err, fmt.Sprint(err))
125 resp, err := client.Do(req)
126 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiand086a362024-06-27 13:20:10 +0200127 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200128 s.AssertHttpStatus(resp, 200)
Matus Fabiand086a362024-06-27 13:20:10 +0200129 s.AssertEqual(false, resp.Close)
Matus Fabian56cefc82024-08-26 18:26:58 +0200130 s.AssertHttpContentLength(resp, int64(0))
Matus Fabiand086a362024-06-27 13:20:10 +0200131 o1 := vpp.Vppctl("show session verbose proto http state ready")
132 s.Log(o1)
133 s.AssertContains(o1, "ESTABLISHED")
134
Matus Fabian56cefc82024-08-26 18:26:58 +0200135 req, err = http.NewRequest("GET", "http://"+serverAddress+":80/test1", nil)
136 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiand086a362024-06-27 13:20:10 +0200137 clientTrace := &httptrace.ClientTrace{
138 GotConn: func(info httptrace.GotConnInfo) {
139 s.AssertEqual(true, info.Reused, "connection not reused")
140 },
141 }
Matus Fabian56cefc82024-08-26 18:26:58 +0200142 req = req.WithContext(httptrace.WithClientTrace(req.Context(), clientTrace))
143 resp, err = client.Do(req)
144 s.AssertNil(err, fmt.Sprint(err))
145 s.Log(DumpHttpResp(resp, true))
146 s.AssertHttpStatus(resp, 200)
147 s.AssertEqual(false, resp.Close)
148 s.AssertHttpBody(resp, "hello")
149 o2 := vpp.Vppctl("show session verbose proto http state ready")
150 s.Log(o2)
151 s.AssertContains(o2, "ESTABLISHED")
152 s.AssertEqual(o1, o2)
153
154 req, err = http.NewRequest("GET", "http://"+serverAddress+":80/test2", nil)
Matus Fabiana647a832024-08-26 18:01:14 +0200155 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiand086a362024-06-27 13:20:10 +0200156 req = req.WithContext(httptrace.WithClientTrace(req.Context(), clientTrace))
157 resp, err = client.Do(req)
158 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiand086a362024-06-27 13:20:10 +0200159 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200160 s.AssertHttpStatus(resp, 200)
Matus Fabiand086a362024-06-27 13:20:10 +0200161 s.AssertEqual(false, resp.Close)
Matus Fabiana647a832024-08-26 18:01:14 +0200162 s.AssertHttpBody(resp, "some data")
Matus Fabian56cefc82024-08-26 18:26:58 +0200163 o2 = vpp.Vppctl("show session verbose proto http state ready")
Matus Fabiand086a362024-06-27 13:20:10 +0200164 s.Log(o2)
165 s.AssertContains(o2, "ESTABLISHED")
166 s.AssertEqual(o1, o2)
Matus Fabiana647a832024-08-26 18:01:14 +0200167
Matus Fabiand086a362024-06-27 13:20:10 +0200168}
169
170func HttpPipeliningTest(s *NoTopoSuite) {
171 // testing url handler app do not support multi-thread
172 s.SkipIfMultiWorker()
173 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200174 serverAddress := s.VppAddr()
Matus Fabiand086a362024-06-27 13:20:10 +0200175 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
176 s.Log(vpp.Vppctl("test-url-handler enable"))
177
178 req1 := "GET /test_delayed HTTP/1.1\r\nHost:" + serverAddress + ":80\r\nUser-Agent:test\r\n\r\n"
179 req2 := "GET /test1 HTTP/1.1\r\nHost:" + serverAddress + ":80\r\nUser-Agent:test\r\n\r\n"
180
181 conn, err := net.DialTimeout("tcp", serverAddress+":80", time.Second*30)
182 s.AssertNil(err, fmt.Sprint(err))
183 defer conn.Close()
184 err = conn.SetDeadline(time.Now().Add(time.Second * 15))
185 s.AssertNil(err, fmt.Sprint(err))
186 n, err := conn.Write([]byte(req1))
187 s.AssertNil(err, fmt.Sprint(err))
188 s.AssertEqual(n, len([]rune(req1)))
189 // send second request a bit later so first is already in progress
190 time.Sleep(500 * time.Millisecond)
191 n, err = conn.Write([]byte(req2))
192 s.AssertNil(err, fmt.Sprint(err))
193 s.AssertEqual(n, len([]rune(req2)))
194 reply := make([]byte, 1024)
195 n, err = conn.Read(reply)
196 s.AssertNil(err, fmt.Sprint(err))
197 s.Log(string(reply))
198 s.AssertContains(string(reply), "delayed data", "first request response not received")
199 s.AssertNotContains(string(reply), "hello", "second request response received")
200 // make sure response for second request is not received later
201 _, err = conn.Read(reply)
202 s.AssertMatchError(err, os.ErrDeadlineExceeded, "second request response received")
203}
204
Adrian Villincee15aa2024-03-14 11:42:55 -0400205func HttpCliTest(s *VethsSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200206 serverContainer := s.GetContainerByName("server-vpp")
207 clientContainer := s.GetContainerByName("client-vpp")
Filip Tehlarb15a0002022-11-10 12:34:17 +0100208
Adrian Villin4677d922024-06-14 09:32:39 +0200209 serverVeth := s.GetInterfaceByName(ServerInterfaceName)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100210
Adrian Villin4677d922024-06-14 09:32:39 +0200211 serverContainer.VppInstance.Vppctl("http cli server")
Filip Tehlarb15a0002022-11-10 12:34:17 +0100212
Adrian Villin4677d922024-06-14 09:32:39 +0200213 uri := "http://" + serverVeth.Ip4AddressString() + "/80"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100214
Adrian Villin4677d922024-06-14 09:32:39 +0200215 o := clientContainer.VppInstance.Vppctl("http cli client" +
Filip Tehlard8944382023-11-27 13:28:36 +0100216 " uri " + uri + " query /show/vlib/graph")
Filip Tehlarb15a0002022-11-10 12:34:17 +0100217
Adrian Villin4677d922024-06-14 09:32:39 +0200218 s.Log(o)
219 s.AssertContains(o, "<html>", "<html> not found in the result!")
Matus Fabianba9ea132024-07-12 11:07:17 +0200220 s.AssertContains(o, "</html>", "</html> not found in the result!")
Filip Tehlarb15a0002022-11-10 12:34:17 +0100221}
Filip Tehlarc204c872022-12-21 08:59:16 +0100222
Matus Fabian3d008932024-05-13 10:29:11 +0200223func HttpCliConnectErrorTest(s *VethsSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200224 clientContainer := s.GetContainerByName("client-vpp")
Adrian Villin4677d922024-06-14 09:32:39 +0200225 serverVeth := s.GetInterfaceByName(ServerInterfaceName)
Matus Fabian3d008932024-05-13 10:29:11 +0200226
Adrian Villin4677d922024-06-14 09:32:39 +0200227 uri := "http://" + serverVeth.Ip4AddressString() + "/80"
Matus Fabian3d008932024-05-13 10:29:11 +0200228
Adrian Villin4677d922024-06-14 09:32:39 +0200229 o := clientContainer.VppInstance.Vppctl("http cli client" +
Matus Fabian3d008932024-05-13 10:29:11 +0200230 " uri " + uri + " query /show/vlib/graph")
231
Adrian Villin4677d922024-06-14 09:32:39 +0200232 s.Log(o)
233 s.AssertContains(o, "failed to connect")
Matus Fabian3d008932024-05-13 10:29:11 +0200234}
235
Matus Fabianba9ea132024-07-12 11:07:17 +0200236func HttpClientTest(s *NoTopoSuite) {
Matus Fabiana647a832024-08-26 18:01:14 +0200237 serverAddress := s.HostAddr()
Matus Fabianba9ea132024-07-12 11:07:17 +0200238 server := ghttp.NewUnstartedServer()
239 l, err := net.Listen("tcp", serverAddress+":80")
240 s.AssertNil(err, fmt.Sprint(err))
241 server.HTTPTestServer.Listener = l
242 server.AppendHandlers(
243 ghttp.CombineHandlers(
Matus Fabiand46e6742024-07-31 16:08:40 +0200244 s.LogHttpReq(true),
Matus Fabianba9ea132024-07-12 11:07:17 +0200245 ghttp.VerifyRequest("GET", "/test"),
246 ghttp.VerifyHeader(http.Header{"User-Agent": []string{"http_cli_client"}}),
Adrian Villin3601b322024-08-19 14:41:35 +0200247 ghttp.VerifyHeader(http.Header{"Accept": []string{"text/html"}}),
Matus Fabianba9ea132024-07-12 11:07:17 +0200248 ghttp.RespondWith(http.StatusOK, "<html><body><p>Hello</p></body></html>"),
249 ))
250 server.Start()
251 defer server.Close()
252 uri := "http://" + serverAddress + "/80"
253 vpp := s.GetContainerByName("vpp").VppInstance
254 o := vpp.Vppctl("http cli client uri " + uri + " query /test")
255
256 s.Log(o)
257 s.AssertContains(o, "<html>", "<html> not found in the result!")
258 s.AssertContains(o, "</html>", "</html> not found in the result!")
259}
260
261func HttpClientErrRespTest(s *NoTopoSuite) {
Matus Fabiana647a832024-08-26 18:01:14 +0200262 serverAddress := s.HostAddr()
Matus Fabianba9ea132024-07-12 11:07:17 +0200263 server := ghttp.NewUnstartedServer()
264 l, err := net.Listen("tcp", serverAddress+":80")
265 s.AssertNil(err, fmt.Sprint(err))
266 server.HTTPTestServer.Listener = l
267 server.AppendHandlers(
268 ghttp.CombineHandlers(
Matus Fabiand46e6742024-07-31 16:08:40 +0200269 s.LogHttpReq(true),
Matus Fabianba9ea132024-07-12 11:07:17 +0200270 ghttp.VerifyRequest("GET", "/test"),
271 ghttp.RespondWith(http.StatusNotFound, "404: Not Found"),
272 ))
273 server.Start()
274 defer server.Close()
275 uri := "http://" + serverAddress + "/80"
276 vpp := s.GetContainerByName("vpp").VppInstance
277 o := vpp.Vppctl("http cli client uri " + uri + " query /test")
278
279 s.Log(o)
280 s.AssertContains(o, "404: Not Found", "error not found in the result!")
281}
282
Matus Fabiand46e6742024-07-31 16:08:40 +0200283func HttpClientPostFormTest(s *NoTopoSuite) {
Matus Fabiana647a832024-08-26 18:01:14 +0200284 serverAddress := s.HostAddr()
Matus Fabiand46e6742024-07-31 16:08:40 +0200285 body := "field1=value1&field2=value2"
286
287 server := ghttp.NewUnstartedServer()
288 l, err := net.Listen("tcp", serverAddress+":80")
289 s.AssertNil(err, fmt.Sprint(err))
290 server.HTTPTestServer.Listener = l
291 server.AppendHandlers(
292 ghttp.CombineHandlers(
293 s.LogHttpReq(true),
294 ghttp.VerifyRequest("POST", "/test"),
Adrian Villin3601b322024-08-19 14:41:35 +0200295 ghttp.VerifyContentType("application/x-www-form-urlencoded"),
Matus Fabiand46e6742024-07-31 16:08:40 +0200296 ghttp.VerifyBody([]byte(body)),
297 ghttp.RespondWith(http.StatusOK, nil),
298 ))
299 server.Start()
300 defer server.Close()
301
302 uri := "http://" + serverAddress + "/80"
303 vpp := s.GetContainerByName("vpp").VppInstance
304 o := vpp.Vppctl("http post uri " + uri + " target /test data " + body)
305
306 s.Log(o)
307 s.AssertNotContains(o, "error")
308}
309
310func httpClientPostFile(s *NoTopoSuite, usePtr bool, fileSize int) {
Matus Fabiana647a832024-08-26 18:01:14 +0200311 serverAddress := s.HostAddr()
Matus Fabiand46e6742024-07-31 16:08:40 +0200312 vpp := s.GetContainerByName("vpp").VppInstance
313 fileName := "/tmp/test_file.txt"
314 s.Log(vpp.Container.Exec("fallocate -l " + strconv.Itoa(fileSize) + " " + fileName))
315 s.Log(vpp.Container.Exec("ls -la " + fileName))
316
317 server := ghttp.NewUnstartedServer()
318 l, err := net.Listen("tcp", serverAddress+":80")
319 s.AssertNil(err, fmt.Sprint(err))
320 server.HTTPTestServer.Listener = l
321 server.AppendHandlers(
322 ghttp.CombineHandlers(
323 s.LogHttpReq(false),
324 ghttp.VerifyRequest("POST", "/test"),
325 ghttp.VerifyHeader(http.Header{"Content-Length": []string{strconv.Itoa(fileSize)}}),
Adrian Villin3601b322024-08-19 14:41:35 +0200326 ghttp.VerifyContentType("application/octet-stream"),
Matus Fabiand46e6742024-07-31 16:08:40 +0200327 ghttp.RespondWith(http.StatusOK, nil),
328 ))
329 server.Start()
330 defer server.Close()
331
332 uri := "http://" + serverAddress + "/80"
333 cmd := "http post uri " + uri + " target /test file " + fileName
334 if usePtr {
335 cmd += " use-ptr"
336 }
337 o := vpp.Vppctl(cmd)
338
339 s.Log(o)
340 s.AssertNotContains(o, "error")
341}
342
343func HttpClientPostFileTest(s *NoTopoSuite) {
344 httpClientPostFile(s, false, 32768)
345}
346
347func HttpClientPostFilePtrTest(s *NoTopoSuite) {
348 httpClientPostFile(s, true, 131072)
349}
350
Matus Fabiand58177c2024-08-08 12:50:32 +0200351func cliTestAuthority(s *NoTopoSuite, authority string) {
352 o := s.GetContainerByName("vpp").VppInstance.Vppctl("test http authority-form " + authority)
353 s.AssertNotContains(o, "error")
354 s.AssertContains(o, authority)
355}
356
357func cliTestAuthorityError(s *NoTopoSuite, authority string) {
358 o := s.GetContainerByName("vpp").VppInstance.Vppctl("test http authority-form " + authority)
359 s.AssertContains(o, "error")
360}
361
362func AuthorityFormTargetTest(s *NoTopoSuite) {
363 cliTestAuthority(s, "10.10.2.45:20")
364 cliTestAuthority(s, "[dead:beef::1234]:443")
365 cliTestAuthorityError(s, "example.com:80")
366 cliTestAuthorityError(s, "10.10.2.45")
367 cliTestAuthorityError(s, "1000.10.2.45:20")
368 cliTestAuthorityError(s, "[xyz0::1234]:443")
369}
370
Adrian Villincee15aa2024-03-14 11:42:55 -0400371func HttpStaticPromTest(s *NoTopoSuite) {
Filip Tehlarcc1475c2023-11-29 12:59:05 +0100372 query := "stats.prom"
Adrian Villin4677d922024-06-14 09:32:39 +0200373 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200374 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200375 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
376 s.Log(vpp.Vppctl("prom enable"))
Adrian Villincee15aa2024-03-14 11:42:55 -0400377 time.Sleep(time.Second * 5)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200378 client := NewHttpClient()
379 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/"+query, nil)
380 s.AssertNil(err, fmt.Sprint(err))
381 resp, err := client.Do(req)
382 s.AssertNil(err, fmt.Sprint(err))
383 defer resp.Body.Close()
384 s.Log(DumpHttpResp(resp, false))
Matus Fabiana647a832024-08-26 18:01:14 +0200385 s.AssertHttpStatus(resp, 200)
386 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/plain")
387 s.AssertGreaterThan(resp.ContentLength, 0)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200388 _, err = io.ReadAll(resp.Body)
Matus Fabiand46e6742024-07-31 16:08:40 +0200389 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200390}
391
Matus Fabianc712de52024-07-24 12:56:14 +0200392func promReq(s *NoTopoSuite, url string) {
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200393 client := NewHttpClient()
394 req, err := http.NewRequest("GET", url, nil)
395 s.AssertNil(err, fmt.Sprint(err))
396 resp, err := client.Do(req)
397 s.AssertNil(err, fmt.Sprint(err))
398 defer resp.Body.Close()
Matus Fabiana647a832024-08-26 18:01:14 +0200399 s.AssertHttpStatus(resp, 200)
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200400 _, err = io.ReadAll(resp.Body)
Matus Fabiand46e6742024-07-31 16:08:40 +0200401 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200402}
403
Matus Fabianc712de52024-07-24 12:56:14 +0200404func promReqWg(s *NoTopoSuite, url string, wg *sync.WaitGroup) {
405 defer GinkgoRecover()
406 defer wg.Done()
407 promReq(s, url)
408}
409
Matus Fabiand46e6742024-07-31 16:08:40 +0200410func PromConcurrentConnectionsTest(s *NoTopoSuite) {
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200411 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200412 serverAddress := s.VppAddr()
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200413 url := "http://" + serverAddress + ":80/stats.prom"
414
415 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
416 s.Log(vpp.Vppctl("prom enable"))
417 time.Sleep(time.Second * 5)
418
419 var wg sync.WaitGroup
420 for i := 0; i < 20; i++ {
421 wg.Add(1)
Matus Fabianc712de52024-07-24 12:56:14 +0200422 go promReqWg(s, url, &wg)
Matus Fabian5c4c1b62024-06-28 16:11:04 +0200423 }
424 wg.Wait()
425 s.Log(vpp.Vppctl("show session verbose proto http"))
426}
427
Matus Fabianc712de52024-07-24 12:56:14 +0200428func PromMemLeakTest(s *NoTopoSuite) {
429 s.SkipUnlessLeakCheck()
430
431 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200432 serverAddress := s.VppAddr()
Matus Fabianc712de52024-07-24 12:56:14 +0200433 url := "http://" + serverAddress + ":80/stats.prom"
434
435 /* no goVPP less noise */
436 vpp.Disconnect()
437
438 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers"))
439 s.Log(vpp.Vppctl("prom enable"))
440 time.Sleep(time.Second * 3)
441
442 /* warmup request (FIB) */
443 promReq(s, url)
444
445 vpp.EnableMemoryTrace()
446 traces1, err := vpp.GetMemoryTrace()
447 s.AssertNil(err, fmt.Sprint(err))
448
449 /* collect stats couple of times */
450 for i := 0; i < 5; i++ {
451 time.Sleep(time.Second * 1)
452 promReq(s, url)
453 }
454
455 /* let's give it some time to clean up sessions */
456 time.Sleep(time.Second * 5)
457
458 traces2, err := vpp.GetMemoryTrace()
459 s.AssertNil(err, fmt.Sprint(err))
460 vpp.MemLeakCheck(traces1, traces2)
461}
462
Matus Fabianba9ea132024-07-12 11:07:17 +0200463func HttpClientGetMemLeakTest(s *VethsSuite) {
464 s.SkipUnlessLeakCheck()
465
466 serverContainer := s.GetContainerByName("server-vpp").VppInstance
467 clientContainer := s.GetContainerByName("client-vpp").VppInstance
468 serverVeth := s.GetInterfaceByName(ServerInterfaceName)
469
470 /* no goVPP less noise */
471 clientContainer.Disconnect()
472
473 serverContainer.Vppctl("http cli server")
474
475 uri := "http://" + serverVeth.Ip4AddressString() + "/80"
476
477 /* warmup request (FIB) */
478 clientContainer.Vppctl("http cli client uri " + uri + " query /show/version")
479
Matus Fabiand46e6742024-07-31 16:08:40 +0200480 /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */
481 time.Sleep(time.Second * 12)
482
Matus Fabianba9ea132024-07-12 11:07:17 +0200483 clientContainer.EnableMemoryTrace()
484 traces1, err := clientContainer.GetMemoryTrace()
485 s.AssertNil(err, fmt.Sprint(err))
486
487 clientContainer.Vppctl("http cli client uri " + uri + " query /show/vlib/graph")
488
489 /* let's give it some time to clean up sessions */
490 time.Sleep(time.Second * 12)
491
492 traces2, err := clientContainer.GetMemoryTrace()
493 s.AssertNil(err, fmt.Sprint(err))
494 clientContainer.MemLeakCheck(traces1, traces2)
495}
496
Matus Fabiand46e6742024-07-31 16:08:40 +0200497func HttpClientPostMemLeakTest(s *NoTopoSuite) {
498 s.SkipUnlessLeakCheck()
499
Matus Fabiana647a832024-08-26 18:01:14 +0200500 serverAddress := s.HostAddr()
Matus Fabiand46e6742024-07-31 16:08:40 +0200501 body := "field1=value1&field2=value2"
502
503 uri := "http://" + serverAddress + "/80"
504 vpp := s.GetContainerByName("vpp").VppInstance
505
506 /* no goVPP less noise */
507 vpp.Disconnect()
508
509 server := ghttp.NewUnstartedServer()
510 l, err := net.Listen("tcp", serverAddress+":80")
511 s.AssertNil(err, fmt.Sprint(err))
512 server.HTTPTestServer.Listener = l
513 server.AppendHandlers(
514 ghttp.CombineHandlers(
515 ghttp.VerifyRequest("POST", "/test"),
516 ghttp.RespondWith(http.StatusOK, nil),
517 ),
518 ghttp.CombineHandlers(
519 ghttp.VerifyRequest("POST", "/test"),
520 ghttp.RespondWith(http.StatusOK, nil),
521 ),
522 )
523 server.Start()
524 defer server.Close()
525
526 /* warmup request (FIB) */
527 vpp.Vppctl("http post uri " + uri + " target /test data " + body)
528
529 /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */
530 time.Sleep(time.Second * 12)
531
532 vpp.EnableMemoryTrace()
533 traces1, err := vpp.GetMemoryTrace()
534 s.AssertNil(err, fmt.Sprint(err))
535
536 vpp.Vppctl("http post uri " + uri + " target /test data " + body)
537
538 /* let's give it some time to clean up sessions */
539 time.Sleep(time.Second * 12)
540
541 traces2, err := vpp.GetMemoryTrace()
542 s.AssertNil(err, fmt.Sprint(err))
543 vpp.MemLeakCheck(traces1, traces2)
544}
545
Matus Fabian55467552024-08-06 15:55:26 +0200546func HttpInvalidClientRequestMemLeakTest(s *NoTopoSuite) {
547 s.SkipUnlessLeakCheck()
548
549 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200550 serverAddress := s.VppAddr()
Matus Fabian55467552024-08-06 15:55:26 +0200551
552 /* no goVPP less noise */
553 vpp.Disconnect()
554
555 vpp.Vppctl("http cli server")
556
557 /* warmup request (FIB) */
558 _, err := TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n")
559 s.AssertNil(err, fmt.Sprint(err))
560
561 /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */
562 time.Sleep(time.Second * 12)
563
564 vpp.EnableMemoryTrace()
565 traces1, err := vpp.GetMemoryTrace()
566 s.AssertNil(err, fmt.Sprint(err))
567
568 _, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n")
569 s.AssertNil(err, fmt.Sprint(err))
570
571 /* let's give it some time to clean up sessions */
572 time.Sleep(time.Second * 12)
573
574 traces2, err := vpp.GetMemoryTrace()
575 s.AssertNil(err, fmt.Sprint(err))
576 vpp.MemLeakCheck(traces1, traces2)
577
578}
579
Adrian Villin86fa9432024-08-08 08:56:34 +0200580func HttpStaticFileHandlerDefaultMaxAgeTest(s *NoTopoSuite) {
581 HttpStaticFileHandlerTestFunction(s, "default")
582}
583
Matus Fabiand46e6742024-07-31 16:08:40 +0200584func HttpStaticFileHandlerTest(s *NoTopoSuite) {
Adrian Villin86fa9432024-08-08 08:56:34 +0200585 HttpStaticFileHandlerTestFunction(s, "123")
586}
587
588func HttpStaticFileHandlerTestFunction(s *NoTopoSuite, max_age string) {
Matus Fabiana647a832024-08-26 18:01:14 +0200589 var maxAgeFormatted string
Adrian Villin86fa9432024-08-08 08:56:34 +0200590 if max_age == "default" {
Matus Fabiana647a832024-08-26 18:01:14 +0200591 maxAgeFormatted = ""
Adrian Villin86fa9432024-08-08 08:56:34 +0200592 max_age = "600"
593 } else {
Matus Fabiana647a832024-08-26 18:01:14 +0200594 maxAgeFormatted = "max-age " + max_age
Adrian Villin86fa9432024-08-08 08:56:34 +0200595 }
596
Matus Fabianba9ea132024-07-12 11:07:17 +0200597 content := "<html><body><p>Hello</p></body></html>"
598 content2 := "<html><body><p>Page</p></body></html>"
Adrian Villin7e6606a2024-08-16 15:23:28 +0200599
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200600 vpp := s.GetContainerByName("vpp").VppInstance
601 vpp.Container.Exec("mkdir -p " + wwwRootPath)
Matus Fabiand46e6742024-07-31 16:08:40 +0200602 err := vpp.Container.CreateFile(wwwRootPath+"/index.html", content)
603 s.AssertNil(err, fmt.Sprint(err))
604 err = vpp.Container.CreateFile(wwwRootPath+"/page.html", content2)
605 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiana647a832024-08-26 18:01:14 +0200606 serverAddress := s.VppAddr()
607 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 +0200608
609 client := NewHttpClient()
610 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/index.html", nil)
611 s.AssertNil(err, fmt.Sprint(err))
612 resp, err := client.Do(req)
613 s.AssertNil(err, fmt.Sprint(err))
Adrian Villin7e6606a2024-08-16 15:23:28 +0200614
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200615 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200616 s.AssertHttpStatus(resp, 200)
617 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html")
618 s.AssertHttpHeaderWithValue(resp, "Cache-Control", "max-age="+max_age)
619 parsedTime, err := time.Parse(time.RFC1123, resp.Header.Get("Last-Modified"))
Matus Fabiand46e6742024-07-31 16:08:40 +0200620 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiana647a832024-08-26 18:01:14 +0200621 s.AssertTimeEqualWithinThreshold(parsedTime, time.Now(), time.Minute*5)
622 s.AssertEqual(len(resp.Header.Get("Last-Modified")), 29)
623 s.AssertHttpContentLength(resp, int64(len([]rune(content))))
624 s.AssertHttpBody(resp, content)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200625 o := vpp.Vppctl("show http static server cache verbose")
626 s.Log(o)
627 s.AssertContains(o, "index.html")
628 s.AssertNotContains(o, "page.html")
629
630 resp, err = client.Do(req)
631 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200632 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200633 s.AssertHttpStatus(resp, 200)
634 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html")
635 s.AssertHttpHeaderWithValue(resp, "Cache-Control", "max-age="+max_age)
636 s.AssertHttpContentLength(resp, int64(len([]rune(content))))
637 s.AssertHttpBody(resp, content)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200638
639 req, err = http.NewRequest("GET", "http://"+serverAddress+":80/page.html", nil)
640 s.AssertNil(err, fmt.Sprint(err))
641 resp, err = client.Do(req)
642 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200643 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200644 s.AssertHttpStatus(resp, 200)
645 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html")
646 s.AssertHttpHeaderWithValue(resp, "Cache-Control", "max-age="+max_age)
647 s.AssertHttpContentLength(resp, int64(len([]rune(content2))))
648 s.AssertHttpBody(resp, content2)
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200649 o = vpp.Vppctl("show http static server cache verbose")
650 s.Log(o)
651 s.AssertContains(o, "index.html")
652 s.AssertContains(o, "page.html")
Filip Tehlarcc1475c2023-11-29 12:59:05 +0100653}
654
Matus Fabian5409d332024-05-28 13:39:13 +0200655func HttpStaticPathTraversalTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200656 vpp := s.GetContainerByName("vpp").VppInstance
657 vpp.Container.Exec("mkdir -p " + wwwRootPath)
658 vpp.Container.Exec("mkdir -p " + "/tmp/secret_folder")
Matus Fabiand46e6742024-07-31 16:08:40 +0200659 err := vpp.Container.CreateFile("/tmp/secret_folder/secret_file.txt", "secret")
660 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiana647a832024-08-26 18:01:14 +0200661 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200662 s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug"))
Matus Fabian5409d332024-05-28 13:39:13 +0200663
Adrian Villin4677d922024-06-14 09:32:39 +0200664 client := NewHttpClient()
Matus Fabian5409d332024-05-28 13:39:13 +0200665 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/../secret_folder/secret_file.txt", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200666 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian5409d332024-05-28 13:39:13 +0200667 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200668 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian5409d332024-05-28 13:39:13 +0200669 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200670 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200671 s.AssertHttpStatus(resp, 404)
672 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
673 s.AssertHttpHeaderNotPresent(resp, "Cache-Control")
674 s.AssertHttpContentLength(resp, int64(0))
Matus Fabian5409d332024-05-28 13:39:13 +0200675}
676
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200677func HttpStaticMovedTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200678 vpp := s.GetContainerByName("vpp").VppInstance
679 vpp.Container.Exec("mkdir -p " + wwwRootPath + "/tmp.aaa")
Matus Fabiand46e6742024-07-31 16:08:40 +0200680 err := vpp.Container.CreateFile(wwwRootPath+"/tmp.aaa/index.html", "<html><body><p>Hello</p></body></html>")
681 s.AssertNil(err, fmt.Sprint(err))
Matus Fabiana647a832024-08-26 18:01:14 +0200682 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200683 s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug"))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200684
Adrian Villin4677d922024-06-14 09:32:39 +0200685 client := NewHttpClient()
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200686 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/tmp.aaa", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200687 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200688 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200689 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200690 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200691 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200692 s.AssertHttpStatus(resp, 301)
693 s.AssertHttpHeaderWithValue(resp, "Location", "http://"+serverAddress+"/tmp.aaa/index.html")
694 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
695 s.AssertHttpHeaderNotPresent(resp, "Cache-Control")
696 s.AssertHttpContentLength(resp, int64(0))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200697}
698
699func HttpStaticNotFoundTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200700 vpp := s.GetContainerByName("vpp").VppInstance
701 vpp.Container.Exec("mkdir -p " + wwwRootPath)
Matus Fabiana647a832024-08-26 18:01:14 +0200702 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200703 s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug"))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200704
Adrian Villin4677d922024-06-14 09:32:39 +0200705 client := NewHttpClient()
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200706 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/notfound.html", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200707 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian595d8cb2024-05-17 11:28:43 +0200708 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200709 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200710 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200711 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200712 s.AssertHttpStatus(resp, 404)
713 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
714 s.AssertHttpHeaderNotPresent(resp, "Cache-Control")
715 s.AssertHttpContentLength(resp, int64(0))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200716}
717
718func HttpCliMethodNotAllowedTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200719 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200720 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200721 vpp.Vppctl("http cli server")
Matus Fabian616201a2024-05-02 11:17:15 +0200722
Adrian Villin4677d922024-06-14 09:32:39 +0200723 client := NewHttpClient()
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200724 req, err := http.NewRequest("POST", "http://"+serverAddress+":80/test", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200725 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian595d8cb2024-05-17 11:28:43 +0200726 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200727 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200728 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200729 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200730 s.AssertHttpStatus(resp, 405)
731 s.AssertHttpHeaderWithValue(resp, "Allow", "GET", "server MUST generate an Allow header")
732 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
733 s.AssertHttpContentLength(resp, int64(0))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200734}
735
736func HttpCliBadRequestTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200737 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200738 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200739 vpp.Vppctl("http cli server")
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200740
Adrian Villin4677d922024-06-14 09:32:39 +0200741 client := NewHttpClient()
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200742 req, err := http.NewRequest("GET", "http://"+serverAddress+":80", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200743 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian595d8cb2024-05-17 11:28:43 +0200744 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200745 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200746 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200747 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200748 s.AssertHttpStatus(resp, 400)
749 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
750 s.AssertHttpContentLength(resp, int64(0))
Matus Fabianb7a9ed72024-05-10 16:20:40 +0200751}
752
Matus Fabian82ad9662024-06-04 19:00:00 +0200753func HttpStaticBuildInUrlGetVersionTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200754 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200755 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200756 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabian82ad9662024-06-04 19:00:00 +0200757
Adrian Villin4677d922024-06-14 09:32:39 +0200758 client := NewHttpClient()
Matus Fabian82ad9662024-06-04 19:00:00 +0200759 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/version.json", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200760 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200761 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200762 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200763 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200764 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200765 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +0200766 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +0200767 s.AssertNil(err, fmt.Sprint(err))
768 s.AssertContains(string(data), "vpp_details")
769 s.AssertContains(string(data), "version")
770 s.AssertContains(string(data), "build_date")
771 s.AssertNotContains(string(data), "build_by")
772 s.AssertNotContains(string(data), "build_host")
773 s.AssertNotContains(string(data), "build_dir")
Matus Fabiana647a832024-08-26 18:01:14 +0200774 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
Matus Fabian82ad9662024-06-04 19:00:00 +0200775}
776
777func HttpStaticBuildInUrlGetVersionVerboseTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200778 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200779 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200780 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabian82ad9662024-06-04 19:00:00 +0200781
Adrian Villin4677d922024-06-14 09:32:39 +0200782 client := NewHttpClient()
Matus Fabian82ad9662024-06-04 19:00:00 +0200783 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/version.json?verbose=true", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200784 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200785 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200786 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200787 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200788 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200789 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +0200790 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +0200791 s.AssertNil(err, fmt.Sprint(err))
792 s.AssertContains(string(data), "vpp_details")
793 s.AssertContains(string(data), "version")
794 s.AssertContains(string(data), "build_date")
795 s.AssertContains(string(data), "build_by")
796 s.AssertContains(string(data), "build_host")
797 s.AssertContains(string(data), "build_dir")
Matus Fabiana647a832024-08-26 18:01:14 +0200798 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
Matus Fabian82ad9662024-06-04 19:00:00 +0200799}
800
801func HttpStaticBuildInUrlGetIfListTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200802 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200803 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200804 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabian82ad9662024-06-04 19:00:00 +0200805
Adrian Villin4677d922024-06-14 09:32:39 +0200806 client := NewHttpClient()
Matus Fabian82ad9662024-06-04 19:00:00 +0200807 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/interface_list.json", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200808 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200809 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200810 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200811 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200812 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200813 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +0200814 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +0200815 s.AssertNil(err, fmt.Sprint(err))
816 s.AssertContains(string(data), "interface_list")
Matus Fabiana647a832024-08-26 18:01:14 +0200817 s.AssertContains(string(data), s.VppIfName())
818 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
Matus Fabian82ad9662024-06-04 19:00:00 +0200819}
820
821func HttpStaticBuildInUrlGetIfStatsTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200822 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200823 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200824 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabian82ad9662024-06-04 19:00:00 +0200825
Adrian Villin4677d922024-06-14 09:32:39 +0200826 client := NewHttpClient()
Matus Fabian82ad9662024-06-04 19:00:00 +0200827 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/interface_stats.json", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200828 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200829 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200830 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200831 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200832 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200833 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +0200834 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +0200835 s.AssertNil(err, fmt.Sprint(err))
836 s.AssertContains(string(data), "interface_stats")
837 s.AssertContains(string(data), "local0")
Matus Fabiana647a832024-08-26 18:01:14 +0200838 s.AssertContains(string(data), s.VppIfName())
839 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
Matus Fabian82ad9662024-06-04 19:00:00 +0200840}
841
842func validatePostInterfaceStats(s *NoTopoSuite, data string) {
Adrian Villin4677d922024-06-14 09:32:39 +0200843 s.AssertContains(data, "interface_stats")
Matus Fabiana647a832024-08-26 18:01:14 +0200844 s.AssertContains(data, s.VppIfName())
Adrian Villin4677d922024-06-14 09:32:39 +0200845 s.AssertNotContains(data, "error")
846 s.AssertNotContains(data, "local0")
Matus Fabian82ad9662024-06-04 19:00:00 +0200847}
848
849func HttpStaticBuildInUrlPostIfStatsTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200850 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200851 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200852 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabiana647a832024-08-26 18:01:14 +0200853 body := []byte(s.VppIfName())
Matus Fabian82ad9662024-06-04 19:00:00 +0200854
Adrian Villin4677d922024-06-14 09:32:39 +0200855 client := NewHttpClient()
Matus Fabian82ad9662024-06-04 19:00:00 +0200856 req, err := http.NewRequest("POST",
857 "http://"+serverAddress+":80/interface_stats.json", bytes.NewBuffer(body))
Adrian Villin4677d922024-06-14 09:32:39 +0200858 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200859 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200860 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200861 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200862 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200863 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +0200864 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +0200865 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200866 validatePostInterfaceStats(s, string(data))
Matus Fabiana647a832024-08-26 18:01:14 +0200867 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
Matus Fabian82ad9662024-06-04 19:00:00 +0200868}
869
870func HttpStaticMacTimeTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200871 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200872 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200873 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabiana647a832024-08-26 18:01:14 +0200874 s.Log(vpp.Vppctl("mactime enable-disable " + s.VppIfName()))
Matus Fabian82ad9662024-06-04 19:00:00 +0200875
Adrian Villin4677d922024-06-14 09:32:39 +0200876 client := NewHttpClient()
Matus Fabian82ad9662024-06-04 19:00:00 +0200877 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/mactime.json", nil)
Adrian Villin4677d922024-06-14 09:32:39 +0200878 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200879 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +0200880 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +0200881 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +0200882 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +0200883 s.AssertHttpStatus(resp, 200)
Matus Fabian82ad9662024-06-04 19:00:00 +0200884 data, err := io.ReadAll(resp.Body)
Adrian Villin4677d922024-06-14 09:32:39 +0200885 s.AssertNil(err, fmt.Sprint(err))
886 s.AssertContains(string(data), "mactime")
Matus Fabiana647a832024-08-26 18:01:14 +0200887 s.AssertContains(string(data), s.HostAddr())
Adrian Villin4677d922024-06-14 09:32:39 +0200888 s.AssertContains(string(data), s.GetInterfaceByName(TapInterfaceName).HwAddress.String())
Matus Fabiana647a832024-08-26 18:01:14 +0200889 s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json")
890 parsedTime, err := time.Parse(time.RFC1123, resp.Header.Get("Date"))
891 s.AssertNil(err, fmt.Sprint(err))
892 s.AssertTimeEqualWithinThreshold(parsedTime, time.Now(), time.Minute*5)
Adrian Villin7e6606a2024-08-16 15:23:28 +0200893 s.AssertEqual(len(resp.Header.Get("Date")), 29)
Matus Fabian82ad9662024-06-04 19:00:00 +0200894}
895
896func HttpInvalidRequestLineTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200897 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200898 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200899 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +0200900
Matus Fabian69123a32024-08-23 17:35:50 +0200901 resp, err := TcpSendReceive(serverAddress+":80", " GET / HTTP/1.1")
902 s.AssertNil(err, fmt.Sprint(err))
903 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid request line start not allowed")
904
905 resp, err = TcpSendReceive(serverAddress+":80", "\rGET / HTTP/1.1")
906 s.AssertNil(err, fmt.Sprint(err))
907 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid request line start not allowed")
908
909 resp, err = TcpSendReceive(serverAddress+":80", "\nGET / HTTP/1.1")
910 s.AssertNil(err, fmt.Sprint(err))
911 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid request line start not allowed")
912
913 resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1")
Adrian Villin4677d922024-06-14 09:32:39 +0200914 s.AssertNil(err, fmt.Sprint(err))
915 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid framing not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +0200916
Adrian Villin4677d922024-06-14 09:32:39 +0200917 resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n")
918 s.AssertNil(err, fmt.Sprint(err))
919 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid framing not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +0200920
Adrian Villin4677d922024-06-14 09:32:39 +0200921 resp, err = TcpSendReceive(serverAddress+":80", "GET /\r\n\r\n")
922 s.AssertNil(err, fmt.Sprint(err))
923 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "HTTP-version must be present")
Matus Fabian82ad9662024-06-04 19:00:00 +0200924
Adrian Villin4677d922024-06-14 09:32:39 +0200925 resp, err = TcpSendReceive(serverAddress+":80", "GET HTTP/1.1\r\n\r\n")
926 s.AssertNil(err, fmt.Sprint(err))
927 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "request-target must be present")
Matus Fabian82ad9662024-06-04 19:00:00 +0200928
Adrian Villin4677d922024-06-14 09:32:39 +0200929 resp, err = TcpSendReceive(serverAddress+":80", "GET HTTP/1.1\r\n\r\n")
930 s.AssertNil(err, fmt.Sprint(err))
931 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "request-target must be present")
Matus Fabian82ad9662024-06-04 19:00:00 +0200932
Adrian Villin4677d922024-06-14 09:32:39 +0200933 resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/x\r\n\r\n")
934 s.AssertNil(err, fmt.Sprint(err))
935 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'HTTP/x' invalid http version not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +0200936
Adrian Villin4677d922024-06-14 09:32:39 +0200937 resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP1.1\r\n\r\n")
938 s.AssertNil(err, fmt.Sprint(err))
939 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'HTTP1.1' invalid http version not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +0200940}
941
Matus Fabian69123a32024-08-23 17:35:50 +0200942func HttpRequestLineTest(s *NoTopoSuite) {
943 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200944 serverAddress := s.VppAddr()
Matus Fabian69123a32024-08-23 17:35:50 +0200945 vpp.Vppctl("http cli server")
946
947 resp, err := TcpSendReceive(serverAddress+":80", "\r\nGET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent:test\r\n\r\n")
948 s.AssertNil(err, fmt.Sprint(err))
949 s.AssertContains(resp, "HTTP/1.1 200 OK")
950 s.AssertContains(resp, "<html>", "html content not found")
951}
952
Matus Fabian82ad9662024-06-04 19:00:00 +0200953func HttpInvalidTargetSyntaxTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +0200954 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +0200955 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +0200956 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabian82ad9662024-06-04 19:00:00 +0200957
Adrian Villin4677d922024-06-14 09:32:39 +0200958 resp, err := TcpSendReceive(serverAddress+":80", "GET /interface|stats.json HTTP/1.1\r\n\r\n")
959 s.AssertNil(err, fmt.Sprint(err))
960 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'|' not allowed in target path")
Matus Fabian82ad9662024-06-04 19:00:00 +0200961
Adrian Villin4677d922024-06-14 09:32:39 +0200962 resp, err = TcpSendReceive(serverAddress+":80", "GET /interface#stats.json HTTP/1.1\r\n\r\n")
963 s.AssertNil(err, fmt.Sprint(err))
964 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'#' not allowed in target path")
Matus Fabian82ad9662024-06-04 19:00:00 +0200965
Adrian Villin4677d922024-06-14 09:32:39 +0200966 resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%stats.json HTTP/1.1\r\n\r\n")
967 s.AssertNil(err, fmt.Sprint(err))
968 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +0200969 "after '%' there must be two hex-digit characters in target path")
970
Adrian Villin4677d922024-06-14 09:32:39 +0200971 resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%1stats.json HTTP/1.1\r\n\r\n")
972 s.AssertNil(err, fmt.Sprint(err))
973 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +0200974 "after '%' there must be two hex-digit characters in target path")
975
Adrian Villin4677d922024-06-14 09:32:39 +0200976 resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%Bstats.json HTTP/1.1\r\n\r\n")
977 s.AssertNil(err, fmt.Sprint(err))
978 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +0200979 "after '%' there must be two hex-digit characters in target path")
980
Adrian Villin4677d922024-06-14 09:32:39 +0200981 resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%stats.json%B HTTP/1.1\r\n\r\n")
982 s.AssertNil(err, fmt.Sprint(err))
983 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +0200984 "after '%' there must be two hex-digit characters in target path")
985
Adrian Villin4677d922024-06-14 09:32:39 +0200986 resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose>true HTTP/1.1\r\n\r\n")
987 s.AssertNil(err, fmt.Sprint(err))
988 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'>' not allowed in target query")
Matus Fabian82ad9662024-06-04 19:00:00 +0200989
Adrian Villin4677d922024-06-14 09:32:39 +0200990 resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose%true HTTP/1.1\r\n\r\n")
991 s.AssertNil(err, fmt.Sprint(err))
992 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +0200993 "after '%' there must be two hex-digit characters in target query")
994
Adrian Villin4677d922024-06-14 09:32:39 +0200995 resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose=%1 HTTP/1.1\r\n\r\n")
996 s.AssertNil(err, fmt.Sprint(err))
997 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +0200998 "after '%' there must be two hex-digit characters in target query")
999}
1000
1001func HttpInvalidContentLengthTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001002 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001003 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001004 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001005
Adrian Villin4677d922024-06-14 09:32:39 +02001006 resp, err := TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length:\r\n\r\n")
1007 s.AssertNil(err, fmt.Sprint(err))
1008 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Content-Length value must be present")
Matus Fabian82ad9662024-06-04 19:00:00 +02001009
Adrian Villin4677d922024-06-14 09:32:39 +02001010 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length: \r\n\r\n")
1011 s.AssertNil(err, fmt.Sprint(err))
1012 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Content-Length value must be present")
Matus Fabian82ad9662024-06-04 19:00:00 +02001013
Adrian Villin4677d922024-06-14 09:32:39 +02001014 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length: a\r\n\r\n")
1015 s.AssertNil(err, fmt.Sprint(err))
1016 s.AssertContains(resp, "HTTP/1.1 400 Bad Request",
Matus Fabian82ad9662024-06-04 19:00:00 +02001017 "Content-Length value other than digit not allowed")
1018}
1019
1020func HttpContentLengthTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001021 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001022 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001023 s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug"))
Matus Fabiana647a832024-08-26 18:01:14 +02001024 ifName := s.VppIfName()
Matus Fabian82ad9662024-06-04 19:00:00 +02001025
Adrian Villin4677d922024-06-14 09:32:39 +02001026 resp, err := TcpSendReceive(serverAddress+":80",
Matus Fabian82ad9662024-06-04 19:00:00 +02001027 "POST /interface_stats.json HTTP/1.1\r\nContent-Length:4\r\n\r\n"+ifName)
Adrian Villin4677d922024-06-14 09:32:39 +02001028 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001029 validatePostInterfaceStats(s, resp)
1030
Adrian Villin4677d922024-06-14 09:32:39 +02001031 resp, err = TcpSendReceive(serverAddress+":80",
Matus Fabian82ad9662024-06-04 19:00:00 +02001032 "POST /interface_stats.json HTTP/1.1\r\n Content-Length: 4 \r\n\r\n"+ifName)
Adrian Villin4677d922024-06-14 09:32:39 +02001033 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001034 validatePostInterfaceStats(s, resp)
1035
Adrian Villin4677d922024-06-14 09:32:39 +02001036 resp, err = TcpSendReceive(serverAddress+":80",
Matus Fabian82ad9662024-06-04 19:00:00 +02001037 "POST /interface_stats.json HTTP/1.1\r\n\tContent-Length:\t\t4\r\n\r\n"+ifName)
Adrian Villin4677d922024-06-14 09:32:39 +02001038 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001039 validatePostInterfaceStats(s, resp)
1040}
1041
1042func HttpMethodNotImplementedTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001043 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001044 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001045 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001046
Adrian Villin4677d922024-06-14 09:32:39 +02001047 client := NewHttpClient()
Matus Fabian82ad9662024-06-04 19:00:00 +02001048 req, err := http.NewRequest("OPTIONS", "http://"+serverAddress+":80/show/version", nil)
Adrian Villin4677d922024-06-14 09:32:39 +02001049 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001050 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +02001051 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian82ad9662024-06-04 19:00:00 +02001052 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +02001053 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +02001054 s.AssertHttpStatus(resp, 501)
1055 s.AssertHttpHeaderNotPresent(resp, "Content-Type")
1056 s.AssertHttpContentLength(resp, int64(0))
Matus Fabian82ad9662024-06-04 19:00:00 +02001057}
1058
1059func HttpVersionNotSupportedTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001060 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001061 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001062 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001063
Adrian Villin4677d922024-06-14 09:32:39 +02001064 resp, err := TcpSendReceive(serverAddress+":80", "GET / HTTP/2\r\n\r\n")
1065 s.AssertNil(err, fmt.Sprint(err))
1066 s.AssertContains(resp, "HTTP/1.1 505 HTTP Version Not Supported")
Matus Fabian82ad9662024-06-04 19:00:00 +02001067}
1068
1069func HttpUriDecodeTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001070 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001071 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001072 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001073
Adrian Villin4677d922024-06-14 09:32:39 +02001074 client := NewHttpClient()
Matus Fabian82ad9662024-06-04 19:00:00 +02001075 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/sh%6fw%20versio%6E%20verbose", 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))
Adrian Villin4677d922024-06-14 09:32:39 +02001084 s.AssertNotContains(string(data), "unknown input")
1085 s.AssertContains(string(data), "Compiler")
Matus Fabiana647a832024-08-26 18:01:14 +02001086 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html")
Matus Fabian82ad9662024-06-04 19:00:00 +02001087}
1088
1089func HttpHeadersTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001090 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001091 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001092 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001093
Adrian Villin4677d922024-06-14 09:32:39 +02001094 resp, err := TcpSendReceive(
Matus Fabian82ad9662024-06-04 19:00:00 +02001095 serverAddress+":80",
1096 "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent:test\r\nAccept:text/xml\r\nAccept:\ttext/plain\t \r\nAccept:text/html\r\n\r\n")
Adrian Villin4677d922024-06-14 09:32:39 +02001097 s.AssertNil(err, fmt.Sprint(err))
1098 s.AssertContains(resp, "HTTP/1.1 200 OK")
Adrian Villin3601b322024-08-19 14:41:35 +02001099 s.AssertContains(resp, "Content-Type: text/plain")
Adrian Villin4677d922024-06-14 09:32:39 +02001100 s.AssertNotContains(resp, "<html>", "html content received instead of plain text")
Matus Fabian82ad9662024-06-04 19:00:00 +02001101}
1102
1103func HttpInvalidHeadersTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001104 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001105 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001106 vpp.Vppctl("http cli server")
Matus Fabian82ad9662024-06-04 19:00:00 +02001107
Adrian Villin4677d922024-06-14 09:32:39 +02001108 resp, err := TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nUser-Agent: test\r\n")
1109 s.AssertNil(err, fmt.Sprint(err))
1110 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Header section must end with CRLF CRLF")
Matus Fabian82ad9662024-06-04 19:00:00 +02001111
Adrian Villin4677d922024-06-14 09:32:39 +02001112 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser@Agent:test\r\n\r\n")
1113 s.AssertNil(err, fmt.Sprint(err))
1114 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'@' not allowed in field name")
Matus Fabian82ad9662024-06-04 19:00:00 +02001115
Adrian Villin4677d922024-06-14 09:32:39 +02001116 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent\r\n\r\n")
1117 s.AssertNil(err, fmt.Sprint(err))
1118 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "incomplete field line not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001119
Adrian Villin4677d922024-06-14 09:32:39 +02001120 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\n: test\r\n\r\n")
1121 s.AssertNil(err, fmt.Sprint(err))
1122 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field name not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001123
Adrian Villin4677d922024-06-14 09:32:39 +02001124 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\rUser-Agent:test\r\n\r\n")
1125 s.AssertNil(err, fmt.Sprint(err))
1126 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid field line end not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001127
Adrian Villin4677d922024-06-14 09:32:39 +02001128 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\nUser-Agent:test\r\n\r\n")
1129 s.AssertNil(err, fmt.Sprint(err))
1130 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid field line end not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001131
Adrian Villin4677d922024-06-14 09:32:39 +02001132 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent:\r\n\r\n")
1133 s.AssertNil(err, fmt.Sprint(err))
1134 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field value not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001135
Adrian Villin4677d922024-06-14 09:32:39 +02001136 resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent: \r\n\r\n")
1137 s.AssertNil(err, fmt.Sprint(err))
1138 s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field value not allowed")
Matus Fabian82ad9662024-06-04 19:00:00 +02001139}
1140
Matus Fabianb7a9ed72024-05-10 16:20:40 +02001141func HeaderServerTest(s *NoTopoSuite) {
Adrian Villin4677d922024-06-14 09:32:39 +02001142 vpp := s.GetContainerByName("vpp").VppInstance
Matus Fabiana647a832024-08-26 18:01:14 +02001143 serverAddress := s.VppAddr()
Adrian Villin4677d922024-06-14 09:32:39 +02001144 vpp.Vppctl("http cli server")
Matus Fabianb7a9ed72024-05-10 16:20:40 +02001145
Adrian Villin4677d922024-06-14 09:32:39 +02001146 client := NewHttpClient()
Matus Fabianb7a9ed72024-05-10 16:20:40 +02001147 req, err := http.NewRequest("GET", "http://"+serverAddress+":80/show/version", nil)
Adrian Villin4677d922024-06-14 09:32:39 +02001148 s.AssertNil(err, fmt.Sprint(err))
Matus Fabian595d8cb2024-05-17 11:28:43 +02001149 resp, err := client.Do(req)
Adrian Villin4677d922024-06-14 09:32:39 +02001150 s.AssertNil(err, fmt.Sprint(err))
Matus Fabianb7a9ed72024-05-10 16:20:40 +02001151 defer resp.Body.Close()
Matus Fabian8ca6ce62024-06-20 17:08:26 +02001152 s.Log(DumpHttpResp(resp, true))
Matus Fabiana647a832024-08-26 18:01:14 +02001153 s.AssertHttpStatus(resp, 200)
1154 s.AssertHttpHeaderWithValue(resp, "Server", "http_cli_server")
1155 s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html")
Matus Fabian616201a2024-05-02 11:17:15 +02001156}