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