From 36c8da3d73761dd8d2975198fcf5460e82987934 Mon Sep 17 00:00:00 2001 From: Sylvia Crowe Date: Tue, 9 Jan 2024 01:06:16 -0800 Subject: [PATCH] add tests for reloading ssh config files This adds 2 tests. The first checks that ssh config data is cached and does not update if the file changes afterward. The second checks that the `ReloadConfigs()` function will discard the cached data and load the current config file contents. --- config_test.go | 66 +++++++++++++++++++++++++++++++++++++++++++++++ testdata/modified | 4 +++ 2 files changed, 70 insertions(+) create mode 100644 testdata/modified diff --git a/config_test.go b/config_test.go index 11b203d..b296ee3 100644 --- a/config_test.go +++ b/config_test.go @@ -259,6 +259,72 @@ func TestGetEqsign(t *testing.T) { } } +var modified1 = []byte(` +Host wap + User modified1 + KexAlgorithms diffie-hellman-group1-sha1 +`) + +var modified2 = []byte(` +Host wap + User modified2 + KexAlgorithms diffie-hellman-group1-sha1 +`) + +func TestCachedConfig(t *testing.T) { + us := &UserSettings{ + userConfigFinder: testConfigFinder("testdata/modified"), + } + + err1 := os.WriteFile("testdata/modified", modified1, 0644) + if err1 != nil { + t.Errorf("error writing to file: %v", err1) + } + + val1 := us.Get("wap", "User") + if val1 != "modified1" { + t.Errorf("expected to find User modified1, got %q", val1) + } + + err2 := os.WriteFile("testdata/modified", modified2, 0644) + if err1 != nil { + t.Errorf("error writing to file: %v", err2) + } + + val2 := us.Get("wap", "User") + if val2 != "modified1" { + t.Errorf("expected to find User modified1, got %q", val2) + } +} + +func TestReloadConfigs(t *testing.T) { + us := &UserSettings{ + userConfigFinder: testConfigFinder("testdata/modified"), + } + + err1 := os.WriteFile("testdata/modified", modified1, 0644) + if err1 != nil { + t.Errorf("error writing to file: %v", err1) + } + + val1 := us.Get("wap", "User") + if val1 != "modified1" { + t.Errorf("expected to find User modified1, got %q", val1) + } + + err2 := os.WriteFile("testdata/modified", modified2, 0644) + if err1 != nil { + t.Errorf("error writing to file: %v", err2) + } + + us.ReloadConfigs() + + val2 := us.Get("wap", "User") + if val2 != "modified2" { + t.Errorf("expected to find User modified2, got %q", val2) + } +} + var includeFile = []byte(` # This host should not exist, so we can use it for test purposes / it won't # interfere with any other configurations. diff --git a/testdata/modified b/testdata/modified new file mode 100644 index 0000000..1158323 --- /dev/null +++ b/testdata/modified @@ -0,0 +1,4 @@ + +Host wap + User modified2 + KexAlgorithms diffie-hellman-group1-sha1