mirror of
https://blitiri.com.ar/repos/chasquid
synced 2026-10-29 15:02:30 +00:00
Apply go fix to simplify using modern features
This patch contains the result of running `go fix ./...`, which applies simplifications that make use of modern language features. There are no logic or behaviour changes.
This commit is contained in:
@@ -113,13 +113,13 @@ func main() {
|
|||||||
|
|
||||||
// Fatalf prints the given message to stderr, then exits the program with an
|
// Fatalf prints the given message to stderr, then exits the program with an
|
||||||
// error code.
|
// error code.
|
||||||
func Fatalf(s string, arg ...interface{}) {
|
func Fatalf(s string, arg ...any) {
|
||||||
fmt.Fprintf(os.Stderr, s+"\n", arg...)
|
fmt.Fprintf(os.Stderr, s+"\n", arg...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warnf prints the given message to stderr, but does not exit the program.
|
// Warnf prints the given message to stderr, but does not exit the program.
|
||||||
func Warnf(s string, arg ...interface{}) {
|
func Warnf(s string, arg ...any) {
|
||||||
fmt.Fprintf(os.Stderr, s+"\n", arg...)
|
fmt.Fprintf(os.Stderr, s+"\n", arg...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -54,7 +54,7 @@ func dkimSign() {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
if _, verbose := args["-v"]; verbose {
|
if _, verbose := args["-v"]; verbose {
|
||||||
ctx = dkim.WithTraceFunc(ctx,
|
ctx = dkim.WithTraceFunc(ctx,
|
||||||
func(format string, args ...interface{}) {
|
func(format string, args ...any) {
|
||||||
fmt.Fprintf(os.Stderr, format+"\n", args...)
|
fmt.Fprintf(os.Stderr, format+"\n", args...)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -77,7 +77,7 @@ func dkimVerify() {
|
|||||||
ctx := context.Background()
|
ctx := context.Background()
|
||||||
if _, verbose := args["-v"]; verbose {
|
if _, verbose := args["-v"]; verbose {
|
||||||
ctx = dkim.WithTraceFunc(ctx,
|
ctx = dkim.WithTraceFunc(ctx,
|
||||||
func(format string, args ...interface{}) {
|
func(format string, args ...any) {
|
||||||
fmt.Fprintf(os.Stderr, format+"\n", args...)
|
fmt.Fprintf(os.Stderr, format+"\n", args...)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,6 @@
|
|||||||
// See the usage below for details.
|
// See the usage below for details.
|
||||||
//
|
//
|
||||||
//go:build !coverage
|
//go:build !coverage
|
||||||
// +build !coverage
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
@@ -51,13 +50,13 @@ Flags:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Exit with EX_TEMPFAIL.
|
// Exit with EX_TEMPFAIL.
|
||||||
func tempExit(format string, args ...interface{}) {
|
func tempExit(format string, args ...any) {
|
||||||
fmt.Printf(format+"\n", args...)
|
fmt.Printf(format+"\n", args...)
|
||||||
// 75 = EX_TEMPFAIL "temporary failure" exit code (sysexits.h).
|
// 75 = EX_TEMPFAIL "temporary failure" exit code (sysexits.h).
|
||||||
os.Exit(75)
|
os.Exit(75)
|
||||||
}
|
}
|
||||||
|
|
||||||
func permExit(format string, args ...interface{}) {
|
func permExit(format string, args ...any) {
|
||||||
fmt.Printf(format+"\n", args...)
|
fmt.Printf(format+"\n", args...)
|
||||||
os.Exit(2)
|
os.Exit(2)
|
||||||
}
|
}
|
||||||
@@ -142,7 +141,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// cmd sends a command and checks it matched the expected code.
|
// cmd sends a command and checks it matched the expected code.
|
||||||
func cmd(conn *textproto.Conn, expectCode int, format string, args ...interface{}) {
|
func cmd(conn *textproto.Conn, expectCode int, format string, args ...any) {
|
||||||
id, err := conn.Cmd(format, args...)
|
id, err := conn.Cmd(format, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
tempExit("Sent %q, got %v", fmt.Sprintf(format, args...), err)
|
tempExit("Sent %q, got %v", fmt.Sprintf(format, args...), err)
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
// smtp-check is a command-line too for checking SMTP setups.
|
// smtp-check is a command-line too for checking SMTP setups.
|
||||||
//
|
//
|
||||||
//go:build !coverage
|
//go:build !coverage
|
||||||
// +build !coverage
|
|
||||||
|
|
||||||
package main
|
package main
|
||||||
|
|
||||||
|
|||||||
@@ -60,6 +60,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"maps"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -384,9 +385,7 @@ func (v *Resolver) AddAliasesFile(domain, path string) (int, error) {
|
|||||||
|
|
||||||
// Add the aliases to the resolver, overriding any previous values.
|
// Add the aliases to the resolver, overriding any previous values.
|
||||||
v.mu.Lock()
|
v.mu.Lock()
|
||||||
for addr, rs := range aliases {
|
maps.Copy(v.aliases, aliases)
|
||||||
v.aliases[addr] = rs
|
|
||||||
}
|
|
||||||
v.mu.Unlock()
|
v.mu.Unlock()
|
||||||
|
|
||||||
return len(aliases), nil
|
return len(aliases), nil
|
||||||
@@ -413,9 +412,7 @@ func (v *Resolver) Reload() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Add the aliases to the resolver, overriding any previous values.
|
// Add the aliases to the resolver, overriding any previous values.
|
||||||
for addr, rs := range aliases {
|
maps.Copy(newAliases, aliases)
|
||||||
newAliases[addr] = rs
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -500,7 +497,7 @@ func parseRHS(rawalias, domain string) ([]Recipient, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
rs := []Recipient{}
|
rs := []Recipient{}
|
||||||
for _, a := range strings.Split(rawalias, ",") {
|
for a := range strings.SplitSeq(rawalias, ",") {
|
||||||
a = strings.TrimSpace(a)
|
a = strings.TrimSpace(a)
|
||||||
if a == "" {
|
if a == "" {
|
||||||
// Ignore empty aliases. This is out of convenience, so we allow
|
// Ignore empty aliases. This is out of convenience, so we allow
|
||||||
@@ -558,7 +555,7 @@ func parseForward(rawalias string) (string, []string, error) {
|
|||||||
// The via part is a list of servers, separated by "/". Split it up.
|
// The via part is a list of servers, separated by "/". Split it up.
|
||||||
// For now we don't validate the servers, but we may in the future.
|
// For now we don't validate the servers, but we may in the future.
|
||||||
via := []string{}
|
via := []string{}
|
||||||
for _, v := range strings.Split(viaS, "/") {
|
for v := range strings.SplitSeq(viaS, "/") {
|
||||||
server := strings.TrimSpace(v)
|
server := strings.TrimSpace(v)
|
||||||
if server == "" {
|
if server == "" {
|
||||||
return "", nil, fmt.Errorf("empty server in via list")
|
return "", nil, fmt.Errorf("empty server in via list")
|
||||||
@@ -572,7 +569,7 @@ func parseForward(rawalias string) (string, []string, error) {
|
|||||||
// removeAllAfter removes everything from s that comes after the separators,
|
// removeAllAfter removes everything from s that comes after the separators,
|
||||||
// including them.
|
// including them.
|
||||||
func removeAllAfter(s, seps string) string {
|
func removeAllAfter(s, seps string) string {
|
||||||
for _, c := range strings.Split(seps, "") {
|
for c := range strings.SplitSeq(seps, "") {
|
||||||
if c == "" {
|
if c == "" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
@@ -590,7 +587,7 @@ func removeAllAfter(s, seps string) string {
|
|||||||
|
|
||||||
// removeChars removes the runes in "chars" from s.
|
// removeChars removes the runes in "chars" from s.
|
||||||
func removeChars(s, chars string) string {
|
func removeChars(s, chars string) string {
|
||||||
for _, c := range strings.Split(chars, "") {
|
for c := range strings.SplitSeq(chars, "") {
|
||||||
s = strings.Replace(s, c, "", -1)
|
s = strings.Replace(s, c, "", -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -650,7 +647,7 @@ func (e *parseError) Error() string {
|
|||||||
return fmt.Sprintf("line %d: %v", e.line, e.err)
|
return fmt.Sprintf("line %d: %v", e.line, e.err)
|
||||||
}
|
}
|
||||||
|
|
||||||
func newParseError(line int, f string, args ...interface{}) error {
|
func newParseError(line int, f string, args ...any) error {
|
||||||
return &parseError{
|
return &parseError{
|
||||||
line: line,
|
line: line,
|
||||||
err: fmt.Errorf(f, args...),
|
err: fmt.Errorf(f, args...),
|
||||||
|
|||||||
@@ -27,8 +27,8 @@ var defaultConfig = &Config{
|
|||||||
|
|
||||||
DataDir: "/var/lib/chasquid",
|
DataDir: "/var/lib/chasquid",
|
||||||
|
|
||||||
SuffixSeparators: proto.String("+"),
|
SuffixSeparators: new("+"),
|
||||||
DropCharacters: proto.String("."),
|
DropCharacters: new("."),
|
||||||
|
|
||||||
MailLogPath: "<syslog>",
|
MailLogPath: "<syslog>",
|
||||||
|
|
||||||
|
|||||||
@@ -86,8 +86,8 @@ func TestFullConfig(t *testing.T) {
|
|||||||
|
|
||||||
DataDir: "/var/lib/chasquid",
|
DataDir: "/var/lib/chasquid",
|
||||||
|
|
||||||
SuffixSeparators: proto.String(""),
|
SuffixSeparators: new(""),
|
||||||
DropCharacters: proto.String(""),
|
DropCharacters: new(""),
|
||||||
|
|
||||||
MailLogPath: "<syslog>",
|
MailLogPath: "<syslog>",
|
||||||
|
|
||||||
|
|||||||
@@ -9,7 +9,7 @@ type contextKey string
|
|||||||
|
|
||||||
const traceKey contextKey = "trace"
|
const traceKey contextKey = "trace"
|
||||||
|
|
||||||
func trace(ctx context.Context, f string, args ...interface{}) {
|
func trace(ctx context.Context, f string, args ...any) {
|
||||||
traceFunc, ok := ctx.Value(traceKey).(TraceFunc)
|
traceFunc, ok := ctx.Value(traceKey).(TraceFunc)
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
@@ -17,7 +17,7 @@ func trace(ctx context.Context, f string, args ...interface{}) {
|
|||||||
traceFunc(f, args...)
|
traceFunc(f, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
type TraceFunc func(f string, a ...interface{})
|
type TraceFunc func(f string, a ...any)
|
||||||
|
|
||||||
func WithTraceFunc(ctx context.Context, trace TraceFunc) context.Context {
|
func WithTraceFunc(ctx context.Context, trace TraceFunc) context.Context {
|
||||||
return context.WithValue(ctx, traceKey, trace)
|
return context.WithValue(ctx, traceKey, trace)
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ func TestTraceNoCtx(t *testing.T) {
|
|||||||
|
|
||||||
func TestTrace(t *testing.T) {
|
func TestTrace(t *testing.T) {
|
||||||
s := ""
|
s := ""
|
||||||
traceF := func(f string, a ...interface{}) {
|
traceF := func(f string, a ...any) {
|
||||||
s = fmt.Sprintf(f, a...)
|
s = fmt.Sprintf(f, a...)
|
||||||
}
|
}
|
||||||
ctx := WithTraceFunc(context.Background(), traceF)
|
ctx := WithTraceFunc(context.Background(), traceF)
|
||||||
|
|||||||
@@ -104,8 +104,8 @@ func parsePublicKey(v string) (*publicKey, error) {
|
|||||||
|
|
||||||
// h is a colon-separated list of hashing algorithm names.
|
// h is a colon-separated list of hashing algorithm names.
|
||||||
if tags["h"] != "" {
|
if tags["h"] != "" {
|
||||||
hs := strings.Split(eatWhitespace.Replace(tags["h"]), ":")
|
hs := strings.SplitSeq(eatWhitespace.Replace(tags["h"]), ":")
|
||||||
for _, h := range hs {
|
for h := range hs {
|
||||||
x, err := hashFromString(h)
|
x, err := hashFromString(h)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Unrecognized algorithms must be ignored.
|
// Unrecognized algorithms must be ignored.
|
||||||
|
|||||||
@@ -113,7 +113,7 @@ func loadDNS(t *testing.T, ctx context.Context, path string) context.Context {
|
|||||||
// Unfold \-terminated lines.
|
// Unfold \-terminated lines.
|
||||||
c = strings.ReplaceAll(c, "\\\n", "")
|
c = strings.ReplaceAll(c, "\\\n", "")
|
||||||
|
|
||||||
for _, line := range strings.Split(c, "\n") {
|
for line := range strings.SplitSeq(c, "\n") {
|
||||||
if line == "" || strings.HasPrefix(line, "#") {
|
if line == "" || strings.HasPrefix(line, "#") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -312,7 +312,7 @@ func parseTags(s string) (tags, error) {
|
|||||||
s = strings.TrimSuffix(s, ";")
|
s = strings.TrimSuffix(s, ";")
|
||||||
|
|
||||||
tags := make(tags)
|
tags := make(tags)
|
||||||
for _, tv := range strings.Split(s, ";") {
|
for tv := range strings.SplitSeq(s, ";") {
|
||||||
t, v, found := strings.Cut(tv, "=")
|
t, v, found := strings.Cut(tv, "=")
|
||||||
if !found {
|
if !found {
|
||||||
return nil, fmt.Errorf("%w: missing '='", errInvalidTag)
|
return nil, fmt.Errorf("%w: missing '='", errInvalidTag)
|
||||||
|
|||||||
@@ -56,8 +56,7 @@ func TestBadServer(t *testing.T) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatal("expected error")
|
t.Fatal("expected error")
|
||||||
}
|
}
|
||||||
var protoErr textproto.ProtocolError
|
if _, ok := errors.AsType[textproto.ProtocolError](err); !ok {
|
||||||
if !errors.As(err, &protoErr) {
|
|
||||||
t.Errorf("wanted textproto.ProtocolError, got: %v (%T)", err, err)
|
t.Errorf("wanted textproto.ProtocolError, got: %v (%T)", err, err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -114,7 +114,7 @@ func TestEndToEnd(t *testing.T) {
|
|||||||
|
|
||||||
func waitForServer(t *testing.T, path string) {
|
func waitForServer(t *testing.T, path string) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
for i := 0; i < 100; i++ {
|
for range 100 {
|
||||||
time.Sleep(10 * time.Millisecond)
|
time.Sleep(10 * time.Millisecond)
|
||||||
conn, err := net.Dial("unix", path)
|
conn, err := net.Dial("unix", path)
|
||||||
if conn != nil {
|
if conn != nil {
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ func NewSyslog() (*Logger, error) {
|
|||||||
return &Logger{inner: inner}, nil
|
return &Logger{inner: inner}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (l *Logger) printf(format string, args ...interface{}) {
|
func (l *Logger) printf(format string, args ...any) {
|
||||||
err := l.inner.Log(log.Info, 2, format, args...)
|
err := l.inner.Log(log.Info, 2, format, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.once.Do(func() {
|
l.once.Do(func() {
|
||||||
|
|||||||
@@ -9,9 +9,9 @@ import (
|
|||||||
func runBench(b *testing.B, events int) {
|
func runBench(b *testing.B, events int) {
|
||||||
nTraces := (b.N + events + 1) / events
|
nTraces := (b.N + events + 1) / events
|
||||||
|
|
||||||
for i := 0; i < nTraces; i++ {
|
for range nTraces {
|
||||||
tr := New("bench", "test")
|
tr := New("bench", "test")
|
||||||
for j := 0; j < events; j++ {
|
for j := range events {
|
||||||
tr.Printf("%d", j)
|
tr.Printf("%d", j)
|
||||||
}
|
}
|
||||||
tr.Finish()
|
tr.Finish()
|
||||||
|
|||||||
@@ -53,7 +53,7 @@ func (h *histogram) Snapshot() *histSnapshot {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var cumCount uint64
|
var cumCount uint64
|
||||||
for i := 0; i < nBuckets; i++ {
|
for i := range nBuckets {
|
||||||
cumCount += h.count[i]
|
cumCount += h.count[i]
|
||||||
l := line{
|
l := line{
|
||||||
Start: buckets[i],
|
Start: buckets[i],
|
||||||
|
|||||||
@@ -141,7 +141,7 @@ func TestHTTP(t *testing.T) {
|
|||||||
func TestHTTPLong(t *testing.T) {
|
func TestHTTPLong(t *testing.T) {
|
||||||
// Test a long trace.
|
// Test a long trace.
|
||||||
tr := New("TestHTTPLong", "verbose")
|
tr := New("TestHTTPLong", "verbose")
|
||||||
for i := 0; i < 1000; i++ {
|
for i := range 1000 {
|
||||||
tr.Printf("entry #%d", i)
|
tr.Printf("entry #%d", i)
|
||||||
}
|
}
|
||||||
tr.Finish()
|
tr.Finish()
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ package nettrace
|
|||||||
import (
|
import (
|
||||||
"container/ring"
|
"container/ring"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"maps"
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -51,11 +52,11 @@ type Trace interface {
|
|||||||
SetError()
|
SetError()
|
||||||
|
|
||||||
// Printf adds a message to the trace.
|
// Printf adds a message to the trace.
|
||||||
Printf(format string, a ...interface{})
|
Printf(format string, a ...any)
|
||||||
|
|
||||||
// Errorf adds a message to the trace, marks it as an error, and returns
|
// Errorf adds a message to the trace, marks it as an error, and returns
|
||||||
// an error for it.
|
// an error for it.
|
||||||
Errorf(format string, a ...interface{}) error
|
Errorf(format string, a ...any) error
|
||||||
|
|
||||||
// Finish marks the trace as complete.
|
// Finish marks the trace as complete.
|
||||||
// The trace should not be used after calling this method.
|
// The trace should not be used after calling this method.
|
||||||
@@ -220,7 +221,7 @@ func (tr *trace) SetError() {
|
|||||||
tr.mu.Unlock()
|
tr.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tr *trace) Printf(format string, a ...interface{}) {
|
func (tr *trace) Printf(format string, a ...any) {
|
||||||
evt := &event{
|
evt := &event{
|
||||||
When: time.Now(),
|
When: time.Now(),
|
||||||
Type: evtLOG,
|
Type: evtLOG,
|
||||||
@@ -230,7 +231,7 @@ func (tr *trace) Printf(format string, a ...interface{}) {
|
|||||||
tr.append(evt)
|
tr.append(evt)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (tr *trace) Errorf(format string, a ...interface{}) error {
|
func (tr *trace) Errorf(format string, a ...any) error {
|
||||||
tr.SetError()
|
tr.SetError()
|
||||||
err := fmt.Errorf(format, a...)
|
err := fmt.Errorf(format, a...)
|
||||||
tr.Printf("%s", err.Error())
|
tr.Printf("%s", err.Error())
|
||||||
@@ -354,7 +355,7 @@ func (r *traceRing) Len() int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (r *traceRing) Do(f func(tr *trace)) {
|
func (r *traceRing) Do(f func(tr *trace)) {
|
||||||
r.ring.Do(func(x interface{}) {
|
r.ring.Do(func(x any) {
|
||||||
if x == nil {
|
if x == nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -382,7 +383,7 @@ type familyTraces struct {
|
|||||||
func newFamilyTraces() *familyTraces {
|
func newFamilyTraces() *familyTraces {
|
||||||
ft := &familyTraces{}
|
ft := &familyTraces{}
|
||||||
ft.active = map[id]*trace{}
|
ft.active = map[id]*trace{}
|
||||||
for i := 0; i < nBuckets; i++ {
|
for i := range nBuckets {
|
||||||
ft.finished[i] = newTraceRing(tracesInBucket)
|
ft.finished[i] = newTraceRing(tracesInBucket)
|
||||||
}
|
}
|
||||||
ft.errors = newTraceRing(tracesInBucket)
|
ft.errors = newTraceRing(tracesInBucket)
|
||||||
@@ -512,9 +513,7 @@ func copyFamilies() map[string]*familyTraces {
|
|||||||
n := map[string]*familyTraces{}
|
n := map[string]*familyTraces{}
|
||||||
|
|
||||||
familiesMu.Lock()
|
familiesMu.Lock()
|
||||||
for f, trs := range families {
|
maps.Copy(n, families)
|
||||||
n[f] = trs
|
|
||||||
}
|
|
||||||
familiesMu.Unlock()
|
familiesMu.Unlock()
|
||||||
|
|
||||||
return n
|
return n
|
||||||
|
|||||||
@@ -37,13 +37,13 @@ func TestLong(t *testing.T) {
|
|||||||
tr.SetMaxEvents(100)
|
tr.SetMaxEvents(100)
|
||||||
|
|
||||||
// First 90 events, no drop.
|
// First 90 events, no drop.
|
||||||
for i := 0; i < 90; i++ {
|
for i := range 90 {
|
||||||
tr.Printf("evt %d", i)
|
tr.Printf("evt %d", i)
|
||||||
}
|
}
|
||||||
expectEvents(t, tr, 90)
|
expectEvents(t, tr, 90)
|
||||||
|
|
||||||
// Up to 99, still no drop.
|
// Up to 99, still no drop.
|
||||||
for i := 0; i < 9; i++ {
|
for i := range 9 {
|
||||||
tr.Printf("evt %d", i)
|
tr.Printf("evt %d", i)
|
||||||
}
|
}
|
||||||
expectEvents(t, tr, 99)
|
expectEvents(t, tr, 99)
|
||||||
@@ -57,7 +57,7 @@ func TestLong(t *testing.T) {
|
|||||||
expectEvents(t, tr, 101)
|
expectEvents(t, tr, 101)
|
||||||
|
|
||||||
// Add more events, expect none of them to exceed 101.
|
// Add more events, expect none of them to exceed 101.
|
||||||
for i := 0; i < 9; i++ {
|
for i := range 9 {
|
||||||
tr.Printf("evt %d", i)
|
tr.Printf("evt %d", i)
|
||||||
expectEvents(t, tr, 101)
|
expectEvents(t, tr, 101)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -192,7 +192,7 @@ func FuzzDomainToUnicode(f *testing.F) {
|
|||||||
func BenchmarkToCRLF(b *testing.B) {
|
func BenchmarkToCRLF(b *testing.B) {
|
||||||
// Generate a 1000-line message.
|
// Generate a 1000-line message.
|
||||||
bb := bytes.Buffer{}
|
bb := bytes.Buffer{}
|
||||||
for i := 0; i < 1000; i++ {
|
for range 1000 {
|
||||||
bb.WriteString("this is a very pretty line 🐅\n")
|
bb.WriteString("this is a very pretty line 🐅\n")
|
||||||
}
|
}
|
||||||
buf := bb.Bytes()
|
buf := bb.Bytes()
|
||||||
@@ -206,7 +206,7 @@ func BenchmarkToCRLF(b *testing.B) {
|
|||||||
func BenchmarkStringToCRLF(b *testing.B) {
|
func BenchmarkStringToCRLF(b *testing.B) {
|
||||||
// Generate a 1000-line message.
|
// Generate a 1000-line message.
|
||||||
sb := strings.Builder{}
|
sb := strings.Builder{}
|
||||||
for i := 0; i < 1000; i++ {
|
for range 1000 {
|
||||||
sb.WriteString("this is a very pretty line 🐅\n")
|
sb.WriteString("this is a very pretty line 🐅\n")
|
||||||
}
|
}
|
||||||
s := sb.String()
|
s := sb.String()
|
||||||
|
|||||||
@@ -168,14 +168,14 @@ func printDiff(print func(s string), expected, got string) {
|
|||||||
|
|
||||||
// expected lines and map.
|
// expected lines and map.
|
||||||
eM := map[string]int{}
|
eM := map[string]int{}
|
||||||
for _, l := range strings.Split(expected, "\n") {
|
for l := range strings.SplitSeq(expected, "\n") {
|
||||||
eM[l]++
|
eM[l]++
|
||||||
lines = append(lines, l)
|
lines = append(lines, l)
|
||||||
}
|
}
|
||||||
|
|
||||||
// got lines and map.
|
// got lines and map.
|
||||||
gM := map[string]int{}
|
gM := map[string]int{}
|
||||||
for _, l := range strings.Split(got, "\n") {
|
for l := range strings.SplitSeq(got, "\n") {
|
||||||
gM[l]++
|
gM[l]++
|
||||||
lines = append(lines, l)
|
lines = append(lines, l)
|
||||||
}
|
}
|
||||||
|
|||||||
+16
-17
@@ -15,6 +15,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@@ -249,26 +250,27 @@ func (q *Queue) Remove(id string) {
|
|||||||
func (q *Queue) DumpString() string {
|
func (q *Queue) DumpString() string {
|
||||||
q.mu.RLock()
|
q.mu.RLock()
|
||||||
defer q.mu.RUnlock()
|
defer q.mu.RUnlock()
|
||||||
s := "# Queue status\n\n"
|
var s strings.Builder
|
||||||
s += fmt.Sprintf("date: %v\n", time.Now())
|
s.WriteString("# Queue status\n\n")
|
||||||
s += fmt.Sprintf("length: %d\n\n", len(q.q))
|
s.WriteString(fmt.Sprintf("date: %v\n", time.Now()))
|
||||||
|
s.WriteString(fmt.Sprintf("length: %d\n\n", len(q.q)))
|
||||||
|
|
||||||
for id, item := range q.q {
|
for id, item := range q.q {
|
||||||
s += fmt.Sprintf("## Item %s\n", id)
|
s.WriteString(fmt.Sprintf("## Item %s\n", id))
|
||||||
item.Lock()
|
item.Lock()
|
||||||
s += fmt.Sprintf("created at: %s\n", item.CreatedAt)
|
s.WriteString(fmt.Sprintf("created at: %s\n", item.CreatedAt))
|
||||||
s += fmt.Sprintf("from: %s\n", item.From)
|
s.WriteString(fmt.Sprintf("from: %s\n", item.From))
|
||||||
s += fmt.Sprintf("to: %s\n", item.To)
|
s.WriteString(fmt.Sprintf("to: %s\n", item.To))
|
||||||
for _, rcpt := range item.Rcpt {
|
for _, rcpt := range item.Rcpt {
|
||||||
s += fmt.Sprintf("%s %s (%s)\n", rcpt.Status, rcpt.Address, rcpt.Type)
|
s.WriteString(fmt.Sprintf("%s %s (%s)\n", rcpt.Status, rcpt.Address, rcpt.Type))
|
||||||
s += fmt.Sprintf(" original address: %s\n", rcpt.OriginalAddress)
|
s.WriteString(fmt.Sprintf(" original address: %s\n", rcpt.OriginalAddress))
|
||||||
s += fmt.Sprintf(" last failure: %q\n", rcpt.LastFailureMessage)
|
s.WriteString(fmt.Sprintf(" last failure: %q\n", rcpt.LastFailureMessage))
|
||||||
}
|
}
|
||||||
item.Unlock()
|
item.Unlock()
|
||||||
s += "\n"
|
s.WriteString("\n")
|
||||||
}
|
}
|
||||||
|
|
||||||
return s
|
return s.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// An Item in the queue.
|
// An Item in the queue.
|
||||||
@@ -458,11 +460,8 @@ func rewriteSender(from, originalAddr string) string {
|
|||||||
func (item *Item) countRcpt(statuses ...Recipient_Status) int {
|
func (item *Item) countRcpt(statuses ...Recipient_Status) int {
|
||||||
c := 0
|
c := 0
|
||||||
for _, rcpt := range item.Rcpt {
|
for _, rcpt := range item.Rcpt {
|
||||||
for _, status := range statuses {
|
if slices.Contains(statuses, rcpt.Status) {
|
||||||
if rcpt.Status == status {
|
c++
|
||||||
c++
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return c
|
return c
|
||||||
|
|||||||
@@ -277,7 +277,7 @@ func TestNextDelay(t *testing.T) {
|
|||||||
}
|
}
|
||||||
for _, c := range cases {
|
for _, c := range cases {
|
||||||
// Repeat each case a few times to exercise the perturbation a bit.
|
// Repeat each case a few times to exercise the perturbation a bit.
|
||||||
for i := 0; i < 10; i++ {
|
for range 10 {
|
||||||
delay := nextDelay(time.Now().Add(-c.since))
|
delay := nextDelay(time.Now().Add(-c.since))
|
||||||
|
|
||||||
max := c.min + 1*time.Minute
|
max := c.min + 1*time.Minute
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ func NewClient(conn net.Conn, host string) (*Client, error) {
|
|||||||
|
|
||||||
// cmd sends a command and returns the response over the text connection.
|
// cmd sends a command and returns the response over the text connection.
|
||||||
// Based on Go's method of the same name.
|
// Based on Go's method of the same name.
|
||||||
func (c *Client) cmd(expectCode int, format string, args ...interface{}) (int, string, error) {
|
func (c *Client) cmd(expectCode int, format string, args ...any) (int, string, error) {
|
||||||
id, err := c.Text.Cmd(format, args...)
|
id, err := c.Text.Cmd(format, args...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, "", err
|
return 0, "", err
|
||||||
|
|||||||
@@ -232,7 +232,7 @@ var _ net.Conn = faker{}
|
|||||||
func fakeDialog(dialog string) (faker, string) {
|
func fakeDialog(dialog string) (faker, string) {
|
||||||
var client, server string
|
var client, server string
|
||||||
|
|
||||||
for _, l := range strings.Split(dialog, "\n") {
|
for l := range strings.SplitSeq(dialog, "\n") {
|
||||||
if strings.HasPrefix(l, "< ") {
|
if strings.HasPrefix(l, "< ") {
|
||||||
server += l[2:] + "\r\n"
|
server += l[2:] + "\r\n"
|
||||||
} else if strings.HasPrefix(l, "> ") {
|
} else if strings.HasPrefix(l, "> ") {
|
||||||
|
|||||||
@@ -519,7 +519,7 @@ func (c *Conn) checkSPF(addr string) (spf.Result, error) {
|
|||||||
defer spfTr.Finish()
|
defer spfTr.Finish()
|
||||||
res, err := spf.CheckHostWithSender(
|
res, err := spf.CheckHostWithSender(
|
||||||
tcp.IP, envelope.DomainOf(addr), addr,
|
tcp.IP, envelope.DomainOf(addr), addr,
|
||||||
spf.WithTraceFunc(func(f string, a ...interface{}) {
|
spf.WithTraceFunc(func(f string, a ...any) {
|
||||||
spfTr.Debugf(f, a...)
|
spfTr.Debugf(f, a...)
|
||||||
}))
|
}))
|
||||||
|
|
||||||
@@ -872,7 +872,7 @@ func checkData(data []byte) error {
|
|||||||
// as post-data hooks), so this function sanitizes the value to make it
|
// as post-data hooks), so this function sanitizes the value to make it
|
||||||
// shell-safe.
|
// shell-safe.
|
||||||
func sanitizeEHLODomain(s string) string {
|
func sanitizeEHLODomain(s string) string {
|
||||||
n := ""
|
var n strings.Builder
|
||||||
for _, c := range s {
|
for _, c := range s {
|
||||||
// Allow a-zA-Z0-9 and []-.:
|
// Allow a-zA-Z0-9 and []-.:
|
||||||
// That's enough for all domains, IPv4 and IPv6 literals, and also
|
// That's enough for all domains, IPv4 and IPv6 literals, and also
|
||||||
@@ -884,11 +884,11 @@ func sanitizeEHLODomain(s string) string {
|
|||||||
c >= '0' && c <= '9',
|
c >= '0' && c <= '9',
|
||||||
c == '-', c == '.',
|
c == '-', c == '.',
|
||||||
c == '[', c == ']', c == ':':
|
c == '[', c == ']', c == ':':
|
||||||
n += string(c)
|
n.WriteString(string(c))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return n
|
return n.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// runPostDataHook and return the new headers to add, and on error a boolean
|
// runPostDataHook and return the new headers to add, and on error a boolean
|
||||||
@@ -909,7 +909,7 @@ func (c *Conn) runPostDataHook(data []byte) ([]byte, bool, error) {
|
|||||||
|
|
||||||
// Prepare the environment, copying some common variables so the hook has
|
// Prepare the environment, copying some common variables so the hook has
|
||||||
// something reasonable, and then setting the specific ones for this case.
|
// something reasonable, and then setting the specific ones for this case.
|
||||||
for _, v := range strings.Fields("USER PWD SHELL PATH") {
|
for v := range strings.FieldsSeq("USER PWD SHELL PATH") {
|
||||||
cmd.Env = append(cmd.Env, v+"="+os.Getenv(v))
|
cmd.Env = append(cmd.Env, v+"="+os.Getenv(v))
|
||||||
}
|
}
|
||||||
cmd.Env = append(cmd.Env, "REMOTE_ADDR="+c.remoteAddr.String())
|
cmd.Env = append(cmd.Env, "REMOTE_ADDR="+c.remoteAddr.String())
|
||||||
@@ -1269,7 +1269,7 @@ func (c *Conn) writeResponse(code int, msg string) error {
|
|||||||
return writeResponse(c.writer, code, msg)
|
return writeResponse(c.writer, code, msg)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Conn) printfLine(format string, args ...interface{}) {
|
func (c *Conn) printfLine(format string, args ...any) {
|
||||||
fmt.Fprintf(c.writer, format+"\r\n", args...)
|
fmt.Fprintf(c.writer, format+"\r\n", args...)
|
||||||
c.writer.Flush()
|
c.writer.Flush()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -307,7 +307,7 @@ func TestTooManyRecipients(t *testing.T) {
|
|||||||
t.Fatalf("Mail: %v", err)
|
t.Fatalf("Mail: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < 101; i++ {
|
for i := range 101 {
|
||||||
if err := c.Rcpt(fmt.Sprintf("to%d@somewhere", i)); err != nil {
|
if err := c.Rcpt(fmt.Sprintf("to%d@somewhere", i)); err != nil {
|
||||||
t.Fatalf("Rcpt: %v", err)
|
t.Fatalf("Rcpt: %v", err)
|
||||||
}
|
}
|
||||||
@@ -363,7 +363,7 @@ func sendLargeEmail(tb testing.TB, c *smtp.Client, sizeMiB int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Write the 1 MiB string sizeMiB times.
|
// Write the 1 MiB string sizeMiB times.
|
||||||
for i := 0; i < sizeMiB; i++ {
|
for range sizeMiB {
|
||||||
if _, err := w.Write([]byte(str1MiB)); err != nil {
|
if _, err := w.Write([]byte(str1MiB)); err != nil {
|
||||||
tb.Fatalf("Data write: %v", err)
|
tb.Fatalf("Data write: %v", err)
|
||||||
}
|
}
|
||||||
@@ -615,7 +615,7 @@ func realMain(m *testing.M) int {
|
|||||||
|
|
||||||
// Create a 1MiB string, which the large message tests use.
|
// Create a 1MiB string, which the large message tests use.
|
||||||
buf := make([]byte, 1024*1024)
|
buf := make([]byte, 1024*1024)
|
||||||
for i := 0; i < len(buf); i++ {
|
for i := range buf {
|
||||||
buf[i] = 'a'
|
buf[i] = 'a'
|
||||||
}
|
}
|
||||||
str1MiB = string(buf)
|
str1MiB = string(buf)
|
||||||
|
|||||||
@@ -229,7 +229,7 @@ func TestFetch(t *testing.T) {
|
|||||||
func TestPolicyTooBig(t *testing.T) {
|
func TestPolicyTooBig(t *testing.T) {
|
||||||
// Construct a valid but very large JSON as a policy.
|
// Construct a valid but very large JSON as a policy.
|
||||||
raw := `{"version": "STSv1", "mode": "enforce", "mx": [`
|
raw := `{"version": "STSv1", "mode": "enforce", "mx": [`
|
||||||
for i := 0; i < 2000; i++ {
|
for i := range 2000 {
|
||||||
raw += fmt.Sprintf("\"mx%d\", ", i)
|
raw += fmt.Sprintf("\"mx%d\", ", i)
|
||||||
}
|
}
|
||||||
raw += `"mxlast"], "max_age": 100}`
|
raw += `"mxlast"], "max_age": 100}`
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ func (t *Trace) NewChild(family, title string) *Trace {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Printf adds this message to the trace's log.
|
// Printf adds this message to the trace's log.
|
||||||
func (t *Trace) Printf(format string, a ...interface{}) {
|
func (t *Trace) Printf(format string, a ...any) {
|
||||||
t.t.Printf(format, a...)
|
t.t.Printf(format, a...)
|
||||||
|
|
||||||
log.Log(log.Info, 1, "%s %s: %s", t.family, t.title,
|
log.Log(log.Info, 1, "%s %s: %s", t.family, t.title,
|
||||||
@@ -46,7 +46,7 @@ func (t *Trace) Printf(format string, a ...interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Debugf adds this message to the trace's log, with a debugging level.
|
// Debugf adds this message to the trace's log, with a debugging level.
|
||||||
func (t *Trace) Debugf(format string, a ...interface{}) {
|
func (t *Trace) Debugf(format string, a ...any) {
|
||||||
t.t.Printf(format, a...)
|
t.t.Printf(format, a...)
|
||||||
|
|
||||||
log.Log(log.Debug, 1, "%s %s: %s",
|
log.Log(log.Debug, 1, "%s %s: %s",
|
||||||
@@ -54,7 +54,7 @@ func (t *Trace) Debugf(format string, a ...interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Errorf adds this message to the trace's log, with an error level.
|
// Errorf adds this message to the trace's log, with an error level.
|
||||||
func (t *Trace) Errorf(format string, a ...interface{}) error {
|
func (t *Trace) Errorf(format string, a ...any) error {
|
||||||
// Note we can't just call t.Error here, as it breaks caller logging.
|
// Note we can't just call t.Error here, as it breaks caller logging.
|
||||||
err := fmt.Errorf(format, a...)
|
err := fmt.Errorf(format, a...)
|
||||||
t.t.SetError()
|
t.t.SetError()
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
//go:build !coverage
|
//go:build !coverage
|
||||||
// +build !coverage
|
|
||||||
|
|
||||||
// SMTP connection generator, for testing purposes.
|
// SMTP connection generator, for testing purposes.
|
||||||
package main
|
package main
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
//go:build !coverage
|
//go:build !coverage
|
||||||
// +build !coverage
|
|
||||||
|
|
||||||
// Generate an HTML visualization of a Go coverage profile.
|
// Generate an HTML visualization of a Go coverage profile.
|
||||||
// Serves a similar purpose to "go tool cover -html", but has a different
|
// Serves a similar purpose to "go tool cover -html", but has a different
|
||||||
@@ -25,7 +24,7 @@ var (
|
|||||||
notes = flag.String("notes", "", "notes to add at the beginning (HTML)")
|
notes = flag.String("notes", "", "notes to add at the beginning (HTML)")
|
||||||
)
|
)
|
||||||
|
|
||||||
func errorf(f string, a ...interface{}) {
|
func errorf(f string, a ...any) {
|
||||||
fmt.Printf(f, a...)
|
fmt.Printf(f, a...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
//go:build !coverage
|
//go:build !coverage
|
||||||
// +build !coverage
|
|
||||||
|
|
||||||
// Fetch an URL, and check if the response matches what we expect.
|
// Fetch an URL, and check if the response matches what we expect.
|
||||||
//
|
//
|
||||||
@@ -187,12 +186,12 @@ func mkTransport(caCert string) http.RoundTripper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func fatalf(s string, a ...interface{}) {
|
func fatalf(s string, a ...any) {
|
||||||
fmt.Fprintf(os.Stderr, s, a...)
|
fmt.Fprintf(os.Stderr, s, a...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func errorf(s string, a ...interface{}) {
|
func errorf(s string, a ...any) {
|
||||||
fmt.Fprintf(os.Stderr, s, a...)
|
fmt.Fprintf(os.Stderr, s, a...)
|
||||||
exitCode = 1
|
exitCode = 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
//go:build !coverage
|
//go:build !coverage
|
||||||
// +build !coverage
|
|
||||||
|
|
||||||
// Utility to generate self-signed certificates.
|
// Utility to generate self-signed certificates.
|
||||||
// It generates a self-signed x509 certificate and key pair, and writes them
|
// It generates a self-signed x509 certificate and key pair, and writes them
|
||||||
@@ -34,7 +33,7 @@ var (
|
|||||||
"Should this cert be its own CA?")
|
"Should this cert be its own CA?")
|
||||||
)
|
)
|
||||||
|
|
||||||
func fatalf(f string, a ...interface{}) {
|
func fatalf(f string, a ...any) {
|
||||||
fmt.Printf(f, a...)
|
fmt.Printf(f, a...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -71,8 +70,8 @@ func main() {
|
|||||||
tmpl.IsCA = true
|
tmpl.IsCA = true
|
||||||
}
|
}
|
||||||
|
|
||||||
hosts := strings.Split(*host, ",")
|
hosts := strings.SplitSeq(*host, ",")
|
||||||
for _, h := range hosts {
|
for h := range hosts {
|
||||||
if ip := net.ParseIP(h); ip != nil {
|
if ip := net.ParseIP(h); ip != nil {
|
||||||
tmpl.IPAddresses = append(tmpl.IPAddresses, ip)
|
tmpl.IPAddresses = append(tmpl.IPAddresses, ip)
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
//go:build !coverage
|
//go:build !coverage
|
||||||
// +build !coverage
|
|
||||||
|
|
||||||
// SMTP load generator, for testing purposes.
|
// SMTP load generator, for testing purposes.
|
||||||
package main
|
package main
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
//go:build !coverage
|
//go:build !coverage
|
||||||
// +build !coverage
|
|
||||||
|
|
||||||
// minidns is a trivial DNS server used for testing.
|
// minidns is a trivial DNS server used for testing.
|
||||||
//
|
//
|
||||||
@@ -63,11 +62,9 @@ func main() {
|
|||||||
srv.loadZones(zonesFile)
|
srv.loadZones(zonesFile)
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(1)
|
wg.Go(func() {
|
||||||
go func() {
|
|
||||||
defer wg.Done()
|
|
||||||
srv.listenAndServeUDP(*addr)
|
srv.listenAndServeUDP(*addr)
|
||||||
}()
|
})
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
srv.listenAndServeTCP(*addr)
|
srv.listenAndServeTCP(*addr)
|
||||||
|
|||||||
@@ -101,7 +101,7 @@ func loadConfig() {
|
|||||||
}
|
}
|
||||||
notnil(err)
|
notnil(err)
|
||||||
|
|
||||||
for _, line := range strings.Split(string(data), "\n") {
|
for line := range strings.SplitSeq(string(data), "\n") {
|
||||||
k, v, ok := strings.Cut(line, " ")
|
k, v, ok := strings.Cut(line, " ")
|
||||||
if !ok {
|
if !ok {
|
||||||
continue
|
continue
|
||||||
|
|||||||
Reference in New Issue
Block a user