From 273a26ca53d56079fc767ea995af7739608c0b08 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Sat, 26 Oct 2024 17:10:50 +0200 Subject: [PATCH] Add tests for Go 1.21+ compatibility Moved some tests that use JSONlog (which requires Go 1.21+) to a separate file that is skipped on older Go versions. --- client_121_test.go | 121 +++++++++++++++++++++++++++++++++++++++++++++ client_test.go | 51 ------------------- 2 files changed, 121 insertions(+), 51 deletions(-) create mode 100644 client_121_test.go diff --git a/client_121_test.go b/client_121_test.go new file mode 100644 index 0000000..84f091a --- /dev/null +++ b/client_121_test.go @@ -0,0 +1,121 @@ +// SPDX-FileCopyrightText: 2024 The go-mail Authors +// +// SPDX-License-Identifier: MIT + +//go:build go1.21 +// +build go1.21 + +package mail + +import ( + "bytes" + "context" + "errors" + "fmt" + "os" + "reflect" + "strings" + "testing" + "time" + + "github.com/wneessen/go-mail/log" +) + +func TestNewClientNewVersionsOnly(t *testing.T) { + tests := []struct { + name string + option Option + expectFunc func(c *Client) error + shouldfail bool + expectErr *error + }{ + { + "WithLogger log.JSONlog", WithLogger(log.NewJSON(os.Stderr, log.LevelDebug)), + func(c *Client) error { + if c.logger == nil { + return errors.New("failed to set logger. Want logger bug got got nil") + } + loggerType := reflect.TypeOf(c.logger).String() + if loggerType != "*log.JSONlog" { + return fmt.Errorf("failed to set logger. Want logger type: %s, got: %s", + "*log.JSONlog", loggerType) + } + return nil + }, + false, nil, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + client, err := NewClient(DefaultHost, tt.option) + if !tt.shouldfail && err != nil { + t.Fatalf("failed to create new client: %s", err) + } + if tt.shouldfail && err == nil { + t.Errorf("client creation was supposed to fail, but it didn't") + } + if tt.shouldfail && tt.expectErr != nil { + if !errors.Is(err, *tt.expectErr) { + t.Errorf("error for NewClient mismatch. Expected: %s, got: %s", + *tt.expectErr, err) + } + } + if tt.expectFunc != nil { + if err = tt.expectFunc(client); err != nil { + t.Errorf("NewClient with custom option failed: %s", err) + } + } + }) + } +} + +func TestClient_DialWithContextNewVersionsOnly(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + PortAdder.Add(1) + serverPort := int(TestServerPortBase + PortAdder.Load()) + featureSet := "250-AUTH PLAIN\r\n250-8BITMIME\r\n250-DSN\r\n250 SMTPUTF8" + go func() { + if err := simpleSMTPServer(ctx, t, &serverProps{FeatureSet: featureSet, ListenPort: serverPort}); err != nil { + t.Errorf("failed to start test server: %s", err) + return + } + }() + time.Sleep(time.Millisecond * 30) + t.Run("connect with full debug logging and auth logging", func(t *testing.T) { + ctxDial, cancelDial := context.WithTimeout(ctx, time.Millisecond*500) + t.Cleanup(cancelDial) + + logBuffer := bytes.NewBuffer(nil) + client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS), + WithDebugLog(), WithLogAuthData(), WithLogger(log.NewJSON(logBuffer, log.LevelDebug)), + WithSMTPAuth(SMTPAuthPlain), WithUsername("test"), WithPassword("password")) + if err != nil { + t.Fatalf("failed to create new client: %s", err) + } + + if err = client.DialWithContext(ctxDial); err != nil { + t.Fatalf("failed to connect to the test server: %s", err) + } + t.Cleanup(func() { + if err = client.Close(); err != nil { + t.Errorf("failed to close the client: %s", err) + } + }) + + logs := parseJSONLog(t, logBuffer) + if len(logs.Lines) == 0 { + t.Errorf("failed to enable debug logging, but no logs were found") + } + authFound := false + for _, logline := range logs.Lines { + if strings.EqualFold(logline.Message, "AUTH PLAIN AHRlc3QAcGFzc3dvcmQ=") && + logline.Direction.From == "client" && logline.Direction.To == "server" { + authFound = true + } + } + if !authFound { + t.Errorf("logAuthData not working, no authentication info found in logs") + } + }) +} diff --git a/client_test.go b/client_test.go index 98a46b6..9d24293 100644 --- a/client_test.go +++ b/client_test.go @@ -268,21 +268,6 @@ func TestNewClient(t *testing.T) { }, false, nil, }, - { - "WithLogger log.JSONlog", WithLogger(log.NewJSON(os.Stderr, log.LevelDebug)), - func(c *Client) error { - if c.logger == nil { - return errors.New("failed to set logger. Want logger bug got got nil") - } - loggerType := reflect.TypeOf(c.logger).String() - if loggerType != "*log.JSONlog" { - return fmt.Errorf("failed to set logger. Want logger type: %s, got: %s", - "*log.JSONlog", loggerType) - } - return nil - }, - false, nil, - }, { "WithHELO", WithHELO(hostname), func(c *Client) error { @@ -1759,42 +1744,6 @@ func TestClient_DialWithContext(t *testing.T) { t.Fatalf("client has connection") } }) - t.Run("connect with full debug logging and auth logging", func(t *testing.T) { - ctxDial, cancelDial := context.WithTimeout(ctx, time.Millisecond*500) - t.Cleanup(cancelDial) - - logBuffer := bytes.NewBuffer(nil) - client, err := NewClient(DefaultHost, WithPort(serverPort), WithTLSPolicy(NoTLS), - WithDebugLog(), WithLogAuthData(), WithLogger(log.NewJSON(logBuffer, log.LevelDebug)), - WithSMTPAuth(SMTPAuthPlain), WithUsername("test"), WithPassword("password")) - if err != nil { - t.Fatalf("failed to create new client: %s", err) - } - - if err = client.DialWithContext(ctxDial); err != nil { - t.Fatalf("failed to connect to the test server: %s", err) - } - t.Cleanup(func() { - if err = client.Close(); err != nil { - t.Errorf("failed to close the client: %s", err) - } - }) - - logs := parseJSONLog(t, logBuffer) - if len(logs.Lines) == 0 { - t.Errorf("failed to enable debug logging, but no logs were found") - } - authFound := false - for _, logline := range logs.Lines { - if strings.EqualFold(logline.Message, "AUTH PLAIN AHRlc3QAcGFzc3dvcmQ=") && - logline.Direction.From == "client" && logline.Direction.To == "server" { - authFound = true - } - } - if !authFound { - t.Errorf("logAuthData not working, no authentication info found in logs") - } - }) t.Run("connect should fail on HELO", func(t *testing.T) { ctxFail, cancelFail := context.WithCancel(ctx) defer cancelFail()