formats_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package office
  2. import (
  3. "encoding/json"
  4. "os"
  5. "strings"
  6. "testing"
  7. )
  8. func readFixture(t *testing.T, path string) []byte {
  9. t.Helper()
  10. b, err := os.ReadFile(path)
  11. if err != nil {
  12. t.Fatal(err)
  13. }
  14. return b
  15. }
  16. func TestAppForFile(t *testing.T) {
  17. tests := []struct {
  18. ext, app string
  19. legacy bool
  20. }{
  21. {".docx", AppDocument, false},
  22. {".DOC", AppDocument, true},
  23. {".dot", AppDocument, true},
  24. {".xls", AppSpreadsheet, true},
  25. {".xlt", AppSpreadsheet, true},
  26. {".xlsx", AppSpreadsheet, false},
  27. {".ppt", AppPresentation, true},
  28. {".pps", AppPresentation, true},
  29. {".odt", "", false},
  30. {".txt", "", false},
  31. }
  32. for _, tc := range tests {
  33. if got := AppForFile(tc.ext); got != tc.app {
  34. t.Errorf("AppForFile(%q) = %q, want %q", tc.ext, got, tc.app)
  35. }
  36. if got := IsLegacyExt(tc.ext); got != tc.legacy {
  37. t.Errorf("IsLegacyExt(%q) = %v", tc.ext, got)
  38. }
  39. }
  40. // the legacy formats are never a save target
  41. for _, ext := range []string{".doc", ".xls", ".ppt"} {
  42. if AppForExt(ext) != "" {
  43. t.Errorf("AppForExt(%q) should not name an app", ext)
  44. }
  45. }
  46. }
  47. var legacyFixtures = []struct {
  48. path, app, text string
  49. }{
  50. {"doc/testdata/features.doc", AppDocument, "ArozOS Legacy Import Test"},
  51. {"xls/testdata/workbook.xls", AppSpreadsheet, "Merged across three columns"},
  52. {"ppt/testdata/deck.ppt", AppPresentation, "Speaker notes for slide two"},
  53. }
  54. func TestLegacyFilesOpenAsNative(t *testing.T) {
  55. for _, fx := range legacyFixtures {
  56. data := readFixture(t, fx.path)
  57. if got := LegacyApp(data); got != fx.app {
  58. t.Fatalf("%s: LegacyApp = %q, want %q", fx.path, got, fx.app)
  59. }
  60. app, ooxml, err := ConvertLegacy(data)
  61. if err != nil || app != fx.app || len(ooxml) < 4 || string(ooxml[:2]) != "PK" {
  62. t.Fatalf("%s: ConvertLegacy = %q, %d bytes, %v", fx.path, app, len(ooxml), err)
  63. }
  64. env, err := ReadNativeFile(fx.app, data, nil)
  65. if err != nil {
  66. t.Fatalf("%s: ReadNativeFile: %v", fx.path, err)
  67. }
  68. var e struct {
  69. Type string `json:"type"`
  70. App string `json:"app"`
  71. Body json.RawMessage `json:"body"`
  72. }
  73. if err := json.Unmarshal([]byte(env), &e); err != nil {
  74. t.Fatalf("%s: envelope is not JSON: %v", fx.path, err)
  75. }
  76. if e.Type != envelopeType || e.App != fx.app || !strings.Contains(string(e.Body), fx.text) {
  77. t.Errorf("%s: envelope %s/%s lacks %q", fx.path, e.Type, e.App, fx.text)
  78. }
  79. gotApp, body, err := ImportLegacy(data)
  80. if err != nil || gotApp != fx.app || !strings.Contains(body, fx.text) {
  81. t.Errorf("%s: ImportLegacy = %q, %v", fx.path, gotApp, err)
  82. }
  83. // pictures go through the sink like any import's
  84. if fx.app == AppDocument {
  85. n := 0
  86. env, err := ReadNativeFile(fx.app, data, func(name string, content []byte) (string, error) {
  87. n++
  88. return "media?file=" + name, nil
  89. })
  90. if err != nil || n == 0 || strings.Contains(env, "data:image/png") {
  91. t.Errorf("legacy pictures should be externalized (%d, %v)", n, err)
  92. }
  93. }
  94. }
  95. }
  96. func TestLegacyFileInTheWrongApp(t *testing.T) {
  97. data := readFixture(t, "xls/testdata/workbook.xls")
  98. if _, err := ReadNativeFile(AppDocument, data, nil); err == nil || !strings.Contains(err.Error(), "Excel 97-2003") {
  99. t.Errorf("a workbook opened as a document: err = %v", err)
  100. }
  101. if LegacyApp([]byte("PK\x03\x04")) != "" {
  102. t.Error("a zip is not a legacy file")
  103. }
  104. if _, _, err := ConvertLegacy([]byte("plain text")); err == nil {
  105. t.Error("ConvertLegacy should refuse other files")
  106. }
  107. }