mirror of
https://github.com/hanzoai/mail.git
synced 2026-09-23 23:53:02 +00:00
Fork of github.com/mjl-/mox at 5656ca6d1b
(upstream v0.0.15 plus 55 commits).
Rename the module path in go.mod and the import paths in 268 Go files. Also
update the five scripts that "go install" the module (genwebsite.sh,
docker-release.sh, apidiff.sh, test-upgrade.sh, Dockerfile.release), which
would otherwise build upstream rather than this fork.
Add NOTICE recording the upstream project, author, license, and fork point,
and stating which license covers which files: MIT for the mox source and our
changes, MPL-2.0 for publicsuffix/public_suffix_list.txt alone, and the
vendored dependencies' own licenses under licenses/. Embed NOTICE so
"mail licenses" prints the lineage from the running binary. LICENSE.MIT,
LICENSE.MPLv2.0 and the licenses/ tree are preserved verbatim; no code is
relicensed.
The mox command name, config keys, data directory layout and website content
are unchanged. LLM.md records the fork point, the rename, what was left alone,
and the steps to stand the server up.
Co-authored-by: Hanzo Dev <[email protected]>
132 lines
3.9 KiB
Go
132 lines
3.9 KiB
Go
package store
|
|
|
|
import (
|
|
"archive/tar"
|
|
"archive/zip"
|
|
"bytes"
|
|
"io"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/hanzoai/mail/mlog"
|
|
"github.com/hanzoai/mail/mox-"
|
|
)
|
|
|
|
func TestExport(t *testing.T) {
|
|
// Set up an account, add 2 messages to different 2 mailboxes. export as tar/zip
|
|
// and maildir/mbox. check there are 2 files in the repo, no errors.txt.
|
|
|
|
log := mlog.New("export", nil)
|
|
|
|
os.RemoveAll("../testdata/store/data")
|
|
mox.ConfigStaticPath = filepath.FromSlash("../testdata/store/mox.conf")
|
|
mox.MustLoadConfig(true, false)
|
|
defer Switchboard()()
|
|
acc, err := OpenAccount(pkglog, "mjl", false)
|
|
tcheck(t, err, "open account")
|
|
defer func() {
|
|
err := acc.Close()
|
|
log.Check(err, "closing account")
|
|
acc.WaitClosed()
|
|
}()
|
|
|
|
msgFile, err := CreateMessageTemp(pkglog, "mox-test-export")
|
|
tcheck(t, err, "create temp")
|
|
defer os.Remove(msgFile.Name()) // To be sure.
|
|
defer msgFile.Close()
|
|
const msg = "test: test\r\n\r\ntest\r\n"
|
|
_, err = msgFile.Write([]byte(msg))
|
|
tcheck(t, err, "write message")
|
|
|
|
var m Message
|
|
acc.WithWLock(func() {
|
|
m = Message{Received: time.Now(), Size: int64(len(msg))}
|
|
err = acc.DeliverMailbox(pkglog, "Inbox", &m, msgFile)
|
|
tcheck(t, err, "deliver")
|
|
|
|
m = Message{Received: time.Now(), Size: int64(len(msg))}
|
|
err = acc.DeliverMailbox(pkglog, "Trash", &m, msgFile)
|
|
tcheck(t, err, "deliver")
|
|
})
|
|
|
|
var maildirZip, maildirTar, mboxZip, mboxTar bytes.Buffer
|
|
|
|
archive := func(archiver Archiver, mailbox string, messageIDs []int64, maildir bool) {
|
|
t.Helper()
|
|
err = ExportMessages(ctxbg, log, acc.DB, acc.Dir, archiver, maildir, mailbox, messageIDs, true)
|
|
tcheck(t, err, "export messages")
|
|
err = archiver.Close()
|
|
tcheck(t, err, "archiver close")
|
|
}
|
|
|
|
os.RemoveAll("../testdata/exportmaildir")
|
|
os.RemoveAll("../testdata/exportmbox")
|
|
|
|
archive(ZipArchiver{zip.NewWriter(&maildirZip)}, "", nil, true)
|
|
archive(ZipArchiver{zip.NewWriter(&mboxZip)}, "", nil, false)
|
|
archive(TarArchiver{tar.NewWriter(&maildirTar)}, "", nil, true)
|
|
archive(TarArchiver{tar.NewWriter(&mboxTar)}, "", nil, false)
|
|
archive(TarArchiver{tar.NewWriter(&mboxTar)}, "Inbox", nil, false)
|
|
archive(TarArchiver{tar.NewWriter(&mboxTar)}, "", []int64{m.ID}, false)
|
|
archive(DirArchiver{filepath.FromSlash("../testdata/exportmaildir")}, "", nil, true)
|
|
archive(DirArchiver{filepath.FromSlash("../testdata/exportmbox")}, "", nil, false)
|
|
|
|
const defaultMailboxes = 6 // Inbox, Drafts, etc
|
|
if r, err := zip.NewReader(bytes.NewReader(maildirZip.Bytes()), int64(maildirZip.Len())); err != nil {
|
|
t.Fatalf("reading maildir zip: %v", err)
|
|
} else if len(r.File) != defaultMailboxes*3+2 {
|
|
t.Fatalf("maildir zip, expected %d*3 dirs, and 2 files, got %d files", defaultMailboxes, len(r.File))
|
|
}
|
|
|
|
if r, err := zip.NewReader(bytes.NewReader(mboxZip.Bytes()), int64(mboxZip.Len())); err != nil {
|
|
t.Fatalf("reading mbox zip: %v", err)
|
|
} else if len(r.File) != defaultMailboxes {
|
|
t.Fatalf("maildir zip, expected %d files, got %d files", defaultMailboxes, len(r.File))
|
|
}
|
|
|
|
checkTarFiles := func(r io.Reader, n int) {
|
|
t.Helper()
|
|
tr := tar.NewReader(r)
|
|
have := 0
|
|
for {
|
|
h, err := tr.Next()
|
|
if err == io.EOF {
|
|
break
|
|
}
|
|
have++
|
|
if h.Name == "errors.txt" {
|
|
t.Fatalf("got errors.txt")
|
|
}
|
|
_, err = io.Copy(io.Discard, tr)
|
|
tcheck(t, err, "copy")
|
|
}
|
|
if have != n {
|
|
t.Fatalf("got %d files, expected %d", have, n)
|
|
}
|
|
}
|
|
|
|
checkTarFiles(&maildirTar, defaultMailboxes*3+2)
|
|
checkTarFiles(&mboxTar, defaultMailboxes)
|
|
|
|
checkDirFiles := func(dir string, n int) {
|
|
t.Helper()
|
|
have := 0
|
|
err := filepath.WalkDir(dir, func(path string, d fs.DirEntry, err error) error {
|
|
if err == nil && !d.IsDir() {
|
|
have++
|
|
}
|
|
return nil
|
|
})
|
|
tcheck(t, err, "walkdir")
|
|
if n != have {
|
|
t.Fatalf("got %d files, expected %d", have, n)
|
|
}
|
|
}
|
|
|
|
checkDirFiles(filepath.FromSlash("../testdata/exportmaildir"), 2)
|
|
checkDirFiles(filepath.FromSlash("../testdata/exportmbox"), defaultMailboxes)
|
|
}
|