| 1 | |
| 2 | |
| 3 | |
| 4 | package plist |
| 5 | |
| 6 | import ( |
| 7 | "bytes" |
| 8 | "encoding/xml" |
| 9 | "fmt" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "path/filepath" |
| 13 | "runtime" |
| 14 | "testing" |
| 15 | "time" |
| 16 | |
| 17 | "gotest.tools/v3/assert" |
| 18 | ) |
| 19 | |
| 20 | func TestUnmarshalPlist(t *testing.T) { |
| 21 | const input = `<?xml version="1.0" encoding="UTF-8"?> |
| 22 | <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> |
| 23 | <plist version="1.0"> |
| 24 | <dict> |
| 25 | <key>arrKey</key> |
| 26 | <array> |
| 27 | <dict> |
| 28 | <key>strKey</key> |
| 29 | <string>strVal</string> |
| 30 | <key> strKey with |
| 31 | spaces</key> |
| 32 | <string> strVal |
| 33 | with space</string> |
| 34 | <key>intKey</key> |
| 35 | <integer>42</integer> |
| 36 | <key> intKey with |
| 37 | spaces</key> |
| 38 | <integer> 43</integer> |
| 39 | <key>intKeyZero</key> |
| 40 | <integer>0</integer> |
| 41 | <key>dataKey</key> |
| 42 | <data> |
| 43 | aGVs |
| 44 | bG8= |
| 45 | </data> |
| 46 | <key>dateKey</key> |
| 47 | <date>2020-01-02T03:04:05Z</date> |
| 48 | <key>trueKey</key> |
| 49 | <true/> |
| 50 | <key>falseKey</key> |
| 51 | <false/> |
| 52 | <key>realKey</key> |
| 53 | <real>3.14</real> |
| 54 | <key>realExpKey</key> |
| 55 | <real>1e-6</real> |
| 56 | </dict> |
| 57 | </array> |
| 58 | </dict> |
| 59 | </plist> |
| 60 | ` |
| 61 | expected := Plist{ |
| 62 | Value: Value{ |
| 63 | Dict: map[string]Value{ |
| 64 | "arrKey": { |
| 65 | Array: Array{ |
| 66 | { |
| 67 | Dict: map[string]Value{ |
| 68 | "strKey": {String: new("strVal")}, |
| 69 | " strKey with\n\t\t\tspaces": {String: new(" strVal\n\t\t\twith space")}, |
| 70 | "intKey": {Integer: new(int64(42))}, |
| 71 | " intKey with\n\t\t\tspaces": {Integer: new(int64(43))}, |
| 72 | "intKeyZero": {Integer: new(int64(0))}, |
| 73 | "dataKey": {Data: []byte("hello")}, |
| 74 | "dateKey": {Date: new(time.Date(2020, time.January, 2, 3, 4, 5, 0, time.UTC))}, |
| 75 | "trueKey": {Boolean: new(true)}, |
| 76 | "falseKey": {Boolean: new(false)}, |
| 77 | "realKey": {Real: new(3.14)}, |
| 78 | "realExpKey": {Real: new(1e-6)}, |
| 79 | }, |
| 80 | }, |
| 81 | }, |
| 82 | }, |
| 83 | }, |
| 84 | }, |
| 85 | } |
| 86 | |
| 87 | plutilExpected := map[string]string{ |
| 88 | "arrKey.0.strKey": "strVal", |
| 89 | "arrKey.0. strKey with\n\t\t\tspaces": " strVal\n\t\t\twith space", |
| 90 | "arrKey.0.intKey": "42", |
| 91 | "arrKey.0. intKey with\n\t\t\tspaces": "43", |
| 92 | "arrKey.0.intKeyZero": "0", |
| 93 | "arrKey.0.dataKey": "aGVsbG8=", |
| 94 | "arrKey.0.dateKey": "2020-01-02T03:04:05Z", |
| 95 | "arrKey.0.trueKey": "true", |
| 96 | "arrKey.0.falseKey": "false", |
| 97 | "arrKey.0.realKey": "3.140000", |
| 98 | "arrKey.0.realExpKey": "0.000001", |
| 99 | } |
| 100 | |
| 101 | var p Plist |
| 102 | err := xml.Unmarshal([]byte(input), &p) |
| 103 | assert.NilError(t, err) |
| 104 | assert.DeepEqual(t, p, expected) |
| 105 | |
| 106 | if runtime.GOOS == "darwin" { |
| 107 | ctx := t.Context() |
| 108 | td := t.TempDir() |
| 109 | testFilePath := filepath.Join(td, "test.plist") |
| 110 | err := os.WriteFile(testFilePath, []byte(input), 0o644) |
| 111 | assert.NilError(t, err) |
| 112 | for plutilExpectedKey, plutilExpectedValue := range plutilExpected { |
| 113 | plutilCmd := exec.CommandContext(ctx, "plutil", "-extract", plutilExpectedKey, "raw", "-n", "-o", "-", testFilePath) |
| 114 | var stderr bytes.Buffer |
| 115 | plutilCmd.Stderr = &stderr |
| 116 | plutilOutput, err := plutilCmd.Output() |
| 117 | assert.NilError(t, err, fmt.Sprintf("failed to run %v (stderr=%#q)", plutilCmd.Args, stderr.String())) |
| 118 | plutilOutputStr := string(plutilOutput) |
| 119 | assert.Equal(t, plutilExpectedValue, plutilOutputStr, "plutil output mismatch for key %#q", plutilExpectedKey) |
| 120 | } |
| 121 | } |
| 122 | } |