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