agi.office_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package agi
  2. import (
  3. "testing"
  4. "github.com/robertkrimen/otto"
  5. "imuslab.com/arozos/mod/agi/static"
  6. user "imuslab.com/arozos/mod/user"
  7. )
  8. // The Office suite's backends call these by name (common/backend/document.agi
  9. // and the apps' convert.agi); a rename or a dropped registration would only
  10. // show as a broken save in the browser.
  11. func TestInjectOfficeLib_JSObjectExposed(t *testing.T) {
  12. g := minimalGateway()
  13. vm := otto.New()
  14. g.injectOfficeLibFunctions(&static.AgiLibInjectionPayload{VM: vm, User: &user.User{Username: "alice"}})
  15. present := []string{
  16. "saveDocument", "loadDocument", "readPayload",
  17. "packToFile", "unpackToWorkdir",
  18. "docxToDocument", "documentToDocx", "xlsxToWorkbook", "workbookToXlsx",
  19. "pptxToPresentation", "presentationToPptx",
  20. "odtToDocument", "documentToOdt", "odsToWorkbook", "workbookToOds",
  21. "odpToPresentation", "presentationToOdp",
  22. "documentToPdf", "workbookPrintToPdf", "writeBinaryFile",
  23. "touchWorkdir", "releaseWorkdir",
  24. }
  25. for _, fn := range present {
  26. val, err := vm.Run(`typeof office.` + fn)
  27. if err != nil {
  28. t.Fatalf("evaluating office.%s: %v", fn, err)
  29. }
  30. if s, _ := val.ToString(); s != "function" {
  31. t.Errorf("office.%s should be a function, got %q", fn, s)
  32. }
  33. }
  34. // the .doca-era reader is gone with the format
  35. val, _ := vm.Run(`typeof office.unpackFromFile`)
  36. if s, _ := val.ToString(); s != "undefined" {
  37. t.Errorf("office.unpackFromFile should no longer exist, got %q", s)
  38. }
  39. }
  40. // The workdir functions delete folders, so they may act only inside the
  41. // suite's own working folders - never on a user's files, never by climbing
  42. // out with "..", never on a working root itself.
  43. func TestIsOfficeWorkdir(t *testing.T) {
  44. cases := []struct {
  45. vpath string
  46. want bool
  47. }{
  48. {"tmp:/.appdata/Office/cache/k3x9a2", true},
  49. {"tmp:/.appdata/Office/uploads/k3x9a2/pic.png", true},
  50. {"tmp:/.appdata/Office/tmp/post-1.json.gz", true},
  51. {"tmp:/.appdata/Office/cache/k3x9a2/", true},
  52. {"tmp:/.appdata/Office/", false},
  53. {"tmp:/.appdata/Office", false},
  54. {"tmp:/.appdata/Other/x", false},
  55. {"tmp:/.appdata/Office/../../secret", false},
  56. {"tmp:/.appdata/Office/cache/../../../x", false},
  57. {"user:/.appdata/Office/cache/ab12", true}, // the old location, swept
  58. {"user:/.appdata/Office/uploads", false},
  59. {"user:/.appdata/Office/session/slides.osession", false},
  60. {"user:/Documents/report.docx", false},
  61. {`tmp:/.appdata\Office\cache\a`, true},
  62. {"no-root/.appdata/Office/cache/a", false},
  63. }
  64. for _, c := range cases {
  65. if got := isOfficeWorkdir(c.vpath); got != c.want {
  66. t.Errorf("isOfficeWorkdir(%q) = %v, want %v", c.vpath, got, c.want)
  67. }
  68. }
  69. }
  70. func TestIsOfficeWorkdirRoot(t *testing.T) {
  71. for vp, want := range map[string]bool{
  72. "tmp:/.appdata/Office/cache": true,
  73. "tmp:/.appdata/Office/uploads/": true,
  74. "tmp:/.appdata/Office/tmp": true,
  75. "user:/.appdata/Office/cache": true,
  76. "user:/.appdata/Office/uploads/": true,
  77. "tmp:/.appdata/Office/cache/k3x9": false,
  78. "tmp:/.appdata/Office": false,
  79. "user:/.appdata/Office/session": false,
  80. "user:/.appdata/Office/cache/../x": false,
  81. } {
  82. if got := isOfficeWorkdirRoot(vp); got != want {
  83. t.Errorf("isOfficeWorkdirRoot(%q) = %v, want %v", vp, got, want)
  84. }
  85. }
  86. }