|
| 1 | +package context |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "net/netip" |
| 6 | + "os" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/free5gc/openapi/models" |
| 10 | + "github.com/free5gc/udr/pkg/factory" |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | +) |
| 13 | + |
| 14 | +func createConfigFile(t *testing.T, postContent []byte) *os.File { |
| 15 | + content := []byte(`info: |
| 16 | + version: "1.1.0" |
| 17 | + description: UDR initial local configuration |
| 18 | +
|
| 19 | +logger: |
| 20 | + enable: true |
| 21 | + level: debug |
| 22 | +
|
| 23 | +configuration: |
| 24 | + nrfUri: http://[2001:db8::1:0:0:10]:8000 |
| 25 | + dbConnectorType: mongodb |
| 26 | + mongodb: |
| 27 | + name: free5gc |
| 28 | + url: mongodb://localhost:27017`) |
| 29 | + |
| 30 | + configFile, err := os.CreateTemp("", "") |
| 31 | + if err != nil { |
| 32 | + t.Errorf("can't create temp file: %+v", err) |
| 33 | + } |
| 34 | + |
| 35 | + if _, err := configFile.Write(content); err != nil { |
| 36 | + t.Errorf("can't write content of temp file: %+v", err) |
| 37 | + } |
| 38 | + if _, err := configFile.Write(postContent); err != nil { |
| 39 | + t.Errorf("can't write content of temp file: %+v", err) |
| 40 | + } |
| 41 | + if err := configFile.Close(); err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + return configFile |
| 45 | +} |
| 46 | + |
| 47 | +func TestInitUdrContextWithConfigIPv6(t *testing.T) { |
| 48 | + postContent := []byte(` |
| 49 | + sbi: |
| 50 | + scheme: http |
| 51 | + registerIP: "2001:db8::1:0:0:4" |
| 52 | + bindingIP: "2001:db8::1:0:0:4" |
| 53 | + port: 8313`) |
| 54 | + |
| 55 | + configFile := createConfigFile(t, postContent) |
| 56 | + |
| 57 | + // Test the initialization with the config file |
| 58 | + cfg, err := factory.ReadConfig(configFile.Name()) |
| 59 | + if err != nil { |
| 60 | + t.Errorf("invalid read config: %+v %+v", err, cfg) |
| 61 | + return |
| 62 | + } |
| 63 | + factory.UdrConfig = cfg |
| 64 | + |
| 65 | + Init() |
| 66 | + |
| 67 | + assert.Equal(t, udrContext.SBIPort, 8313) |
| 68 | + assert.Equal(t, udrContext.RegisterIP.String(), "2001:db8::1:0:0:4") |
| 69 | + assert.Equal(t, udrContext.BindingIP.String(), "2001:db8::1:0:0:4") |
| 70 | + assert.Equal(t, udrContext.UriScheme, models.UriScheme("http")) |
| 71 | + |
| 72 | + // Close the config file |
| 73 | + t.Cleanup(func() { |
| 74 | + if err := os.RemoveAll(configFile.Name()); err != nil { |
| 75 | + t.Fatal(err) |
| 76 | + } |
| 77 | + }) |
| 78 | +} |
| 79 | + |
| 80 | +func TestInitUdrContextWithConfigIPv4(t *testing.T) { |
| 81 | + postContent := []byte(` |
| 82 | + sbi: |
| 83 | + scheme: http |
| 84 | + registerIP: "127.0.0.4" |
| 85 | + bindingIP: "127.0.0.4" |
| 86 | + port: 8131`) |
| 87 | + |
| 88 | + configFile := createConfigFile(t, postContent) |
| 89 | + |
| 90 | + // Test the initialization with the config file |
| 91 | + cfg, err := factory.ReadConfig(configFile.Name()) |
| 92 | + if err != nil { |
| 93 | + t.Errorf("invalid read config: %+v %+v", err, cfg) |
| 94 | + } |
| 95 | + factory.UdrConfig = cfg |
| 96 | + |
| 97 | + Init() |
| 98 | + |
| 99 | + assert.Equal(t, udrContext.SBIPort, 8131) |
| 100 | + assert.Equal(t, udrContext.RegisterIP.String(), "127.0.0.4") |
| 101 | + assert.Equal(t, udrContext.BindingIP.String(), "127.0.0.4") |
| 102 | + assert.Equal(t, udrContext.UriScheme, models.UriScheme("http")) |
| 103 | + |
| 104 | + // Close the config file |
| 105 | + t.Cleanup(func() { |
| 106 | + if err := os.RemoveAll(configFile.Name()); err != nil { |
| 107 | + t.Fatal(err) |
| 108 | + } |
| 109 | + }) |
| 110 | +} |
| 111 | + |
| 112 | +func TestInitUdrContextWithConfigDeprecated(t *testing.T) { |
| 113 | + postContent := []byte(` |
| 114 | + sbi: |
| 115 | + scheme: http |
| 116 | + registerIPv4: "127.0.0.30" |
| 117 | + bindingIPv4: "127.0.0.30" |
| 118 | + port: 8003`) |
| 119 | + |
| 120 | + configFile := createConfigFile(t, postContent) |
| 121 | + |
| 122 | + // Test the initialization with the config file |
| 123 | + cfg, err := factory.ReadConfig(configFile.Name()) |
| 124 | + if err != nil { |
| 125 | + t.Errorf("invalid read config: %+v %+v", err, cfg) |
| 126 | + } |
| 127 | + factory.UdrConfig = cfg |
| 128 | + |
| 129 | + Init() |
| 130 | + |
| 131 | + assert.Equal(t, udrContext.SBIPort, 8003) |
| 132 | + assert.Equal(t, udrContext.RegisterIP.String(), "127.0.0.30") |
| 133 | + assert.Equal(t, udrContext.BindingIP.String(), "127.0.0.30") |
| 134 | + assert.Equal(t, udrContext.UriScheme, models.UriScheme("http")) |
| 135 | + |
| 136 | + // Close the config file |
| 137 | + t.Cleanup(func() { |
| 138 | + if err := os.RemoveAll(configFile.Name()); err != nil { |
| 139 | + t.Fatal(err) |
| 140 | + } |
| 141 | + }) |
| 142 | +} |
| 143 | + |
| 144 | +func TestInitUdrContextWithConfigEmptySBI(t *testing.T) { |
| 145 | + postContent := []byte(``) |
| 146 | + |
| 147 | + configFile := createConfigFile(t, postContent) |
| 148 | + |
| 149 | + // Test the initialization with the config file fails |
| 150 | + _, err := factory.ReadConfig(configFile.Name()) |
| 151 | + assert.Equal(t, err, errors.New("Config validate Error")) |
| 152 | + |
| 153 | + // Close the config file |
| 154 | + t.Cleanup(func() { |
| 155 | + if err := os.RemoveAll(configFile.Name()); err != nil { |
| 156 | + t.Fatal(err) |
| 157 | + } |
| 158 | + }) |
| 159 | +} |
| 160 | + |
| 161 | +func TestInitUdrContextWithConfigMissingRegisterIP(t *testing.T) { |
| 162 | + postContent := []byte(` |
| 163 | + sbi: |
| 164 | + bindingIP: "2001:db8::1:0:0:130"`) |
| 165 | + |
| 166 | + configFile := createConfigFile(t, postContent) |
| 167 | + |
| 168 | + // Test the initialization with the config file |
| 169 | + cfg, err := factory.ReadConfig(configFile.Name()) |
| 170 | + if err != nil { |
| 171 | + t.Errorf("invalid read config: %+v %+v", err, cfg) |
| 172 | + } |
| 173 | + factory.UdrConfig = cfg |
| 174 | + |
| 175 | + Init() |
| 176 | + |
| 177 | + assert.Equal(t, udrContext.SBIPort, 8000) |
| 178 | + assert.Equal(t, udrContext.BindingIP.String(), "2001:db8::1:0:0:130") |
| 179 | + assert.Equal(t, udrContext.RegisterIP.String(), "2001:db8::1:0:0:130") |
| 180 | + assert.Equal(t, udrContext.UriScheme, models.UriScheme("https")) |
| 181 | + |
| 182 | + // Close the config file |
| 183 | + t.Cleanup(func() { |
| 184 | + if err := os.RemoveAll(configFile.Name()); err != nil { |
| 185 | + t.Fatal(err) |
| 186 | + } |
| 187 | + }) |
| 188 | +} |
| 189 | + |
| 190 | +func TestInitUdrContextWithConfigMissingBindingIP(t *testing.T) { |
| 191 | + postContent := []byte(` |
| 192 | + sbi: |
| 193 | + registerIP: "2001:db8::1:0:0:131"`) |
| 194 | + |
| 195 | + configFile := createConfigFile(t, postContent) |
| 196 | + |
| 197 | + // Test the initialization with the config file |
| 198 | + cfg, err := factory.ReadConfig(configFile.Name()) |
| 199 | + if err != nil { |
| 200 | + t.Errorf("invalid read config: %+v %+v", err, cfg) |
| 201 | + } |
| 202 | + factory.UdrConfig = cfg |
| 203 | + |
| 204 | + Init() |
| 205 | + |
| 206 | + assert.Equal(t, udrContext.SBIPort, 8000) |
| 207 | + assert.Equal(t, udrContext.BindingIP.String(), "2001:db8::1:0:0:131") |
| 208 | + assert.Equal(t, udrContext.RegisterIP.String(), "2001:db8::1:0:0:131") |
| 209 | + assert.Equal(t, udrContext.UriScheme, models.UriScheme("https")) |
| 210 | + |
| 211 | + // Close the config file |
| 212 | + t.Cleanup(func() { |
| 213 | + if err := os.RemoveAll(configFile.Name()); err != nil { |
| 214 | + t.Fatal(err) |
| 215 | + } |
| 216 | + }) |
| 217 | +} |
| 218 | + |
| 219 | +func TestInitUdrContextWithConfigIPv6FromEnv(t *testing.T) { |
| 220 | + postContent := []byte(` |
| 221 | + sbi: |
| 222 | + scheme: http |
| 223 | + registerIP: "MY_REGISTER_IP" |
| 224 | + bindingIP: "MY_BINDING_IP" |
| 225 | + port: 8313`) |
| 226 | + |
| 227 | + configFile := createConfigFile(t, postContent) |
| 228 | + |
| 229 | + if err := os.Setenv("MY_REGISTER_IP", "2001:db8::1:0:0:130"); err != nil { |
| 230 | + t.Errorf("Can't set MY_BINDING_IP variable environnement: %+v", err) |
| 231 | + } |
| 232 | + if err := os.Setenv("MY_BINDING_IP", "2001:db8::1:0:0:130"); err != nil { |
| 233 | + t.Errorf("Can't set MY_BINDING_IP variable environnement: %+v", err) |
| 234 | + } |
| 235 | + |
| 236 | + // Test the initialization with the config file |
| 237 | + cfg, err := factory.ReadConfig(configFile.Name()) |
| 238 | + if err != nil { |
| 239 | + t.Errorf("invalid read config: %+v %+v", err, cfg) |
| 240 | + } |
| 241 | + factory.UdrConfig = cfg |
| 242 | + |
| 243 | + Init() |
| 244 | + |
| 245 | + assert.Equal(t, udrContext.SBIPort, 8313) |
| 246 | + assert.Equal(t, udrContext.RegisterIP.String(), "2001:db8::1:0:0:130") |
| 247 | + assert.Equal(t, udrContext.BindingIP.String(), "2001:db8::1:0:0:130") |
| 248 | + assert.Equal(t, udrContext.UriScheme, models.UriScheme("http")) |
| 249 | + |
| 250 | + // Close the config file |
| 251 | + t.Cleanup(func() { |
| 252 | + if err := os.RemoveAll(configFile.Name()); err != nil { |
| 253 | + t.Fatal(err) |
| 254 | + } |
| 255 | + }) |
| 256 | +} |
| 257 | + |
| 258 | +func TestResolveIPLocalhost(t *testing.T) { |
| 259 | + expectedAddr, err := netip.ParseAddr("::1") |
| 260 | + if err != nil { |
| 261 | + t.Errorf("invalid expected IP: %+v", expectedAddr) |
| 262 | + } |
| 263 | + |
| 264 | + addr := resolveIP("localhost") |
| 265 | + if addr != expectedAddr { |
| 266 | + t.Errorf("invalid IP: %+v", addr) |
| 267 | + } |
| 268 | + assert.Equal(t, addr, expectedAddr) |
| 269 | +} |
| 270 | + |
| 271 | +func TestResolveIPv4(t *testing.T) { |
| 272 | + expectedAddr, err := netip.ParseAddr("127.0.0.1") |
| 273 | + if err != nil { |
| 274 | + t.Errorf("invalid expected IP: %+v", expectedAddr) |
| 275 | + } |
| 276 | + |
| 277 | + addr := resolveIP("127.0.0.1") |
| 278 | + if addr != expectedAddr { |
| 279 | + t.Errorf("invalid IP: %+v", addr) |
| 280 | + } |
| 281 | +} |
| 282 | + |
| 283 | +func TestResolveIPv6(t *testing.T) { |
| 284 | + expectedAddr, err := netip.ParseAddr("2001:db8::1:0:0:1") |
| 285 | + if err != nil { |
| 286 | + t.Errorf("invalid expected IP: %+v", expectedAddr) |
| 287 | + } |
| 288 | + |
| 289 | + addr := resolveIP("2001:db8::1:0:0:1") |
| 290 | + if addr != expectedAddr { |
| 291 | + t.Errorf("invalid IP: %+v", addr) |
| 292 | + } |
| 293 | +} |
0 commit comments