From 5443a130701c22949bebad1c7e450512948678bd Mon Sep 17 00:00:00 2001 From: Muneeb Ullah Khan Date: Thu, 1 Jan 2026 15:39:15 +0500 Subject: [PATCH] [INS-165] Fix Jdbc tests and updated the deprecated code --- pkg/detectors/jdbc/jdbc_integration_test.go | 24 ++++++++++++++----- pkg/detectors/jdbc/mysql_integration_test.go | 3 ++- .../jdbc/postgres_integration_test.go | 9 ++----- .../jdbc/sqlserver_integration_test.go | 8 +++---- 4 files changed, 26 insertions(+), 18 deletions(-) diff --git a/pkg/detectors/jdbc/jdbc_integration_test.go b/pkg/detectors/jdbc/jdbc_integration_test.go index c525b1cbe..f1a390029 100644 --- a/pkg/detectors/jdbc/jdbc_integration_test.go +++ b/pkg/detectors/jdbc/jdbc_integration_test.go @@ -29,8 +29,8 @@ func TestJdbcVerified(t *testing.T) { postgresUser := gofakeit.Username() postgresPass := gofakeit.Password(true, true, true, false, false, 10) postgresDB := gofakeit.Word() - postgresContainer, err := postgres.RunContainer(ctx, - testcontainers.WithImage("postgres:13-alpine"), + postgresContainer, err := postgres.Run(ctx, + "postgres:13-alpine", postgres.WithDatabase(postgresDB), postgres.WithUsername(postgresUser), postgres.WithPassword(postgresPass), @@ -56,8 +56,8 @@ func TestJdbcVerified(t *testing.T) { mysqlUser := gofakeit.Username() mysqlPass := gofakeit.Password(true, true, true, false, false, 10) mysqlDatabase := gofakeit.Word() - mysqlC, err := mysql.RunContainer(ctx, - mysql.WithDatabase(mysqlDatabase), + mysqlC, err := mysql.Run(ctx, + "mysql:8.0.36", mysql.WithUsername(mysqlUser), mysql.WithPassword(mysqlPass), ) @@ -79,8 +79,8 @@ func TestJdbcVerified(t *testing.T) { sqlServerPass := gofakeit.Password(true, true, true, false, false, 10) sqlServerDatabase := "master" - mssqlContainer, err := mssql.RunContainer(ctx, - testcontainers.WithImage("mcr.microsoft.com/azure-sql-edge"), + mssqlContainer, err := mssql.Run(ctx, + "mcr.microsoft.com/azure-sql-edge", mssql.WithAcceptEULA(), mssql.WithPassword(sqlServerPass), ) @@ -125,6 +125,10 @@ func TestJdbcVerified(t *testing.T) { Verified: true, Redacted: fmt.Sprintf("jdbc:postgresql://%s:%s/%s?sslmode=disable&password=%s&user=%s", postgresHost, postgresPort.Port(), postgresDB, strings.Repeat("*", len(postgresPass)), postgresUser), + AnalysisInfo: map[string]string{ + "connection_string": fmt.Sprintf("jdbc:postgresql://%s:%s/%s?sslmode=disable&password=%s&user=%s", + postgresHost, postgresPort.Port(), postgresDB, postgresPass, postgresUser), + }, }, }, wantErr: false, @@ -143,6 +147,10 @@ func TestJdbcVerified(t *testing.T) { Verified: true, Redacted: fmt.Sprintf(`jdbc:mysql://%s:%s@tcp(%s:%s)/%s`, mysqlUser, strings.Repeat("*", len(mysqlPass)), mysqlHost, mysqlPort.Port(), mysqlDatabase), + AnalysisInfo: map[string]string{ + "connection_string": fmt.Sprintf(`jdbc:mysql://%s:%s@tcp(%s:%s)/%s`, + mysqlUser, mysqlPass, mysqlHost, mysqlPort.Port(), mysqlDatabase), + }, }, }, wantErr: false, @@ -161,6 +169,10 @@ func TestJdbcVerified(t *testing.T) { Verified: true, Redacted: fmt.Sprintf("jdbc:sqlserver://odbc:server=%s;port=%s;database=%s;password=%s", sqlServerHost, sqlServerPort.Port(), sqlServerDatabase, strings.Repeat("*", len(sqlServerPass))), + AnalysisInfo: map[string]string{ + "connection_string": fmt.Sprintf("jdbc:sqlserver://odbc:server=%s;port=%s;database=%s;password=%s", + sqlServerHost, sqlServerPort.Port(), sqlServerDatabase, sqlServerPass), + }, }, }, wantErr: false, diff --git a/pkg/detectors/jdbc/mysql_integration_test.go b/pkg/detectors/jdbc/mysql_integration_test.go index a0b3f9709..30a8ec3ac 100644 --- a/pkg/detectors/jdbc/mysql_integration_test.go +++ b/pkg/detectors/jdbc/mysql_integration_test.go @@ -23,7 +23,8 @@ func TestMySQL(t *testing.T) { ctx := context.Background() - mysqlC, err := mysql.RunContainer(ctx, + mysqlC, err := mysql.Run(ctx, + "mysql:8.0.36", mysql.WithDatabase(mysqlDatabase), mysql.WithUsername(mysqlUser), mysql.WithPassword(mysqlPass), diff --git a/pkg/detectors/jdbc/postgres_integration_test.go b/pkg/detectors/jdbc/postgres_integration_test.go index 4fc8d4408..f3caca54d 100644 --- a/pkg/detectors/jdbc/postgres_integration_test.go +++ b/pkg/detectors/jdbc/postgres_integration_test.go @@ -6,7 +6,6 @@ package jdbc import ( "context" "fmt" - "log" "testing" "time" @@ -34,8 +33,8 @@ func TestPostgres(t *testing.T) { t.Log("dbName: ", dbName) ctx := context.Background() - postgresContainer, err := postgres.RunContainer(ctx, - testcontainers.WithImage("postgres:13-alpine"), + postgresContainer, err := postgres.Run(ctx, + "postgres:13-alpine", postgres.WithDatabase(dbName), postgres.WithUsername(user), postgres.WithPassword(pass), @@ -56,10 +55,6 @@ func TestPostgres(t *testing.T) { if err != nil { t.Fatal(err) } - - if err != nil { - log.Fatalf("failed to start container: %s", err) - } defer postgresContainer.Terminate(ctx) tests := []struct { diff --git a/pkg/detectors/jdbc/sqlserver_integration_test.go b/pkg/detectors/jdbc/sqlserver_integration_test.go index 1e0239601..3c8aacc80 100644 --- a/pkg/detectors/jdbc/sqlserver_integration_test.go +++ b/pkg/detectors/jdbc/sqlserver_integration_test.go @@ -10,7 +10,6 @@ import ( "github.com/brianvoe/gofakeit/v7" "github.com/stretchr/testify/assert" - "github.com/testcontainers/testcontainers-go" "github.com/testcontainers/testcontainers-go/modules/mssql" logContext "github.com/trufflesecurity/trufflehog/v3/pkg/context" ) @@ -22,8 +21,8 @@ func TestSqlServer(t *testing.T) { sqlServerPass := gofakeit.Password(true, true, true, false, false, 10) sqlServerDB := "master" - mssqlContainer, err := mssql.RunContainer(ctx, - testcontainers.WithImage("mcr.microsoft.com/azure-sql-edge"), + mssqlContainer, err := mssql.Run(ctx, + "mcr.microsoft.com/azure-sql-edge", mssql.WithAcceptEULA(), mssql.WithPassword(sqlServerPass), ) @@ -62,7 +61,7 @@ func TestSqlServer(t *testing.T) { want: result{pingOk: true, pingDeterminate: true}, }, { - input: "//server=badhost;user id=sa;database=master;password=", + input: fmt.Sprintf("//server=badhost;user id=sa;database=master;password=%s", sqlServerPass), want: result{pingOk: false, pingDeterminate: false}, }, { @@ -97,4 +96,5 @@ func TestSqlServer(t *testing.T) { assert.Equal(t, tt.want, got) }) } + //a }