| 1 | package reversesshfs |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "os/exec" |
| 6 | "runtime" |
| 7 | "testing" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | |
| 12 | func TestHelperProcess(t *testing.T) { |
| 13 | if os.Getenv("REVERSESSHFS_TEST_SLEEP") == "" { |
| 14 | return |
| 15 | } |
| 16 | time.Sleep(time.Minute) |
| 17 | } |
| 18 | |
| 19 | |
| 20 | func TestCloseReaps(t *testing.T) { |
| 21 | start := func() *exec.Cmd { |
| 22 | cmd := exec.Command(os.Args[0], "-test.run=^TestHelperProcess$") |
| 23 | cmd.Env = append(os.Environ(), "REVERSESSHFS_TEST_SLEEP=1") |
| 24 | if err := cmd.Start(); err != nil { |
| 25 | t.Fatal(err) |
| 26 | } |
| 27 | return cmd |
| 28 | } |
| 29 | rsf := &ReverseSSHFS{sshCmd: start(), opensshSftpServerCmd: start()} |
| 30 | if err := rsf.Close(); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | for _, cmd := range []*exec.Cmd{rsf.sshCmd, rsf.opensshSftpServerCmd} { |
| 34 | if cmd.ProcessState == nil { |
| 35 | t.Error("a killed process was not waited for") |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func TestAddQuotes(t *testing.T) { |
| 41 | type testCase struct { |
| 42 | input string |
| 43 | expected string |
| 44 | } |
| 45 | var testCases []testCase |
| 46 | if runtime.GOOS != "windows" { |
| 47 | testCases = []testCase{ |
| 48 | { |
| 49 | input: "/user/test/path", |
| 50 | expected: "\"/user/test/path\"", |
| 51 | }, |
| 52 | } |
| 53 | } else { |
| 54 | testCases = []testCase{ |
| 55 | { |
| 56 | input: "/user/test/path", |
| 57 | expected: "'/user/test/path'", |
| 58 | }, |
| 59 | } |
| 60 | } |
| 61 | for i, tc := range testCases { |
| 62 | got := addQuotes(tc.input) |
| 63 | if got != tc.expected { |
| 64 | t.Errorf("#%d: expected %q, got %q", i, tc.expected, got) |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func TestConvertMSYS2Path(t *testing.T) { |
| 70 | inputPath := "/c/Users/lts" |
| 71 | expectedPath := "C:\\Users\\lts" |
| 72 | |
| 73 | actualPath := convertMSYS2Path(inputPath) |
| 74 | |
| 75 | if actualPath != expectedPath { |
| 76 | t.Errorf("Conversion failed: expected %q, got %q", expectedPath, actualPath) |
| 77 | } else { |
| 78 | t.Logf("Success! Converted path for native Windows OpenSSH: %q", actualPath) |
| 79 | } |
| 80 | } |