papero/fs/maildir_test.go

98 lines
2.4 KiB
Go
Raw Normal View History

2021-01-14 18:08:04 +01:00
package fs
import (
2021-01-17 20:07:40 +01:00
"fmt"
2021-01-18 21:14:19 +01:00
"strconv"
2021-01-14 18:08:04 +01:00
"testing"
"time"
2021-01-14 18:08:04 +01:00
"github.com/google/go-cmp/cmp"
)
func TestFlagsFromString(t *testing.T) {
flags, err := flagsFromString("RTDSF")
if err != nil {
t.Errorf("Flag parsing errored unexpectedly: %s\n", err)
}
expect := []Flag{FlagDraft, FlagFlagged, FlagAnswered, FlagSeen, FlagDeleted}
if !cmp.Equal(flags, expect) {
t.Errorf("Flags mismatch: %s", cmp.Diff(expect, flags))
}
_, err = flagsFromString("RDX")
if err != ErrUnknownFlag {
t.Error("Flag parsing should have errored")
}
_, err = flagsFromString("DDF")
if err != ErrMalformedName {
t.Error("Flag parsing should have errored")
}
}
2021-01-14 18:08:04 +01:00
func TestMailFileString(t *testing.T) {
clock := mockClock{}
2021-01-17 20:07:40 +01:00
timestamp := clock.Now()
progressive := 4
pid := 1312
hostname := "myplace"
name := "main_account" // known md5sum: f2cf513ad46d4d9b9684103e468803a0
uid := 33243
mf := &MailFile{timestamp: timestamp, progressive: progressive, pid: pid, hostname: hostname}
mf.SetFlags([]Flag{FlagSeen, FlagAnswered})
2021-01-17 20:07:40 +01:00
mf.SetMd5FromName(name)
mf.SetUid(uid)
2021-01-14 18:08:04 +01:00
2021-01-17 20:07:40 +01:00
expect := fmt.Sprintf(
"%d_%d.%d.%s,U=%d,FMD5=f2cf513ad46d4d9b9684103e468803a0:2,RS",
timestamp.Unix(),
progressive,
pid,
hostname,
uid)
result := fmt.Sprint(mf)
2021-01-14 18:08:04 +01:00
2021-01-17 20:07:40 +01:00
if expect != result {
t.Errorf("Mismatch\n\tExpect: %s\n\tResult: %s\n", expect, result)
t.Logf("diff: %s", cmp.Diff(expect, result))
2021-01-14 18:08:04 +01:00
}
}
func TestNewMailFile(t *testing.T) {
mf, err := NewMailFile("12345_0.1312.myplace,U=666,FMD5=f2cf513ad46d4d9b9684103e468803a0:2,")
if err != nil {
t.Errorf("Unexpected error: %s\n", err)
}
expect := &MailFile{
timestamp: time.Unix(12345, 0),
progressive: 0,
pid: 1312,
hostname: "myplace",
uid: 666,
md5: "f2cf513ad46d4d9b9684103e468803a0",
flags: []Flag{},
}
if !cmp.Equal(mf, expect, cmp.AllowUnexported(MailFile{})) {
t.Errorf("MailFile mismatch: %s", cmp.Diff(expect, mf, cmp.AllowUnexported(MailFile{})))
}
}
2021-01-18 21:14:19 +01:00
func TestNewMailFileErrs(t *testing.T) {
var err error
// The name has the wrong shape
_, err = NewMailFile("pippo")
if err != ErrMalformedName {
t.Error("Function should have errored")
}
// The shape is correct, but one of the integer field is instead a string
_, err = NewMailFile("aaaa_123.456.myplace,U=1312,FMD5=f2cf513ad46d4d9b9684103e468803a0:2,")
switch err.(type) {
case *strconv.NumError:
// Happy path
default:
t.Error("Unexpected error")
}
}