Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: unmarshal automatically removes non-charchater characters #1

Merged
merged 6 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/on-push-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
format-build-test:
strategy:
matrix:
go-version: ['1.21.x', '1.22.x']
go-version: ['1.22.x', '1.23.x']
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
Expand Down
22 changes: 14 additions & 8 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,18 +106,22 @@ func (d *Decoder) unmarshal(pval *plistValue, v reflect.Value) error {

if v.CanInterface() && v.Type().Implements(unmarshalerType) {
u := v.Interface().(Unmarshaler)
return u.UnmarshalPlist(func(i interface{}) error {
return d.unmarshal(pval, reflect.ValueOf(i))
})
return u.UnmarshalPlist(
func(i interface{}) error {
return d.unmarshal(pval, reflect.ValueOf(i))
},
)
}

if v.CanAddr() {
pv := v.Addr()
if pv.CanInterface() && pv.Type().Implements(unmarshalerType) {
u := pv.Interface().(Unmarshaler)
return u.UnmarshalPlist(func(i interface{}) error {
return d.unmarshal(pval, reflect.ValueOf(i))
})
return u.UnmarshalPlist(
func(i interface{}) error {
return d.unmarshal(pval, reflect.ValueOf(i))
},
)
}

}
Expand Down Expand Up @@ -271,12 +275,14 @@ func (d *Decoder) unmarshalInteger(pval *plistValue, v reflect.Value) error {
// Make sure plistValue isn't negative when decoding into uint.
if pval.value.(signedInt).signed {
return UnmarshalTypeError{
fmt.Sprintf("%v", int64(pval.value.(signedInt).value)), v.Type()}
fmt.Sprintf("%v", int64(pval.value.(signedInt).value)), v.Type(),
}
}
v.SetUint(pval.value.(signedInt).value)
default:
return UnmarshalTypeError{
fmt.Sprintf("%v", pval.value.(signedInt).value), v.Type()}
fmt.Sprintf("%v", pval.value.(signedInt).value), v.Type(),
}
}
return nil
}
Expand Down
109 changes: 55 additions & 54 deletions decode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"bytes"
"encoding/base64"
"io"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"os/exec"
"os"
"path/filepath"
"reflect"
"testing"
Expand Down Expand Up @@ -233,6 +232,57 @@ func TestDecodeData(t *testing.T) {
}
}

func TestDecodeDataInvalidCharacter(t *testing.T) {
type data map[string]any
expect := data{
"CommandUUID": "6222776f-1a8a-462c-b931-e1c1981134d2",
"InstalledApplicationList": []any{
map[string]any{
"BundleSize": uint64(61116416),
"Identifier": "daojiasuyun58",
"Name": "快狗打车",
"ShortVersion": "5.18.0",
},
},
"Status": "Acknowledged",
"UDID": "device-uuid",
}

out := data{}
if err := Unmarshal(
[]byte(`<?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>CommandUUID</key>
<string>6222776f-1a8a-462c-b931-e1c1981134d2</string>
<key>InstalledApplicationList</key>
<array>
<dict>
<key>BundleSize</key>
<integer>61116416</integer>
<key>Identifier</key>
<string>daojiasuyun58</string>
<key>Name</key>
<string>快狗打车</string>
<key>ShortVersion</key>
<string>5.18.0</string>
</dict>
</array>
<key>Status</key>
<string>Acknowledged</string>
<key>UDID</key>
<string>device-uuid</string>
</dict>`), &out,
); err != nil {
t.Fatal(err)
}

if !reflect.DeepEqual(expect, out) {
t.Errorf("\nexpect: \n%#v,\nout: \n%#v\n", expect, out)
}
}

func TestDecodeData_emptyData(t *testing.T) {
var before, after []byte
if err := Unmarshal([]byte(emptyDataRef), &after); err != nil {
Expand Down Expand Up @@ -346,7 +396,7 @@ func TestDecodeBinaryPlist(t *testing.T) {
Data [][]byte `plist:"data"`
}

content, err := ioutil.ReadFile(filepath.Join("testdata", tt.filename))
content, err := os.ReadFile(filepath.Join("testdata", tt.filename))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -471,7 +521,7 @@ func TestUnmarshaler(t *testing.T) {

func TestFuzzCrashers(t *testing.T) {
dir := filepath.Join("testdata", "crashers")
testDir, err := ioutil.ReadDir(dir)
testDir, err := os.ReadDir(dir)
if err != nil {
t.Fatalf("reading dir %q: %s", dir, err)
}
Expand All @@ -482,7 +532,7 @@ func TestFuzzCrashers(t *testing.T) {
tc.Name(), func(t *testing.T) {
t.Parallel()

crasher, err := ioutil.ReadFile(filepath.Join("testdata", "crashers", tc.Name()))
crasher, err := os.ReadFile(filepath.Join("testdata", "crashers", tc.Name()))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -529,52 +579,3 @@ 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 := ioutil.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 := ioutil.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)
}
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/sawyer523/plist

go 1.14
go 1.22
Empty file added go.sum
Empty file.
8 changes: 0 additions & 8 deletions testdata/xml/empty-doctype.plist

This file was deleted.

4 changes: 0 additions & 4 deletions testdata/xml/empty-plist.plist

This file was deleted.

8 changes: 0 additions & 8 deletions testdata/xml/empty-xml.plist

This file was deleted.

9 changes: 0 additions & 9 deletions testdata/xml/invalid-before-plist.plist

This file was deleted.

9 changes: 0 additions & 9 deletions testdata/xml/invalid-data.plist

This file was deleted.

9 changes: 0 additions & 9 deletions testdata/xml/invalid-end.plist

This file was deleted.

9 changes: 0 additions & 9 deletions testdata/xml/invalid-middle.plist

This file was deleted.

8 changes: 0 additions & 8 deletions testdata/xml/invalid-start.plist

This file was deleted.

8 changes: 0 additions & 8 deletions testdata/xml/malformed-xml.plist

This file was deleted.

6 changes: 0 additions & 6 deletions testdata/xml/no-both.plist

This file was deleted.

7 changes: 0 additions & 7 deletions testdata/xml/no-dict-end.plist

This file was deleted.

7 changes: 0 additions & 7 deletions testdata/xml/no-doctype.plist

This file was deleted.

7 changes: 0 additions & 7 deletions testdata/xml/no-plist-end.plist

This file was deleted.

8 changes: 0 additions & 8 deletions testdata/xml/no-plist-version.plist

This file was deleted.

7 changes: 0 additions & 7 deletions testdata/xml/no-xml-tag.plist

This file was deleted.

8 changes: 0 additions & 8 deletions testdata/xml/swapped.plist

This file was deleted.

8 changes: 0 additions & 8 deletions testdata/xml/unescaped-plist.plist

This file was deleted.

8 changes: 0 additions & 8 deletions testdata/xml/unescaped-xml.plist

This file was deleted.

8 changes: 0 additions & 8 deletions testdata/xml/valid.plist

This file was deleted.

Loading