Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
Filip Tehlar | c204c87 | 2022-12-21 08:59:16 +0100 | [diff] [blame] | 3 | import ( |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 4 | "bytes" |
Filip Tehlar | 8df3de4 | 2023-01-27 13:14:34 +0100 | [diff] [blame] | 5 | "fmt" |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 6 | "github.com/onsi/gomega/ghttp" |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 7 | "github.com/onsi/gomega/gmeasure" |
| 8 | "io" |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 9 | "net" |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 10 | "net/http" |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 11 | "net/http/httptrace" |
| 12 | "os" |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 13 | "strconv" |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 14 | "sync" |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 15 | "time" |
| 16 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 17 | . "fd.io/hs-test/infra" |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 18 | . "github.com/onsi/ginkgo/v2" |
Filip Tehlar | c204c87 | 2022-12-21 08:59:16 +0100 | [diff] [blame] | 19 | ) |
| 20 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 21 | func init() { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 22 | RegisterVethTests(HttpCliTest, HttpCliConnectErrorTest) |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 23 | RegisterSoloVethTests(HttpClientGetMemLeakTest) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 24 | RegisterNoTopoTests(HeaderServerTest, HttpPersistentConnectionTest, HttpPipeliningTest, |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 25 | HttpStaticMovedTest, HttpStaticNotFoundTest, HttpCliMethodNotAllowedTest, |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 26 | HttpCliBadRequestTest, HttpStaticBuildInUrlGetIfStatsTest, HttpStaticBuildInUrlPostIfStatsTest, |
| 27 | HttpInvalidRequestLineTest, HttpMethodNotImplementedTest, HttpInvalidHeadersTest, |
| 28 | HttpContentLengthTest, HttpStaticBuildInUrlGetIfListTest, HttpStaticBuildInUrlGetVersionTest, |
| 29 | HttpStaticMacTimeTest, HttpStaticBuildInUrlGetVersionVerboseTest, HttpVersionNotSupportedTest, |
| 30 | HttpInvalidContentLengthTest, HttpInvalidTargetSyntaxTest, HttpStaticPathTraversalTest, HttpUriDecodeTest, |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 31 | HttpHeadersTest, HttpStaticFileHandlerTest, HttpStaticFileHandlerDefaultMaxAgeTest, HttpClientTest, HttpClientErrRespTest, HttpClientPostFormTest, |
Matus Fabian | d58177c | 2024-08-08 12:50:32 +0200 | [diff] [blame] | 32 | HttpClientPostFileTest, HttpClientPostFilePtrTest, AuthorityFormTargetTest) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 33 | RegisterNoTopoSoloTests(HttpStaticPromTest, HttpTpsTest, HttpTpsInterruptModeTest, PromConcurrentConnectionsTest, |
Matus Fabian | 5546755 | 2024-08-06 15:55:26 +0200 | [diff] [blame] | 34 | PromMemLeakTest, HttpClientPostMemLeakTest, HttpInvalidClientRequestMemLeakTest) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 37 | const wwwRootPath = "/tmp/www_root" |
| 38 | |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 39 | func httpDownloadBenchmark(s *HstSuite, experiment *gmeasure.Experiment, data interface{}) { |
| 40 | url, isValid := data.(string) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 41 | s.AssertEqual(true, isValid) |
| 42 | client := NewHttpClient() |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 43 | req, err := http.NewRequest("GET", url, nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 44 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 45 | t := time.Now() |
| 46 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 47 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 48 | defer resp.Body.Close() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 49 | s.AssertEqual(200, resp.StatusCode) |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 50 | _, err = io.ReadAll(resp.Body) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 51 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 52 | duration := time.Since(t) |
| 53 | experiment.RecordValue("Download Speed", (float64(resp.ContentLength)/1024/1024)/duration.Seconds(), gmeasure.Units("MB/s"), gmeasure.Precision(2)) |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 54 | } |
| 55 | |
Adrian Villin | 1fde999 | 2024-06-24 08:14:05 -0400 | [diff] [blame] | 56 | func HttpTpsInterruptModeTest(s *NoTopoSuite) { |
| 57 | HttpTpsTest(s) |
| 58 | } |
| 59 | |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 60 | func HttpTpsTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 61 | vpp := s.GetContainerByName("vpp").VppInstance |
| 62 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 63 | url := "http://" + serverAddress + ":8080/test_file_10M" |
| 64 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 65 | vpp.Vppctl("http tps uri tcp://0.0.0.0/8080") |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 66 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 67 | s.RunBenchmark("HTTP tps 10M", 10, 0, httpDownloadBenchmark, url) |
Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 68 | } |
| 69 | |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 70 | func HttpPersistentConnectionTest(s *NoTopoSuite) { |
| 71 | // testing url handler app do not support multi-thread |
| 72 | s.SkipIfMultiWorker() |
| 73 | vpp := s.GetContainerByName("vpp").VppInstance |
| 74 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 75 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers")) |
| 76 | s.Log(vpp.Vppctl("test-url-handler enable")) |
| 77 | |
| 78 | transport := http.DefaultTransport |
| 79 | transport.(*http.Transport).Proxy = nil |
| 80 | transport.(*http.Transport).DisableKeepAlives = false |
| 81 | client := &http.Client{ |
| 82 | Transport: transport, |
| 83 | Timeout: time.Second * 30, |
| 84 | CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 85 | return http.ErrUseLastResponse |
| 86 | }} |
| 87 | |
| 88 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/test1", nil) |
| 89 | s.AssertNil(err, fmt.Sprint(err)) |
| 90 | resp, err := client.Do(req) |
| 91 | s.AssertNil(err, fmt.Sprint(err)) |
| 92 | defer resp.Body.Close() |
| 93 | s.Log(DumpHttpResp(resp, true)) |
| 94 | s.AssertEqual(200, resp.StatusCode) |
| 95 | s.AssertEqual(false, resp.Close) |
| 96 | body, err := io.ReadAll(resp.Body) |
| 97 | s.AssertNil(err, fmt.Sprint(err)) |
| 98 | s.AssertEqual(string(body), "hello") |
| 99 | o1 := vpp.Vppctl("show session verbose proto http state ready") |
| 100 | s.Log(o1) |
| 101 | s.AssertContains(o1, "ESTABLISHED") |
| 102 | |
| 103 | req, err = http.NewRequest("GET", "http://"+serverAddress+":80/test2", nil) |
| 104 | s.AssertNil(err, fmt.Sprint(err)) |
| 105 | clientTrace := &httptrace.ClientTrace{ |
| 106 | GotConn: func(info httptrace.GotConnInfo) { |
| 107 | s.AssertEqual(true, info.Reused, "connection not reused") |
| 108 | }, |
| 109 | } |
| 110 | req = req.WithContext(httptrace.WithClientTrace(req.Context(), clientTrace)) |
| 111 | resp, err = client.Do(req) |
| 112 | s.AssertNil(err, fmt.Sprint(err)) |
| 113 | defer resp.Body.Close() |
| 114 | s.Log(DumpHttpResp(resp, true)) |
| 115 | s.AssertEqual(200, resp.StatusCode) |
| 116 | s.AssertEqual(false, resp.Close) |
| 117 | body, err = io.ReadAll(resp.Body) |
| 118 | s.AssertNil(err, fmt.Sprint(err)) |
| 119 | s.AssertEqual(string(body), "some data") |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 120 | o2 := vpp.Vppctl("show session verbose proto http state ready") |
| 121 | s.Log(o2) |
| 122 | s.AssertContains(o2, "ESTABLISHED") |
| 123 | s.AssertEqual(o1, o2) |
| 124 | } |
| 125 | |
| 126 | func HttpPipeliningTest(s *NoTopoSuite) { |
| 127 | // testing url handler app do not support multi-thread |
| 128 | s.SkipIfMultiWorker() |
| 129 | vpp := s.GetContainerByName("vpp").VppInstance |
| 130 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 131 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug")) |
| 132 | s.Log(vpp.Vppctl("test-url-handler enable")) |
| 133 | |
| 134 | req1 := "GET /test_delayed HTTP/1.1\r\nHost:" + serverAddress + ":80\r\nUser-Agent:test\r\n\r\n" |
| 135 | req2 := "GET /test1 HTTP/1.1\r\nHost:" + serverAddress + ":80\r\nUser-Agent:test\r\n\r\n" |
| 136 | |
| 137 | conn, err := net.DialTimeout("tcp", serverAddress+":80", time.Second*30) |
| 138 | s.AssertNil(err, fmt.Sprint(err)) |
| 139 | defer conn.Close() |
| 140 | err = conn.SetDeadline(time.Now().Add(time.Second * 15)) |
| 141 | s.AssertNil(err, fmt.Sprint(err)) |
| 142 | n, err := conn.Write([]byte(req1)) |
| 143 | s.AssertNil(err, fmt.Sprint(err)) |
| 144 | s.AssertEqual(n, len([]rune(req1))) |
| 145 | // send second request a bit later so first is already in progress |
| 146 | time.Sleep(500 * time.Millisecond) |
| 147 | n, err = conn.Write([]byte(req2)) |
| 148 | s.AssertNil(err, fmt.Sprint(err)) |
| 149 | s.AssertEqual(n, len([]rune(req2))) |
| 150 | reply := make([]byte, 1024) |
| 151 | n, err = conn.Read(reply) |
| 152 | s.AssertNil(err, fmt.Sprint(err)) |
| 153 | s.Log(string(reply)) |
| 154 | s.AssertContains(string(reply), "delayed data", "first request response not received") |
| 155 | s.AssertNotContains(string(reply), "hello", "second request response received") |
| 156 | // make sure response for second request is not received later |
| 157 | _, err = conn.Read(reply) |
| 158 | s.AssertMatchError(err, os.ErrDeadlineExceeded, "second request response received") |
| 159 | } |
| 160 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 161 | func HttpCliTest(s *VethsSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 162 | serverContainer := s.GetContainerByName("server-vpp") |
| 163 | clientContainer := s.GetContainerByName("client-vpp") |
Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 164 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 165 | serverVeth := s.GetInterfaceByName(ServerInterfaceName) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 166 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 167 | serverContainer.VppInstance.Vppctl("http cli server") |
Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 168 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 169 | uri := "http://" + serverVeth.Ip4AddressString() + "/80" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 170 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 171 | o := clientContainer.VppInstance.Vppctl("http cli client" + |
Filip Tehlar | d894438 | 2023-11-27 13:28:36 +0100 | [diff] [blame] | 172 | " uri " + uri + " query /show/vlib/graph") |
Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 173 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 174 | s.Log(o) |
| 175 | s.AssertContains(o, "<html>", "<html> not found in the result!") |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 176 | s.AssertContains(o, "</html>", "</html> not found in the result!") |
Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 177 | } |
Filip Tehlar | c204c87 | 2022-12-21 08:59:16 +0100 | [diff] [blame] | 178 | |
Matus Fabian | 3d00893 | 2024-05-13 10:29:11 +0200 | [diff] [blame] | 179 | func HttpCliConnectErrorTest(s *VethsSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 180 | clientContainer := s.GetContainerByName("client-vpp") |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 181 | serverVeth := s.GetInterfaceByName(ServerInterfaceName) |
Matus Fabian | 3d00893 | 2024-05-13 10:29:11 +0200 | [diff] [blame] | 182 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 183 | uri := "http://" + serverVeth.Ip4AddressString() + "/80" |
Matus Fabian | 3d00893 | 2024-05-13 10:29:11 +0200 | [diff] [blame] | 184 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 185 | o := clientContainer.VppInstance.Vppctl("http cli client" + |
Matus Fabian | 3d00893 | 2024-05-13 10:29:11 +0200 | [diff] [blame] | 186 | " uri " + uri + " query /show/vlib/graph") |
| 187 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 188 | s.Log(o) |
| 189 | s.AssertContains(o, "failed to connect") |
Matus Fabian | 3d00893 | 2024-05-13 10:29:11 +0200 | [diff] [blame] | 190 | } |
| 191 | |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 192 | func HttpClientTest(s *NoTopoSuite) { |
| 193 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Ip4AddressString() |
| 194 | server := ghttp.NewUnstartedServer() |
| 195 | l, err := net.Listen("tcp", serverAddress+":80") |
| 196 | s.AssertNil(err, fmt.Sprint(err)) |
| 197 | server.HTTPTestServer.Listener = l |
| 198 | server.AppendHandlers( |
| 199 | ghttp.CombineHandlers( |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 200 | s.LogHttpReq(true), |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 201 | ghttp.VerifyRequest("GET", "/test"), |
| 202 | ghttp.VerifyHeader(http.Header{"User-Agent": []string{"http_cli_client"}}), |
Adrian Villin | 3601b32 | 2024-08-19 14:41:35 +0200 | [diff] [blame] | 203 | ghttp.VerifyHeader(http.Header{"Accept": []string{"text/html"}}), |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 204 | ghttp.RespondWith(http.StatusOK, "<html><body><p>Hello</p></body></html>"), |
| 205 | )) |
| 206 | server.Start() |
| 207 | defer server.Close() |
| 208 | uri := "http://" + serverAddress + "/80" |
| 209 | vpp := s.GetContainerByName("vpp").VppInstance |
| 210 | o := vpp.Vppctl("http cli client uri " + uri + " query /test") |
| 211 | |
| 212 | s.Log(o) |
| 213 | s.AssertContains(o, "<html>", "<html> not found in the result!") |
| 214 | s.AssertContains(o, "</html>", "</html> not found in the result!") |
| 215 | } |
| 216 | |
| 217 | func HttpClientErrRespTest(s *NoTopoSuite) { |
| 218 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Ip4AddressString() |
| 219 | server := ghttp.NewUnstartedServer() |
| 220 | l, err := net.Listen("tcp", serverAddress+":80") |
| 221 | s.AssertNil(err, fmt.Sprint(err)) |
| 222 | server.HTTPTestServer.Listener = l |
| 223 | server.AppendHandlers( |
| 224 | ghttp.CombineHandlers( |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 225 | s.LogHttpReq(true), |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 226 | ghttp.VerifyRequest("GET", "/test"), |
| 227 | ghttp.RespondWith(http.StatusNotFound, "404: Not Found"), |
| 228 | )) |
| 229 | server.Start() |
| 230 | defer server.Close() |
| 231 | uri := "http://" + serverAddress + "/80" |
| 232 | vpp := s.GetContainerByName("vpp").VppInstance |
| 233 | o := vpp.Vppctl("http cli client uri " + uri + " query /test") |
| 234 | |
| 235 | s.Log(o) |
| 236 | s.AssertContains(o, "404: Not Found", "error not found in the result!") |
| 237 | } |
| 238 | |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 239 | func HttpClientPostFormTest(s *NoTopoSuite) { |
| 240 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Ip4AddressString() |
| 241 | body := "field1=value1&field2=value2" |
| 242 | |
| 243 | server := ghttp.NewUnstartedServer() |
| 244 | l, err := net.Listen("tcp", serverAddress+":80") |
| 245 | s.AssertNil(err, fmt.Sprint(err)) |
| 246 | server.HTTPTestServer.Listener = l |
| 247 | server.AppendHandlers( |
| 248 | ghttp.CombineHandlers( |
| 249 | s.LogHttpReq(true), |
| 250 | ghttp.VerifyRequest("POST", "/test"), |
Adrian Villin | 3601b32 | 2024-08-19 14:41:35 +0200 | [diff] [blame] | 251 | ghttp.VerifyContentType("application/x-www-form-urlencoded"), |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 252 | ghttp.VerifyBody([]byte(body)), |
| 253 | ghttp.RespondWith(http.StatusOK, nil), |
| 254 | )) |
| 255 | server.Start() |
| 256 | defer server.Close() |
| 257 | |
| 258 | uri := "http://" + serverAddress + "/80" |
| 259 | vpp := s.GetContainerByName("vpp").VppInstance |
| 260 | o := vpp.Vppctl("http post uri " + uri + " target /test data " + body) |
| 261 | |
| 262 | s.Log(o) |
| 263 | s.AssertNotContains(o, "error") |
| 264 | } |
| 265 | |
| 266 | func httpClientPostFile(s *NoTopoSuite, usePtr bool, fileSize int) { |
| 267 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Ip4AddressString() |
| 268 | vpp := s.GetContainerByName("vpp").VppInstance |
| 269 | fileName := "/tmp/test_file.txt" |
| 270 | s.Log(vpp.Container.Exec("fallocate -l " + strconv.Itoa(fileSize) + " " + fileName)) |
| 271 | s.Log(vpp.Container.Exec("ls -la " + fileName)) |
| 272 | |
| 273 | server := ghttp.NewUnstartedServer() |
| 274 | l, err := net.Listen("tcp", serverAddress+":80") |
| 275 | s.AssertNil(err, fmt.Sprint(err)) |
| 276 | server.HTTPTestServer.Listener = l |
| 277 | server.AppendHandlers( |
| 278 | ghttp.CombineHandlers( |
| 279 | s.LogHttpReq(false), |
| 280 | ghttp.VerifyRequest("POST", "/test"), |
| 281 | ghttp.VerifyHeader(http.Header{"Content-Length": []string{strconv.Itoa(fileSize)}}), |
Adrian Villin | 3601b32 | 2024-08-19 14:41:35 +0200 | [diff] [blame] | 282 | ghttp.VerifyContentType("application/octet-stream"), |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 283 | ghttp.RespondWith(http.StatusOK, nil), |
| 284 | )) |
| 285 | server.Start() |
| 286 | defer server.Close() |
| 287 | |
| 288 | uri := "http://" + serverAddress + "/80" |
| 289 | cmd := "http post uri " + uri + " target /test file " + fileName |
| 290 | if usePtr { |
| 291 | cmd += " use-ptr" |
| 292 | } |
| 293 | o := vpp.Vppctl(cmd) |
| 294 | |
| 295 | s.Log(o) |
| 296 | s.AssertNotContains(o, "error") |
| 297 | } |
| 298 | |
| 299 | func HttpClientPostFileTest(s *NoTopoSuite) { |
| 300 | httpClientPostFile(s, false, 32768) |
| 301 | } |
| 302 | |
| 303 | func HttpClientPostFilePtrTest(s *NoTopoSuite) { |
| 304 | httpClientPostFile(s, true, 131072) |
| 305 | } |
| 306 | |
Matus Fabian | d58177c | 2024-08-08 12:50:32 +0200 | [diff] [blame] | 307 | func cliTestAuthority(s *NoTopoSuite, authority string) { |
| 308 | o := s.GetContainerByName("vpp").VppInstance.Vppctl("test http authority-form " + authority) |
| 309 | s.AssertNotContains(o, "error") |
| 310 | s.AssertContains(o, authority) |
| 311 | } |
| 312 | |
| 313 | func cliTestAuthorityError(s *NoTopoSuite, authority string) { |
| 314 | o := s.GetContainerByName("vpp").VppInstance.Vppctl("test http authority-form " + authority) |
| 315 | s.AssertContains(o, "error") |
| 316 | } |
| 317 | |
| 318 | func AuthorityFormTargetTest(s *NoTopoSuite) { |
| 319 | cliTestAuthority(s, "10.10.2.45:20") |
| 320 | cliTestAuthority(s, "[dead:beef::1234]:443") |
| 321 | cliTestAuthorityError(s, "example.com:80") |
| 322 | cliTestAuthorityError(s, "10.10.2.45") |
| 323 | cliTestAuthorityError(s, "1000.10.2.45:20") |
| 324 | cliTestAuthorityError(s, "[xyz0::1234]:443") |
| 325 | } |
| 326 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 327 | func HttpStaticPromTest(s *NoTopoSuite) { |
Filip Tehlar | cc1475c | 2023-11-29 12:59:05 +0100 | [diff] [blame] | 328 | query := "stats.prom" |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 329 | vpp := s.GetContainerByName("vpp").VppInstance |
| 330 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 331 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers")) |
| 332 | s.Log(vpp.Vppctl("prom enable")) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 333 | time.Sleep(time.Second * 5) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 334 | client := NewHttpClient() |
| 335 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/"+query, nil) |
| 336 | s.AssertNil(err, fmt.Sprint(err)) |
| 337 | resp, err := client.Do(req) |
| 338 | s.AssertNil(err, fmt.Sprint(err)) |
| 339 | defer resp.Body.Close() |
| 340 | s.Log(DumpHttpResp(resp, false)) |
| 341 | s.AssertEqual(200, resp.StatusCode) |
| 342 | s.AssertContains(resp.Header.Get("Content-Type"), "text") |
| 343 | s.AssertContains(resp.Header.Get("Content-Type"), "plain") |
| 344 | s.AssertNotEqual(int64(0), resp.ContentLength) |
| 345 | _, err = io.ReadAll(resp.Body) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 346 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 347 | } |
| 348 | |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 349 | func promReq(s *NoTopoSuite, url string) { |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 350 | client := NewHttpClient() |
| 351 | req, err := http.NewRequest("GET", url, nil) |
| 352 | s.AssertNil(err, fmt.Sprint(err)) |
| 353 | resp, err := client.Do(req) |
| 354 | s.AssertNil(err, fmt.Sprint(err)) |
| 355 | defer resp.Body.Close() |
| 356 | s.AssertEqual(200, resp.StatusCode) |
| 357 | _, err = io.ReadAll(resp.Body) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 358 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 359 | } |
| 360 | |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 361 | func promReqWg(s *NoTopoSuite, url string, wg *sync.WaitGroup) { |
| 362 | defer GinkgoRecover() |
| 363 | defer wg.Done() |
| 364 | promReq(s, url) |
| 365 | } |
| 366 | |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 367 | func PromConcurrentConnectionsTest(s *NoTopoSuite) { |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 368 | vpp := s.GetContainerByName("vpp").VppInstance |
| 369 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 370 | url := "http://" + serverAddress + ":80/stats.prom" |
| 371 | |
| 372 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers")) |
| 373 | s.Log(vpp.Vppctl("prom enable")) |
| 374 | time.Sleep(time.Second * 5) |
| 375 | |
| 376 | var wg sync.WaitGroup |
| 377 | for i := 0; i < 20; i++ { |
| 378 | wg.Add(1) |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 379 | go promReqWg(s, url, &wg) |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 380 | } |
| 381 | wg.Wait() |
| 382 | s.Log(vpp.Vppctl("show session verbose proto http")) |
| 383 | } |
| 384 | |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 385 | func PromMemLeakTest(s *NoTopoSuite) { |
| 386 | s.SkipUnlessLeakCheck() |
| 387 | |
| 388 | vpp := s.GetContainerByName("vpp").VppInstance |
| 389 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 390 | url := "http://" + serverAddress + ":80/stats.prom" |
| 391 | |
| 392 | /* no goVPP less noise */ |
| 393 | vpp.Disconnect() |
| 394 | |
| 395 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers")) |
| 396 | s.Log(vpp.Vppctl("prom enable")) |
| 397 | time.Sleep(time.Second * 3) |
| 398 | |
| 399 | /* warmup request (FIB) */ |
| 400 | promReq(s, url) |
| 401 | |
| 402 | vpp.EnableMemoryTrace() |
| 403 | traces1, err := vpp.GetMemoryTrace() |
| 404 | s.AssertNil(err, fmt.Sprint(err)) |
| 405 | |
| 406 | /* collect stats couple of times */ |
| 407 | for i := 0; i < 5; i++ { |
| 408 | time.Sleep(time.Second * 1) |
| 409 | promReq(s, url) |
| 410 | } |
| 411 | |
| 412 | /* let's give it some time to clean up sessions */ |
| 413 | time.Sleep(time.Second * 5) |
| 414 | |
| 415 | traces2, err := vpp.GetMemoryTrace() |
| 416 | s.AssertNil(err, fmt.Sprint(err)) |
| 417 | vpp.MemLeakCheck(traces1, traces2) |
| 418 | } |
| 419 | |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 420 | func HttpClientGetMemLeakTest(s *VethsSuite) { |
| 421 | s.SkipUnlessLeakCheck() |
| 422 | |
| 423 | serverContainer := s.GetContainerByName("server-vpp").VppInstance |
| 424 | clientContainer := s.GetContainerByName("client-vpp").VppInstance |
| 425 | serverVeth := s.GetInterfaceByName(ServerInterfaceName) |
| 426 | |
| 427 | /* no goVPP less noise */ |
| 428 | clientContainer.Disconnect() |
| 429 | |
| 430 | serverContainer.Vppctl("http cli server") |
| 431 | |
| 432 | uri := "http://" + serverVeth.Ip4AddressString() + "/80" |
| 433 | |
| 434 | /* warmup request (FIB) */ |
| 435 | clientContainer.Vppctl("http cli client uri " + uri + " query /show/version") |
| 436 | |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 437 | /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */ |
| 438 | time.Sleep(time.Second * 12) |
| 439 | |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 440 | clientContainer.EnableMemoryTrace() |
| 441 | traces1, err := clientContainer.GetMemoryTrace() |
| 442 | s.AssertNil(err, fmt.Sprint(err)) |
| 443 | |
| 444 | clientContainer.Vppctl("http cli client uri " + uri + " query /show/vlib/graph") |
| 445 | |
| 446 | /* let's give it some time to clean up sessions */ |
| 447 | time.Sleep(time.Second * 12) |
| 448 | |
| 449 | traces2, err := clientContainer.GetMemoryTrace() |
| 450 | s.AssertNil(err, fmt.Sprint(err)) |
| 451 | clientContainer.MemLeakCheck(traces1, traces2) |
| 452 | } |
| 453 | |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 454 | func HttpClientPostMemLeakTest(s *NoTopoSuite) { |
| 455 | s.SkipUnlessLeakCheck() |
| 456 | |
| 457 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Ip4AddressString() |
| 458 | body := "field1=value1&field2=value2" |
| 459 | |
| 460 | uri := "http://" + serverAddress + "/80" |
| 461 | vpp := s.GetContainerByName("vpp").VppInstance |
| 462 | |
| 463 | /* no goVPP less noise */ |
| 464 | vpp.Disconnect() |
| 465 | |
| 466 | server := ghttp.NewUnstartedServer() |
| 467 | l, err := net.Listen("tcp", serverAddress+":80") |
| 468 | s.AssertNil(err, fmt.Sprint(err)) |
| 469 | server.HTTPTestServer.Listener = l |
| 470 | server.AppendHandlers( |
| 471 | ghttp.CombineHandlers( |
| 472 | ghttp.VerifyRequest("POST", "/test"), |
| 473 | ghttp.RespondWith(http.StatusOK, nil), |
| 474 | ), |
| 475 | ghttp.CombineHandlers( |
| 476 | ghttp.VerifyRequest("POST", "/test"), |
| 477 | ghttp.RespondWith(http.StatusOK, nil), |
| 478 | ), |
| 479 | ) |
| 480 | server.Start() |
| 481 | defer server.Close() |
| 482 | |
| 483 | /* warmup request (FIB) */ |
| 484 | vpp.Vppctl("http post uri " + uri + " target /test data " + body) |
| 485 | |
| 486 | /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */ |
| 487 | time.Sleep(time.Second * 12) |
| 488 | |
| 489 | vpp.EnableMemoryTrace() |
| 490 | traces1, err := vpp.GetMemoryTrace() |
| 491 | s.AssertNil(err, fmt.Sprint(err)) |
| 492 | |
| 493 | vpp.Vppctl("http post uri " + uri + " target /test data " + body) |
| 494 | |
| 495 | /* let's give it some time to clean up sessions */ |
| 496 | time.Sleep(time.Second * 12) |
| 497 | |
| 498 | traces2, err := vpp.GetMemoryTrace() |
| 499 | s.AssertNil(err, fmt.Sprint(err)) |
| 500 | vpp.MemLeakCheck(traces1, traces2) |
| 501 | } |
| 502 | |
Matus Fabian | 5546755 | 2024-08-06 15:55:26 +0200 | [diff] [blame] | 503 | func HttpInvalidClientRequestMemLeakTest(s *NoTopoSuite) { |
| 504 | s.SkipUnlessLeakCheck() |
| 505 | |
| 506 | vpp := s.GetContainerByName("vpp").VppInstance |
| 507 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 508 | |
| 509 | /* no goVPP less noise */ |
| 510 | vpp.Disconnect() |
| 511 | |
| 512 | vpp.Vppctl("http cli server") |
| 513 | |
| 514 | /* warmup request (FIB) */ |
| 515 | _, err := TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n") |
| 516 | s.AssertNil(err, fmt.Sprint(err)) |
| 517 | |
| 518 | /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */ |
| 519 | time.Sleep(time.Second * 12) |
| 520 | |
| 521 | vpp.EnableMemoryTrace() |
| 522 | traces1, err := vpp.GetMemoryTrace() |
| 523 | s.AssertNil(err, fmt.Sprint(err)) |
| 524 | |
| 525 | _, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n") |
| 526 | s.AssertNil(err, fmt.Sprint(err)) |
| 527 | |
| 528 | /* let's give it some time to clean up sessions */ |
| 529 | time.Sleep(time.Second * 12) |
| 530 | |
| 531 | traces2, err := vpp.GetMemoryTrace() |
| 532 | s.AssertNil(err, fmt.Sprint(err)) |
| 533 | vpp.MemLeakCheck(traces1, traces2) |
| 534 | |
| 535 | } |
| 536 | |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 537 | func HttpStaticFileHandlerDefaultMaxAgeTest(s *NoTopoSuite) { |
| 538 | HttpStaticFileHandlerTestFunction(s, "default") |
| 539 | } |
| 540 | |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 541 | func HttpStaticFileHandlerTest(s *NoTopoSuite) { |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 542 | HttpStaticFileHandlerTestFunction(s, "123") |
| 543 | } |
| 544 | |
| 545 | func HttpStaticFileHandlerTestFunction(s *NoTopoSuite, max_age string) { |
| 546 | var max_age_formatted string |
| 547 | if max_age == "default" { |
| 548 | max_age_formatted = "" |
| 549 | max_age = "600" |
| 550 | } else { |
| 551 | max_age_formatted = "max-age " + max_age |
| 552 | } |
| 553 | |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 554 | content := "<html><body><p>Hello</p></body></html>" |
| 555 | content2 := "<html><body><p>Page</p></body></html>" |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 556 | currentDate := time.Now().In(time.FixedZone("GMT", 0)).Format(http.TimeFormat)[:17] |
| 557 | |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 558 | vpp := s.GetContainerByName("vpp").VppInstance |
| 559 | vpp.Container.Exec("mkdir -p " + wwwRootPath) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 560 | err := vpp.Container.CreateFile(wwwRootPath+"/index.html", content) |
| 561 | s.AssertNil(err, fmt.Sprint(err)) |
| 562 | err = vpp.Container.CreateFile(wwwRootPath+"/page.html", content2) |
| 563 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 564 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 565 | s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug cache-size 2m " + max_age_formatted)) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 566 | |
| 567 | client := NewHttpClient() |
| 568 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/index.html", nil) |
| 569 | s.AssertNil(err, fmt.Sprint(err)) |
| 570 | resp, err := client.Do(req) |
| 571 | s.AssertNil(err, fmt.Sprint(err)) |
| 572 | defer resp.Body.Close() |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 573 | |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 574 | s.Log(DumpHttpResp(resp, true)) |
| 575 | s.AssertEqual(200, resp.StatusCode) |
| 576 | s.AssertContains(resp.Header.Get("Content-Type"), "html") |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 577 | s.AssertContains(resp.Header.Get("Cache-Control"), "max-age="+max_age) |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 578 | // only checking date |
| 579 | s.AssertContains(resp.Header.Get("Last-Modified"), currentDate) |
| 580 | s.AssertEqual(len(resp.Header.Get("Last-Modified")), 29) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 581 | s.AssertEqual(int64(len([]rune(content))), resp.ContentLength) |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 582 | |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 583 | body, err := io.ReadAll(resp.Body) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 584 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 585 | s.AssertEqual(string(body), content) |
| 586 | o := vpp.Vppctl("show http static server cache verbose") |
| 587 | s.Log(o) |
| 588 | s.AssertContains(o, "index.html") |
| 589 | s.AssertNotContains(o, "page.html") |
| 590 | |
| 591 | resp, err = client.Do(req) |
| 592 | s.AssertNil(err, fmt.Sprint(err)) |
| 593 | defer resp.Body.Close() |
| 594 | s.Log(DumpHttpResp(resp, true)) |
| 595 | s.AssertEqual(200, resp.StatusCode) |
| 596 | s.AssertContains(resp.Header.Get("Content-Type"), "html") |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 597 | s.AssertContains(resp.Header.Get("Cache-Control"), "max-age="+max_age) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 598 | s.AssertEqual(int64(len([]rune(content))), resp.ContentLength) |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 599 | |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 600 | body, err = io.ReadAll(resp.Body) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 601 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 602 | s.AssertEqual(string(body), content) |
| 603 | |
| 604 | req, err = http.NewRequest("GET", "http://"+serverAddress+":80/page.html", nil) |
| 605 | s.AssertNil(err, fmt.Sprint(err)) |
| 606 | resp, err = client.Do(req) |
| 607 | s.AssertNil(err, fmt.Sprint(err)) |
| 608 | defer resp.Body.Close() |
| 609 | s.Log(DumpHttpResp(resp, true)) |
| 610 | s.AssertEqual(200, resp.StatusCode) |
| 611 | s.AssertContains(resp.Header.Get("Content-Type"), "html") |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 612 | s.AssertContains(resp.Header.Get("Cache-Control"), "max-age="+max_age) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 613 | s.AssertEqual(int64(len([]rune(content2))), resp.ContentLength) |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 614 | |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 615 | body, err = io.ReadAll(resp.Body) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 616 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 617 | s.AssertEqual(string(body), content2) |
| 618 | o = vpp.Vppctl("show http static server cache verbose") |
| 619 | s.Log(o) |
| 620 | s.AssertContains(o, "index.html") |
| 621 | s.AssertContains(o, "page.html") |
Filip Tehlar | cc1475c | 2023-11-29 12:59:05 +0100 | [diff] [blame] | 622 | } |
| 623 | |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 624 | func HttpStaticPathTraversalTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 625 | vpp := s.GetContainerByName("vpp").VppInstance |
| 626 | vpp.Container.Exec("mkdir -p " + wwwRootPath) |
| 627 | vpp.Container.Exec("mkdir -p " + "/tmp/secret_folder") |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 628 | err := vpp.Container.CreateFile("/tmp/secret_folder/secret_file.txt", "secret") |
| 629 | s.AssertNil(err, fmt.Sprint(err)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 630 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 631 | s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug")) |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 632 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 633 | client := NewHttpClient() |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 634 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/../secret_folder/secret_file.txt", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 635 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 636 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 637 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 638 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 639 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 640 | s.AssertEqual(404, resp.StatusCode) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 641 | s.AssertEmpty(resp.Header.Get("Content-Type")) |
| 642 | s.AssertEmpty(resp.Header.Get("Cache-Control")) |
| 643 | s.AssertEqual(int64(0), resp.ContentLength) |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 644 | } |
| 645 | |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 646 | func HttpStaticMovedTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 647 | vpp := s.GetContainerByName("vpp").VppInstance |
| 648 | vpp.Container.Exec("mkdir -p " + wwwRootPath + "/tmp.aaa") |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 649 | err := vpp.Container.CreateFile(wwwRootPath+"/tmp.aaa/index.html", "<html><body><p>Hello</p></body></html>") |
| 650 | s.AssertNil(err, fmt.Sprint(err)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 651 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 652 | s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug")) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 653 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 654 | client := NewHttpClient() |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 655 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/tmp.aaa", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 656 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 657 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 658 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 659 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 660 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 661 | s.AssertEqual(301, resp.StatusCode) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 662 | s.AssertEqual("http://"+serverAddress+"/tmp.aaa/index.html", resp.Header.Get("Location")) |
| 663 | s.AssertEmpty(resp.Header.Get("Content-Type")) |
| 664 | s.AssertEmpty(resp.Header.Get("Cache-Control")) |
| 665 | s.AssertEqual(int64(0), resp.ContentLength) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | func HttpStaticNotFoundTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 669 | vpp := s.GetContainerByName("vpp").VppInstance |
| 670 | vpp.Container.Exec("mkdir -p " + wwwRootPath) |
| 671 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 672 | s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 debug")) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 673 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 674 | client := NewHttpClient() |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 675 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/notfound.html", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 676 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 595d8cb | 2024-05-17 11:28:43 +0200 | [diff] [blame] | 677 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 678 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 679 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 680 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 681 | s.AssertEqual(404, resp.StatusCode) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 682 | s.AssertEmpty(resp.Header.Get("Content-Type")) |
| 683 | s.AssertEmpty(resp.Header.Get("Cache-Control")) |
| 684 | s.AssertEqual(int64(0), resp.ContentLength) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 685 | } |
| 686 | |
| 687 | func HttpCliMethodNotAllowedTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 688 | vpp := s.GetContainerByName("vpp").VppInstance |
| 689 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 690 | vpp.Vppctl("http cli server") |
Matus Fabian | 616201a | 2024-05-02 11:17:15 +0200 | [diff] [blame] | 691 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 692 | client := NewHttpClient() |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 693 | req, err := http.NewRequest("POST", "http://"+serverAddress+":80/test", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 694 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 595d8cb | 2024-05-17 11:28:43 +0200 | [diff] [blame] | 695 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 696 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 697 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 698 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 699 | s.AssertEqual(405, resp.StatusCode) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 700 | s.AssertNotEqual("", resp.Header.Get("Allow"), "server MUST generate an Allow header") |
| 701 | s.AssertEmpty(resp.Header.Get("Content-Type")) |
| 702 | s.AssertEqual(int64(0), resp.ContentLength) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 703 | } |
| 704 | |
| 705 | func HttpCliBadRequestTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 706 | vpp := s.GetContainerByName("vpp").VppInstance |
| 707 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 708 | vpp.Vppctl("http cli server") |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 709 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 710 | client := NewHttpClient() |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 711 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 712 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 595d8cb | 2024-05-17 11:28:43 +0200 | [diff] [blame] | 713 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 714 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 715 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 716 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 717 | s.AssertEqual(400, resp.StatusCode) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 718 | s.AssertEmpty(resp.Header.Get("Content-Type")) |
| 719 | s.AssertEqual(int64(0), resp.ContentLength) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 720 | } |
| 721 | |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 722 | func HttpStaticBuildInUrlGetVersionTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 723 | vpp := s.GetContainerByName("vpp").VppInstance |
| 724 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 725 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug")) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 726 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 727 | client := NewHttpClient() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 728 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/version.json", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 729 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 730 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 731 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 732 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 733 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 734 | s.AssertEqual(200, resp.StatusCode) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 735 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 736 | s.AssertNil(err, fmt.Sprint(err)) |
| 737 | s.AssertContains(string(data), "vpp_details") |
| 738 | s.AssertContains(string(data), "version") |
| 739 | s.AssertContains(string(data), "build_date") |
| 740 | s.AssertNotContains(string(data), "build_by") |
| 741 | s.AssertNotContains(string(data), "build_host") |
| 742 | s.AssertNotContains(string(data), "build_dir") |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 743 | s.AssertContains(resp.Header.Get("Content-Type"), "json") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 744 | } |
| 745 | |
| 746 | func HttpStaticBuildInUrlGetVersionVerboseTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 747 | vpp := s.GetContainerByName("vpp").VppInstance |
| 748 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 749 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug")) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 750 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 751 | client := NewHttpClient() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 752 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/version.json?verbose=true", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 753 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 754 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 755 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 756 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 757 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 758 | s.AssertEqual(200, resp.StatusCode) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 759 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 760 | s.AssertNil(err, fmt.Sprint(err)) |
| 761 | s.AssertContains(string(data), "vpp_details") |
| 762 | s.AssertContains(string(data), "version") |
| 763 | s.AssertContains(string(data), "build_date") |
| 764 | s.AssertContains(string(data), "build_by") |
| 765 | s.AssertContains(string(data), "build_host") |
| 766 | s.AssertContains(string(data), "build_dir") |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 767 | s.AssertContains(resp.Header.Get("Content-Type"), "json") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 768 | } |
| 769 | |
| 770 | func HttpStaticBuildInUrlGetIfListTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 771 | vpp := s.GetContainerByName("vpp").VppInstance |
| 772 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 773 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug")) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 774 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 775 | client := NewHttpClient() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 776 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/interface_list.json", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 777 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 778 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 779 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 780 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 781 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 782 | s.AssertEqual(200, resp.StatusCode) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 783 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 784 | s.AssertNil(err, fmt.Sprint(err)) |
| 785 | s.AssertContains(string(data), "interface_list") |
| 786 | s.AssertContains(string(data), s.GetInterfaceByName(TapInterfaceName).Peer.Name()) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 787 | s.AssertContains(resp.Header.Get("Content-Type"), "json") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 788 | } |
| 789 | |
| 790 | func HttpStaticBuildInUrlGetIfStatsTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 791 | vpp := s.GetContainerByName("vpp").VppInstance |
| 792 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 793 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug")) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 794 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 795 | client := NewHttpClient() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 796 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/interface_stats.json", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 797 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 798 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 799 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 800 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 801 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 802 | s.AssertEqual(200, resp.StatusCode) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 803 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 804 | s.AssertNil(err, fmt.Sprint(err)) |
| 805 | s.AssertContains(string(data), "interface_stats") |
| 806 | s.AssertContains(string(data), "local0") |
| 807 | s.AssertContains(string(data), s.GetInterfaceByName(TapInterfaceName).Peer.Name()) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 808 | s.AssertContains(resp.Header.Get("Content-Type"), "json") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 809 | } |
| 810 | |
| 811 | func validatePostInterfaceStats(s *NoTopoSuite, data string) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 812 | s.AssertContains(data, "interface_stats") |
| 813 | s.AssertContains(data, s.GetInterfaceByName(TapInterfaceName).Peer.Name()) |
| 814 | s.AssertNotContains(data, "error") |
| 815 | s.AssertNotContains(data, "local0") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | func HttpStaticBuildInUrlPostIfStatsTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 819 | vpp := s.GetContainerByName("vpp").VppInstance |
| 820 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 821 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug")) |
| 822 | body := []byte(s.GetInterfaceByName(TapInterfaceName).Peer.Name()) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 823 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 824 | client := NewHttpClient() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 825 | req, err := http.NewRequest("POST", |
| 826 | "http://"+serverAddress+":80/interface_stats.json", bytes.NewBuffer(body)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 827 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 828 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 829 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 830 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 831 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 832 | s.AssertEqual(200, resp.StatusCode) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 833 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 834 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 835 | validatePostInterfaceStats(s, string(data)) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 836 | s.AssertContains(resp.Header.Get("Content-Type"), "json") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 837 | } |
| 838 | |
| 839 | func HttpStaticMacTimeTest(s *NoTopoSuite) { |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 840 | currentDate := time.Now().In(time.FixedZone("GMT", 0)).Format(http.TimeFormat)[:17] |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 841 | vpp := s.GetContainerByName("vpp").VppInstance |
| 842 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 843 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug")) |
| 844 | s.Log(vpp.Vppctl("mactime enable-disable " + s.GetInterfaceByName(TapInterfaceName).Peer.Name())) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 845 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 846 | client := NewHttpClient() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 847 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/mactime.json", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 848 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 849 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 850 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 851 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 852 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 853 | s.AssertEqual(200, resp.StatusCode) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 854 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 855 | s.AssertNil(err, fmt.Sprint(err)) |
| 856 | s.AssertContains(string(data), "mactime") |
| 857 | s.AssertContains(string(data), s.GetInterfaceByName(TapInterfaceName).Ip4AddressString()) |
| 858 | s.AssertContains(string(data), s.GetInterfaceByName(TapInterfaceName).HwAddress.String()) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 859 | s.AssertContains(resp.Header.Get("Content-Type"), "json") |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 860 | s.AssertEqual(len(resp.Header.Get("Date")), 29) |
| 861 | // only checking date |
Adrian Villin | 1fb7ae3 | 2024-08-19 10:51:59 +0200 | [diff] [blame] | 862 | s.AssertContains(resp.Header.Get("Date"), currentDate) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | func HttpInvalidRequestLineTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 866 | vpp := s.GetContainerByName("vpp").VppInstance |
| 867 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 868 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 869 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 870 | resp, err := TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1") |
| 871 | s.AssertNil(err, fmt.Sprint(err)) |
| 872 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid framing not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 873 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 874 | resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n") |
| 875 | s.AssertNil(err, fmt.Sprint(err)) |
| 876 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid framing not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 877 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 878 | resp, err = TcpSendReceive(serverAddress+":80", "GET /\r\n\r\n") |
| 879 | s.AssertNil(err, fmt.Sprint(err)) |
| 880 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "HTTP-version must be present") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 881 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 882 | resp, err = TcpSendReceive(serverAddress+":80", "GET HTTP/1.1\r\n\r\n") |
| 883 | s.AssertNil(err, fmt.Sprint(err)) |
| 884 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "request-target must be present") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 885 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 886 | resp, err = TcpSendReceive(serverAddress+":80", "GET HTTP/1.1\r\n\r\n") |
| 887 | s.AssertNil(err, fmt.Sprint(err)) |
| 888 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "request-target must be present") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 889 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 890 | resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/x\r\n\r\n") |
| 891 | s.AssertNil(err, fmt.Sprint(err)) |
| 892 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'HTTP/x' invalid http version not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 893 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 894 | resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP1.1\r\n\r\n") |
| 895 | s.AssertNil(err, fmt.Sprint(err)) |
| 896 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'HTTP1.1' invalid http version not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 897 | } |
| 898 | |
| 899 | func HttpInvalidTargetSyntaxTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 900 | vpp := s.GetContainerByName("vpp").VppInstance |
| 901 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 902 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug")) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 903 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 904 | resp, err := TcpSendReceive(serverAddress+":80", "GET /interface|stats.json HTTP/1.1\r\n\r\n") |
| 905 | s.AssertNil(err, fmt.Sprint(err)) |
| 906 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'|' not allowed in target path") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 907 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 908 | resp, err = TcpSendReceive(serverAddress+":80", "GET /interface#stats.json HTTP/1.1\r\n\r\n") |
| 909 | s.AssertNil(err, fmt.Sprint(err)) |
| 910 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'#' not allowed in target path") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 911 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 912 | resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%stats.json HTTP/1.1\r\n\r\n") |
| 913 | s.AssertNil(err, fmt.Sprint(err)) |
| 914 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 915 | "after '%' there must be two hex-digit characters in target path") |
| 916 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 917 | resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%1stats.json HTTP/1.1\r\n\r\n") |
| 918 | s.AssertNil(err, fmt.Sprint(err)) |
| 919 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 920 | "after '%' there must be two hex-digit characters in target path") |
| 921 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 922 | resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%Bstats.json HTTP/1.1\r\n\r\n") |
| 923 | s.AssertNil(err, fmt.Sprint(err)) |
| 924 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 925 | "after '%' there must be two hex-digit characters in target path") |
| 926 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 927 | resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%stats.json%B HTTP/1.1\r\n\r\n") |
| 928 | s.AssertNil(err, fmt.Sprint(err)) |
| 929 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 930 | "after '%' there must be two hex-digit characters in target path") |
| 931 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 932 | resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose>true HTTP/1.1\r\n\r\n") |
| 933 | s.AssertNil(err, fmt.Sprint(err)) |
| 934 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'>' not allowed in target query") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 935 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 936 | resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose%true HTTP/1.1\r\n\r\n") |
| 937 | s.AssertNil(err, fmt.Sprint(err)) |
| 938 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 939 | "after '%' there must be two hex-digit characters in target query") |
| 940 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 941 | resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose=%1 HTTP/1.1\r\n\r\n") |
| 942 | s.AssertNil(err, fmt.Sprint(err)) |
| 943 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 944 | "after '%' there must be two hex-digit characters in target query") |
| 945 | } |
| 946 | |
| 947 | func HttpInvalidContentLengthTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 948 | vpp := s.GetContainerByName("vpp").VppInstance |
| 949 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 950 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 951 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 952 | resp, err := TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length:\r\n\r\n") |
| 953 | s.AssertNil(err, fmt.Sprint(err)) |
| 954 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Content-Length value must be present") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 955 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 956 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length: \r\n\r\n") |
| 957 | s.AssertNil(err, fmt.Sprint(err)) |
| 958 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Content-Length value must be present") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 959 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 960 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length: a\r\n\r\n") |
| 961 | s.AssertNil(err, fmt.Sprint(err)) |
| 962 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 963 | "Content-Length value other than digit not allowed") |
| 964 | } |
| 965 | |
| 966 | func HttpContentLengthTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 967 | vpp := s.GetContainerByName("vpp").VppInstance |
| 968 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 969 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug")) |
| 970 | ifName := s.GetInterfaceByName(TapInterfaceName).Peer.Name() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 971 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 972 | resp, err := TcpSendReceive(serverAddress+":80", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 973 | "POST /interface_stats.json HTTP/1.1\r\nContent-Length:4\r\n\r\n"+ifName) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 974 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 975 | validatePostInterfaceStats(s, resp) |
| 976 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 977 | resp, err = TcpSendReceive(serverAddress+":80", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 978 | "POST /interface_stats.json HTTP/1.1\r\n Content-Length: 4 \r\n\r\n"+ifName) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 979 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 980 | validatePostInterfaceStats(s, resp) |
| 981 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 982 | resp, err = TcpSendReceive(serverAddress+":80", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 983 | "POST /interface_stats.json HTTP/1.1\r\n\tContent-Length:\t\t4\r\n\r\n"+ifName) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 984 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 985 | validatePostInterfaceStats(s, resp) |
| 986 | } |
| 987 | |
| 988 | func HttpMethodNotImplementedTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 989 | vpp := s.GetContainerByName("vpp").VppInstance |
| 990 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 991 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 992 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 993 | client := NewHttpClient() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 994 | req, err := http.NewRequest("OPTIONS", "http://"+serverAddress+":80/show/version", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 995 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 996 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 997 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 998 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 999 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1000 | s.AssertEqual(501, resp.StatusCode) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1001 | s.AssertEmpty(resp.Header.Get("Content-Type")) |
| 1002 | s.AssertEqual(int64(0), resp.ContentLength) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | func HttpVersionNotSupportedTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1006 | vpp := s.GetContainerByName("vpp").VppInstance |
| 1007 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 1008 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1009 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1010 | resp, err := TcpSendReceive(serverAddress+":80", "GET / HTTP/2\r\n\r\n") |
| 1011 | s.AssertNil(err, fmt.Sprint(err)) |
| 1012 | s.AssertContains(resp, "HTTP/1.1 505 HTTP Version Not Supported") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | func HttpUriDecodeTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1016 | vpp := s.GetContainerByName("vpp").VppInstance |
| 1017 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 1018 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1019 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1020 | client := NewHttpClient() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1021 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/sh%6fw%20versio%6E%20verbose", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1022 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1023 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1024 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1025 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1026 | s.Log(DumpHttpResp(resp, true)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1027 | s.AssertEqual(200, resp.StatusCode) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1028 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1029 | s.AssertNil(err, fmt.Sprint(err)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1030 | s.AssertNotContains(string(data), "unknown input") |
| 1031 | s.AssertContains(string(data), "Compiler") |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1032 | s.AssertContains(resp.Header.Get("Content-Type"), "html") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | func HttpHeadersTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1036 | vpp := s.GetContainerByName("vpp").VppInstance |
| 1037 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 1038 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1039 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1040 | resp, err := TcpSendReceive( |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1041 | serverAddress+":80", |
| 1042 | "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 Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1043 | s.AssertNil(err, fmt.Sprint(err)) |
| 1044 | s.AssertContains(resp, "HTTP/1.1 200 OK") |
Adrian Villin | 3601b32 | 2024-08-19 14:41:35 +0200 | [diff] [blame] | 1045 | s.AssertContains(resp, "Content-Type: text/plain") |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1046 | s.AssertNotContains(resp, "<html>", "html content received instead of plain text") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1047 | } |
| 1048 | |
| 1049 | func HttpInvalidHeadersTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1050 | vpp := s.GetContainerByName("vpp").VppInstance |
| 1051 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 1052 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1053 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1054 | resp, err := TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nUser-Agent: test\r\n") |
| 1055 | s.AssertNil(err, fmt.Sprint(err)) |
| 1056 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Header section must end with CRLF CRLF") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1057 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1058 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser@Agent:test\r\n\r\n") |
| 1059 | s.AssertNil(err, fmt.Sprint(err)) |
| 1060 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'@' not allowed in field name") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1061 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1062 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent\r\n\r\n") |
| 1063 | s.AssertNil(err, fmt.Sprint(err)) |
| 1064 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "incomplete field line not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1065 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1066 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\n: test\r\n\r\n") |
| 1067 | s.AssertNil(err, fmt.Sprint(err)) |
| 1068 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field name not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1069 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1070 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\rUser-Agent:test\r\n\r\n") |
| 1071 | s.AssertNil(err, fmt.Sprint(err)) |
| 1072 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid field line end not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1073 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1074 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\nUser-Agent:test\r\n\r\n") |
| 1075 | s.AssertNil(err, fmt.Sprint(err)) |
| 1076 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid field line end not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1077 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1078 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent:\r\n\r\n") |
| 1079 | s.AssertNil(err, fmt.Sprint(err)) |
| 1080 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field value not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1081 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1082 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent: \r\n\r\n") |
| 1083 | s.AssertNil(err, fmt.Sprint(err)) |
| 1084 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field value not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1085 | } |
| 1086 | |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 1087 | func HeaderServerTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1088 | vpp := s.GetContainerByName("vpp").VppInstance |
| 1089 | serverAddress := s.GetInterfaceByName(TapInterfaceName).Peer.Ip4AddressString() |
| 1090 | vpp.Vppctl("http cli server") |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 1091 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1092 | client := NewHttpClient() |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 1093 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/show/version", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1094 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 595d8cb | 2024-05-17 11:28:43 +0200 | [diff] [blame] | 1095 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1096 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 1097 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1098 | s.Log(DumpHttpResp(resp, true)) |
| 1099 | s.AssertEqual(200, resp.StatusCode) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1100 | s.AssertEqual("http_cli_server", resp.Header.Get("Server")) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1101 | s.AssertContains(resp.Header.Get("Content-Type"), "html") |
Matus Fabian | 616201a | 2024-05-02 11:17:15 +0200 | [diff] [blame] | 1102 | } |