Skip to content

Commit

Permalink
added test for parity with macOS's plutil -lint
Browse files Browse the repository at this point in the history
  • Loading branch information
korylprince committed Nov 10, 2022
1 parent 63fa881 commit 2779718
Show file tree
Hide file tree
Showing 20 changed files with 197 additions and 0 deletions.
51 changes: 51 additions & 0 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"log"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"path/filepath"
"reflect"
"testing"
Expand Down Expand Up @@ -513,3 +515,52 @@ func TestDecodeTagSkip(t *testing.T) {
t.Error("field decoded when it was tagged as -")
}
}

// test parity with plutil -lint on macOS
var xmlParityTestFailures = map[string]struct{}{
"empty-plist.plist": {},
"invalid-before-plist.plist": {},
"invalid-data.plist": {},
"invalid-middle.plist": {},
"invalid-start.plist": {},
"no-dict-end.plist": {},
"no-plist-end.plist": {},
"unescaped-plist.plist": {},
"unescaped-xml.plist": {},
}

func TestXMLPlutilParity(t *testing.T) {
type data struct {
Key string `plist:"key"`
}
tests, err := os.ReadDir("testdata/xml/")
if err != nil {
t.Fatalf("could not open testdata/xml: %v", err)
}

plutil, _ := exec.LookPath("plutil")

for _, test := range tests {
testPath := filepath.Join("testdata/xml/", test.Name())
buf, err := os.ReadFile(testPath)
if err != nil {
t.Errorf("could not read test %s: %v", test.Name(), err)
continue
}
v := new(data)
err = Unmarshal(buf, v)

_, check := xmlParityTestFailures[test.Name()]
if plutil != "" {
check = exec.Command(plutil, "-lint", testPath).Run() != nil
}

if check && err == nil {
t.Errorf("expected error for test %s but got: nil", test.Name())
} else if !check && err != nil {
t.Errorf("expected no error for test %s but got: %v", test.Name(), err)
} else if !check && v.Key != "val" {
t.Errorf("expected key=val for test %s but got: key=%s", test.Name(), v.Key)
}
}
}
8 changes: 8 additions & 0 deletions testdata/xml/empty-doctype.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE>
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
4 changes: 4 additions & 0 deletions testdata/xml/empty-plist.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
</plist>
8 changes: 8 additions & 0 deletions testdata/xml/empty-xml.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
9 changes: 9 additions & 0 deletions testdata/xml/invalid-before-plist.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
invalid
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
9 changes: 9 additions & 0 deletions testdata/xml/invalid-data.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
invalid
</dict>
</plist>
9 changes: 9 additions & 0 deletions testdata/xml/invalid-end.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
invalid
9 changes: 9 additions & 0 deletions testdata/xml/invalid-middle.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
invalid
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
8 changes: 8 additions & 0 deletions testdata/xml/invalid-start.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
invalid<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
8 changes: 8 additions & 0 deletions testdata/xml/malformed-xml.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
6 changes: 6 additions & 0 deletions testdata/xml/no-both.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
7 changes: 7 additions & 0 deletions testdata/xml/no-dict-end.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</plist>
7 changes: 7 additions & 0 deletions testdata/xml/no-doctype.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
7 changes: 7 additions & 0 deletions testdata/xml/no-plist-end.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
8 changes: 8 additions & 0 deletions testdata/xml/no-plist-version.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist>
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
7 changes: 7 additions & 0 deletions testdata/xml/no-xml-tag.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
8 changes: 8 additions & 0 deletions testdata/xml/swapped.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
8 changes: 8 additions & 0 deletions testdata/xml/unescaped-plist.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0"
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
8 changes: 8 additions & 0 deletions testdata/xml/unescaped-xml.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>
8 changes: 8 additions & 0 deletions testdata/xml/valid.plist
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>key</key>
<string>val</string>
</dict>
</plist>

0 comments on commit 2779718

Please sign in to comment.