|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "strings" |
| 9 | + "syscall" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/rfjakob/gocryptfs/v2/internal/configfile" |
| 13 | + |
| 14 | + "github.com/rfjakob/gocryptfs/v2/tests/test_helpers" |
| 15 | +) |
| 16 | + |
| 17 | +// Create & test fs with -longnamemax=100 |
| 18 | +func TestLongnamemax100(t *testing.T) { |
| 19 | + cDir := test_helpers.InitFS(nil, "-longnamemax", "100") |
| 20 | + pDir := cDir + ".mnt" |
| 21 | + |
| 22 | + // Check config file sanity |
| 23 | + _, c, err := configfile.LoadAndDecrypt(cDir+"/"+configfile.ConfDefaultName, testPw) |
| 24 | + if err != nil { |
| 25 | + fmt.Println(err) |
| 26 | + os.Exit(1) |
| 27 | + } |
| 28 | + if !c.IsFeatureFlagSet(configfile.FlagLongNameMax) { |
| 29 | + t.Error("FlagLongNameMax should be on") |
| 30 | + } |
| 31 | + if c.LongNameMax != 100 { |
| 32 | + t.Errorf("LongNameMax=%d, want 100", c.LongNameMax) |
| 33 | + } |
| 34 | + |
| 35 | + // Check that it takes effect |
| 36 | + test_helpers.MountOrExit(cDir, pDir, "-extpass", "echo test") |
| 37 | + defer test_helpers.UnmountPanic(pDir) |
| 38 | + |
| 39 | + for l := 1; l <= 255; l++ { |
| 40 | + path := pDir + "/" + strings.Repeat("x", l) |
| 41 | + if err := ioutil.WriteFile(path, nil, 0600); err != nil { |
| 42 | + t.Fatal(err) |
| 43 | + } |
| 44 | + matches, err := filepath.Glob(cDir + "/gocryptfs.longname.*") |
| 45 | + if err != nil { |
| 46 | + t.Fatal(err) |
| 47 | + } |
| 48 | + err = syscall.Unlink(path) |
| 49 | + if err != nil { |
| 50 | + t.Fatal(err) |
| 51 | + } |
| 52 | + // As determined experimentally, a name of length >= 64 causes a longname |
| 53 | + // to be created. |
| 54 | + if l <= 63 && len(matches) != 0 { |
| 55 | + t.Errorf("l=%d: should not see a longname yet", l) |
| 56 | + } |
| 57 | + if l >= 64 && len(matches) != 2 { |
| 58 | + t.Errorf("l=%d: should see a longname now", l) |
| 59 | + } |
| 60 | + } |
| 61 | +} |
0 commit comments