| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
package plist |
| 9 |
|
| 10 |
import ( |
| 11 |
"encoding/gob" |
| 12 |
"fmt" |
| 13 |
"io/ioutil" |
| 14 |
"os" |
| 15 |
"path/filepath" |
| 16 |
"strings" |
| 17 |
"testing" |
| 18 |
) |
| 19 |
|
| 20 |
var filenameReplacer = strings.NewReplacer(`<`, `_`, `>`, `_`, `:`, `_`, `"`, `_`, `/`, `_`, `\`, `_`, `|`, `_`, `?`, `_`, `*`, `_`) |
| 21 |
|
| 22 |
var extensions = map[int]string{ |
| 23 |
BinaryFormat: ".binary.plist", |
| 24 |
XMLFormat: ".xml.plist", |
| 25 |
GNUStepFormat: ".gnustep.plist", |
| 26 |
OpenStepFormat: ".openstep.plist", |
| 27 |
} |
| 28 |
|
| 29 |
func sanitizeFilename(f string) string { |
| 30 |
return filenameReplacer.Replace(f) |
| 31 |
} |
| 32 |
|
| 33 |
func oneshotGob(v interface{}, path string) { |
| 34 |
f, _ := os.Create(path) |
| 35 |
defer f.Close() |
| 36 |
enc := gob.NewEncoder(f) |
| 37 |
enc.Encode(v) |
| 38 |
} |
| 39 |
|
| 40 |
func makeDirs(dirs ...string) error { |
| 41 |
for _, v := range dirs { |
| 42 |
err := os.MkdirAll(v, 0777) |
| 43 |
if err != nil { |
| 44 |
return err |
| 45 |
} |
| 46 |
} |
| 47 |
return nil |
| 48 |
} |
| 49 |
|
| 50 |
func touch(path string) { |
| 51 |
f, _ := os.Create(path) |
| 52 |
f.Close() |
| 53 |
} |
| 54 |
|
| 55 |
func TestDump(t *testing.T) { |
| 56 |
dir := os.Getenv("PLIST_DUMP_DIR") |
| 57 |
if dir == "" { |
| 58 |
dir = "dump" |
| 59 |
} |
| 60 |
|
| 61 |
dir, err := filepath.Abs(dir) |
| 62 |
if err != nil { |
| 63 |
t.Fatal(err) |
| 64 |
} |
| 65 |
|
| 66 |
documentDir := filepath.Join(dir, "golden") |
| 67 |
encodeDir := filepath.Join(dir, "encode_from") |
| 68 |
decodeDir := filepath.Join(dir, "decode_as") |
| 69 |
invalidDir := filepath.Join(dir, "invalid") |
| 70 |
err = makeDirs(dir, documentDir, encodeDir, decodeDir, invalidDir) |
| 71 |
if err != nil { |
| 72 |
t.Fatal(err) |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
for _, td := range tests { |
| 77 |
t.Log("Dumping", td.Name) |
| 78 |
|
| 79 |
saneName := sanitizeFilename(td.Name) |
| 80 |
|
| 81 |
encv := td.Value |
| 82 |
if encv != nil && len(td.SkipEncode) < len(extensions) { |
| 83 |
|
| 84 |
oneshotGob(encv, filepath.Join(encodeDir, saneName+".gob")) |
| 85 |
} |
| 86 |
|
| 87 |
decv := td.DecodeValue |
| 88 |
if decv != nil && len(td.SkipDecode) < len(extensions) { |
| 89 |
|
| 90 |
oneshotGob(decv, filepath.Join(decodeDir, saneName+".gob")) |
| 91 |
} |
| 92 |
|
| 93 |
for k, v := range td.Documents { |
| 94 |
extName := saneName + extensions[k] |
| 95 |
path := filepath.Join(documentDir, extName) |
| 96 |
_ = ioutil.WriteFile(path, v, 0666) |
| 97 |
if td.SkipEncode[k] { |
| 98 |
touch(path + ".decode_only") |
| 99 |
} |
| 100 |
if td.SkipDecode[k] { |
| 101 |
touch(path + ".encode_only") |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
for _, td := range InvalidTextPlists { |
| 108 |
saneName := sanitizeFilename(td.Name) |
| 109 |
ext := extensions[OpenStepFormat] |
| 110 |
if strings.Contains(td.Name, "GNUStep") { |
| 111 |
ext = extensions[GNUStepFormat] |
| 112 |
} |
| 113 |
|
| 114 |
ioutil.WriteFile(filepath.Join(invalidDir, saneName+ext), []byte(td.Data), 0666) |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
for i, v := range InvalidXMLPlists { |
| 119 |
ioutil.WriteFile(filepath.Join(invalidDir, fmt.Sprintf("invalid-x-%2.02d", i)+extensions[XMLFormat]), []byte(v), 0666) |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
for i, v := range InvalidBplists { |
| 124 |
ioutil.WriteFile(filepath.Join(invalidDir, fmt.Sprintf("invalid-b-%2.02d", i)+extensions[BinaryFormat]), v, 0666) |
| 125 |
} |
| 126 |
} |