| 
									
										
										
										
											2019-06-30 16:07:58 -06:00
										 |  |  | // Copyright 2015 Matthew Holt and The Caddy Authors | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Licensed under the Apache License, Version 2.0 (the "License"); | 
					
						
							|  |  |  | // you may not use this file except in compliance with the License. | 
					
						
							|  |  |  | // You may obtain a copy of the License at | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | //     http://www.apache.org/licenses/LICENSE-2.0 | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // Unless required by applicable law or agreed to in writing, software | 
					
						
							|  |  |  | // distributed under the License is distributed on an "AS IS" BASIS, | 
					
						
							|  |  |  | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 
					
						
							|  |  |  | // See the License for the specific language governing permissions and | 
					
						
							|  |  |  | // limitations under the License. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-08 16:46:38 -06:00
										 |  |  | package caddy | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"reflect" | 
					
						
							|  |  |  | 	"testing" | 
					
						
							| 
									
										
										
										
											2023-08-06 02:09:16 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/caddyserver/caddy/v2/internal" | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:14:39 -06:00
										 |  |  | func TestSplitNetworkAddress(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 	for i, tc := range []struct { | 
					
						
							|  |  |  | 		input         string | 
					
						
							|  |  |  | 		expectNetwork string | 
					
						
							|  |  |  | 		expectHost    string | 
					
						
							|  |  |  | 		expectPort    string | 
					
						
							|  |  |  | 		expectErr     bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2025-06-03 02:24:32 +03:00
										 |  |  | 			input:      "", | 
					
						
							| 
									
										
										
										
											2024-10-21 10:02:29 -04:00
										 |  |  | 			expectHost: "", | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2022-07-25 17:28:20 -06:00
										 |  |  | 			input:      "foo", | 
					
						
							|  |  |  | 			expectHost: "foo", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: ":", // empty host & empty port | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2025-06-03 02:24:32 +03:00
										 |  |  | 			input:      "::", | 
					
						
							| 
									
										
										
										
											2024-10-21 10:02:29 -04:00
										 |  |  | 			expectHost: "::", | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2022-07-25 17:28:20 -06:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			input:      "[::]", | 
					
						
							|  |  |  | 			expectHost: "::", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:      ":1234", | 
					
						
							|  |  |  | 			expectPort: "1234", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			input:      "foo:1234", | 
					
						
							|  |  |  | 			expectHost: "foo", | 
					
						
							|  |  |  | 			expectPort: "1234", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:      "foo:1234-5678", | 
					
						
							|  |  |  | 			expectHost: "foo", | 
					
						
							|  |  |  | 			expectPort: "1234-5678", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:         "udp/foo:1234", | 
					
						
							|  |  |  | 			expectNetwork: "udp", | 
					
						
							|  |  |  | 			expectHost:    "foo", | 
					
						
							|  |  |  | 			expectPort:    "1234", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:         "tcp6/foo:1234-5678", | 
					
						
							|  |  |  | 			expectNetwork: "tcp6", | 
					
						
							|  |  |  | 			expectHost:    "foo", | 
					
						
							|  |  |  | 			expectPort:    "1234-5678", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:         "udp/", | 
					
						
							|  |  |  | 			expectNetwork: "udp", | 
					
						
							| 
									
										
										
										
											2024-10-21 10:02:29 -04:00
										 |  |  | 			expectHost:    "", | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-07-08 16:46:38 -06:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			input:         "unix//foo/bar", | 
					
						
							|  |  |  | 			expectNetwork: "unix", | 
					
						
							|  |  |  | 			expectHost:    "/foo/bar", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-09-05 13:14:39 -06:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			input:         "unixgram//foo/bar", | 
					
						
							|  |  |  | 			expectNetwork: "unixgram", | 
					
						
							|  |  |  | 			expectHost:    "/foo/bar", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:         "unixpacket//foo/bar", | 
					
						
							|  |  |  | 			expectNetwork: "unixpacket", | 
					
						
							|  |  |  | 			expectHost:    "/foo/bar", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 	} { | 
					
						
							| 
									
										
										
										
											2019-09-05 13:14:39 -06:00
										 |  |  | 		actualNetwork, actualHost, actualPort, err := SplitNetworkAddress(tc.input) | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 		if tc.expectErr && err == nil { | 
					
						
							| 
									
										
										
										
											2022-07-25 17:28:20 -06:00
										 |  |  | 			t.Errorf("Test %d: Expected error but got %v", i, err) | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if !tc.expectErr && err != nil { | 
					
						
							| 
									
										
										
										
											2022-07-25 17:28:20 -06:00
										 |  |  | 			t.Errorf("Test %d: Expected no error but got %v", i, err) | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		if actualNetwork != tc.expectNetwork { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected network '%s' but got '%s'", i, tc.expectNetwork, actualNetwork) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if actualHost != tc.expectHost { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected host '%s' but got '%s'", i, tc.expectHost, actualHost) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if actualPort != tc.expectPort { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected port '%s' but got '%s'", i, tc.expectPort, actualPort) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:14:39 -06:00
										 |  |  | func TestJoinNetworkAddress(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 	for i, tc := range []struct { | 
					
						
							|  |  |  | 		network, host, port string | 
					
						
							|  |  |  | 		expect              string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			network: "", host: "", port: "", | 
					
						
							|  |  |  | 			expect: "", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			network: "tcp", host: "", port: "", | 
					
						
							|  |  |  | 			expect: "tcp/", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			network: "", host: "foo", port: "", | 
					
						
							|  |  |  | 			expect: "foo", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			network: "", host: "", port: "1234", | 
					
						
							|  |  |  | 			expect: ":1234", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			network: "", host: "", port: "1234-5678", | 
					
						
							|  |  |  | 			expect: ":1234-5678", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			network: "", host: "foo", port: "1234", | 
					
						
							|  |  |  | 			expect: "foo:1234", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			network: "udp", host: "foo", port: "1234", | 
					
						
							|  |  |  | 			expect: "udp/foo:1234", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			network: "udp", host: "", port: "1234", | 
					
						
							|  |  |  | 			expect: "udp/:1234", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-07-08 16:46:38 -06:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			network: "unix", host: "/foo/bar", port: "", | 
					
						
							|  |  |  | 			expect: "unix//foo/bar", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2020-04-10 17:31:38 -06:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			network: "unix", host: "/foo/bar", port: "0", | 
					
						
							|  |  |  | 			expect: "unix//foo/bar", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			network: "unix", host: "/foo/bar", port: "1234", | 
					
						
							|  |  |  | 			expect: "unix//foo/bar", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-10-11 14:25:39 -06:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			network: "", host: "::1", port: "1234", | 
					
						
							|  |  |  | 			expect: "[::1]:1234", | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 	} { | 
					
						
							| 
									
										
										
										
											2019-09-05 13:14:39 -06:00
										 |  |  | 		actual := JoinNetworkAddress(tc.network, tc.host, tc.port) | 
					
						
							| 
									
										
										
										
											2019-05-07 10:15:46 -06:00
										 |  |  | 		if actual != tc.expect { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-09-05 13:14:39 -06:00
										 |  |  | func TestParseNetworkAddress(t *testing.T) { | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 	for i, tc := range []struct { | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 		input          string | 
					
						
							|  |  |  | 		defaultNetwork string | 
					
						
							|  |  |  | 		defaultPort    uint | 
					
						
							|  |  |  | 		expectAddr     NetworkAddress | 
					
						
							|  |  |  | 		expectErr      bool | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2025-06-03 02:24:32 +03:00
										 |  |  | 			input:      "", | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{}, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          ":", | 
					
						
							|  |  |  | 			defaultNetwork: "udp", | 
					
						
							| 
									
										
										
										
											2022-07-25 17:28:20 -06:00
										 |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 				Network: "udp", | 
					
						
							| 
									
										
										
										
											2022-07-25 17:28:20 -06:00
										 |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          "[::]", | 
					
						
							|  |  |  | 			defaultNetwork: "udp", | 
					
						
							|  |  |  | 			defaultPort:    53, | 
					
						
							| 
									
										
										
										
											2022-07-25 17:28:20 -06:00
										 |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 				Network:   "udp", | 
					
						
							|  |  |  | 				Host:      "::", | 
					
						
							|  |  |  | 				StartPort: 53, | 
					
						
							|  |  |  | 				EndPort:   53, | 
					
						
							| 
									
										
										
										
											2022-07-25 17:28:20 -06:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          ":1234", | 
					
						
							|  |  |  | 			defaultNetwork: "udp", | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 				Network:   "udp", | 
					
						
							| 
									
										
										
										
											2019-11-12 01:33:38 +03:00
										 |  |  | 				Host:      "", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          "udp/:1234", | 
					
						
							|  |  |  | 			defaultNetwork: "udp", | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 				Network:   "udp", | 
					
						
							| 
									
										
										
										
											2019-11-12 01:33:38 +03:00
										 |  |  | 				Host:      "", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          "tcp6/:1234", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2019-11-12 01:33:38 +03:00
										 |  |  | 				Network:   "tcp6", | 
					
						
							|  |  |  | 				Host:      "", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          "tcp4/localhost:1234", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2019-11-12 01:33:38 +03:00
										 |  |  | 				Network:   "tcp4", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          "unix//foo/bar", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2019-11-12 01:33:38 +03:00
										 |  |  | 				Network: "unix", | 
					
						
							|  |  |  | 				Host:    "/foo/bar", | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          "localhost:1234-1234", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2019-11-12 01:33:38 +03:00
										 |  |  | 				Network:   "tcp", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          "localhost:2-1", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							|  |  |  | 			expectErr:      true, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          "localhost:0", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2019-11-12 01:33:38 +03:00
										 |  |  | 				Network:   "tcp", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 0, | 
					
						
							|  |  |  | 				EndPort:   0, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 			input:          "localhost:1-999999999999", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							|  |  |  | 			expectErr:      true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} { | 
					
						
							|  |  |  | 		actualAddr, err := ParseNetworkAddressWithDefaults(tc.input, tc.defaultNetwork, tc.defaultPort) | 
					
						
							|  |  |  | 		if tc.expectErr && err == nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected error but got: %v", i, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !tc.expectErr && err != nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected no error but got: %v", i, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if actualAddr.Network != tc.expectAddr.Network { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected network '%v' but got '%v'", i, tc.expectAddr, actualAddr) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !reflect.DeepEqual(tc.expectAddr, actualAddr) { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddr, actualAddr) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestParseNetworkAddressWithDefaults(t *testing.T) { | 
					
						
							|  |  |  | 	for i, tc := range []struct { | 
					
						
							|  |  |  | 		input          string | 
					
						
							|  |  |  | 		defaultNetwork string | 
					
						
							|  |  |  | 		defaultPort    uint | 
					
						
							|  |  |  | 		expectAddr     NetworkAddress | 
					
						
							|  |  |  | 		expectErr      bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2025-06-03 02:24:32 +03:00
										 |  |  | 			input:      "", | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{}, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			input:          ":", | 
					
						
							|  |  |  | 			defaultNetwork: "udp", | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							|  |  |  | 				Network: "udp", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "[::]", | 
					
						
							|  |  |  | 			defaultNetwork: "udp", | 
					
						
							|  |  |  | 			defaultPort:    53, | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "udp", | 
					
						
							|  |  |  | 				Host:      "::", | 
					
						
							|  |  |  | 				StartPort: 53, | 
					
						
							|  |  |  | 				EndPort:   53, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          ":1234", | 
					
						
							|  |  |  | 			defaultNetwork: "udp", | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "udp", | 
					
						
							|  |  |  | 				Host:      "", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "udp/:1234", | 
					
						
							|  |  |  | 			defaultNetwork: "udp", | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "udp", | 
					
						
							|  |  |  | 				Host:      "", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "tcp6/:1234", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "tcp6", | 
					
						
							|  |  |  | 				Host:      "", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "tcp4/localhost:1234", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "tcp4", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "unix//foo/bar", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							|  |  |  | 				Network: "unix", | 
					
						
							|  |  |  | 				Host:    "/foo/bar", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "localhost:1234-1234", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "tcp", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "localhost:2-1", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							|  |  |  | 			expectErr:      true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "localhost:0", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							|  |  |  | 			expectAddr: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "tcp", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 0, | 
					
						
							|  |  |  | 				EndPort:   0, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "localhost:1-999999999999", | 
					
						
							|  |  |  | 			defaultNetwork: "tcp", | 
					
						
							|  |  |  | 			expectErr:      true, | 
					
						
							|  |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 	} { | 
					
						
							| 
									
										
										
										
											2023-05-03 13:07:22 -04:00
										 |  |  | 		actualAddr, err := ParseNetworkAddressWithDefaults(tc.input, tc.defaultNetwork, tc.defaultPort) | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		if tc.expectErr && err == nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected error but got: %v", i, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !tc.expectErr && err != nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected no error but got: %v", i, err) | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-11-12 01:33:38 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if actualAddr.Network != tc.expectAddr.Network { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected network '%v' but got '%v'", i, tc.expectAddr, actualAddr) | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2019-11-12 01:33:38 +03:00
										 |  |  | 		if !reflect.DeepEqual(tc.expectAddr, actualAddr) { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected addresses %v but got %v", i, tc.expectAddr, actualAddr) | 
					
						
							| 
									
										
										
										
											2019-03-26 15:45:51 -06:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-12-06 11:45:50 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestJoinHostPort(t *testing.T) { | 
					
						
							|  |  |  | 	for i, tc := range []struct { | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 		pa     NetworkAddress | 
					
						
							| 
									
										
										
										
											2019-12-06 11:45:50 -07:00
										 |  |  | 		offset uint | 
					
						
							|  |  |  | 		expect string | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			pa: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2019-12-06 11:45:50 -07:00
										 |  |  | 				Network:   "tcp", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1234, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expect: "localhost:1234", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			pa: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2019-12-06 11:45:50 -07:00
										 |  |  | 				Network:   "tcp", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1235, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expect: "localhost:1234", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			pa: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2019-12-06 11:45:50 -07:00
										 |  |  | 				Network:   "tcp", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 1234, | 
					
						
							|  |  |  | 				EndPort:   1235, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			offset: 1, | 
					
						
							|  |  |  | 			expect: "localhost:1235", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2020-04-07 08:33:45 -06:00
										 |  |  | 			pa: NetworkAddress{ | 
					
						
							| 
									
										
										
										
											2019-12-06 11:45:50 -07:00
										 |  |  | 				Network: "unix", | 
					
						
							|  |  |  | 				Host:    "/run/php/php7.3-fpm.sock", | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expect: "/run/php/php7.3-fpm.sock", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} { | 
					
						
							|  |  |  | 		actual := tc.pa.JoinHostPort(tc.offset) | 
					
						
							|  |  |  | 		if actual != tc.expect { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected '%s' but got '%s'", i, tc.expect, actual) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2022-08-03 11:04:51 -06:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestExpand(t *testing.T) { | 
					
						
							|  |  |  | 	for i, tc := range []struct { | 
					
						
							|  |  |  | 		input  NetworkAddress | 
					
						
							|  |  |  | 		expect []NetworkAddress | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "tcp", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 2000, | 
					
						
							|  |  |  | 				EndPort:   2000, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expect: []NetworkAddress{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Network:   "tcp", | 
					
						
							|  |  |  | 					Host:      "localhost", | 
					
						
							|  |  |  | 					StartPort: 2000, | 
					
						
							|  |  |  | 					EndPort:   2000, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "tcp", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 2000, | 
					
						
							|  |  |  | 				EndPort:   2002, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expect: []NetworkAddress{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Network:   "tcp", | 
					
						
							|  |  |  | 					Host:      "localhost", | 
					
						
							|  |  |  | 					StartPort: 2000, | 
					
						
							|  |  |  | 					EndPort:   2000, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Network:   "tcp", | 
					
						
							|  |  |  | 					Host:      "localhost", | 
					
						
							|  |  |  | 					StartPort: 2001, | 
					
						
							|  |  |  | 					EndPort:   2001, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Network:   "tcp", | 
					
						
							|  |  |  | 					Host:      "localhost", | 
					
						
							|  |  |  | 					StartPort: 2002, | 
					
						
							|  |  |  | 					EndPort:   2002, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "tcp", | 
					
						
							|  |  |  | 				Host:      "localhost", | 
					
						
							|  |  |  | 				StartPort: 2000, | 
					
						
							|  |  |  | 				EndPort:   1999, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expect: []NetworkAddress{}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input: NetworkAddress{ | 
					
						
							|  |  |  | 				Network:   "unix", | 
					
						
							|  |  |  | 				Host:      "/foo/bar", | 
					
						
							|  |  |  | 				StartPort: 0, | 
					
						
							|  |  |  | 				EndPort:   0, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 			expect: []NetworkAddress{ | 
					
						
							|  |  |  | 				{ | 
					
						
							|  |  |  | 					Network:   "unix", | 
					
						
							|  |  |  | 					Host:      "/foo/bar", | 
					
						
							|  |  |  | 					StartPort: 0, | 
					
						
							|  |  |  | 					EndPort:   0, | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 			}, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} { | 
					
						
							|  |  |  | 		actual := tc.input.Expand() | 
					
						
							|  |  |  | 		if !reflect.DeepEqual(actual, tc.expect) { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected %+v but got %+v", i, tc.expect, actual) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2023-06-23 22:49:41 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | func TestSplitUnixSocketPermissionsBits(t *testing.T) { | 
					
						
							|  |  |  | 	for i, tc := range []struct { | 
					
						
							|  |  |  | 		input          string | 
					
						
							|  |  |  | 		expectNetwork  string | 
					
						
							|  |  |  | 		expectPath     string | 
					
						
							|  |  |  | 		expectFileMode string | 
					
						
							|  |  |  | 		expectErr      bool | 
					
						
							|  |  |  | 	}{ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "./foo.socket", | 
					
						
							|  |  |  | 			expectPath:     "./foo.socket", | 
					
						
							|  |  |  | 			expectFileMode: "--w-------", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          `.\relative\path.socket`, | 
					
						
							|  |  |  | 			expectPath:     `.\relative\path.socket`, | 
					
						
							|  |  |  | 			expectFileMode: "--w-------", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// literal colon in resulting address | 
					
						
							|  |  |  | 			// and defaulting to 0200 bits | 
					
						
							|  |  |  | 			input:          "./foo.socket:0666", | 
					
						
							|  |  |  | 			expectPath:     "./foo.socket:0666", | 
					
						
							|  |  |  | 			expectFileMode: "--w-------", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "./foo.socket|0220", | 
					
						
							|  |  |  | 			expectPath:     "./foo.socket", | 
					
						
							|  |  |  | 			expectFileMode: "--w--w----", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "/var/run/foo|222", | 
					
						
							|  |  |  | 			expectPath:     "/var/run/foo", | 
					
						
							|  |  |  | 			expectFileMode: "--w--w--w-", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "./foo.socket|0660", | 
					
						
							|  |  |  | 			expectPath:     "./foo.socket", | 
					
						
							|  |  |  | 			expectFileMode: "-rw-rw----", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "./foo.socket|0666", | 
					
						
							|  |  |  | 			expectPath:     "./foo.socket", | 
					
						
							|  |  |  | 			expectFileMode: "-rw-rw-rw-", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          "/var/run/foo|666", | 
					
						
							|  |  |  | 			expectPath:     "/var/run/foo", | 
					
						
							|  |  |  | 			expectFileMode: "-rw-rw-rw-", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			input:          `c:\absolute\path.socket|220`, | 
					
						
							|  |  |  | 			expectPath:     `c:\absolute\path.socket`, | 
					
						
							|  |  |  | 			expectFileMode: "--w--w----", | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// symbolic permission representation is not supported for now | 
					
						
							|  |  |  | 			input:     "./foo.socket|u=rw,g=rw,o=rw", | 
					
						
							|  |  |  | 			expectErr: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// octal (base-8) permission representation has to be between | 
					
						
							|  |  |  | 			// `0` for no read, no write, no exec (`---`) and | 
					
						
							|  |  |  | 			// `7` for read (4), write (2), exec (1) (`rwx` => `4+2+1 = 7`) | 
					
						
							|  |  |  | 			input:     "./foo.socket|888", | 
					
						
							|  |  |  | 			expectErr: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// too many colons in address | 
					
						
							|  |  |  | 			input:     "./foo.socket|123456|0660", | 
					
						
							|  |  |  | 			expectErr: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			// owner is missing write perms | 
					
						
							|  |  |  | 			input:     "./foo.socket|0522", | 
					
						
							|  |  |  | 			expectErr: true, | 
					
						
							|  |  |  | 		}, | 
					
						
							|  |  |  | 	} { | 
					
						
							| 
									
										
										
										
											2023-08-06 02:09:16 +02:00
										 |  |  | 		actualPath, actualFileMode, err := internal.SplitUnixSocketPermissionsBits(tc.input) | 
					
						
							| 
									
										
										
										
											2023-06-23 22:49:41 +02:00
										 |  |  | 		if tc.expectErr && err == nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected error but got: %v", i, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if !tc.expectErr && err != nil { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected no error but got: %v", i, err) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		if actualPath != tc.expectPath { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected path '%s' but got '%s'", i, tc.expectPath, actualPath) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		// fileMode.Perm().String() parses 0 to "----------" | 
					
						
							|  |  |  | 		if !tc.expectErr && actualFileMode.Perm().String() != tc.expectFileMode { | 
					
						
							|  |  |  | 			t.Errorf("Test %d: Expected perms '%s' but got '%s'", i, tc.expectFileMode, actualFileMode.Perm().String()) | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |