2024-10-29 13:30:14 +01:00
|
|
|
package callog
|
2024-09-13 23:08:20 +08:00
|
|
|
|
|
|
|
import (
|
2024-11-04 09:09:28 +01:00
|
|
|
"bytes"
|
2024-10-24 23:46:23 +02:00
|
|
|
"os"
|
2024-09-13 23:08:20 +08:00
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
|
2024-11-04 09:09:28 +01:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2024-10-24 23:46:23 +02:00
|
|
|
|
2024-09-13 23:08:20 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/status-im/status-go/logutils/requestlog"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRemoveSensitiveInfo(t *testing.T) {
|
|
|
|
testCases := []struct {
|
|
|
|
name string
|
|
|
|
input string
|
|
|
|
expected string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "basic test",
|
|
|
|
input: `{"username":"user1","password":"secret123","mnemonic":"mnemonic123 xyz"}`,
|
|
|
|
expected: `{"username":"user1","password":"***","mnemonic":"***"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "uppercase password field",
|
|
|
|
input: `{"USERNAME":"user1","PASSWORD":"secret123"}`,
|
|
|
|
expected: `{"USERNAME":"user1","PASSWORD":"***"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "password field with spaces",
|
|
|
|
input: `{"username":"user1", "password" : "secret123"}`,
|
|
|
|
expected: `{"username":"user1", "password":"***"}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "multiple password fields",
|
|
|
|
input: `{"password":"secret123","data":{"nested_password":"nested_secret"}}`,
|
|
|
|
expected: `{"password":"***","data":{"nested_password":"***"}}`,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no password field",
|
|
|
|
input: `{"username":"user1","email":"user1@example.com"}`,
|
|
|
|
expected: `{"username":"user1","email":"user1@example.com"}`,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
result := removeSensitiveInfo(tc.input)
|
|
|
|
if result != tc.expected {
|
|
|
|
t.Errorf("Expected: %s, Got: %s", tc.expected, result)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-27 06:37:32 +08:00
|
|
|
func TestCall(t *testing.T) {
|
2024-11-04 09:09:28 +01:00
|
|
|
// Create default logger
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
logger := zap.New(zapcore.NewCore(
|
|
|
|
zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
|
|
|
|
zapcore.AddSync(buffer),
|
|
|
|
zap.DebugLevel,
|
|
|
|
))
|
|
|
|
|
|
|
|
// Create a temporary file for request logging
|
2024-10-24 23:46:23 +02:00
|
|
|
tempLogFile, err := os.CreateTemp(t.TempDir(), "TestCall*.log")
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2024-09-13 23:08:20 +08:00
|
|
|
// Enable request logging
|
2024-11-04 09:09:28 +01:00
|
|
|
requestLogger, err := requestlog.CreateRequestLogger(tempLogFile.Name())
|
2024-10-24 23:46:23 +02:00
|
|
|
require.NoError(t, err)
|
2024-11-04 09:09:28 +01:00
|
|
|
require.NotNil(t, requestLogger)
|
2024-09-13 23:08:20 +08:00
|
|
|
|
|
|
|
// Test case 1: Normal execution
|
|
|
|
testFunc := func(param string) string {
|
|
|
|
return "test result: " + param
|
|
|
|
}
|
|
|
|
testParam := "test input"
|
|
|
|
expectedResult := "test result: test input"
|
|
|
|
|
2024-11-04 09:09:28 +01:00
|
|
|
result := CallWithResponse(logger, requestLogger, testFunc, testParam)
|
2024-09-13 23:08:20 +08:00
|
|
|
|
|
|
|
// Check the result
|
|
|
|
if result != expectedResult {
|
|
|
|
t.Errorf("Expected result %s, got %s", expectedResult, result)
|
|
|
|
}
|
|
|
|
|
2024-10-24 23:46:23 +02:00
|
|
|
// Read the log file
|
|
|
|
logData, err := os.ReadFile(tempLogFile.Name())
|
|
|
|
require.NoError(t, err)
|
2024-11-04 09:09:28 +01:00
|
|
|
requestLogOutput := string(logData)
|
2024-10-24 23:46:23 +02:00
|
|
|
|
2024-09-13 23:08:20 +08:00
|
|
|
// Check if the log contains expected information
|
|
|
|
expectedLogParts := []string{getShortFunctionName(testFunc), "params", testParam, "resp", expectedResult}
|
|
|
|
for _, part := range expectedLogParts {
|
2024-11-04 09:09:28 +01:00
|
|
|
if !strings.Contains(requestLogOutput, part) {
|
2024-09-13 23:08:20 +08:00
|
|
|
t.Errorf("Log output doesn't contain expected part: %s", part)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test case 2: Panic -> recovery -> re-panic
|
2024-11-04 09:09:28 +01:00
|
|
|
require.Empty(t, buffer.String())
|
2024-10-24 23:46:23 +02:00
|
|
|
|
2024-09-13 23:08:20 +08:00
|
|
|
e := "test panic"
|
|
|
|
panicFunc := func() {
|
|
|
|
panic(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.PanicsWithValue(t, e, func() {
|
2024-11-04 09:09:28 +01:00
|
|
|
Call(logger, requestLogger, panicFunc)
|
2024-09-13 23:08:20 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
// Check if the panic was logged
|
2024-11-04 09:09:28 +01:00
|
|
|
if !strings.Contains(buffer.String(), "panic found in call") {
|
2024-09-13 23:08:20 +08:00
|
|
|
t.Errorf("Log output doesn't contain panic information")
|
|
|
|
}
|
2024-11-04 09:09:28 +01:00
|
|
|
if !strings.Contains(buffer.String(), e) {
|
2024-09-13 23:08:20 +08:00
|
|
|
t.Errorf("Log output doesn't contain panic message")
|
|
|
|
}
|
2024-11-04 09:09:28 +01:00
|
|
|
if !strings.Contains(buffer.String(), "stacktrace") {
|
2024-09-13 23:08:20 +08:00
|
|
|
t.Errorf("Log output doesn't contain stacktrace")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-29 13:30:14 +01:00
|
|
|
func initializeApplication(requestJSON string) string {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2024-09-13 23:08:20 +08:00
|
|
|
func TestGetFunctionName(t *testing.T) {
|
|
|
|
fn := getShortFunctionName(initializeApplication)
|
|
|
|
require.Equal(t, "initializeApplication", fn)
|
|
|
|
}
|