|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright 2025 gRPC authors. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package xdsresource |
| 18 | + |
| 19 | +import ( |
| 20 | + "testing" |
| 21 | + |
| 22 | + v3corepb "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| 23 | + "github.com/google/go-cmp/cmp" |
| 24 | + "google.golang.org/grpc/internal/testutils" |
| 25 | +) |
| 26 | + |
| 27 | +const proxyAddressTypeURL = "type.googleapis.com/envoy.config.core.v3.Address" |
| 28 | + |
| 29 | +func (s) TestProxyAddressConverterSuccess(t *testing.T) { |
| 30 | + converter := metadataConverterForType(proxyAddressTypeURL) |
| 31 | + if converter == nil { |
| 32 | + t.Fatalf("Converter for %q not found in registry", proxyAddressTypeURL) |
| 33 | + } |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + addr *v3corepb.Address |
| 37 | + want ProxyAddressMetadataValue |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "valid IPv4 address and port", |
| 41 | + addr: &v3corepb.Address{ |
| 42 | + Address: &v3corepb.Address_SocketAddress{ |
| 43 | + SocketAddress: &v3corepb.SocketAddress{ |
| 44 | + Address: "192.168.1.1", |
| 45 | + PortSpecifier: &v3corepb.SocketAddress_PortValue{ |
| 46 | + PortValue: 8080, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + want: ProxyAddressMetadataValue{ |
| 52 | + Address: "192.168.1.1:8080", |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "valid full IPv6 address and port", |
| 57 | + addr: &v3corepb.Address{ |
| 58 | + Address: &v3corepb.Address_SocketAddress{ |
| 59 | + SocketAddress: &v3corepb.SocketAddress{ |
| 60 | + Address: "2001:0db8:85a3:0000:0000:8a2e:0370:7334", |
| 61 | + PortSpecifier: &v3corepb.SocketAddress_PortValue{ |
| 62 | + PortValue: 9090, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + want: ProxyAddressMetadataValue{ |
| 68 | + Address: "[2001:0db8:85a3:0000:0000:8a2e:0370:7334]:9090", |
| 69 | + }, |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "valid shortened IPv6 address", |
| 73 | + addr: &v3corepb.Address{ |
| 74 | + Address: &v3corepb.Address_SocketAddress{ |
| 75 | + SocketAddress: &v3corepb.SocketAddress{ |
| 76 | + Address: "2001:db8::1", |
| 77 | + PortSpecifier: &v3corepb.SocketAddress_PortValue{ |
| 78 | + PortValue: 9090, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + want: ProxyAddressMetadataValue{ |
| 84 | + Address: "[2001:db8::1]:9090", |
| 85 | + }, |
| 86 | + }, |
| 87 | + { |
| 88 | + name: "valid link-local IPv6 address", |
| 89 | + addr: &v3corepb.Address{ |
| 90 | + Address: &v3corepb.Address_SocketAddress{ |
| 91 | + SocketAddress: &v3corepb.SocketAddress{ |
| 92 | + Address: "fe80::1ff:fe23:4567:890a", |
| 93 | + PortSpecifier: &v3corepb.SocketAddress_PortValue{ |
| 94 | + PortValue: 8888, |
| 95 | + }, |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + want: ProxyAddressMetadataValue{ |
| 100 | + Address: "[fe80::1ff:fe23:4567:890a]:8888", |
| 101 | + }, |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "valid IPv4-mapped IPv6 address", |
| 105 | + addr: &v3corepb.Address{ |
| 106 | + Address: &v3corepb.Address_SocketAddress{ |
| 107 | + SocketAddress: &v3corepb.SocketAddress{ |
| 108 | + Address: "::ffff:192.0.2.128", |
| 109 | + PortSpecifier: &v3corepb.SocketAddress_PortValue{ |
| 110 | + PortValue: 1234, |
| 111 | + }, |
| 112 | + }, |
| 113 | + }, |
| 114 | + }, |
| 115 | + want: ProxyAddressMetadataValue{ |
| 116 | + Address: "[::ffff:192.0.2.128]:1234", |
| 117 | + }, |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + for _, tt := range tests { |
| 122 | + t.Run(tt.name, func(t *testing.T) { |
| 123 | + anyProto := testutils.MarshalAny(t, tt.addr) |
| 124 | + got, err := converter.convert(anyProto) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("convert() failed with error: %v", err) |
| 127 | + } |
| 128 | + if diff := cmp.Diff(tt.want, got, cmp.AllowUnexported(ProxyAddressMetadataValue{})); diff != "" { |
| 129 | + t.Errorf("convert() returned unexpected value (-want +got):\n%s", diff) |
| 130 | + } |
| 131 | + }) |
| 132 | + } |
| 133 | +} |
| 134 | + |
| 135 | +func (s) TestProxyAddressConverterFailure(t *testing.T) { |
| 136 | + converter := metadataConverterForType(proxyAddressTypeURL) |
| 137 | + if converter == nil { |
| 138 | + t.Fatalf("Converter for %q not found in registry", proxyAddressTypeURL) |
| 139 | + } |
| 140 | + tests := []struct { |
| 141 | + name string |
| 142 | + addr *v3corepb.Address |
| 143 | + wantErr string |
| 144 | + }{ |
| 145 | + { |
| 146 | + name: "invalid address", |
| 147 | + addr: &v3corepb.Address{ |
| 148 | + Address: &v3corepb.Address_SocketAddress{ |
| 149 | + SocketAddress: &v3corepb.SocketAddress{ |
| 150 | + Address: "invalid-ip", |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + wantErr: "address field is not a valid IPv4 or IPv6 address: \"invalid-ip\"", |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "missing socket_address", |
| 158 | + addr: &v3corepb.Address{ |
| 159 | + // No SocketAddress field set. |
| 160 | + }, |
| 161 | + wantErr: "no socket_address field in metadata", |
| 162 | + }, |
| 163 | + { |
| 164 | + name: "address is not a socket address", |
| 165 | + addr: &v3corepb.Address{ |
| 166 | + Address: &v3corepb.Address_EnvoyInternalAddress{ |
| 167 | + EnvoyInternalAddress: &v3corepb.EnvoyInternalAddress{ |
| 168 | + AddressNameSpecifier: &v3corepb.EnvoyInternalAddress_ServerListenerName{ |
| 169 | + ServerListenerName: "some-internal-listener", |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + wantErr: "no socket_address field in metadata", |
| 175 | + }, |
| 176 | + { |
| 177 | + name: "port value not set", |
| 178 | + addr: &v3corepb.Address{ |
| 179 | + Address: &v3corepb.Address_SocketAddress{ |
| 180 | + SocketAddress: &v3corepb.SocketAddress{ |
| 181 | + Address: "127.0.0.1", |
| 182 | + PortSpecifier: nil, |
| 183 | + }, |
| 184 | + }, |
| 185 | + }, |
| 186 | + wantErr: "port value not set in socket_address", |
| 187 | + }, |
| 188 | + } |
| 189 | + |
| 190 | + for _, tt := range tests { |
| 191 | + t.Run(tt.name, func(t *testing.T) { |
| 192 | + anyProto := testutils.MarshalAny(t, tt.addr) |
| 193 | + _, err := converter.convert(anyProto) |
| 194 | + if err == nil || err.Error() != tt.wantErr { |
| 195 | + t.Errorf("convert() got error = %v, wantErr = %q", err, tt.wantErr) |
| 196 | + } |
| 197 | + }) |
| 198 | + } |
| 199 | +} |
0 commit comments