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 | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 6 | "io" |
Matus Fabian | 9bb0762 | 2024-09-04 18:04:54 +0200 | [diff] [blame] | 7 | "math/rand" |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 8 | "net" |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 9 | "net/http" |
Adrian Villin | d74e440 | 2024-11-04 13:16:24 +0100 | [diff] [blame] | 10 | "net/http/httptest" |
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" |
Adrian Villin | 5a612a4 | 2024-08-28 09:34:14 +0200 | [diff] [blame] | 14 | "strings" |
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 | 5a4c7a9 | 2024-09-26 11:24:34 +0200 | [diff] [blame] | 18 | "github.com/onsi/gomega/ghttp" |
| 19 | "github.com/onsi/gomega/gmeasure" |
| 20 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 21 | . "fd.io/hs-test/infra" |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 22 | . "github.com/onsi/ginkgo/v2" |
Filip Tehlar | c204c87 | 2022-12-21 08:59:16 +0100 | [diff] [blame] | 23 | ) |
| 24 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 25 | func init() { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 26 | RegisterVethTests(HttpCliTest, HttpCliConnectErrorTest) |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 27 | RegisterSoloVethTests(HttpClientGetMemLeakTest) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 28 | RegisterNoTopoTests(HeaderServerTest, HttpPersistentConnectionTest, HttpPipeliningTest, |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 29 | HttpStaticMovedTest, HttpStaticNotFoundTest, HttpCliMethodNotAllowedTest, |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 30 | HttpCliBadRequestTest, HttpStaticBuildInUrlGetIfStatsTest, HttpStaticBuildInUrlPostIfStatsTest, |
| 31 | HttpInvalidRequestLineTest, HttpMethodNotImplementedTest, HttpInvalidHeadersTest, |
| 32 | HttpContentLengthTest, HttpStaticBuildInUrlGetIfListTest, HttpStaticBuildInUrlGetVersionTest, |
| 33 | HttpStaticMacTimeTest, HttpStaticBuildInUrlGetVersionVerboseTest, HttpVersionNotSupportedTest, |
| 34 | HttpInvalidContentLengthTest, HttpInvalidTargetSyntaxTest, HttpStaticPathTraversalTest, HttpUriDecodeTest, |
Adrian Villin | 5a612a4 | 2024-08-28 09:34:14 +0200 | [diff] [blame] | 35 | HttpHeadersTest, HttpStaticFileHandlerTest, HttpStaticFileHandlerDefaultMaxAgeTest, HttpClientTest, |
| 36 | HttpClientErrRespTest, HttpClientPostFormTest, HttpClientGet128kbResponseTest, HttpClientGetResponseBodyTest, |
| 37 | HttpClientGetNoResponseBodyTest, HttpClientPostFileTest, HttpClientPostFilePtrTest, HttpUnitTest, |
Adrian Villin | d74e440 | 2024-11-04 13:16:24 +0100 | [diff] [blame] | 38 | HttpRequestLineTest, HttpClientGetTimeout, HttpStaticFileHandlerWrkTest, HttpStaticUrlHandlerWrkTest, HttpConnTimeoutTest, |
Matus Fabian | 82b3cc1 | 2024-11-21 17:01:45 +0100 | [diff] [blame^] | 39 | HttpClientGetRepeat, HttpClientPostRepeat, HttpIgnoreH2UpgradeTest) |
Matus Fabian | 9bb0762 | 2024-09-04 18:04:54 +0200 | [diff] [blame] | 40 | RegisterNoTopoSoloTests(HttpStaticPromTest, HttpGetTpsTest, HttpGetTpsInterruptModeTest, PromConcurrentConnectionsTest, |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 41 | PromMemLeakTest, HttpClientPostMemLeakTest, HttpInvalidClientRequestMemLeakTest, HttpPostTpsTest, HttpPostTpsInterruptModeTest, |
| 42 | PromConsecutiveConnectionsTest) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 43 | } |
| 44 | |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 45 | const wwwRootPath = "/tmp/www_root" |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 46 | const defaultHttpTimeout = time.Second * 10 |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 47 | |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 48 | func httpDownloadBenchmark(s *HstSuite, experiment *gmeasure.Experiment, data interface{}) { |
| 49 | url, isValid := data.(string) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 50 | s.AssertEqual(true, isValid) |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 51 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 52 | req, err := http.NewRequest("GET", url, nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 53 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 54 | t := time.Now() |
| 55 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 56 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 57 | defer resp.Body.Close() |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 58 | s.AssertHttpStatus(resp, 200) |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 59 | _, err = io.ReadAll(resp.Body) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 60 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 61 | duration := time.Since(t) |
| 62 | 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] | 63 | } |
| 64 | |
Matus Fabian | 9bb0762 | 2024-09-04 18:04:54 +0200 | [diff] [blame] | 65 | func HttpGetTpsInterruptModeTest(s *NoTopoSuite) { |
| 66 | HttpGetTpsTest(s) |
Adrian Villin | 1fde999 | 2024-06-24 08:14:05 -0400 | [diff] [blame] | 67 | } |
| 68 | |
Matus Fabian | 9bb0762 | 2024-09-04 18:04:54 +0200 | [diff] [blame] | 69 | func HttpGetTpsTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 70 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 71 | serverAddress := s.VppAddr() |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 72 | url := "http://" + serverAddress + ":8080/test_file_10M" |
| 73 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 74 | vpp.Vppctl("http tps uri tcp://0.0.0.0/8080") |
Matus Fabian | 2d1f0e6 | 2024-06-06 11:24:36 +0200 | [diff] [blame] | 75 | |
Matus Fabian | 9bb0762 | 2024-09-04 18:04:54 +0200 | [diff] [blame] | 76 | s.RunBenchmark("HTTP tps download 10M", 10, 0, httpDownloadBenchmark, url) |
| 77 | } |
| 78 | |
| 79 | func httpUploadBenchmark(s *HstSuite, experiment *gmeasure.Experiment, data interface{}) { |
| 80 | url, isValid := data.(string) |
| 81 | s.AssertEqual(true, isValid) |
| 82 | body := make([]byte, 10485760) |
| 83 | _, err := rand.Read(body) |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 84 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 9bb0762 | 2024-09-04 18:04:54 +0200 | [diff] [blame] | 85 | req, err := http.NewRequest("POST", url, bytes.NewBuffer(body)) |
| 86 | s.AssertNil(err, fmt.Sprint(err)) |
| 87 | t := time.Now() |
| 88 | resp, err := client.Do(req) |
| 89 | s.AssertNil(err, fmt.Sprint(err)) |
| 90 | defer resp.Body.Close() |
| 91 | s.AssertHttpStatus(resp, 200) |
| 92 | _, err = io.ReadAll(resp.Body) |
| 93 | s.AssertNil(err, fmt.Sprint(err)) |
| 94 | duration := time.Since(t) |
| 95 | experiment.RecordValue("Upload Speed", (float64(req.ContentLength)/1024/1024)/duration.Seconds(), gmeasure.Units("MB/s"), gmeasure.Precision(2)) |
| 96 | } |
| 97 | |
| 98 | func HttpPostTpsInterruptModeTest(s *NoTopoSuite) { |
| 99 | HttpPostTpsTest(s) |
| 100 | } |
| 101 | |
| 102 | func HttpPostTpsTest(s *NoTopoSuite) { |
| 103 | vpp := s.GetContainerByName("vpp").VppInstance |
| 104 | serverAddress := s.VppAddr() |
| 105 | url := "http://" + serverAddress + ":8080/test_file_10M" |
| 106 | |
| 107 | vpp.Vppctl("http tps uri tcp://0.0.0.0/8080") |
| 108 | |
| 109 | s.RunBenchmark("HTTP tps upload 10M", 10, 0, httpUploadBenchmark, url) |
Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 110 | } |
| 111 | |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 112 | func HttpPersistentConnectionTest(s *NoTopoSuite) { |
| 113 | // testing url handler app do not support multi-thread |
| 114 | s.SkipIfMultiWorker() |
| 115 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 116 | serverAddress := s.VppAddr() |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 117 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers")) |
| 118 | s.Log(vpp.Vppctl("test-url-handler enable")) |
| 119 | |
| 120 | transport := http.DefaultTransport |
| 121 | transport.(*http.Transport).Proxy = nil |
| 122 | transport.(*http.Transport).DisableKeepAlives = false |
| 123 | client := &http.Client{ |
| 124 | Transport: transport, |
| 125 | Timeout: time.Second * 30, |
| 126 | CheckRedirect: func(req *http.Request, via []*http.Request) error { |
| 127 | return http.ErrUseLastResponse |
| 128 | }} |
| 129 | |
Matus Fabian | 56cefc8 | 2024-08-26 18:26:58 +0200 | [diff] [blame] | 130 | body := []byte("{\"sandwich\": {\"spam\": 2, \"eggs\": 1}}") |
| 131 | req, err := http.NewRequest("POST", "http://"+serverAddress+":80/test3", bytes.NewBuffer(body)) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 132 | s.AssertNil(err, fmt.Sprint(err)) |
| 133 | resp, err := client.Do(req) |
| 134 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 135 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 136 | s.AssertHttpStatus(resp, 200) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 137 | s.AssertEqual(false, resp.Close) |
Matus Fabian | 56cefc8 | 2024-08-26 18:26:58 +0200 | [diff] [blame] | 138 | s.AssertHttpContentLength(resp, int64(0)) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 139 | o1 := vpp.Vppctl("show session verbose proto http state ready") |
| 140 | s.Log(o1) |
Matus Fabian | 1c9d1de | 2024-11-13 16:31:53 +0100 | [diff] [blame] | 141 | s.AssertContains(o1, "established") |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 142 | |
Matus Fabian | 56cefc8 | 2024-08-26 18:26:58 +0200 | [diff] [blame] | 143 | req, err = http.NewRequest("GET", "http://"+serverAddress+":80/test1", nil) |
| 144 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 145 | clientTrace := &httptrace.ClientTrace{ |
| 146 | GotConn: func(info httptrace.GotConnInfo) { |
| 147 | s.AssertEqual(true, info.Reused, "connection not reused") |
| 148 | }, |
| 149 | } |
Matus Fabian | 56cefc8 | 2024-08-26 18:26:58 +0200 | [diff] [blame] | 150 | req = req.WithContext(httptrace.WithClientTrace(req.Context(), clientTrace)) |
| 151 | resp, err = client.Do(req) |
| 152 | s.AssertNil(err, fmt.Sprint(err)) |
| 153 | s.Log(DumpHttpResp(resp, true)) |
| 154 | s.AssertHttpStatus(resp, 200) |
| 155 | s.AssertEqual(false, resp.Close) |
| 156 | s.AssertHttpBody(resp, "hello") |
| 157 | o2 := vpp.Vppctl("show session verbose proto http state ready") |
| 158 | s.Log(o2) |
Matus Fabian | 1c9d1de | 2024-11-13 16:31:53 +0100 | [diff] [blame] | 159 | s.AssertContains(o2, "established") |
Matus Fabian | 56cefc8 | 2024-08-26 18:26:58 +0200 | [diff] [blame] | 160 | s.AssertEqual(o1, o2) |
| 161 | |
| 162 | req, err = http.NewRequest("GET", "http://"+serverAddress+":80/test2", nil) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 163 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 164 | req = req.WithContext(httptrace.WithClientTrace(req.Context(), clientTrace)) |
| 165 | resp, err = client.Do(req) |
| 166 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 167 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 168 | s.AssertHttpStatus(resp, 200) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 169 | s.AssertEqual(false, resp.Close) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 170 | s.AssertHttpBody(resp, "some data") |
Matus Fabian | 56cefc8 | 2024-08-26 18:26:58 +0200 | [diff] [blame] | 171 | o2 = vpp.Vppctl("show session verbose proto http state ready") |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 172 | s.Log(o2) |
Matus Fabian | 1c9d1de | 2024-11-13 16:31:53 +0100 | [diff] [blame] | 173 | s.AssertContains(o2, "established") |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 174 | s.AssertEqual(o1, o2) |
| 175 | } |
| 176 | |
| 177 | func HttpPipeliningTest(s *NoTopoSuite) { |
| 178 | // testing url handler app do not support multi-thread |
| 179 | s.SkipIfMultiWorker() |
| 180 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 181 | serverAddress := s.VppAddr() |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 182 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug")) |
| 183 | s.Log(vpp.Vppctl("test-url-handler enable")) |
| 184 | |
| 185 | req1 := "GET /test_delayed HTTP/1.1\r\nHost:" + serverAddress + ":80\r\nUser-Agent:test\r\n\r\n" |
| 186 | req2 := "GET /test1 HTTP/1.1\r\nHost:" + serverAddress + ":80\r\nUser-Agent:test\r\n\r\n" |
| 187 | |
| 188 | conn, err := net.DialTimeout("tcp", serverAddress+":80", time.Second*30) |
| 189 | s.AssertNil(err, fmt.Sprint(err)) |
| 190 | defer conn.Close() |
| 191 | err = conn.SetDeadline(time.Now().Add(time.Second * 15)) |
| 192 | s.AssertNil(err, fmt.Sprint(err)) |
| 193 | n, err := conn.Write([]byte(req1)) |
| 194 | s.AssertNil(err, fmt.Sprint(err)) |
| 195 | s.AssertEqual(n, len([]rune(req1))) |
| 196 | // send second request a bit later so first is already in progress |
| 197 | time.Sleep(500 * time.Millisecond) |
| 198 | n, err = conn.Write([]byte(req2)) |
| 199 | s.AssertNil(err, fmt.Sprint(err)) |
| 200 | s.AssertEqual(n, len([]rune(req2))) |
| 201 | reply := make([]byte, 1024) |
Adrian Villin | 5a612a4 | 2024-08-28 09:34:14 +0200 | [diff] [blame] | 202 | _, err = conn.Read(reply) |
Matus Fabian | d086a36 | 2024-06-27 13:20:10 +0200 | [diff] [blame] | 203 | s.AssertNil(err, fmt.Sprint(err)) |
| 204 | s.Log(string(reply)) |
| 205 | s.AssertContains(string(reply), "delayed data", "first request response not received") |
| 206 | s.AssertNotContains(string(reply), "hello", "second request response received") |
| 207 | // make sure response for second request is not received later |
| 208 | _, err = conn.Read(reply) |
| 209 | s.AssertMatchError(err, os.ErrDeadlineExceeded, "second request response received") |
| 210 | } |
| 211 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 212 | func HttpCliTest(s *VethsSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 213 | serverContainer := s.GetContainerByName("server-vpp") |
| 214 | clientContainer := s.GetContainerByName("client-vpp") |
Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 215 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 216 | serverVeth := s.GetInterfaceByName(ServerInterfaceName) |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 217 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 218 | serverContainer.VppInstance.Vppctl("http cli server") |
Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 219 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 220 | uri := "http://" + serverVeth.Ip4AddressString() + "/80" |
Maros Ondrejicka | ffa3f60 | 2023-01-26 10:07:29 +0100 | [diff] [blame] | 221 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 222 | o := clientContainer.VppInstance.Vppctl("http cli client" + |
Filip Tehlar | d894438 | 2023-11-27 13:28:36 +0100 | [diff] [blame] | 223 | " uri " + uri + " query /show/vlib/graph") |
Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 224 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 225 | s.Log(o) |
| 226 | s.AssertContains(o, "<html>", "<html> not found in the result!") |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 227 | s.AssertContains(o, "</html>", "</html> not found in the result!") |
Filip Tehlar | b15a000 | 2022-11-10 12:34:17 +0100 | [diff] [blame] | 228 | } |
Filip Tehlar | c204c87 | 2022-12-21 08:59:16 +0100 | [diff] [blame] | 229 | |
Matus Fabian | 3d00893 | 2024-05-13 10:29:11 +0200 | [diff] [blame] | 230 | func HttpCliConnectErrorTest(s *VethsSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 231 | clientContainer := s.GetContainerByName("client-vpp") |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 232 | serverVeth := s.GetInterfaceByName(ServerInterfaceName) |
Matus Fabian | 3d00893 | 2024-05-13 10:29:11 +0200 | [diff] [blame] | 233 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 234 | uri := "http://" + serverVeth.Ip4AddressString() + "/80" |
Matus Fabian | 3d00893 | 2024-05-13 10:29:11 +0200 | [diff] [blame] | 235 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 236 | o := clientContainer.VppInstance.Vppctl("http cli client" + |
Matus Fabian | 3d00893 | 2024-05-13 10:29:11 +0200 | [diff] [blame] | 237 | " uri " + uri + " query /show/vlib/graph") |
| 238 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 239 | s.Log(o) |
| 240 | s.AssertContains(o, "failed to connect") |
Matus Fabian | 3d00893 | 2024-05-13 10:29:11 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 243 | func HttpClientTest(s *NoTopoSuite) { |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 244 | serverAddress := s.HostAddr() |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 245 | server := ghttp.NewUnstartedServer() |
| 246 | l, err := net.Listen("tcp", serverAddress+":80") |
| 247 | s.AssertNil(err, fmt.Sprint(err)) |
| 248 | server.HTTPTestServer.Listener = l |
| 249 | server.AppendHandlers( |
| 250 | ghttp.CombineHandlers( |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 251 | s.LogHttpReq(true), |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 252 | ghttp.VerifyRequest("GET", "/test"), |
| 253 | ghttp.VerifyHeader(http.Header{"User-Agent": []string{"http_cli_client"}}), |
Adrian Villin | 3601b32 | 2024-08-19 14:41:35 +0200 | [diff] [blame] | 254 | ghttp.VerifyHeader(http.Header{"Accept": []string{"text/html"}}), |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 255 | ghttp.RespondWith(http.StatusOK, "<html><body><p>Hello</p></body></html>"), |
| 256 | )) |
| 257 | server.Start() |
| 258 | defer server.Close() |
| 259 | uri := "http://" + serverAddress + "/80" |
| 260 | vpp := s.GetContainerByName("vpp").VppInstance |
| 261 | o := vpp.Vppctl("http cli client uri " + uri + " query /test") |
| 262 | |
| 263 | s.Log(o) |
| 264 | s.AssertContains(o, "<html>", "<html> not found in the result!") |
| 265 | s.AssertContains(o, "</html>", "</html> not found in the result!") |
| 266 | } |
| 267 | |
| 268 | func HttpClientErrRespTest(s *NoTopoSuite) { |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 269 | serverAddress := s.HostAddr() |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 270 | server := ghttp.NewUnstartedServer() |
| 271 | l, err := net.Listen("tcp", serverAddress+":80") |
| 272 | s.AssertNil(err, fmt.Sprint(err)) |
| 273 | server.HTTPTestServer.Listener = l |
| 274 | server.AppendHandlers( |
| 275 | ghttp.CombineHandlers( |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 276 | s.LogHttpReq(true), |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 277 | ghttp.VerifyRequest("GET", "/test"), |
| 278 | ghttp.RespondWith(http.StatusNotFound, "404: Not Found"), |
| 279 | )) |
| 280 | server.Start() |
| 281 | defer server.Close() |
| 282 | uri := "http://" + serverAddress + "/80" |
| 283 | vpp := s.GetContainerByName("vpp").VppInstance |
| 284 | o := vpp.Vppctl("http cli client uri " + uri + " query /test") |
| 285 | |
| 286 | s.Log(o) |
| 287 | s.AssertContains(o, "404: Not Found", "error not found in the result!") |
| 288 | } |
| 289 | |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 290 | func HttpClientPostFormTest(s *NoTopoSuite) { |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 291 | serverAddress := s.HostAddr() |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 292 | body := "field1=value1&field2=value2" |
| 293 | |
| 294 | server := ghttp.NewUnstartedServer() |
| 295 | l, err := net.Listen("tcp", serverAddress+":80") |
| 296 | s.AssertNil(err, fmt.Sprint(err)) |
| 297 | server.HTTPTestServer.Listener = l |
| 298 | server.AppendHandlers( |
| 299 | ghttp.CombineHandlers( |
| 300 | s.LogHttpReq(true), |
| 301 | ghttp.VerifyRequest("POST", "/test"), |
Adrian Villin | 3601b32 | 2024-08-19 14:41:35 +0200 | [diff] [blame] | 302 | ghttp.VerifyContentType("application/x-www-form-urlencoded"), |
Adrian Villin | 5a612a4 | 2024-08-28 09:34:14 +0200 | [diff] [blame] | 303 | ghttp.VerifyHeaderKV("Hello", "World"), |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 304 | ghttp.VerifyBody([]byte(body)), |
| 305 | ghttp.RespondWith(http.StatusOK, nil), |
| 306 | )) |
| 307 | server.Start() |
| 308 | defer server.Close() |
| 309 | |
| 310 | uri := "http://" + serverAddress + "/80" |
| 311 | vpp := s.GetContainerByName("vpp").VppInstance |
Adrian Villin | 5a612a4 | 2024-08-28 09:34:14 +0200 | [diff] [blame] | 312 | o := vpp.Vppctl("http client post verbose header Hello:World uri " + uri + " target /test data " + body) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 313 | |
| 314 | s.Log(o) |
Adrian Villin | 5a612a4 | 2024-08-28 09:34:14 +0200 | [diff] [blame] | 315 | s.AssertContains(o, "200 OK") |
| 316 | } |
| 317 | |
| 318 | func HttpClientGetResponseBodyTest(s *NoTopoSuite) { |
| 319 | response := "<body>hello world</body>" |
| 320 | size := len(response) |
| 321 | httpClientGet(s, response, size) |
| 322 | } |
| 323 | |
| 324 | func HttpClientGet128kbResponseTest(s *NoTopoSuite) { |
| 325 | response := strings.Repeat("a", 128*1024) |
| 326 | size := len(response) |
| 327 | httpClientGet(s, response, size) |
| 328 | } |
| 329 | |
| 330 | func HttpClientGetNoResponseBodyTest(s *NoTopoSuite) { |
| 331 | response := "" |
| 332 | httpClientGet(s, response, 0) |
| 333 | } |
| 334 | |
| 335 | func httpClientGet(s *NoTopoSuite, response string, size int) { |
| 336 | serverAddress := s.HostAddr() |
| 337 | vpp := s.GetContainerByName("vpp").VppInstance |
| 338 | |
| 339 | server := ghttp.NewUnstartedServer() |
| 340 | l, err := net.Listen("tcp", serverAddress+":80") |
| 341 | s.AssertNil(err, fmt.Sprint(err)) |
| 342 | server.HTTPTestServer.Listener = l |
| 343 | server.AppendHandlers( |
| 344 | ghttp.CombineHandlers( |
| 345 | s.LogHttpReq(false), |
| 346 | ghttp.VerifyRequest("GET", "/test"), |
| 347 | ghttp.VerifyHeaderKV("Hello", "World"), |
| 348 | ghttp.VerifyHeaderKV("Test-H2", "Test-K2"), |
| 349 | ghttp.RespondWith(http.StatusOK, string(response), http.Header{"Content-Length": {strconv.Itoa(size)}}), |
| 350 | )) |
| 351 | server.Start() |
| 352 | defer server.Close() |
| 353 | |
| 354 | uri := "http://" + serverAddress + "/80" |
| 355 | cmd := "http client use-ptr verbose header Hello:World header Test-H2:Test-K2 save-to response.txt uri " + uri + " target /test" |
| 356 | |
| 357 | o := vpp.Vppctl(cmd) |
| 358 | outputLen := len(o) |
| 359 | if outputLen > 500 { |
| 360 | s.Log(o[:500]) |
| 361 | s.Log("* HST Framework: output limited to 500 chars to avoid flooding the console. Output length: " + fmt.Sprint(outputLen)) |
| 362 | } else { |
| 363 | s.Log(o) |
| 364 | } |
| 365 | s.AssertContains(o, "200 OK") |
| 366 | s.AssertContains(o, response) |
| 367 | s.AssertContains(o, "Content-Length: "+strconv.Itoa(size)) |
| 368 | |
| 369 | file_contents := vpp.Container.Exec(false, "cat /tmp/response.txt") |
| 370 | s.AssertContains(file_contents, response) |
| 371 | } |
| 372 | |
Adrian Villin | d74e440 | 2024-11-04 13:16:24 +0100 | [diff] [blame] | 373 | func startSimpleServer(s *NoTopoSuite, replyCount *int, serverAddress string) (server *httptest.Server) { |
| 374 | var err error |
| 375 | server = httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 376 | fmt.Fprintf(w, "Hello") |
| 377 | *replyCount++ |
| 378 | })) |
| 379 | server.Listener, err = net.Listen("tcp", serverAddress+":80") |
| 380 | s.AssertNil(err, "Error while creating listener.") |
| 381 | |
| 382 | server.Start() |
| 383 | |
| 384 | return server |
| 385 | } |
| 386 | |
| 387 | func HttpClientGetRepeat(s *NoTopoSuite) { |
| 388 | httpClientRepeat(s, "") |
| 389 | } |
| 390 | |
| 391 | func HttpClientPostRepeat(s *NoTopoSuite) { |
| 392 | httpClientRepeat(s, "post") |
| 393 | } |
| 394 | |
| 395 | func httpClientRepeat(s *NoTopoSuite, requestMethod string) { |
| 396 | replyCount := 0 |
| 397 | vpp := s.GetContainerByName("vpp").VppInstance |
| 398 | serverAddress := s.HostAddr() |
| 399 | repeatAmount := 10000 |
| 400 | server := startSimpleServer(s, &replyCount, serverAddress) |
| 401 | defer server.Close() |
| 402 | |
| 403 | if requestMethod == "post" { |
| 404 | fileName := "/tmp/test_file.txt" |
| 405 | s.Log(vpp.Container.Exec(false, "fallocate -l 64 "+fileName)) |
| 406 | s.Log(vpp.Container.Exec(false, "ls -la "+fileName)) |
| 407 | requestMethod += " file /tmp/test_file.txt" |
| 408 | } |
| 409 | |
| 410 | uri := "http://" + serverAddress + "/80" |
| 411 | cmd := fmt.Sprintf("http client %s use-ptr duration 10 header Hello:World uri %s target /index.html", |
| 412 | requestMethod, uri) |
| 413 | |
| 414 | s.Log("Duration 10s") |
| 415 | o := vpp.Vppctl(cmd) |
| 416 | outputLen := len(o) |
| 417 | if outputLen > 500 { |
| 418 | s.Log(o[:500]) |
| 419 | s.Log("* HST Framework: output limited to 500 chars to avoid flooding the console. Output length: " + fmt.Sprint(outputLen)) |
| 420 | } else { |
| 421 | s.Log(o) |
| 422 | } |
| 423 | s.Log("Server response count: %d", replyCount) |
| 424 | s.AssertNotNil(o) |
| 425 | s.AssertNotContains(o, "error") |
| 426 | s.AssertGreaterThan(replyCount, 15000) |
| 427 | |
| 428 | cmd = fmt.Sprintf("http client %s use-ptr repeat %d header Hello:World uri %s target /index.html", |
| 429 | requestMethod, repeatAmount, uri) |
| 430 | |
| 431 | replyCount = 0 |
| 432 | s.Log("Repeat %d", repeatAmount) |
| 433 | o = vpp.Vppctl(cmd) |
| 434 | outputLen = len(o) |
| 435 | if outputLen > 500 { |
| 436 | s.Log(o[:500]) |
| 437 | s.Log("* HST Framework: output limited to 500 chars to avoid flooding the console. Output length: " + fmt.Sprint(outputLen)) |
| 438 | } else { |
| 439 | s.Log(o) |
| 440 | } |
| 441 | s.Log("Server response count: %d", replyCount) |
| 442 | s.AssertNotNil(o) |
| 443 | s.AssertNotContains(o, "error") |
| 444 | s.AssertEqual(repeatAmount, replyCount) |
| 445 | } |
| 446 | |
Adrian Villin | 5a612a4 | 2024-08-28 09:34:14 +0200 | [diff] [blame] | 447 | func HttpClientGetTimeout(s *NoTopoSuite) { |
| 448 | serverAddress := s.HostAddr() |
| 449 | vpp := s.GetContainerByName("vpp").VppInstance |
| 450 | |
| 451 | server := ghttp.NewUnstartedServer() |
| 452 | l, err := net.Listen("tcp", serverAddress+":"+s.GetPortFromPpid()) |
| 453 | s.AssertNil(err, fmt.Sprint(err)) |
| 454 | server.HTTPTestServer.Listener = l |
| 455 | server.AppendHandlers( |
| 456 | ghttp.CombineHandlers( |
| 457 | s.LogHttpReq(false), |
| 458 | ghttp.VerifyRequest("GET", "/timeout"), |
| 459 | http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 460 | time.Sleep(5 * time.Second) |
| 461 | }), |
| 462 | ghttp.RespondWith(http.StatusOK, nil), |
| 463 | )) |
| 464 | server.Start() |
| 465 | defer server.Close() |
| 466 | uri := "http://" + serverAddress + "/" + s.GetPortFromPpid() |
| 467 | cmd := "http client verbose timeout 1 uri " + uri + " target /timeout" |
| 468 | |
| 469 | o := vpp.Vppctl(cmd) |
| 470 | s.Log(o) |
| 471 | s.AssertContains(o, "error: timeout") |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | func httpClientPostFile(s *NoTopoSuite, usePtr bool, fileSize int) { |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 475 | serverAddress := s.HostAddr() |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 476 | vpp := s.GetContainerByName("vpp").VppInstance |
| 477 | fileName := "/tmp/test_file.txt" |
Adrian Villin | 2acdf1e | 2024-09-25 14:49:11 +0200 | [diff] [blame] | 478 | s.Log(vpp.Container.Exec(false, "fallocate -l "+strconv.Itoa(fileSize)+" "+fileName)) |
| 479 | s.Log(vpp.Container.Exec(false, "ls -la "+fileName)) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 480 | |
| 481 | server := ghttp.NewUnstartedServer() |
| 482 | l, err := net.Listen("tcp", serverAddress+":80") |
| 483 | s.AssertNil(err, fmt.Sprint(err)) |
| 484 | server.HTTPTestServer.Listener = l |
| 485 | server.AppendHandlers( |
| 486 | ghttp.CombineHandlers( |
| 487 | s.LogHttpReq(false), |
| 488 | ghttp.VerifyRequest("POST", "/test"), |
| 489 | ghttp.VerifyHeader(http.Header{"Content-Length": []string{strconv.Itoa(fileSize)}}), |
Adrian Villin | 3601b32 | 2024-08-19 14:41:35 +0200 | [diff] [blame] | 490 | ghttp.VerifyContentType("application/octet-stream"), |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 491 | ghttp.RespondWith(http.StatusOK, nil), |
| 492 | )) |
| 493 | server.Start() |
| 494 | defer server.Close() |
| 495 | |
| 496 | uri := "http://" + serverAddress + "/80" |
Adrian Villin | 5a612a4 | 2024-08-28 09:34:14 +0200 | [diff] [blame] | 497 | cmd := "http client post verbose uri " + uri + " target /test file " + fileName |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 498 | if usePtr { |
| 499 | cmd += " use-ptr" |
| 500 | } |
| 501 | o := vpp.Vppctl(cmd) |
| 502 | |
| 503 | s.Log(o) |
Adrian Villin | 5a612a4 | 2024-08-28 09:34:14 +0200 | [diff] [blame] | 504 | s.AssertContains(o, "200 OK") |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | func HttpClientPostFileTest(s *NoTopoSuite) { |
| 508 | httpClientPostFile(s, false, 32768) |
| 509 | } |
| 510 | |
| 511 | func HttpClientPostFilePtrTest(s *NoTopoSuite) { |
| 512 | httpClientPostFile(s, true, 131072) |
| 513 | } |
| 514 | |
Matus Fabian | 769a3b7 | 2024-10-29 15:06:49 +0100 | [diff] [blame] | 515 | func HttpUnitTest(s *NoTopoSuite) { |
| 516 | vpp := s.GetContainerByName("vpp").VppInstance |
| 517 | o := vpp.Vppctl("test http all") |
| 518 | s.Log(o) |
Matus Fabian | 3d0b4ab | 2024-12-06 17:45:53 +0100 | [diff] [blame] | 519 | s.AssertContains(o, "SUCCESS") |
Matus Fabian | d58177c | 2024-08-08 12:50:32 +0200 | [diff] [blame] | 520 | } |
| 521 | |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 522 | func HttpStaticPromTest(s *NoTopoSuite) { |
Filip Tehlar | cc1475c | 2023-11-29 12:59:05 +0100 | [diff] [blame] | 523 | query := "stats.prom" |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 524 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 525 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 526 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers")) |
| 527 | s.Log(vpp.Vppctl("prom enable")) |
Adrian Villin | cee15aa | 2024-03-14 11:42:55 -0400 | [diff] [blame] | 528 | time.Sleep(time.Second * 5) |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 529 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 530 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/"+query, nil) |
| 531 | s.AssertNil(err, fmt.Sprint(err)) |
| 532 | resp, err := client.Do(req) |
| 533 | s.AssertNil(err, fmt.Sprint(err)) |
| 534 | defer resp.Body.Close() |
| 535 | s.Log(DumpHttpResp(resp, false)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 536 | s.AssertHttpStatus(resp, 200) |
| 537 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/plain") |
| 538 | s.AssertGreaterThan(resp.ContentLength, 0) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 539 | _, err = io.ReadAll(resp.Body) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 540 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 541 | } |
| 542 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 543 | func promReq(s *NoTopoSuite, url string, timeout time.Duration) { |
| 544 | client := NewHttpClient(timeout) |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 545 | req, err := http.NewRequest("GET", url, nil) |
| 546 | s.AssertNil(err, fmt.Sprint(err)) |
| 547 | resp, err := client.Do(req) |
| 548 | s.AssertNil(err, fmt.Sprint(err)) |
| 549 | defer resp.Body.Close() |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 550 | s.AssertHttpStatus(resp, 200) |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 551 | _, err = io.ReadAll(resp.Body) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 552 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 555 | func promReqWg(s *NoTopoSuite, url string, wg *sync.WaitGroup) { |
| 556 | defer GinkgoRecover() |
| 557 | defer wg.Done() |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 558 | promReq(s, url, defaultHttpTimeout) |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 559 | } |
| 560 | |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 561 | func PromConcurrentConnectionsTest(s *NoTopoSuite) { |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 562 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 563 | serverAddress := s.VppAddr() |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 564 | url := "http://" + serverAddress + ":80/stats.prom" |
| 565 | |
| 566 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers")) |
| 567 | s.Log(vpp.Vppctl("prom enable")) |
| 568 | time.Sleep(time.Second * 5) |
| 569 | |
| 570 | var wg sync.WaitGroup |
| 571 | for i := 0; i < 20; i++ { |
| 572 | wg.Add(1) |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 573 | go promReqWg(s, url, &wg) |
Matus Fabian | 5c4c1b6 | 2024-06-28 16:11:04 +0200 | [diff] [blame] | 574 | } |
| 575 | wg.Wait() |
| 576 | s.Log(vpp.Vppctl("show session verbose proto http")) |
| 577 | } |
| 578 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 579 | func PromConsecutiveConnectionsTest(s *NoTopoSuite) { |
| 580 | vpp := s.GetContainerByName("vpp").VppInstance |
| 581 | serverAddress := s.VppAddr() |
| 582 | url := "http://" + serverAddress + ":80/stats.prom" |
| 583 | |
| 584 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers")) |
| 585 | s.Log(vpp.Vppctl("prom enable")) |
| 586 | time.Sleep(time.Second * 5) |
| 587 | |
| 588 | for i := 0; i < 1000; i++ { |
| 589 | promReq(s, url, time.Millisecond*500) |
| 590 | } |
| 591 | } |
| 592 | |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 593 | func PromMemLeakTest(s *NoTopoSuite) { |
| 594 | s.SkipUnlessLeakCheck() |
| 595 | |
| 596 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 597 | serverAddress := s.VppAddr() |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 598 | url := "http://" + serverAddress + ":80/stats.prom" |
| 599 | |
| 600 | /* no goVPP less noise */ |
| 601 | vpp.Disconnect() |
| 602 | |
| 603 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers")) |
| 604 | s.Log(vpp.Vppctl("prom enable")) |
| 605 | time.Sleep(time.Second * 3) |
| 606 | |
| 607 | /* warmup request (FIB) */ |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 608 | promReq(s, url, defaultHttpTimeout) |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 609 | |
| 610 | vpp.EnableMemoryTrace() |
| 611 | traces1, err := vpp.GetMemoryTrace() |
| 612 | s.AssertNil(err, fmt.Sprint(err)) |
| 613 | |
| 614 | /* collect stats couple of times */ |
| 615 | for i := 0; i < 5; i++ { |
| 616 | time.Sleep(time.Second * 1) |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 617 | promReq(s, url, defaultHttpTimeout) |
Matus Fabian | c712de5 | 2024-07-24 12:56:14 +0200 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | /* let's give it some time to clean up sessions */ |
| 621 | time.Sleep(time.Second * 5) |
| 622 | |
| 623 | traces2, err := vpp.GetMemoryTrace() |
| 624 | s.AssertNil(err, fmt.Sprint(err)) |
| 625 | vpp.MemLeakCheck(traces1, traces2) |
| 626 | } |
| 627 | |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 628 | func HttpClientGetMemLeakTest(s *VethsSuite) { |
| 629 | s.SkipUnlessLeakCheck() |
| 630 | |
| 631 | serverContainer := s.GetContainerByName("server-vpp").VppInstance |
| 632 | clientContainer := s.GetContainerByName("client-vpp").VppInstance |
| 633 | serverVeth := s.GetInterfaceByName(ServerInterfaceName) |
| 634 | |
| 635 | /* no goVPP less noise */ |
| 636 | clientContainer.Disconnect() |
| 637 | |
| 638 | serverContainer.Vppctl("http cli server") |
| 639 | |
| 640 | uri := "http://" + serverVeth.Ip4AddressString() + "/80" |
| 641 | |
| 642 | /* warmup request (FIB) */ |
| 643 | clientContainer.Vppctl("http cli client uri " + uri + " query /show/version") |
| 644 | |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 645 | /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */ |
| 646 | time.Sleep(time.Second * 12) |
| 647 | |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 648 | clientContainer.EnableMemoryTrace() |
| 649 | traces1, err := clientContainer.GetMemoryTrace() |
| 650 | s.AssertNil(err, fmt.Sprint(err)) |
| 651 | |
| 652 | clientContainer.Vppctl("http cli client uri " + uri + " query /show/vlib/graph") |
| 653 | |
| 654 | /* let's give it some time to clean up sessions */ |
| 655 | time.Sleep(time.Second * 12) |
| 656 | |
| 657 | traces2, err := clientContainer.GetMemoryTrace() |
| 658 | s.AssertNil(err, fmt.Sprint(err)) |
| 659 | clientContainer.MemLeakCheck(traces1, traces2) |
| 660 | } |
| 661 | |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 662 | func HttpClientPostMemLeakTest(s *NoTopoSuite) { |
| 663 | s.SkipUnlessLeakCheck() |
| 664 | |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 665 | serverAddress := s.HostAddr() |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 666 | body := "field1=value1&field2=value2" |
| 667 | |
| 668 | uri := "http://" + serverAddress + "/80" |
| 669 | vpp := s.GetContainerByName("vpp").VppInstance |
| 670 | |
| 671 | /* no goVPP less noise */ |
| 672 | vpp.Disconnect() |
| 673 | |
| 674 | server := ghttp.NewUnstartedServer() |
| 675 | l, err := net.Listen("tcp", serverAddress+":80") |
| 676 | s.AssertNil(err, fmt.Sprint(err)) |
| 677 | server.HTTPTestServer.Listener = l |
| 678 | server.AppendHandlers( |
| 679 | ghttp.CombineHandlers( |
| 680 | ghttp.VerifyRequest("POST", "/test"), |
| 681 | ghttp.RespondWith(http.StatusOK, nil), |
| 682 | ), |
| 683 | ghttp.CombineHandlers( |
| 684 | ghttp.VerifyRequest("POST", "/test"), |
| 685 | ghttp.RespondWith(http.StatusOK, nil), |
| 686 | ), |
| 687 | ) |
| 688 | server.Start() |
| 689 | defer server.Close() |
| 690 | |
| 691 | /* warmup request (FIB) */ |
| 692 | vpp.Vppctl("http post uri " + uri + " target /test data " + body) |
| 693 | |
| 694 | /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */ |
| 695 | time.Sleep(time.Second * 12) |
| 696 | |
| 697 | vpp.EnableMemoryTrace() |
| 698 | traces1, err := vpp.GetMemoryTrace() |
| 699 | s.AssertNil(err, fmt.Sprint(err)) |
| 700 | |
| 701 | vpp.Vppctl("http post uri " + uri + " target /test data " + body) |
| 702 | |
| 703 | /* let's give it some time to clean up sessions */ |
| 704 | time.Sleep(time.Second * 12) |
| 705 | |
| 706 | traces2, err := vpp.GetMemoryTrace() |
| 707 | s.AssertNil(err, fmt.Sprint(err)) |
| 708 | vpp.MemLeakCheck(traces1, traces2) |
| 709 | } |
| 710 | |
Matus Fabian | 5546755 | 2024-08-06 15:55:26 +0200 | [diff] [blame] | 711 | func HttpInvalidClientRequestMemLeakTest(s *NoTopoSuite) { |
| 712 | s.SkipUnlessLeakCheck() |
| 713 | |
| 714 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 715 | serverAddress := s.VppAddr() |
Matus Fabian | 5546755 | 2024-08-06 15:55:26 +0200 | [diff] [blame] | 716 | |
| 717 | /* no goVPP less noise */ |
| 718 | vpp.Disconnect() |
| 719 | |
| 720 | vpp.Vppctl("http cli server") |
| 721 | |
| 722 | /* warmup request (FIB) */ |
| 723 | _, err := TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n") |
| 724 | s.AssertNil(err, fmt.Sprint(err)) |
| 725 | |
| 726 | /* let's give it some time to clean up sessions, so local port can be reused and we have less noise */ |
| 727 | time.Sleep(time.Second * 12) |
| 728 | |
| 729 | vpp.EnableMemoryTrace() |
| 730 | traces1, err := vpp.GetMemoryTrace() |
| 731 | s.AssertNil(err, fmt.Sprint(err)) |
| 732 | |
| 733 | _, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n") |
| 734 | s.AssertNil(err, fmt.Sprint(err)) |
| 735 | |
| 736 | /* let's give it some time to clean up sessions */ |
| 737 | time.Sleep(time.Second * 12) |
| 738 | |
| 739 | traces2, err := vpp.GetMemoryTrace() |
| 740 | s.AssertNil(err, fmt.Sprint(err)) |
| 741 | vpp.MemLeakCheck(traces1, traces2) |
| 742 | |
| 743 | } |
| 744 | |
Matus Fabian | 6885d5f | 2024-10-08 13:58:37 +0200 | [diff] [blame] | 745 | func runWrkPerf(s *NoTopoSuite) { |
| 746 | nConnections := 1000 |
| 747 | serverAddress := s.VppAddr() |
| 748 | |
| 749 | wrkCont := s.GetContainerByName("wrk") |
| 750 | args := fmt.Sprintf("-c %d -t 2 -d 30s http://%s:80/64B", nConnections, serverAddress) |
| 751 | wrkCont.ExtraRunningArgs = args |
| 752 | wrkCont.Run() |
| 753 | s.Log("Please wait for 30s, test is running.") |
| 754 | o, err := wrkCont.GetOutput() |
| 755 | s.Log(o) |
| 756 | s.AssertEmpty(err, "err: '%s'", err) |
| 757 | } |
| 758 | |
| 759 | func HttpStaticFileHandlerWrkTest(s *NoTopoSuite) { |
| 760 | vpp := s.GetContainerByName("vpp").VppInstance |
| 761 | serverAddress := s.VppAddr() |
Matus Fabian | 15106be | 2024-10-08 20:06:32 +0200 | [diff] [blame] | 762 | vpp.Container.Exec(false, "mkdir -p "+wwwRootPath) |
Matus Fabian | 6885d5f | 2024-10-08 13:58:37 +0200 | [diff] [blame] | 763 | content := "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx" |
| 764 | err := vpp.Container.CreateFile(wwwRootPath+"/64B", content) |
| 765 | s.AssertNil(err, fmt.Sprint(err)) |
| 766 | s.Log(vpp.Vppctl("http static server www-root " + wwwRootPath + " uri tcp://" + serverAddress + "/80 private-segment-size 256m")) |
| 767 | runWrkPerf(s) |
| 768 | } |
| 769 | |
| 770 | func HttpStaticUrlHandlerWrkTest(s *NoTopoSuite) { |
| 771 | vpp := s.GetContainerByName("vpp").VppInstance |
| 772 | serverAddress := s.VppAddr() |
| 773 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers private-segment-size 256m")) |
| 774 | s.Log(vpp.Vppctl("test-url-handler enable")) |
| 775 | runWrkPerf(s) |
| 776 | } |
| 777 | |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 778 | func HttpStaticFileHandlerDefaultMaxAgeTest(s *NoTopoSuite) { |
| 779 | HttpStaticFileHandlerTestFunction(s, "default") |
| 780 | } |
| 781 | |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 782 | func HttpStaticFileHandlerTest(s *NoTopoSuite) { |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 783 | HttpStaticFileHandlerTestFunction(s, "123") |
| 784 | } |
| 785 | |
| 786 | func HttpStaticFileHandlerTestFunction(s *NoTopoSuite, max_age string) { |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 787 | var maxAgeFormatted string |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 788 | if max_age == "default" { |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 789 | maxAgeFormatted = "" |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 790 | max_age = "600" |
| 791 | } else { |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 792 | maxAgeFormatted = "max-age " + max_age |
Adrian Villin | 86fa943 | 2024-08-08 08:56:34 +0200 | [diff] [blame] | 793 | } |
| 794 | |
Matus Fabian | ba9ea13 | 2024-07-12 11:07:17 +0200 | [diff] [blame] | 795 | content := "<html><body><p>Hello</p></body></html>" |
| 796 | content2 := "<html><body><p>Page</p></body></html>" |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 797 | |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 798 | vpp := s.GetContainerByName("vpp").VppInstance |
Adrian Villin | 2acdf1e | 2024-09-25 14:49:11 +0200 | [diff] [blame] | 799 | vpp.Container.Exec(false, "mkdir -p "+wwwRootPath) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 800 | err := vpp.Container.CreateFile(wwwRootPath+"/index.html", content) |
| 801 | s.AssertNil(err, fmt.Sprint(err)) |
| 802 | err = vpp.Container.CreateFile(wwwRootPath+"/page.html", content2) |
| 803 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 804 | serverAddress := s.VppAddr() |
| 805 | 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] | 806 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 807 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 808 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/index.html", nil) |
| 809 | s.AssertNil(err, fmt.Sprint(err)) |
| 810 | resp, err := client.Do(req) |
| 811 | s.AssertNil(err, fmt.Sprint(err)) |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 812 | |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 813 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 814 | s.AssertHttpStatus(resp, 200) |
| 815 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html") |
| 816 | s.AssertHttpHeaderWithValue(resp, "Cache-Control", "max-age="+max_age) |
| 817 | parsedTime, err := time.Parse(time.RFC1123, resp.Header.Get("Last-Modified")) |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 818 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 819 | s.AssertTimeEqualWithinThreshold(parsedTime, time.Now(), time.Minute*5) |
| 820 | s.AssertEqual(len(resp.Header.Get("Last-Modified")), 29) |
| 821 | s.AssertHttpContentLength(resp, int64(len([]rune(content)))) |
| 822 | s.AssertHttpBody(resp, content) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 823 | o := vpp.Vppctl("show http static server cache verbose") |
| 824 | s.Log(o) |
| 825 | s.AssertContains(o, "index.html") |
| 826 | s.AssertNotContains(o, "page.html") |
| 827 | |
| 828 | resp, err = client.Do(req) |
| 829 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 830 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 831 | s.AssertHttpStatus(resp, 200) |
| 832 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html") |
| 833 | s.AssertHttpHeaderWithValue(resp, "Cache-Control", "max-age="+max_age) |
| 834 | s.AssertHttpContentLength(resp, int64(len([]rune(content)))) |
| 835 | s.AssertHttpBody(resp, content) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 836 | |
| 837 | req, err = http.NewRequest("GET", "http://"+serverAddress+":80/page.html", nil) |
| 838 | s.AssertNil(err, fmt.Sprint(err)) |
| 839 | resp, err = client.Do(req) |
| 840 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 841 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 842 | s.AssertHttpStatus(resp, 200) |
| 843 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html") |
| 844 | s.AssertHttpHeaderWithValue(resp, "Cache-Control", "max-age="+max_age) |
| 845 | s.AssertHttpContentLength(resp, int64(len([]rune(content2)))) |
| 846 | s.AssertHttpBody(resp, content2) |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 847 | o = vpp.Vppctl("show http static server cache verbose") |
| 848 | s.Log(o) |
| 849 | s.AssertContains(o, "index.html") |
| 850 | s.AssertContains(o, "page.html") |
Filip Tehlar | cc1475c | 2023-11-29 12:59:05 +0100 | [diff] [blame] | 851 | } |
| 852 | |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 853 | func HttpStaticPathTraversalTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 854 | vpp := s.GetContainerByName("vpp").VppInstance |
Adrian Villin | 2acdf1e | 2024-09-25 14:49:11 +0200 | [diff] [blame] | 855 | vpp.Container.Exec(false, "mkdir -p "+wwwRootPath) |
| 856 | vpp.Container.Exec(false, "mkdir -p "+"/tmp/secret_folder") |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 857 | err := vpp.Container.CreateFile("/tmp/secret_folder/secret_file.txt", "secret") |
| 858 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 859 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 860 | 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] | 861 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 862 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 863 | 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] | 864 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 865 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 866 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 867 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 868 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 869 | s.AssertHttpStatus(resp, 404) |
| 870 | s.AssertHttpHeaderNotPresent(resp, "Content-Type") |
| 871 | s.AssertHttpHeaderNotPresent(resp, "Cache-Control") |
| 872 | s.AssertHttpContentLength(resp, int64(0)) |
Matus Fabian | 5409d33 | 2024-05-28 13:39:13 +0200 | [diff] [blame] | 873 | } |
| 874 | |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 875 | func HttpStaticMovedTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 876 | vpp := s.GetContainerByName("vpp").VppInstance |
Adrian Villin | 2acdf1e | 2024-09-25 14:49:11 +0200 | [diff] [blame] | 877 | vpp.Container.Exec(false, "mkdir -p "+wwwRootPath+"/tmp.aaa") |
Matus Fabian | d46e674 | 2024-07-31 16:08:40 +0200 | [diff] [blame] | 878 | err := vpp.Container.CreateFile(wwwRootPath+"/tmp.aaa/index.html", "<html><body><p>Hello</p></body></html>") |
| 879 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 880 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 881 | 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] | 882 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 883 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 884 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/tmp.aaa", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 885 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 886 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 887 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 888 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 889 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 890 | s.AssertHttpStatus(resp, 301) |
| 891 | s.AssertHttpHeaderWithValue(resp, "Location", "http://"+serverAddress+"/tmp.aaa/index.html") |
| 892 | s.AssertHttpHeaderNotPresent(resp, "Content-Type") |
| 893 | s.AssertHttpHeaderNotPresent(resp, "Cache-Control") |
| 894 | s.AssertHttpContentLength(resp, int64(0)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | func HttpStaticNotFoundTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 898 | vpp := s.GetContainerByName("vpp").VppInstance |
Adrian Villin | 2acdf1e | 2024-09-25 14:49:11 +0200 | [diff] [blame] | 899 | vpp.Container.Exec(false, "mkdir -p "+wwwRootPath) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 900 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 901 | 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] | 902 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 903 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 904 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/notfound.html", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 905 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 595d8cb | 2024-05-17 11:28:43 +0200 | [diff] [blame] | 906 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 907 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 908 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 909 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 910 | s.AssertHttpStatus(resp, 404) |
| 911 | s.AssertHttpHeaderNotPresent(resp, "Content-Type") |
| 912 | s.AssertHttpHeaderNotPresent(resp, "Cache-Control") |
| 913 | s.AssertHttpContentLength(resp, int64(0)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | func HttpCliMethodNotAllowedTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 917 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 918 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 919 | vpp.Vppctl("http cli server") |
Matus Fabian | 616201a | 2024-05-02 11:17:15 +0200 | [diff] [blame] | 920 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 921 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 922 | req, err := http.NewRequest("POST", "http://"+serverAddress+":80/test", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 923 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 595d8cb | 2024-05-17 11:28:43 +0200 | [diff] [blame] | 924 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 925 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 926 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 927 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 928 | s.AssertHttpStatus(resp, 405) |
| 929 | s.AssertHttpHeaderWithValue(resp, "Allow", "GET", "server MUST generate an Allow header") |
| 930 | s.AssertHttpHeaderNotPresent(resp, "Content-Type") |
| 931 | s.AssertHttpContentLength(resp, int64(0)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | func HttpCliBadRequestTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 935 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 936 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 937 | vpp.Vppctl("http cli server") |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 938 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 939 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 940 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 941 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 595d8cb | 2024-05-17 11:28:43 +0200 | [diff] [blame] | 942 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 943 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 944 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 945 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 946 | s.AssertHttpStatus(resp, 400) |
| 947 | s.AssertHttpHeaderNotPresent(resp, "Content-Type") |
| 948 | s.AssertHttpContentLength(resp, int64(0)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 949 | } |
| 950 | |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 951 | func HttpStaticBuildInUrlGetVersionTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 952 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 953 | serverAddress := s.VppAddr() |
Matus Fabian | 8416722 | 2024-10-17 13:41:51 +0200 | [diff] [blame] | 954 | s.Log(vpp.Vppctl("http static server uri tls://" + serverAddress + "/80 url-handlers debug")) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 955 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 956 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 8416722 | 2024-10-17 13:41:51 +0200 | [diff] [blame] | 957 | req, err := http.NewRequest("GET", "https://"+serverAddress+":80/version.json", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 958 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 959 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 960 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 961 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 962 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 963 | s.AssertHttpStatus(resp, 200) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 964 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 965 | s.AssertNil(err, fmt.Sprint(err)) |
| 966 | s.AssertContains(string(data), "vpp_details") |
| 967 | s.AssertContains(string(data), "version") |
| 968 | s.AssertContains(string(data), "build_date") |
| 969 | s.AssertNotContains(string(data), "build_by") |
| 970 | s.AssertNotContains(string(data), "build_host") |
| 971 | s.AssertNotContains(string(data), "build_dir") |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 972 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | func HttpStaticBuildInUrlGetVersionVerboseTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 976 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 977 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 978 | 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] | 979 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 980 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 981 | 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] | 982 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 983 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 984 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 985 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 986 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 987 | s.AssertHttpStatus(resp, 200) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 988 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 989 | s.AssertNil(err, fmt.Sprint(err)) |
| 990 | s.AssertContains(string(data), "vpp_details") |
| 991 | s.AssertContains(string(data), "version") |
| 992 | s.AssertContains(string(data), "build_date") |
| 993 | s.AssertContains(string(data), "build_by") |
| 994 | s.AssertContains(string(data), "build_host") |
| 995 | s.AssertContains(string(data), "build_dir") |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 996 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 997 | } |
| 998 | |
| 999 | func HttpStaticBuildInUrlGetIfListTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1000 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1001 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1002 | 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] | 1003 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 1004 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1005 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/interface_list.json", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1006 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1007 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1008 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1009 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1010 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1011 | s.AssertHttpStatus(resp, 200) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1012 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1013 | s.AssertNil(err, fmt.Sprint(err)) |
| 1014 | s.AssertContains(string(data), "interface_list") |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1015 | s.AssertContains(string(data), s.VppIfName()) |
| 1016 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | func HttpStaticBuildInUrlGetIfStatsTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1020 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1021 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1022 | 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] | 1023 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 1024 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1025 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/interface_stats.json", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1026 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1027 | resp, err := client.Do(req) |
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 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1030 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1031 | s.AssertHttpStatus(resp, 200) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1032 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1033 | s.AssertNil(err, fmt.Sprint(err)) |
| 1034 | s.AssertContains(string(data), "interface_stats") |
| 1035 | s.AssertContains(string(data), "local0") |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1036 | s.AssertContains(string(data), s.VppIfName()) |
| 1037 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | func validatePostInterfaceStats(s *NoTopoSuite, data string) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1041 | s.AssertContains(data, "interface_stats") |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1042 | s.AssertContains(data, s.VppIfName()) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1043 | s.AssertNotContains(data, "error") |
| 1044 | s.AssertNotContains(data, "local0") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1045 | } |
| 1046 | |
| 1047 | func HttpStaticBuildInUrlPostIfStatsTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1048 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1049 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1050 | 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] | 1051 | body := []byte(s.VppIfName()) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1052 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 1053 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1054 | req, err := http.NewRequest("POST", |
| 1055 | "http://"+serverAddress+":80/interface_stats.json", bytes.NewBuffer(body)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1056 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1057 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1058 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1059 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1060 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1061 | s.AssertHttpStatus(resp, 200) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1062 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1063 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1064 | validatePostInterfaceStats(s, string(data)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1065 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | func HttpStaticMacTimeTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1069 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1070 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1071 | 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] | 1072 | s.Log(vpp.Vppctl("mactime enable-disable " + s.VppIfName())) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1073 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 1074 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1075 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/mactime.json", 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)) |
| 1084 | s.AssertContains(string(data), "mactime") |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1085 | s.AssertContains(string(data), s.HostAddr()) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1086 | s.AssertContains(string(data), s.GetInterfaceByName(TapInterfaceName).HwAddress.String()) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1087 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "application/json") |
| 1088 | parsedTime, err := time.Parse(time.RFC1123, resp.Header.Get("Date")) |
| 1089 | s.AssertNil(err, fmt.Sprint(err)) |
| 1090 | s.AssertTimeEqualWithinThreshold(parsedTime, time.Now(), time.Minute*5) |
Adrian Villin | 7e6606a | 2024-08-16 15:23:28 +0200 | [diff] [blame] | 1091 | s.AssertEqual(len(resp.Header.Get("Date")), 29) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | func HttpInvalidRequestLineTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1095 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1096 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1097 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1098 | |
Matus Fabian | 69123a3 | 2024-08-23 17:35:50 +0200 | [diff] [blame] | 1099 | resp, err := TcpSendReceive(serverAddress+":80", " GET / HTTP/1.1") |
| 1100 | s.AssertNil(err, fmt.Sprint(err)) |
| 1101 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid request line start not allowed") |
| 1102 | |
| 1103 | resp, err = TcpSendReceive(serverAddress+":80", "\rGET / HTTP/1.1") |
| 1104 | s.AssertNil(err, fmt.Sprint(err)) |
| 1105 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid request line start not allowed") |
| 1106 | |
| 1107 | resp, err = TcpSendReceive(serverAddress+":80", "\nGET / HTTP/1.1") |
| 1108 | s.AssertNil(err, fmt.Sprint(err)) |
| 1109 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid request line start not allowed") |
| 1110 | |
| 1111 | resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1") |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1112 | s.AssertNil(err, fmt.Sprint(err)) |
| 1113 | 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] | 1114 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1115 | resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/1.1\r\n") |
| 1116 | s.AssertNil(err, fmt.Sprint(err)) |
| 1117 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid framing not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1118 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1119 | resp, err = TcpSendReceive(serverAddress+":80", "GET /\r\n\r\n") |
| 1120 | s.AssertNil(err, fmt.Sprint(err)) |
| 1121 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "HTTP-version must be present") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1122 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1123 | resp, err = TcpSendReceive(serverAddress+":80", "GET HTTP/1.1\r\n\r\n") |
| 1124 | s.AssertNil(err, fmt.Sprint(err)) |
| 1125 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "request-target must be present") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1126 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1127 | resp, err = TcpSendReceive(serverAddress+":80", "GET HTTP/1.1\r\n\r\n") |
| 1128 | s.AssertNil(err, fmt.Sprint(err)) |
| 1129 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "request-target must be present") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1130 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1131 | resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP/x\r\n\r\n") |
| 1132 | s.AssertNil(err, fmt.Sprint(err)) |
| 1133 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'HTTP/x' invalid http version not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1134 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1135 | resp, err = TcpSendReceive(serverAddress+":80", "GET / HTTP1.1\r\n\r\n") |
| 1136 | s.AssertNil(err, fmt.Sprint(err)) |
| 1137 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'HTTP1.1' invalid http version not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1138 | } |
| 1139 | |
Matus Fabian | 69123a3 | 2024-08-23 17:35:50 +0200 | [diff] [blame] | 1140 | func HttpRequestLineTest(s *NoTopoSuite) { |
| 1141 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1142 | serverAddress := s.VppAddr() |
Matus Fabian | 69123a3 | 2024-08-23 17:35:50 +0200 | [diff] [blame] | 1143 | vpp.Vppctl("http cli server") |
| 1144 | |
| 1145 | resp, err := TcpSendReceive(serverAddress+":80", "\r\nGET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent:test\r\n\r\n") |
| 1146 | s.AssertNil(err, fmt.Sprint(err)) |
| 1147 | s.AssertContains(resp, "HTTP/1.1 200 OK") |
| 1148 | s.AssertContains(resp, "<html>", "html content not found") |
| 1149 | } |
| 1150 | |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1151 | func HttpInvalidTargetSyntaxTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1152 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1153 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1154 | 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] | 1155 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1156 | resp, err := TcpSendReceive(serverAddress+":80", "GET /interface|stats.json HTTP/1.1\r\n\r\n") |
| 1157 | s.AssertNil(err, fmt.Sprint(err)) |
| 1158 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'|' not allowed in target path") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1159 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1160 | resp, err = TcpSendReceive(serverAddress+":80", "GET /interface#stats.json HTTP/1.1\r\n\r\n") |
| 1161 | s.AssertNil(err, fmt.Sprint(err)) |
| 1162 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'#' not allowed in target path") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1163 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1164 | resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%stats.json HTTP/1.1\r\n\r\n") |
| 1165 | s.AssertNil(err, fmt.Sprint(err)) |
| 1166 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1167 | "after '%' there must be two hex-digit characters in target path") |
| 1168 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1169 | resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%1stats.json HTTP/1.1\r\n\r\n") |
| 1170 | s.AssertNil(err, fmt.Sprint(err)) |
| 1171 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1172 | "after '%' there must be two hex-digit characters in target path") |
| 1173 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1174 | resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%Bstats.json HTTP/1.1\r\n\r\n") |
| 1175 | s.AssertNil(err, fmt.Sprint(err)) |
| 1176 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1177 | "after '%' there must be two hex-digit characters in target path") |
| 1178 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1179 | resp, err = TcpSendReceive(serverAddress+":80", "GET /interface%stats.json%B HTTP/1.1\r\n\r\n") |
| 1180 | s.AssertNil(err, fmt.Sprint(err)) |
| 1181 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1182 | "after '%' there must be two hex-digit characters in target path") |
| 1183 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1184 | resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose>true HTTP/1.1\r\n\r\n") |
| 1185 | s.AssertNil(err, fmt.Sprint(err)) |
| 1186 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'>' not allowed in target query") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1187 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1188 | resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose%true HTTP/1.1\r\n\r\n") |
| 1189 | s.AssertNil(err, fmt.Sprint(err)) |
| 1190 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1191 | "after '%' there must be two hex-digit characters in target query") |
| 1192 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1193 | resp, err = TcpSendReceive(serverAddress+":80", "GET /version.json?verbose=%1 HTTP/1.1\r\n\r\n") |
| 1194 | s.AssertNil(err, fmt.Sprint(err)) |
| 1195 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1196 | "after '%' there must be two hex-digit characters in target query") |
| 1197 | } |
| 1198 | |
| 1199 | func HttpInvalidContentLengthTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1200 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1201 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1202 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1203 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1204 | resp, err := TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length:\r\n\r\n") |
| 1205 | s.AssertNil(err, fmt.Sprint(err)) |
| 1206 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Content-Length value must be present") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1207 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1208 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length: \r\n\r\n") |
| 1209 | s.AssertNil(err, fmt.Sprint(err)) |
| 1210 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Content-Length value must be present") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1211 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1212 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nContent-Length: a\r\n\r\n") |
| 1213 | s.AssertNil(err, fmt.Sprint(err)) |
| 1214 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1215 | "Content-Length value other than digit not allowed") |
| 1216 | } |
| 1217 | |
| 1218 | func HttpContentLengthTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1219 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1220 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1221 | 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] | 1222 | ifName := s.VppIfName() |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1223 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1224 | resp, err := TcpSendReceive(serverAddress+":80", |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1225 | "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] | 1226 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1227 | validatePostInterfaceStats(s, resp) |
| 1228 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1229 | resp, err = TcpSendReceive(serverAddress+":80", |
Matus Fabian | 82b3cc1 | 2024-11-21 17:01:45 +0100 | [diff] [blame^] | 1230 | "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] | 1231 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1232 | validatePostInterfaceStats(s, resp) |
| 1233 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1234 | resp, err = TcpSendReceive(serverAddress+":80", |
Matus Fabian | 82b3cc1 | 2024-11-21 17:01:45 +0100 | [diff] [blame^] | 1235 | "POST /interface_stats.json HTTP/1.1\r\nContent-Length:\t\t4\r\n\r\n"+ifName) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1236 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1237 | validatePostInterfaceStats(s, resp) |
| 1238 | } |
| 1239 | |
| 1240 | func HttpMethodNotImplementedTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1241 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1242 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1243 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1244 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 1245 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1246 | req, err := http.NewRequest("OPTIONS", "http://"+serverAddress+":80/show/version", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1247 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1248 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1249 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1250 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1251 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1252 | s.AssertHttpStatus(resp, 501) |
| 1253 | s.AssertHttpHeaderNotPresent(resp, "Content-Type") |
| 1254 | s.AssertHttpContentLength(resp, int64(0)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1255 | } |
| 1256 | |
| 1257 | func HttpVersionNotSupportedTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1258 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1259 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1260 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1261 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1262 | resp, err := TcpSendReceive(serverAddress+":80", "GET / HTTP/2\r\n\r\n") |
| 1263 | s.AssertNil(err, fmt.Sprint(err)) |
| 1264 | s.AssertContains(resp, "HTTP/1.1 505 HTTP Version Not Supported") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | func HttpUriDecodeTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1268 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1269 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1270 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1271 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 1272 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1273 | 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] | 1274 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1275 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1276 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1277 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1278 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1279 | s.AssertHttpStatus(resp, 200) |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1280 | data, err := io.ReadAll(resp.Body) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1281 | s.AssertNil(err, fmt.Sprint(err)) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1282 | s.AssertNotContains(string(data), "unknown input") |
| 1283 | s.AssertContains(string(data), "Compiler") |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1284 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1285 | } |
| 1286 | |
| 1287 | func HttpHeadersTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1288 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1289 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1290 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1291 | |
Matus Fabian | 82b3cc1 | 2024-11-21 17:01:45 +0100 | [diff] [blame^] | 1292 | transport := http.DefaultTransport |
| 1293 | transport.(*http.Transport).Proxy = nil |
| 1294 | transport.(*http.Transport).DisableKeepAlives = false |
| 1295 | client := &http.Client{ |
| 1296 | Transport: transport, |
| 1297 | Timeout: time.Second * 30, |
| 1298 | } |
| 1299 | |
| 1300 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/show/version", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1301 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 82b3cc1 | 2024-11-21 17:01:45 +0100 | [diff] [blame^] | 1302 | req.Header.Add("Accept", "text/xml") |
| 1303 | req.Header.Add("Accept-Language", "*") |
| 1304 | req.Header.Add("Accept", "text/plain") |
| 1305 | req.Header.Add("Accept", "text/html") |
| 1306 | resp, err := client.Do(req) |
| 1307 | s.AssertNil(err, fmt.Sprint(err)) |
| 1308 | defer resp.Body.Close() |
| 1309 | s.Log(DumpHttpResp(resp, true)) |
| 1310 | s.AssertHttpStatus(resp, 200) |
| 1311 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/plain") |
| 1312 | data, err := io.ReadAll(resp.Body) |
| 1313 | s.AssertNil(err, fmt.Sprint(err)) |
| 1314 | s.AssertNotContains(string(data), "<html>", "html content received instead of plain text") |
| 1315 | |
| 1316 | req2, err := http.NewRequest("GET", "http://"+serverAddress+":80/show/version", nil) |
| 1317 | s.AssertNil(err, fmt.Sprint(err)) |
| 1318 | req2.Header.Add("Accept", "text/html") |
| 1319 | resp2, err := client.Do(req2) |
| 1320 | s.AssertNil(err, fmt.Sprint(err)) |
| 1321 | defer resp2.Body.Close() |
| 1322 | s.Log(DumpHttpResp(resp2, true)) |
| 1323 | s.AssertHttpStatus(resp2, 200) |
| 1324 | s.AssertHttpHeaderWithValue(resp2, "Content-Type", "text/html") |
| 1325 | data2, err := io.ReadAll(resp2.Body) |
| 1326 | s.AssertNil(err, fmt.Sprint(err)) |
| 1327 | s.AssertContains(string(data2), "<html>", "html content not received") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1328 | } |
| 1329 | |
| 1330 | func HttpInvalidHeadersTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1331 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1332 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1333 | vpp.Vppctl("http cli server") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1334 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1335 | resp, err := TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nUser-Agent: test\r\n") |
| 1336 | s.AssertNil(err, fmt.Sprint(err)) |
| 1337 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "Header section must end with CRLF CRLF") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1338 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1339 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser@Agent:test\r\n\r\n") |
| 1340 | s.AssertNil(err, fmt.Sprint(err)) |
| 1341 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "'@' not allowed in field name") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1342 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1343 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent\r\n\r\n") |
| 1344 | s.AssertNil(err, fmt.Sprint(err)) |
| 1345 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "incomplete field line not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1346 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1347 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\n: test\r\n\r\n") |
| 1348 | s.AssertNil(err, fmt.Sprint(err)) |
| 1349 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field name not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1350 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1351 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\rUser-Agent:test\r\n\r\n") |
| 1352 | s.AssertNil(err, fmt.Sprint(err)) |
| 1353 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid field line end not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1354 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1355 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\nUser-Agent:test\r\n\r\n") |
| 1356 | s.AssertNil(err, fmt.Sprint(err)) |
| 1357 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "invalid field line end not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1358 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1359 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent:\r\n\r\n") |
| 1360 | s.AssertNil(err, fmt.Sprint(err)) |
| 1361 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field value not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1362 | |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1363 | resp, err = TcpSendReceive(serverAddress+":80", "GET /show/version HTTP/1.1\r\nHost:"+serverAddress+":80\r\nUser-Agent: \r\n\r\n") |
| 1364 | s.AssertNil(err, fmt.Sprint(err)) |
| 1365 | s.AssertContains(resp, "HTTP/1.1 400 Bad Request", "empty field value not allowed") |
Matus Fabian | 82ad966 | 2024-06-04 19:00:00 +0200 | [diff] [blame] | 1366 | } |
| 1367 | |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 1368 | func HeaderServerTest(s *NoTopoSuite) { |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1369 | vpp := s.GetContainerByName("vpp").VppInstance |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1370 | serverAddress := s.VppAddr() |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1371 | vpp.Vppctl("http cli server") |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 1372 | |
Matus Fabian | fa5defd | 2024-10-02 09:00:19 +0200 | [diff] [blame] | 1373 | client := NewHttpClient(defaultHttpTimeout) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 1374 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/show/version", nil) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1375 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | 595d8cb | 2024-05-17 11:28:43 +0200 | [diff] [blame] | 1376 | resp, err := client.Do(req) |
Adrian Villin | 4677d92 | 2024-06-14 09:32:39 +0200 | [diff] [blame] | 1377 | s.AssertNil(err, fmt.Sprint(err)) |
Matus Fabian | b7a9ed7 | 2024-05-10 16:20:40 +0200 | [diff] [blame] | 1378 | defer resp.Body.Close() |
Matus Fabian | 8ca6ce6 | 2024-06-20 17:08:26 +0200 | [diff] [blame] | 1379 | s.Log(DumpHttpResp(resp, true)) |
Matus Fabian | a647a83 | 2024-08-26 18:01:14 +0200 | [diff] [blame] | 1380 | s.AssertHttpStatus(resp, 200) |
| 1381 | s.AssertHttpHeaderWithValue(resp, "Server", "http_cli_server") |
| 1382 | s.AssertHttpHeaderWithValue(resp, "Content-Type", "text/html") |
Matus Fabian | 616201a | 2024-05-02 11:17:15 +0200 | [diff] [blame] | 1383 | } |
Matus Fabian | 8416722 | 2024-10-17 13:41:51 +0200 | [diff] [blame] | 1384 | |
| 1385 | func HttpConnTimeoutTest(s *NoTopoSuite) { |
| 1386 | vpp := s.GetContainerByName("vpp").VppInstance |
| 1387 | serverAddress := s.VppAddr() |
| 1388 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers debug keepalive-timeout 2")) |
| 1389 | |
| 1390 | req := "GET /version.json HTTP/1.1\r\nHost:" + serverAddress + ":80\r\nUser-Agent:test\r\n\r\n" |
| 1391 | conn, err := net.DialTimeout("tcp", serverAddress+":80", time.Second*30) |
| 1392 | s.AssertNil(err, fmt.Sprint(err)) |
| 1393 | defer conn.Close() |
| 1394 | err = conn.SetDeadline(time.Now().Add(time.Second * 30)) |
| 1395 | s.AssertNil(err, fmt.Sprint(err)) |
| 1396 | _, err = conn.Write([]byte(req)) |
| 1397 | s.AssertNil(err, fmt.Sprint(err)) |
| 1398 | reply := make([]byte, 1024) |
| 1399 | _, err = conn.Read(reply) |
| 1400 | s.AssertNil(err, fmt.Sprint(err)) |
| 1401 | s.AssertContains(string(reply), "HTTP/1.1 200 OK") |
| 1402 | s.Log(vpp.Vppctl("show session verbose 2")) |
| 1403 | |
| 1404 | s.Log("waiting for close on the server side") |
| 1405 | time.Sleep(time.Second * 5) |
| 1406 | s.Log(vpp.Vppctl("show session verbose 2")) |
| 1407 | |
| 1408 | _, err = conn.Write([]byte(req)) |
| 1409 | s.AssertNil(err, fmt.Sprint(err)) |
| 1410 | reply = make([]byte, 1024) |
| 1411 | _, err = conn.Read(reply) |
| 1412 | s.AssertMatchError(err, io.EOF, "connection not closed by server") |
| 1413 | } |
Matus Fabian | 82b3cc1 | 2024-11-21 17:01:45 +0100 | [diff] [blame^] | 1414 | |
| 1415 | func HttpIgnoreH2UpgradeTest(s *NoTopoSuite) { |
| 1416 | vpp := s.GetContainerByName("vpp").VppInstance |
| 1417 | serverAddress := s.VppAddr() |
| 1418 | s.Log(vpp.Vppctl("http static server uri tcp://" + serverAddress + "/80 url-handlers")) |
| 1419 | |
| 1420 | transport := http.DefaultTransport |
| 1421 | transport.(*http.Transport).Proxy = nil |
| 1422 | transport.(*http.Transport).DisableKeepAlives = false |
| 1423 | client := &http.Client{ |
| 1424 | Transport: transport, |
| 1425 | Timeout: time.Second * 30, |
| 1426 | } |
| 1427 | |
| 1428 | req, err := http.NewRequest("GET", "http://"+serverAddress+":80/version.json", nil) |
| 1429 | s.AssertNil(err, fmt.Sprint(err)) |
| 1430 | req.Header.Add("Connection", "Upgrade") |
| 1431 | req.Header.Add("Upgrade", "HTTP/2.0") |
| 1432 | resp, err := client.Do(req) |
| 1433 | s.AssertNil(err, fmt.Sprint(err)) |
| 1434 | defer resp.Body.Close() |
| 1435 | s.Log(DumpHttpResp(resp, true)) |
| 1436 | s.AssertHttpStatus(resp, 200) |
| 1437 | s.AssertHttpHeaderNotPresent(resp, "Upgrade") |
| 1438 | } |