Add tests for Client debug logging behavior

Introduce unit tests to verify the behavior of the debug logging in the Client. Confirm that logs are correctly produced when debug mode is enabled and appropriately suppressed when it is disabled.
This commit is contained in:
Winni Neessen 2024-11-11 18:25:39 +01:00
parent 3b9085e19d
commit c58aa35454
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -3512,6 +3512,34 @@ func TestClient_GetTLSConnectionState(t *testing.T) {
}) })
} }
func TestClient_debugLog(t *testing.T) {
t.Run("debug log is enabled", func(t *testing.T) {
buffer := bytes.NewBuffer(nil)
logger := log.New(buffer, log.LevelDebug)
client := &Client{logger: logger, debug: true}
client.debugLog(log.DirClientToServer, "%s", "simple string")
client.debugLog(log.DirServerToClient, "%d", 1234)
want := "DEBUG: C --> S: simple string"
if !strings.Contains(buffer.String(), want) {
t.Errorf("expected debug log to contain %q, got: %q", want, buffer.String())
}
want = "DEBUG: C <-- S: 1234"
if !strings.Contains(buffer.String(), want) {
t.Errorf("expected debug log to contain %q, got: %q", want, buffer.String())
}
})
t.Run("debug log is disable", func(t *testing.T) {
buffer := bytes.NewBuffer(nil)
logger := log.New(buffer, log.LevelDebug)
client := &Client{logger: logger, debug: false}
client.debugLog(log.DirClientToServer, "%s", "simple string")
client.debugLog(log.DirServerToClient, "%d", 1234)
if buffer.Len() > 0 {
t.Errorf("expected debug log to be empty, got: %q", buffer.String())
}
})
}
// faker is a struct embedding io.ReadWriter to simulate network connections for testing purposes. // faker is a struct embedding io.ReadWriter to simulate network connections for testing purposes.
type faker struct { type faker struct {
io.ReadWriter io.ReadWriter