Skip to content

Commit 3abae0b

Browse files
authored
[client] Set default wg port for new profiles (#4651)
1 parent 8252ff4 commit 3abae0b

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

client/internal/profilemanager/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ func createNewConfig(input ConfigInput) (*Config, error) {
195195
config := &Config{
196196
// defaults to false only for new (post 0.26) configurations
197197
ServerSSHAllowed: util.False(),
198+
WgPort: iface.DefaultWgPort,
198199
}
199200

200201
if _, err := config.apply(input); err != nil {

client/internal/profilemanager/config_test.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import (
55
"errors"
66
"os"
77
"path/filepath"
8+
"runtime"
89
"testing"
910

1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
1213

14+
"github.com/netbirdio/netbird/client/iface"
15+
"github.com/netbirdio/netbird/client/internal/routemanager/dynamic"
1316
"github.com/netbirdio/netbird/util"
1417
)
1518

@@ -141,6 +144,95 @@ func TestHiddenPreSharedKey(t *testing.T) {
141144
}
142145
}
143146

147+
func TestNewProfileDefaults(t *testing.T) {
148+
tempDir := t.TempDir()
149+
configPath := filepath.Join(tempDir, "config.json")
150+
151+
config, err := UpdateOrCreateConfig(ConfigInput{
152+
ConfigPath: configPath,
153+
})
154+
require.NoError(t, err, "should create new config")
155+
156+
assert.Equal(t, DefaultManagementURL, config.ManagementURL.String(), "ManagementURL should have default")
157+
assert.Equal(t, DefaultAdminURL, config.AdminURL.String(), "AdminURL should have default")
158+
assert.NotEmpty(t, config.PrivateKey, "PrivateKey should be generated")
159+
assert.NotEmpty(t, config.SSHKey, "SSHKey should be generated")
160+
assert.Equal(t, iface.WgInterfaceDefault, config.WgIface, "WgIface should have default")
161+
assert.Equal(t, iface.DefaultWgPort, config.WgPort, "WgPort should default to 51820")
162+
assert.Equal(t, uint16(iface.DefaultMTU), config.MTU, "MTU should have default")
163+
assert.Equal(t, dynamic.DefaultInterval, config.DNSRouteInterval, "DNSRouteInterval should have default")
164+
assert.NotNil(t, config.ServerSSHAllowed, "ServerSSHAllowed should be set")
165+
assert.NotNil(t, config.DisableNotifications, "DisableNotifications should be set")
166+
assert.NotEmpty(t, config.IFaceBlackList, "IFaceBlackList should have defaults")
167+
168+
if runtime.GOOS == "windows" || runtime.GOOS == "darwin" {
169+
assert.NotNil(t, config.NetworkMonitor, "NetworkMonitor should be set on Windows/macOS")
170+
assert.True(t, *config.NetworkMonitor, "NetworkMonitor should be enabled by default on Windows/macOS")
171+
}
172+
}
173+
174+
func TestWireguardPortZeroExplicit(t *testing.T) {
175+
tempDir := t.TempDir()
176+
configPath := filepath.Join(tempDir, "config.json")
177+
178+
// Create a new profile with explicit port 0 (random port)
179+
explicitZero := 0
180+
config, err := UpdateOrCreateConfig(ConfigInput{
181+
ConfigPath: configPath,
182+
WireguardPort: &explicitZero,
183+
})
184+
require.NoError(t, err, "should create config with explicit port 0")
185+
186+
assert.Equal(t, 0, config.WgPort, "WgPort should be 0 when explicitly set by user")
187+
188+
// Verify it persists
189+
readConfig, err := GetConfig(configPath)
190+
require.NoError(t, err)
191+
assert.Equal(t, 0, readConfig.WgPort, "WgPort should remain 0 after reading from file")
192+
}
193+
194+
func TestWireguardPortDefaultVsExplicit(t *testing.T) {
195+
tests := []struct {
196+
name string
197+
wireguardPort *int
198+
expectedPort int
199+
description string
200+
}{
201+
{
202+
name: "no port specified uses default",
203+
wireguardPort: nil,
204+
expectedPort: iface.DefaultWgPort,
205+
description: "When user doesn't specify port, default to 51820",
206+
},
207+
{
208+
name: "explicit zero for random port",
209+
wireguardPort: func() *int { v := 0; return &v }(),
210+
expectedPort: 0,
211+
description: "When user explicitly sets 0, use 0 for random port",
212+
},
213+
{
214+
name: "explicit custom port",
215+
wireguardPort: func() *int { v := 52000; return &v }(),
216+
expectedPort: 52000,
217+
description: "When user sets custom port, use that port",
218+
},
219+
}
220+
221+
for _, tt := range tests {
222+
t.Run(tt.name, func(t *testing.T) {
223+
tempDir := t.TempDir()
224+
configPath := filepath.Join(tempDir, "config.json")
225+
226+
config, err := UpdateOrCreateConfig(ConfigInput{
227+
ConfigPath: configPath,
228+
WireguardPort: tt.wireguardPort,
229+
})
230+
require.NoError(t, err, tt.description)
231+
assert.Equal(t, tt.expectedPort, config.WgPort, tt.description)
232+
})
233+
}
234+
}
235+
144236
func TestUpdateOldManagementURL(t *testing.T) {
145237
tests := []struct {
146238
name string

0 commit comments

Comments
 (0)