| 1 | package vmname |
| 2 | |
| 3 | import ( |
| 4 | "regexp" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | |
| 10 | |
| 11 | var limaID = regexp.MustCompile(`^[A-Za-z0-9]+([._-][A-Za-z0-9]+)*$`) |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | func FuzzName(f *testing.F) { |
| 17 | for _, s := range []string{"/home/u/proj", "/", "/home/u/__", "/home/u/-x-", "/home/u/café", "/home/u/base", "/home/u/x-scratch", "/a\nb", "/home/u/" + strings.Repeat("y", 90)} { |
| 18 | f.Add(s) |
| 19 | } |
| 20 | f.Fuzz(func(t *testing.T, dir string) { |
| 21 | n := Name(dir) |
| 22 | if !limaID.MatchString(n) || n == Template { |
| 23 | t.Fatalf("Name(%q) = %q", dir, n) |
| 24 | } |
| 25 | if IsScratch(n) && !strings.HasSuffix(strings.TrimSuffix(dir, "/"), "-scratch") && !strings.HasSuffix(strings.TrimSuffix(dir, "/"), "scratch") { |
| 26 | t.Fatalf("Name(%q) = %q looks like a scratch VM", dir, n) |
| 27 | } |
| 28 | s, err := Scratch(dir) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | if !limaID.MatchString(s) || len(s) > 76 || !IsScratch(s) { |
| 33 | t.Fatalf("Scratch(%q) = %q", dir, s) |
| 34 | } |
| 35 | }) |
| 36 | } |