maildir_test.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package fs
  2. import (
  3. "fmt"
  4. "strconv"
  5. "testing"
  6. "time"
  7. "github.com/google/go-cmp/cmp"
  8. )
  9. func TestFlagsFromString(t *testing.T) {
  10. flags, err := flagsFromString("RTDSF")
  11. if err != nil {
  12. t.Errorf("Flag parsing errored unexpectedly: %s\n", err)
  13. }
  14. expect := []Flag{FlagDraft, FlagFlagged, FlagAnswered, FlagSeen, FlagDeleted}
  15. if !cmp.Equal(flags, expect) {
  16. t.Errorf("Flags mismatch: %s", cmp.Diff(expect, flags))
  17. }
  18. _, err = flagsFromString("RDX")
  19. if err != ErrUnknownFlag {
  20. t.Error("Flag parsing should have errored")
  21. }
  22. _, err = flagsFromString("DDF")
  23. if err != ErrMalformedName {
  24. t.Error("Flag parsing should have errored")
  25. }
  26. }
  27. func TestMailFileString(t *testing.T) {
  28. clock := mockClock{}
  29. timestamp := clock.Now()
  30. progressive := 4
  31. pid := 1312
  32. hostname := "myplace"
  33. name := "main_account" // known md5sum: f2cf513ad46d4d9b9684103e468803a0
  34. uid := 33243
  35. mf := &MailFile{timestamp: timestamp, progressive: progressive, pid: pid, hostname: hostname}
  36. mf.SetFlags([]Flag{FlagSeen, FlagAnswered})
  37. mf.SetMd5FromName(name)
  38. mf.SetUid(uid)
  39. expect := fmt.Sprintf(
  40. "%d_%d.%d.%s,U=%d,FMD5=f2cf513ad46d4d9b9684103e468803a0:2,RS",
  41. timestamp.Unix(),
  42. progressive,
  43. pid,
  44. hostname,
  45. uid)
  46. result := fmt.Sprint(mf)
  47. if expect != result {
  48. t.Errorf("Mismatch\n\tExpect: %s\n\tResult: %s\n", expect, result)
  49. t.Logf("diff: %s", cmp.Diff(expect, result))
  50. }
  51. }
  52. func TestNewMailFile(t *testing.T) {
  53. mf, err := NewMailFile("12345_0.1312.myplace,U=666,FMD5=f2cf513ad46d4d9b9684103e468803a0:2,")
  54. if err != nil {
  55. t.Errorf("Unexpected error: %s\n", err)
  56. }
  57. expect := &MailFile{
  58. timestamp: time.Unix(12345, 0),
  59. progressive: 0,
  60. pid: 1312,
  61. hostname: "myplace",
  62. uid: 666,
  63. md5: "f2cf513ad46d4d9b9684103e468803a0",
  64. flags: []Flag{},
  65. }
  66. if !cmp.Equal(mf, expect, cmp.AllowUnexported(MailFile{})) {
  67. t.Errorf("MailFile mismatch: %s", cmp.Diff(expect, mf, cmp.AllowUnexported(MailFile{})))
  68. }
  69. }
  70. func TestNewMailFileErrs(t *testing.T) {
  71. var err error
  72. // The name has the wrong shape
  73. _, err = NewMailFile("pippo")
  74. if err != ErrMalformedName {
  75. t.Error("Function should have errored")
  76. }
  77. // The shape is correct, but one of the integer field is instead a string
  78. _, err = NewMailFile("aaaa_123.456.myplace,U=1312,FMD5=f2cf513ad46d4d9b9684103e468803a0:2,")
  79. switch err.(type) {
  80. case *strconv.NumError:
  81. // Happy path
  82. default:
  83. t.Error("Unexpected error")
  84. }
  85. }