|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/hmac" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestIsWSLFromVersion(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + content string |
| 18 | + want bool |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "microsoft keyword", |
| 22 | + content: "Linux version 5.15.0-microsoft-standard-WSL2", |
| 23 | + want: true, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "Microsoft capitalised", |
| 27 | + content: "Linux version 5.15.0-Microsoft-standard", |
| 28 | + want: true, |
| 29 | + }, |
| 30 | + { |
| 31 | + name: "wsl keyword", |
| 32 | + content: "Linux version 5.15.0 (wsl@build)", |
| 33 | + want: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "WSL uppercase", |
| 37 | + content: "Linux version 5.15.0 (WSL2)", |
| 38 | + want: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "plain linux", |
| 42 | + content: "Linux version 6.1.0-28-amd64 ([email protected])", |
| 43 | + want: false, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "empty", |
| 47 | + content: "", |
| 48 | + want: false, |
| 49 | + }, |
| 50 | + } |
| 51 | + |
| 52 | + for _, tt := range tests { |
| 53 | + t.Run(tt.name, func(t *testing.T) { |
| 54 | + require.Equal(t, tt.want, isWSLFromVersion(tt.content)) |
| 55 | + }) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func TestIsWSL_UnreadableProcVersion(t *testing.T) { |
| 60 | + // isWSL() returns false when /proc/version cannot be read; we can verify |
| 61 | + // the helper directly covers that path via isWSLFromVersion with empty input. |
| 62 | + require.False(t, isWSLFromVersion("")) |
| 63 | +} |
| 64 | + |
| 65 | +func wslExpectedPassword(t *testing.T, machineID, bootID string) string { |
| 66 | + t.Helper() |
| 67 | + const appKey = "stripe-cli-keyring-v1" |
| 68 | + mac := hmac.New(sha256.New, []byte(appKey)) |
| 69 | + mac.Write([]byte(machineID)) |
| 70 | + mac.Write([]byte(bootID)) |
| 71 | + return hex.EncodeToString(mac.Sum(nil)) |
| 72 | +} |
| 73 | + |
| 74 | +func TestWslFilePasswordFromPaths_BothFiles(t *testing.T) { |
| 75 | + dir := t.TempDir() |
| 76 | + machineIDPath := filepath.Join(dir, "machine-id") |
| 77 | + bootIDPath := filepath.Join(dir, "boot_id") |
| 78 | + |
| 79 | + require.NoError(t, os.WriteFile(machineIDPath, []byte("abc123\n"), 0600)) |
| 80 | + require.NoError(t, os.WriteFile(bootIDPath, []byte("def456\n"), 0600)) |
| 81 | + |
| 82 | + got, err := wslFilePasswordFromPaths(machineIDPath, bootIDPath) |
| 83 | + require.NoError(t, err) |
| 84 | + require.Equal(t, wslExpectedPassword(t, "abc123", "def456"), got) |
| 85 | +} |
| 86 | + |
| 87 | +func TestWslFilePasswordFromPaths_MachineIDMissing(t *testing.T) { |
| 88 | + dir := t.TempDir() |
| 89 | + machineIDPath := filepath.Join(dir, "machine-id") // does not exist |
| 90 | + bootIDPath := filepath.Join(dir, "boot_id") |
| 91 | + |
| 92 | + require.NoError(t, os.WriteFile(bootIDPath, []byte("def456\n"), 0600)) |
| 93 | + |
| 94 | + _, err := wslFilePasswordFromPaths(machineIDPath, bootIDPath) |
| 95 | + require.Error(t, err) |
| 96 | + require.Contains(t, err.Error(), machineIDPath) |
| 97 | +} |
| 98 | + |
| 99 | +func TestWslFilePasswordFromPaths_BootIDMissing(t *testing.T) { |
| 100 | + dir := t.TempDir() |
| 101 | + machineIDPath := filepath.Join(dir, "machine-id") |
| 102 | + bootIDPath := filepath.Join(dir, "boot_id") // does not exist |
| 103 | + |
| 104 | + require.NoError(t, os.WriteFile(machineIDPath, []byte("abc123\n"), 0600)) |
| 105 | + |
| 106 | + _, err := wslFilePasswordFromPaths(machineIDPath, bootIDPath) |
| 107 | + require.Error(t, err) |
| 108 | + require.Contains(t, err.Error(), bootIDPath) |
| 109 | +} |
| 110 | + |
| 111 | +func TestWslFilePasswordFromPaths_BothMissing(t *testing.T) { |
| 112 | + dir := t.TempDir() |
| 113 | + machineIDPath := filepath.Join(dir, "machine-id") |
| 114 | + bootIDPath := filepath.Join(dir, "boot_id") |
| 115 | + |
| 116 | + _, err := wslFilePasswordFromPaths(machineIDPath, bootIDPath) |
| 117 | + require.Error(t, err) |
| 118 | +} |
| 119 | + |
| 120 | +func TestWslFilePasswordFromPaths_Deterministic(t *testing.T) { |
| 121 | + dir := t.TempDir() |
| 122 | + machineIDPath := filepath.Join(dir, "machine-id") |
| 123 | + bootIDPath := filepath.Join(dir, "boot_id") |
| 124 | + |
| 125 | + require.NoError(t, os.WriteFile(machineIDPath, []byte("stable-id\n"), 0600)) |
| 126 | + require.NoError(t, os.WriteFile(bootIDPath, []byte("stable-boot\n"), 0600)) |
| 127 | + |
| 128 | + first, err := wslFilePasswordFromPaths(machineIDPath, bootIDPath) |
| 129 | + require.NoError(t, err) |
| 130 | + second, err := wslFilePasswordFromPaths(machineIDPath, bootIDPath) |
| 131 | + require.NoError(t, err) |
| 132 | + |
| 133 | + require.Equal(t, first, second) |
| 134 | +} |
| 135 | + |
| 136 | +func TestWslFilePasswordFromPaths_DifferentIDsDifferentPasswords(t *testing.T) { |
| 137 | + dir := t.TempDir() |
| 138 | + bootIDPath := filepath.Join(dir, "boot_id") |
| 139 | + require.NoError(t, os.WriteFile(bootIDPath, []byte("same-boot\n"), 0600)) |
| 140 | + |
| 141 | + pathA := filepath.Join(dir, "machine-id-a") |
| 142 | + pathB := filepath.Join(dir, "machine-id-b") |
| 143 | + require.NoError(t, os.WriteFile(pathA, []byte("id-aaa\n"), 0600)) |
| 144 | + require.NoError(t, os.WriteFile(pathB, []byte("id-bbb\n"), 0600)) |
| 145 | + |
| 146 | + pwA, err := wslFilePasswordFromPaths(pathA, bootIDPath) |
| 147 | + require.NoError(t, err) |
| 148 | + pwB, err := wslFilePasswordFromPaths(pathB, bootIDPath) |
| 149 | + require.NoError(t, err) |
| 150 | + |
| 151 | + require.NotEqual(t, pwA, pwB) |
| 152 | +} |
| 153 | + |
| 154 | +func TestWslFilePasswordFromPaths_TrimsWhitespace(t *testing.T) { |
| 155 | + dir := t.TempDir() |
| 156 | + machineIDPath := filepath.Join(dir, "machine-id") |
| 157 | + bootIDPath := filepath.Join(dir, "boot_id") |
| 158 | + |
| 159 | + require.NoError(t, os.WriteFile(machineIDPath, []byte(" trimmed-id \n"), 0600)) |
| 160 | + require.NoError(t, os.WriteFile(bootIDPath, []byte(" trimmed-boot \n"), 0600)) |
| 161 | + |
| 162 | + got, err := wslFilePasswordFromPaths(machineIDPath, bootIDPath) |
| 163 | + require.NoError(t, err) |
| 164 | + require.Equal(t, wslExpectedPassword(t, "trimmed-id", "trimmed-boot"), got) |
| 165 | +} |
0 commit comments