|
| 1 | +package configuration |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/haproxytech/client-native/v6/config-parser/params" |
| 7 | + "github.com/haproxytech/client-native/v6/config-parser/types" |
| 8 | + "github.com/haproxytech/client-native/v6/models" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestParseDgramBindIPv6(t *testing.T) { |
| 14 | + tests := []struct { |
| 15 | + name string |
| 16 | + path string |
| 17 | + params []params.DgramBindOption |
| 18 | + expectedAddress string |
| 19 | + expectedPort *int64 |
| 20 | + expectedName string |
| 21 | + }{ |
| 22 | + { |
| 23 | + name: "bracketed IPv6 with port", |
| 24 | + path: "[fd66:c3ec:c7fc::7c]:123", |
| 25 | + params: []params.DgramBindOption{¶ms.BindOptionValue{Name: "name", Value: "ntp6"}}, |
| 26 | + expectedAddress: "fd66:c3ec:c7fc::7c", |
| 27 | + expectedPort: int64P(123), |
| 28 | + expectedName: "ntp6", |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "bracketed IPv6 with port and no name param", |
| 32 | + path: "[2a04:4b07:21dc:218::1]:53", |
| 33 | + expectedAddress: "2a04:4b07:21dc:218::1", |
| 34 | + expectedPort: int64P(53), |
| 35 | + expectedName: "[2a04:4b07:21dc:218::1]:53", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "double colon with port", |
| 39 | + path: ":::443", |
| 40 | + expectedAddress: "::", |
| 41 | + expectedPort: int64P(443), |
| 42 | + expectedName: ":::443", |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "IPv4 with port", |
| 46 | + path: "10.0.0.1:8080", |
| 47 | + params: []params.DgramBindOption{¶ms.BindOptionValue{Name: "name", Value: "test"}}, |
| 48 | + expectedAddress: "10.0.0.1", |
| 49 | + expectedPort: int64P(8080), |
| 50 | + expectedName: "test", |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "wildcard with port", |
| 54 | + path: ":443", |
| 55 | + expectedAddress: "", |
| 56 | + expectedPort: int64P(443), |
| 57 | + expectedName: ":443", |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "unix socket path", |
| 61 | + path: "/var/run/haproxy.sock", |
| 62 | + expectedAddress: "/var/run/haproxy.sock", |
| 63 | + expectedPort: nil, |
| 64 | + expectedName: "/var/run/haproxy.sock", |
| 65 | + }, |
| 66 | + } |
| 67 | + |
| 68 | + for _, tc := range tests { |
| 69 | + t.Run(tc.name, func(t *testing.T) { |
| 70 | + ondisk := types.DgramBind{ |
| 71 | + Path: tc.path, |
| 72 | + Params: tc.params, |
| 73 | + } |
| 74 | + result := ParseDgramBind(ondisk) |
| 75 | + require.NotNil(t, result, "ParseDgramBind returned nil for path %q", tc.path) |
| 76 | + require.Equal(t, tc.expectedAddress, result.Address, "address mismatch") |
| 77 | + if tc.expectedPort != nil { |
| 78 | + require.NotNil(t, result.Port, "expected port to be set") |
| 79 | + require.Equal(t, *tc.expectedPort, *result.Port, "port mismatch") |
| 80 | + } else { |
| 81 | + require.Nil(t, result.Port, "expected port to be nil") |
| 82 | + } |
| 83 | + require.Equal(t, tc.expectedName, result.Name, "name mismatch") |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func TestSerializeDgramBindIPv6(t *testing.T) { |
| 89 | + tests := []struct { |
| 90 | + name string |
| 91 | + dgramBind models.DgramBind |
| 92 | + expectedPath string |
| 93 | + }{ |
| 94 | + { |
| 95 | + name: "IPv6 address with port", |
| 96 | + dgramBind: models.DgramBind{ |
| 97 | + Address: "fd66:c3ec:c7fc::7c", |
| 98 | + Port: int64P(123), |
| 99 | + Name: "ntp6", |
| 100 | + }, |
| 101 | + expectedPath: "[fd66:c3ec:c7fc::7c]:123", |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "IPv6 double colon with port", |
| 105 | + dgramBind: models.DgramBind{ |
| 106 | + Address: "::", |
| 107 | + Port: int64P(443), |
| 108 | + Name: "test", |
| 109 | + }, |
| 110 | + expectedPath: "[::]:443", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "IPv4 address with port", |
| 114 | + dgramBind: models.DgramBind{ |
| 115 | + Address: "10.0.0.1", |
| 116 | + Port: int64P(8080), |
| 117 | + Name: "test", |
| 118 | + }, |
| 119 | + expectedPath: "10.0.0.1:8080", |
| 120 | + }, |
| 121 | + { |
| 122 | + name: "address only, no port", |
| 123 | + dgramBind: models.DgramBind{ |
| 124 | + Address: "/var/run/haproxy.sock", |
| 125 | + Name: "test", |
| 126 | + }, |
| 127 | + expectedPath: "/var/run/haproxy.sock", |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + for _, tc := range tests { |
| 132 | + t.Run(tc.name, func(t *testing.T) { |
| 133 | + result := SerializeDgramBind(tc.dgramBind) |
| 134 | + require.Equal(t, tc.expectedPath, result.Path, "serialized path mismatch") |
| 135 | + }) |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +func TestParseDgramBindRoundTrip(t *testing.T) { |
| 140 | + tests := []struct { |
| 141 | + name string |
| 142 | + path string |
| 143 | + }{ |
| 144 | + {"IPv6 bracketed", "[fd66:c3ec:c7fc::7c]:123"}, |
| 145 | + {"IPv6 full", "[2a04:4b07:21dc:218::1]:53"}, |
| 146 | + {"IPv6 double colon", "[::]:443"}, |
| 147 | + {"IPv4", "10.0.0.1:8080"}, |
| 148 | + {"wildcard", ":443"}, |
| 149 | + } |
| 150 | + |
| 151 | + for _, tc := range tests { |
| 152 | + t.Run(tc.name, func(t *testing.T) { |
| 153 | + ondisk := types.DgramBind{ |
| 154 | + Path: tc.path, |
| 155 | + Params: []params.DgramBindOption{¶ms.BindOptionValue{Name: "name", Value: "roundtrip"}}, |
| 156 | + } |
| 157 | + parsed := ParseDgramBind(ondisk) |
| 158 | + require.NotNil(t, parsed, "ParseDgramBind returned nil for path %q", tc.path) |
| 159 | + |
| 160 | + serialized := SerializeDgramBind(*parsed) |
| 161 | + require.Equal(t, tc.path, serialized.Path, "round-trip path mismatch") |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +func int64P(v int64) *int64 { |
| 167 | + return &v |
| 168 | +} |
0 commit comments