mox/subjectpass/subjectpass_test.go

36 lines
1,011 B
Go
Raw Normal View History

2023-01-30 16:27:06 +03:00
package subjectpass
import (
"errors"
"fmt"
"strings"
"testing"
"time"
"github.com/mjl-/mox/mlog"
2023-01-30 16:27:06 +03:00
"github.com/mjl-/mox/smtp"
)
var xlog = mlog.New("subjectpass")
2023-01-30 16:27:06 +03:00
func TestSubjectPass(t *testing.T) {
key := []byte("secret token")
addr, _ := smtp.ParseAddress("mox@mox.example")
sig := Generate(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(xlog, strings.NewReader(message), key, time.Hour); err != nil {
2023-01-30 16:27:06 +03:00
t.Fatalf("verifyPassToken: %s", err)
}
if err := Verify(xlog, strings.NewReader(message), []byte("bad key"), time.Hour); err == nil {
2023-01-30 16:27:06 +03:00
t.Fatalf("verifyPassToken did not fail")
}
sig = Generate(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(xlog, strings.NewReader(message), key, time.Hour); !errors.Is(err, ErrExpired) {
2023-01-30 16:27:06 +03:00
t.Fatalf("verifyPassToken should have expired")
}
}