2023-01-30 16:27:06 +03:00
|
|
|
//go:build integration
|
|
|
|
|
|
|
|
// Run this using docker-compose.yml, see Makefile.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/base64"
|
2023-02-17 21:30:30 +03:00
|
|
|
"errors"
|
2023-01-30 16:27:06 +03:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"os"
|
2023-02-17 21:30:30 +03:00
|
|
|
"path/filepath"
|
2023-01-30 16:27:06 +03:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2023-02-17 21:30:30 +03:00
|
|
|
bolt "go.etcd.io/bbolt"
|
|
|
|
|
2023-01-30 16:27:06 +03:00
|
|
|
"github.com/mjl-/bstore"
|
|
|
|
|
|
|
|
"github.com/mjl-/mox/mlog"
|
|
|
|
"github.com/mjl-/mox/mox-"
|
|
|
|
"github.com/mjl-/mox/smtpclient"
|
|
|
|
"github.com/mjl-/mox/store"
|
|
|
|
)
|
|
|
|
|
2023-05-22 15:40:36 +03:00
|
|
|
var ctxbg = context.Background()
|
|
|
|
|
2023-01-30 16:27:06 +03:00
|
|
|
func tcheck(t *testing.T, err error, msg string) {
|
|
|
|
t.Helper()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("%s: %s", msg, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Submit a message to mox, which sends it to postfix, which forwards back to mox.
|
|
|
|
// We check if we receive the message.
|
|
|
|
func TestDeliver(t *testing.T) {
|
|
|
|
mlog.Logfmt = true
|
|
|
|
|
|
|
|
// Remove state.
|
change mox to start as root, bind to network sockets, then drop to regular unprivileged mox user
makes it easier to run on bsd's, where you cannot (easily?) let non-root users
bind to ports <1024. starting as root also paves the way for future improvements
with privilege separation.
unfortunately, this requires changes to how you start mox. though mox will help
by automatically fix up dir/file permissions/ownership.
if you start mox from the systemd unit file, you should update it so it starts
as root and adds a few additional capabilities:
# first update the mox binary, then, as root:
./mox config printservice >mox.service
systemctl daemon-reload
systemctl restart mox
journalctl -f -u mox &
# you should see mox start up, with messages about fixing permissions on dirs/files.
if you used the recommended config/ and data/ directory, in a directory just for
mox, and with the mox user called "mox", this should be enough.
if you don't want mox to modify dir/file permissions, set "NoFixPermissions:
true" in mox.conf.
if you named the mox user something else than mox, e.g. "_mox", add "User: _mox"
to mox.conf.
if you created a shared service user as originally suggested, you may want to
get rid of that as it is no longer useful and may get in the way. e.g. if you
had /home/service/mox with a "service" user, that service user can no longer
access any files: only mox and root can.
this also adds scripts for building mox docker images for alpine-supported
platforms.
the "restart" subcommand has been removed. it wasn't all that useful and got in
the way.
and another change: when adding a domain while mtasts isn't enabled, don't add
the per-domain mtasts config, as it would cause failure to add the domain.
based on report from setting up mox on openbsd from mteege.
and based on issue #3. thanks for the feedback!
2023-02-27 14:19:55 +03:00
|
|
|
os.RemoveAll("testdata/integration/data")
|
|
|
|
os.MkdirAll("testdata/integration/data", 0750)
|
|
|
|
|
|
|
|
// Cleanup afterwards, these are owned by root, annoying to have around due to
|
|
|
|
// permission errors.
|
|
|
|
defer os.RemoveAll("testdata/integration/data")
|
2023-01-30 16:27:06 +03:00
|
|
|
|
|
|
|
// Load mox config.
|
change mox to start as root, bind to network sockets, then drop to regular unprivileged mox user
makes it easier to run on bsd's, where you cannot (easily?) let non-root users
bind to ports <1024. starting as root also paves the way for future improvements
with privilege separation.
unfortunately, this requires changes to how you start mox. though mox will help
by automatically fix up dir/file permissions/ownership.
if you start mox from the systemd unit file, you should update it so it starts
as root and adds a few additional capabilities:
# first update the mox binary, then, as root:
./mox config printservice >mox.service
systemctl daemon-reload
systemctl restart mox
journalctl -f -u mox &
# you should see mox start up, with messages about fixing permissions on dirs/files.
if you used the recommended config/ and data/ directory, in a directory just for
mox, and with the mox user called "mox", this should be enough.
if you don't want mox to modify dir/file permissions, set "NoFixPermissions:
true" in mox.conf.
if you named the mox user something else than mox, e.g. "_mox", add "User: _mox"
to mox.conf.
if you created a shared service user as originally suggested, you may want to
get rid of that as it is no longer useful and may get in the way. e.g. if you
had /home/service/mox with a "service" user, that service user can no longer
access any files: only mox and root can.
this also adds scripts for building mox docker images for alpine-supported
platforms.
the "restart" subcommand has been removed. it wasn't all that useful and got in
the way.
and another change: when adding a domain while mtasts isn't enabled, don't add
the per-domain mtasts config, as it would cause failure to add the domain.
based on report from setting up mox on openbsd from mteege.
and based on issue #3. thanks for the feedback!
2023-02-27 14:19:55 +03:00
|
|
|
mox.ConfigStaticPath = "testdata/integration/config/mox.conf"
|
2023-02-17 21:30:30 +03:00
|
|
|
filepath.Join(filepath.Dir(mox.ConfigStaticPath), "domains.conf")
|
2023-06-16 14:27:27 +03:00
|
|
|
if errs := mox.LoadConfig(ctxbg, true, false); len(errs) > 0 {
|
2023-01-30 16:27:06 +03:00
|
|
|
t.Fatalf("loading mox config: %v", errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create new accounts
|
|
|
|
createAccount := func(email, password string) {
|
|
|
|
t.Helper()
|
|
|
|
acc, _, err := store.OpenEmail(email)
|
|
|
|
tcheck(t, err, "open account")
|
|
|
|
err = acc.SetPassword(password)
|
|
|
|
tcheck(t, err, "setting password")
|
|
|
|
err = acc.Close()
|
|
|
|
tcheck(t, err, "closing account")
|
|
|
|
}
|
|
|
|
|
|
|
|
createAccount("moxtest1@mox1.example", "pass1234")
|
|
|
|
createAccount("moxtest2@mox2.example", "pass1234")
|
|
|
|
createAccount("moxtest3@mox3.example", "pass1234")
|
|
|
|
|
|
|
|
// Start mox.
|
change mox to start as root, bind to network sockets, then drop to regular unprivileged mox user
makes it easier to run on bsd's, where you cannot (easily?) let non-root users
bind to ports <1024. starting as root also paves the way for future improvements
with privilege separation.
unfortunately, this requires changes to how you start mox. though mox will help
by automatically fix up dir/file permissions/ownership.
if you start mox from the systemd unit file, you should update it so it starts
as root and adds a few additional capabilities:
# first update the mox binary, then, as root:
./mox config printservice >mox.service
systemctl daemon-reload
systemctl restart mox
journalctl -f -u mox &
# you should see mox start up, with messages about fixing permissions on dirs/files.
if you used the recommended config/ and data/ directory, in a directory just for
mox, and with the mox user called "mox", this should be enough.
if you don't want mox to modify dir/file permissions, set "NoFixPermissions:
true" in mox.conf.
if you named the mox user something else than mox, e.g. "_mox", add "User: _mox"
to mox.conf.
if you created a shared service user as originally suggested, you may want to
get rid of that as it is no longer useful and may get in the way. e.g. if you
had /home/service/mox with a "service" user, that service user can no longer
access any files: only mox and root can.
this also adds scripts for building mox docker images for alpine-supported
platforms.
the "restart" subcommand has been removed. it wasn't all that useful and got in
the way.
and another change: when adding a domain while mtasts isn't enabled, don't add
the per-domain mtasts config, as it would cause failure to add the domain.
based on report from setting up mox on openbsd from mteege.
and based on issue #3. thanks for the feedback!
2023-02-27 14:19:55 +03:00
|
|
|
const mtastsdbRefresher = false
|
|
|
|
const skipForkExec = true
|
|
|
|
err := start(mtastsdbRefresher, skipForkExec)
|
2023-01-30 16:27:06 +03:00
|
|
|
tcheck(t, err, "starting mox")
|
|
|
|
|
|
|
|
// todo: we should probably hook store.Comm to get updates.
|
|
|
|
latestMsgID := func(username string) int64 {
|
|
|
|
// We open the account index database created by mox for the test user. And we keep looking for the email we sent.
|
change mox to start as root, bind to network sockets, then drop to regular unprivileged mox user
makes it easier to run on bsd's, where you cannot (easily?) let non-root users
bind to ports <1024. starting as root also paves the way for future improvements
with privilege separation.
unfortunately, this requires changes to how you start mox. though mox will help
by automatically fix up dir/file permissions/ownership.
if you start mox from the systemd unit file, you should update it so it starts
as root and adds a few additional capabilities:
# first update the mox binary, then, as root:
./mox config printservice >mox.service
systemctl daemon-reload
systemctl restart mox
journalctl -f -u mox &
# you should see mox start up, with messages about fixing permissions on dirs/files.
if you used the recommended config/ and data/ directory, in a directory just for
mox, and with the mox user called "mox", this should be enough.
if you don't want mox to modify dir/file permissions, set "NoFixPermissions:
true" in mox.conf.
if you named the mox user something else than mox, e.g. "_mox", add "User: _mox"
to mox.conf.
if you created a shared service user as originally suggested, you may want to
get rid of that as it is no longer useful and may get in the way. e.g. if you
had /home/service/mox with a "service" user, that service user can no longer
access any files: only mox and root can.
this also adds scripts for building mox docker images for alpine-supported
platforms.
the "restart" subcommand has been removed. it wasn't all that useful and got in
the way.
and another change: when adding a domain while mtasts isn't enabled, don't add
the per-domain mtasts config, as it would cause failure to add the domain.
based on report from setting up mox on openbsd from mteege.
and based on issue #3. thanks for the feedback!
2023-02-27 14:19:55 +03:00
|
|
|
dbpath := fmt.Sprintf("testdata/integration/data/accounts/%s/index.db", username)
|
add a "backup" subcommand to make consistent backups, and a "verifydata" subcommand to verify a backup before restoring, and add tests for future upgrades
the backup command will make consistent snapshots of all the database files. i
had been copying the db files before, and it usually works. but if the file is
modified during the backup, it is inconsistent and is likely to generate errors
when reading (can be at any moment in the future, when reading some db page).
"mox backup" opens the database file and writes out a copy in a transaction.
it also duplicates the message files.
before doing a restore, you could run "mox verifydata" on the to-be-restored
"data" directory. it check the database files, and compares the message files
with the database.
the new "gentestdata" subcommand generates a basic "data" directory, with a
queue and a few accounts. we will use it in the future along with "verifydata"
to test upgrades from old version to the latest version. both when going to the
next version, and when skipping several versions. the script test-upgrades.sh
executes these tests and doesn't do anything at the moment, because no releases
have this subcommand yet.
inspired by a failed upgrade attempt of a pre-release version.
2023-05-26 20:26:51 +03:00
|
|
|
db, err := bstore.Open(ctxbg, dbpath, &bstore.Options{Timeout: 3 * time.Second}, store.DBTypes...)
|
2023-02-17 21:30:30 +03:00
|
|
|
if err != nil && errors.Is(err, bolt.ErrTimeout) {
|
|
|
|
log.Printf("db open timeout (normal delay for new sender with account and db file kept open)")
|
|
|
|
return 0
|
|
|
|
}
|
2023-01-30 16:27:06 +03:00
|
|
|
tcheck(t, err, "open test account database")
|
|
|
|
defer db.Close()
|
|
|
|
|
2023-05-22 15:40:36 +03:00
|
|
|
q := bstore.QueryDB[store.Mailbox](ctxbg, db)
|
2023-01-30 16:27:06 +03:00
|
|
|
q.FilterNonzero(store.Mailbox{Name: "Inbox"})
|
|
|
|
inbox, err := q.Get()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("inbox for finding latest message id: %v", err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2023-05-22 15:40:36 +03:00
|
|
|
qm := bstore.QueryDB[store.Message](ctxbg, db)
|
2023-01-30 16:27:06 +03:00
|
|
|
qm.FilterNonzero(store.Message{MailboxID: inbox.ID})
|
|
|
|
qm.SortDesc("ID")
|
|
|
|
qm.Limit(1)
|
|
|
|
m, err := qm.Get()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("finding latest message id: %v", err)
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return m.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
waitForMsg := func(prevMsgID int64, username string) int64 {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
msgID := latestMsgID(username)
|
|
|
|
if msgID > prevMsgID {
|
|
|
|
return msgID
|
|
|
|
}
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
}
|
|
|
|
t.Fatalf("timeout waiting for message")
|
|
|
|
return 0 // not reached
|
|
|
|
}
|
|
|
|
|
|
|
|
deliver := func(username, desthost, mailfrom, password, rcptto string) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
prevMsgID := latestMsgID(username)
|
|
|
|
|
|
|
|
conn, err := net.Dial("tcp", desthost+":587")
|
|
|
|
tcheck(t, err, "dial submission")
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
// todo: this is "aware" (hopefully) of the config smtpclient/client.go sets up... tricky
|
|
|
|
mox.Conf.Static.HostnameDomain.ASCII = desthost
|
|
|
|
msg := fmt.Sprintf(`From: <%s>
|
|
|
|
To: <%s>
|
|
|
|
Subject: test message
|
|
|
|
|
|
|
|
This is the message.
|
|
|
|
`, mailfrom, rcptto)
|
|
|
|
msg = strings.ReplaceAll(msg, "\n", "\r\n")
|
|
|
|
auth := bytes.Join([][]byte{nil, []byte(mailfrom), []byte(password)}, []byte{0})
|
|
|
|
authLine := fmt.Sprintf("AUTH PLAIN %s", base64.StdEncoding.EncodeToString(auth))
|
|
|
|
c, err := smtpclient.New(mox.Context, mlog.New("test"), conn, smtpclient.TLSOpportunistic, desthost, authLine)
|
|
|
|
tcheck(t, err, "smtp hello")
|
|
|
|
err = c.Deliver(mox.Context, mailfrom, rcptto, int64(len(msg)), strings.NewReader(msg), false, false)
|
|
|
|
tcheck(t, err, "deliver with smtp")
|
|
|
|
err = c.Close()
|
|
|
|
tcheck(t, err, "close smtpclient")
|
|
|
|
|
|
|
|
waitForMsg(prevMsgID, username)
|
|
|
|
}
|
|
|
|
|
|
|
|
deliver("moxtest1", "moxmail1.mox1.example", "moxtest1@mox1.example", "pass1234", "root@postfix.example")
|
|
|
|
deliver("moxtest3", "moxmail2.mox2.example", "moxtest2@mox2.example", "pass1234", "moxtest3@mox3.example")
|
|
|
|
}
|