snapshots_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package smbserver
  2. import (
  3. "encoding/binary"
  4. "os"
  5. "path/filepath"
  6. "strings"
  7. "testing"
  8. "time"
  9. "imuslab.com/arozos/mod/storage/smbserver/wire"
  10. )
  11. var versionTime1 = time.Date(2026, 11, 8, 2, 0, 0, 0, time.UTC)
  12. var versionTime2 = time.Date(2026, 11, 7, 1, 30, 15, 0, time.UTC)
  13. func versionHarness(t *testing.T) *harness {
  14. t.Helper()
  15. h := newHarness(t, noPoll)
  16. root := h.share.root
  17. os.MkdirAll(filepath.Join(root, ".metadata", "v1"), 0755)
  18. os.WriteFile(filepath.Join(root, "report.txt"), []byte("current text"), 0644)
  19. os.WriteFile(filepath.Join(root, ".metadata", "v1", "report.txt"), []byte("older text"), 0644)
  20. h.share.versions = map[string]map[time.Time]string{
  21. "report.txt": {versionTime1: ".metadata/v1/report.txt", versionTime2: ".metadata/v1/report.txt"},
  22. }
  23. return h
  24. }
  25. func snapshotTokens(t *testing.T, out []byte) (int, int, []string) {
  26. t.Helper()
  27. total := int(binary.LittleEndian.Uint32(out))
  28. returned := int(binary.LittleEndian.Uint32(out[4:]))
  29. size := int(binary.LittleEndian.Uint32(out[8:]))
  30. tokens := []string{}
  31. if returned > 0 {
  32. text, _ := wire.DecodeUTF16(out[12 : 12+size])
  33. for _, tok := range strings.Split(text, "\x00") {
  34. if tok != "" {
  35. tokens = append(tokens, tok)
  36. }
  37. }
  38. }
  39. return total, returned, tokens
  40. }
  41. func TestEnumerateSnapshots(t *testing.T) {
  42. h := versionHarness(t)
  43. c := newRawClient(t, h)
  44. id := mustCreate(t, c, "report.txt", openRead)
  45. //Windows first asks with room for the counts only
  46. r := c.call(wire.CmdIoctl, ioctlBody(id, wire.FsctlEnumerateSnapshots, 16))
  47. if r.hdr.Status != 0 {
  48. t.Fatalf("status %x", r.hdr.Status)
  49. }
  50. total, returned, _ := snapshotTokens(t, ioctlOutput(t, r))
  51. if total != 2 || returned != 0 {
  52. t.Errorf("small buffer: %d total, %d returned", total, returned)
  53. }
  54. r = c.call(wire.CmdIoctl, ioctlBody(id, wire.FsctlEnumerateSnapshots, 4096))
  55. total, returned, tokens := snapshotTokens(t, ioctlOutput(t, r))
  56. want := []string{"@GMT-2026.11.08-02.00.00", "@GMT-2026.11.07-01.30.15"}
  57. if total != 2 || returned != 2 || len(tokens) != 2 || tokens[0] != want[0] || tokens[1] != want[1] {
  58. t.Errorf("tokens %v (%d/%d), want newest first %v", tokens, returned, total, want)
  59. }
  60. plain := mustCreate(t, c, "other.txt", openRW)
  61. r = c.call(wire.CmdIoctl, ioctlBody(plain, wire.FsctlEnumerateSnapshots, 4096))
  62. if total, _, _ := snapshotTokens(t, ioctlOutput(t, r)); total != 0 {
  63. t.Errorf("a file without versions has %d", total)
  64. }
  65. }
  66. func timewarpCtx(at time.Time) createCtx {
  67. w := wire.NewWriter(8)
  68. w.U64(wire.Filetime(at))
  69. return createCtx{wire.ContextTimewarp, w.B}
  70. }
  71. func TestOpenAVersion(t *testing.T) {
  72. h := versionHarness(t)
  73. c := newRawClient(t, h)
  74. read := func(r rawReply) string {
  75. t.Helper()
  76. got := parseCreateReply(r)
  77. if got.status != 0 {
  78. t.Fatalf("open: %x", got.status)
  79. }
  80. rr := c.call(wire.CmdRead, readBodyOf(got.id, 0, 64))
  81. n := binary.LittleEndian.Uint32(rr.body[4:])
  82. return string(rr.body[16 : 16+n])
  83. }
  84. byContext := c.call(wire.CmdCreate, createWithContexts("report.txt", openRead, 0, []createCtx{timewarpCtx(versionTime1)}))
  85. if got := read(byContext); got != "older text" {
  86. t.Errorf("TWrp read %q", got)
  87. }
  88. byPath := c.call(wire.CmdCreate, createBody(`@GMT-2026.11.08-02.00.00\report.txt`, openRead))
  89. if got := read(byPath); got != "older text" {
  90. t.Errorf("@GMT path read %q", got)
  91. }
  92. //Versions cannot be changed, and unknown times do not exist
  93. write := c.call(wire.CmdCreate, createWithContexts("report.txt", openRW, 0, []createCtx{timewarpCtx(versionTime1)}))
  94. if st := write.hdr.Status; st == 0 {
  95. t.Error("a version opened for writing")
  96. }
  97. missing := c.call(wire.CmdCreate, createWithContexts("report.txt", openRead, 0, []createCtx{timewarpCtx(versionTime1.Add(time.Hour))}))
  98. if missing.hdr.Status != wire.StatusObjectNameNotFound {
  99. t.Errorf("unknown time: %x", missing.hdr.Status)
  100. }
  101. if got, _ := os.ReadFile(filepath.Join(h.share.root, "report.txt")); string(got) != "current text" {
  102. t.Errorf("the current file changed: %q", got)
  103. }
  104. }
  105. func TestGMTTokens(t *testing.T) {
  106. rest, at, ok := splitGMTToken("docs/@GMT-2026.11.08-02.00.00/report.txt")
  107. if !ok || rest != "docs/report.txt" || !at.Equal(versionTime1) {
  108. t.Errorf("%q %v %v", rest, at, ok)
  109. }
  110. for _, bad := range []string{"docs/report.txt", "@GMT-2026.11.08", "@GMT-2026.13.08-02.00.00"} {
  111. if _, _, ok := splitGMTToken(bad); ok {
  112. t.Errorf("%q parsed", bad)
  113. }
  114. }
  115. if out := snapshotArray(nil, 4096); len(out) != 16 || binary.LittleEndian.Uint32(out) != 0 {
  116. t.Errorf("no versions: %x", out)
  117. }
  118. }