mirror of
https://github.com/mjl-/mox.git
synced 2024-12-25 16:03:48 +03:00
5b20cba50a
we don't want external software to include internal details like mlog. slog.Logger is/will be the standard. we still have mlog for its helper functions, and its handler that logs in concise logfmt used by mox. packages that are not meant for reuse still pass around mlog.Log for convenience. we use golang.org/x/exp/slog because we also support the previous Go toolchain version. with the next Go release, we'll switch to the builtin slog.
35 lines
1 KiB
Go
35 lines
1 KiB
Go
package subjectpass
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/mjl-/mox/mlog"
|
|
"github.com/mjl-/mox/smtp"
|
|
)
|
|
|
|
func TestSubjectPass(t *testing.T) {
|
|
log := mlog.New("subjectpass", nil)
|
|
|
|
key := []byte("secret token")
|
|
addr, _ := smtp.ParseAddress("mox@mox.example")
|
|
sig := Generate(log.Logger, addr, key, time.Now())
|
|
|
|
message := fmt.Sprintf("From: <mox@mox.example>\r\nSubject: let me in %s\r\n\r\nthe message", sig)
|
|
if err := Verify(log.Logger, strings.NewReader(message), key, time.Hour); err != nil {
|
|
t.Fatalf("verifyPassToken: %s", err)
|
|
}
|
|
|
|
if err := Verify(log.Logger, strings.NewReader(message), []byte("bad key"), time.Hour); err == nil {
|
|
t.Fatalf("verifyPassToken did not fail")
|
|
}
|
|
|
|
sig = Generate(log.Logger, addr, key, time.Now().Add(-time.Hour-257))
|
|
message = fmt.Sprintf("From: <mox@mox.example>\r\nSubject: let me in %s\r\n\r\nthe message", sig)
|
|
if err := Verify(log.Logger, strings.NewReader(message), key, time.Hour); !errors.Is(err, ErrExpired) {
|
|
t.Fatalf("verifyPassToken should have expired")
|
|
}
|
|
}
|