From 0c16616117e9866cc49adc85738e3c9e3adf413a Mon Sep 17 00:00:00 2001 From: Jakob Unterwurzacher Date: Tue, 10 Aug 2021 19:42:33 +0200 Subject: main: add testcases for convertToDoubleDash & parseCliOpts --- cli_args.go | 10 ++++-- cli_args_test.go | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ main.go | 2 +- 3 files changed, 113 insertions(+), 3 deletions(-) diff --git a/cli_args.go b/cli_args.go index 5ff1861..fa9e35c 100644 --- a/cli_args.go +++ b/cli_args.go @@ -114,9 +114,15 @@ func prefixOArgs(osArgs []string) ([]string, error) { func convertToDoubleDash(osArgs []string) (out []string) { out = append(out, osArgs...) for i, v := range out { + // Leave "-h" alone so the help text keeps working if v == "-h" { continue } + // Don't touch anything after "--" + if v == "--" { + break + } + // Convert "-foo" to "--foo" if len(v) >= 2 && v[0] == '-' && v[1] != '-' { out[i] = "-" + out[i] } @@ -125,11 +131,11 @@ func convertToDoubleDash(osArgs []string) (out []string) { } // parseCliOpts - parse command line options (i.e. arguments that start with "-") -func parseCliOpts() (args argContainer) { +func parseCliOpts(osArgs []string) (args argContainer) { var err error var opensslAuto string - osArgsPreprocessed, err := prefixOArgs(os.Args) + osArgsPreprocessed, err := prefixOArgs(osArgs) if err != nil { tlog.Fatal.Println(err) os.Exit(exitcodes.Usage) diff --git a/cli_args_test.go b/cli_args_test.go index 6dff1f1..97f9473 100644 --- a/cli_args_test.go +++ b/cli_args_test.go @@ -3,6 +3,8 @@ package main import ( "reflect" "testing" + + "github.com/rfjakob/gocryptfs/internal/stupidgcm" ) // TestPrefixOArgs checks that the "-o x,y,z" parsing works correctly. @@ -75,3 +77,105 @@ func TestPrefixOArgs(t *testing.T) { } } } + +func TestConvertToDoubleDash(t *testing.T) { + testcases := []struct { + // i is the input + i []string + // o is the expected output + o []string + }{ + { + i: nil, + o: nil, + }, + { + i: []string{"gocryptfs"}, + o: []string{"gocryptfs"}, + }, + { + i: []string{"gocryptfs", "foo"}, + o: []string{"gocryptfs", "foo"}, + }, + { + i: []string{"gocryptfs", "-v", "-quiet"}, + o: []string{"gocryptfs", "--v", "--quiet"}, + }, + { + i: []string{"gocryptfs", "--", "-foo"}, + o: []string{"gocryptfs", "--", "-foo"}, + }, + } + for _, tc := range testcases { + o := convertToDoubleDash(tc.i) + if !reflect.DeepEqual(o, tc.o) { + t.Errorf("in=%q\nwant=%q\nhave=%q", tc.i, tc.o, o) + } + } +} + +func TestParseCliOpts(t *testing.T) { + defaultArgs := argContainer{ + longnames: true, + raw64: true, + hkdf: true, + openssl: stupidgcm.PreferOpenSSL(), // depends on CPU and build flags + scryptn: 16, + } + + type testcaseContainer struct { + // i is the input + i []string + // o is the expected output + o argContainer + } + + var testcases []testcaseContainer + + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs"}, + o: defaultArgs, + }) + + o := defaultArgs + o.quiet = true + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "-q"}, + o: o, + }) + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "--q"}, + o: o, + }) + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "-quiet"}, + o: o, + }) + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "--quiet"}, + o: o, + }) + + o = defaultArgs + o.exclude = []string{"foo", "bar"} + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "-e", "foo", "-e", "bar"}, + o: o, + }) + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "--exclude", "foo", "--exclude", "bar"}, + o: o, + }) + /* TODO BROKEN + testcases = append(testcases, testcaseContainer{ + i: []string{"gocryptfs", "--exclude", "foo", "-e", "bar"}, + o: o, + }) + */ + for _, tc := range testcases { + o := parseCliOpts(tc.i) + if !reflect.DeepEqual(o, tc.o) { + t.Errorf("in=%v\nwant=%v\nhave=%v", tc.i, tc.o, o) + } + } +} diff --git a/main.go b/main.go index 9bd0c78..370feac 100644 --- a/main.go +++ b/main.go @@ -166,7 +166,7 @@ func main() { var err error // Parse all command-line options (i.e. arguments starting with "-") // into "args". Path arguments are parsed below. - args := parseCliOpts() + args := parseCliOpts(os.Args) // Fork a child into the background if "-fg" is not set AND we are mounting // a filesystem. The child will do all the work. if !args.fg && flagSet.NArg() == 2 { -- cgit v1.2.3