|
| 1 | +// Copyright 2024 Jigsaw Operations LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package main |
| 16 | + |
| 17 | +import ( |
| 18 | + "os" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/stretchr/testify/require" |
| 22 | +) |
| 23 | + |
| 24 | +func TestValidateConfigFails(t *testing.T) { |
| 25 | + tests := []struct { |
| 26 | + name string |
| 27 | + cfg *Config |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "WithUnknownListenerType", |
| 31 | + cfg: &Config{ |
| 32 | + Services: []ServiceConfig{ |
| 33 | + ServiceConfig{ |
| 34 | + Listeners: []ListenerConfig{ |
| 35 | + ListenerConfig{Type: "foo", Address: "[::]:9000"}, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "WithInvalidListenerAddress", |
| 43 | + cfg: &Config{ |
| 44 | + Services: []ServiceConfig{ |
| 45 | + ServiceConfig{ |
| 46 | + Listeners: []ListenerConfig{ |
| 47 | + ListenerConfig{Type: listenerTypeTCP, Address: "tcp/[::]:9000"}, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + }, |
| 53 | + { |
| 54 | + name: "WithHostnameAddress", |
| 55 | + cfg: &Config{ |
| 56 | + Services: []ServiceConfig{ |
| 57 | + ServiceConfig{ |
| 58 | + Listeners: []ListenerConfig{ |
| 59 | + ListenerConfig{Type: listenerTypeTCP, Address: "example.com:9000"}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "WithDuplicateListeners", |
| 67 | + cfg: &Config{ |
| 68 | + Services: []ServiceConfig{ |
| 69 | + ServiceConfig{ |
| 70 | + Listeners: []ListenerConfig{ |
| 71 | + ListenerConfig{Type: listenerTypeTCP, Address: "[::]:9000"}, |
| 72 | + }, |
| 73 | + }, |
| 74 | + ServiceConfig{ |
| 75 | + Listeners: []ListenerConfig{ |
| 76 | + ListenerConfig{Type: listenerTypeTCP, Address: "[::]:9000"}, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + } |
| 83 | + |
| 84 | + for _, tc := range tests { |
| 85 | + t.Run(tc.name, func(t *testing.T) { |
| 86 | + err := tc.cfg.Validate() |
| 87 | + require.Error(t, err) |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestReadConfig(t *testing.T) { |
| 93 | + config, err := readConfigFile("./config_example.yml") |
| 94 | + |
| 95 | + require.NoError(t, err) |
| 96 | + expected := Config{ |
| 97 | + Services: []ServiceConfig{ |
| 98 | + ServiceConfig{ |
| 99 | + Listeners: []ListenerConfig{ |
| 100 | + ListenerConfig{Type: listenerTypeTCP, Address: "[::]:9000"}, |
| 101 | + ListenerConfig{Type: listenerTypeUDP, Address: "[::]:9000"}, |
| 102 | + }, |
| 103 | + Keys: []KeyConfig{ |
| 104 | + KeyConfig{"user-0", "chacha20-ietf-poly1305", "Secret0"}, |
| 105 | + KeyConfig{"user-1", "chacha20-ietf-poly1305", "Secret1"}, |
| 106 | + }, |
| 107 | + }, |
| 108 | + ServiceConfig{ |
| 109 | + Listeners: []ListenerConfig{ |
| 110 | + ListenerConfig{Type: listenerTypeTCP, Address: "[::]:9001"}, |
| 111 | + ListenerConfig{Type: listenerTypeUDP, Address: "[::]:9001"}, |
| 112 | + }, |
| 113 | + Keys: []KeyConfig{ |
| 114 | + KeyConfig{"user-2", "chacha20-ietf-poly1305", "Secret2"}, |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + } |
| 119 | + require.Equal(t, expected, *config) |
| 120 | +} |
| 121 | + |
| 122 | +func TestReadConfigParsesDeprecatedFormat(t *testing.T) { |
| 123 | + config, err := readConfigFile("./config_example.deprecated.yml") |
| 124 | + |
| 125 | + require.NoError(t, err) |
| 126 | + expected := Config{ |
| 127 | + Keys: []LegacyKeyServiceConfig{ |
| 128 | + LegacyKeyServiceConfig{ |
| 129 | + KeyConfig: KeyConfig{ID: "user-0", Cipher: "chacha20-ietf-poly1305", Secret: "Secret0"}, |
| 130 | + Port: 9000, |
| 131 | + }, |
| 132 | + LegacyKeyServiceConfig{ |
| 133 | + KeyConfig: KeyConfig{ID: "user-1", Cipher: "chacha20-ietf-poly1305", Secret: "Secret1"}, |
| 134 | + Port: 9000, |
| 135 | + }, |
| 136 | + LegacyKeyServiceConfig{ |
| 137 | + KeyConfig: KeyConfig{ID: "user-2", Cipher: "chacha20-ietf-poly1305", Secret: "Secret2"}, |
| 138 | + Port: 9001, |
| 139 | + }, |
| 140 | + }, |
| 141 | + } |
| 142 | + require.Equal(t, expected, *config) |
| 143 | +} |
| 144 | + |
| 145 | +func TestReadConfigFromEmptyFile(t *testing.T) { |
| 146 | + file, _ := os.CreateTemp("", "empty.yaml") |
| 147 | + |
| 148 | + config, err := readConfigFile(file.Name()) |
| 149 | + |
| 150 | + require.NoError(t, err) |
| 151 | + require.ElementsMatch(t, Config{}, config) |
| 152 | +} |
| 153 | + |
| 154 | +func TestReadConfigFromIncorrectFormatFails(t *testing.T) { |
| 155 | + file, _ := os.CreateTemp("", "empty.yaml") |
| 156 | + file.WriteString("foo") |
| 157 | + |
| 158 | + config, err := readConfigFile(file.Name()) |
| 159 | + |
| 160 | + require.Error(t, err) |
| 161 | + require.ElementsMatch(t, Config{}, config) |
| 162 | +} |
| 163 | + |
| 164 | +func readConfigFile(filename string) (*Config, error) { |
| 165 | + configData, _ := os.ReadFile(filename) |
| 166 | + return readConfig(configData) |
| 167 | +} |
0 commit comments