| 1 | package copydesk |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestCopyContractRejectsCommandEscape(t *testing.T) { |
| 9 | c := Contract{Version: 1, Paths: []string{"app.go"}, Extract: []string{"python3", "scripts/copy.py", "extract"}, Apply: []string{"python3", "scripts/copy.py", "apply"}, Accept: []string{"python3", "scripts/copy.py", "accept"}, Classify: []string{"python3", "scripts/copy.py", "classify"}, Regenerate: []string{"python3", "scripts/copy.py", "regenerate"}} |
| 10 | raw, _ := json.Marshal(c) |
| 11 | if _, err := ParseContract(raw); err != nil { |
| 12 | t.Fatal(err) |
| 13 | } |
| 14 | for _, cmd := range [][]string{{"sh", "-c", "evil"}, {"python3", "../evil.py"}, {"python3", "/etc/evil.py"}} { |
| 15 | c.Extract = cmd |
| 16 | raw, _ = json.Marshal(c) |
| 17 | if _, err := ParseContract(raw); err == nil { |
| 18 | t.Fatalf("escaped command %v", cmd) |
| 19 | } |
| 20 | } |
| 21 | } |
| 22 | func TestGenericDetectionAndByteScope(t *testing.T) { |
| 23 | old := []byte("package a\nvar title=\"Old words\"\n") |
| 24 | now := []byte("package a\nvar title=\"New words\"\n") |
| 25 | ex, err := Generic("a.go", old, now) |
| 26 | if err != nil || len(ex) != 1 { |
| 27 | t.Fatalf("%+v %v", ex, err) |
| 28 | } |
| 29 | if ex[0].Text != "New words" { |
| 30 | t.Fatal(ex) |
| 31 | } |
| 32 | ex, err = Generic("README.md", old, now) |
| 33 | if err != nil || len(ex) != 0 { |
| 34 | t.Fatal("docs trigger") |
| 35 | } |
| 36 | if got, err := Generic("view.swift", old, now); err != nil || len(got) != 0 { |
| 37 | t.Fatal("unsupported interface was not skipped") |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestGenericUnquotedDisplayIsUnknown(t *testing.T) { |
| 42 | if _, err := Generic("view.html", []byte("<p>Old words</p>"), []byte("<p>New words</p>")); err == nil { |
| 43 | t.Fatal("unquoted display text skipped") |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestGenericTemplateExpressionsNeedContract(t *testing.T) { |
| 48 | if _, err := Generic("view.js", []byte("const label=`Old words`"), []byte("const label=`Hi ${(()=>{run()})()}`")); err == nil { |
| 49 | t.Fatal("template expression exposed as editable wording") |
| 50 | } |
| 51 | } |
| 52 | |