From fb558cf4ca9581a6d93c7183217c81cf479e00b3 Mon Sep 17 00:00:00 2001 From: Atanas Janeshliev Date: Mon, 22 Apr 2024 15:10:08 +0200 Subject: [PATCH] fix(BRIDGE-10): linter issues --- internal/db_impl/sqlite3/client.go | 5 ++- internal/db_impl/sqlite3/client_test.go | 47 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 internal/db_impl/sqlite3/client_test.go diff --git a/internal/db_impl/sqlite3/client.go b/internal/db_impl/sqlite3/client.go index 2819d0c3..880e4a31 100644 --- a/internal/db_impl/sqlite3/client.go +++ b/internal/db_impl/sqlite3/client.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io/fs" + "net/url" "os" "path/filepath" "sync" @@ -287,7 +288,9 @@ func pathExists(path string) (bool, error) { } func getDatabaseConn(dir, userID, path string) string { - return fmt.Sprintf("file:%v?cache=shared&_fk=1&_journal=WAL", path) + // We need to escape special characters in the db path, such as # + escapedPath := url.PathEscape(path) + return fmt.Sprintf("file:%v?cache=shared&_fk=1&_journal=WAL", escapedPath) } func TestUpdateDBVersion(ctx context.Context, dbPath, userID string, version int) error { diff --git a/internal/db_impl/sqlite3/client_test.go b/internal/db_impl/sqlite3/client_test.go new file mode 100644 index 00000000..3da560f5 --- /dev/null +++ b/internal/db_impl/sqlite3/client_test.go @@ -0,0 +1,47 @@ +package sqlite3 + +import ( + "database/sql" + "fmt" + "os" + "path/filepath" + "testing" +) + +func TestClient_DBConnectionSpecialCharacterPath(t *testing.T) { + dbDirs := []string{ + "#test", + "test_test", + "test#test#test", + "test$test$test", + } + + testingDir := t.TempDir() + + for _, dirName := range dbDirs { + path := filepath.Join(testingDir, dirName) + if err := os.MkdirAll(path, 0777); err != nil { + fmt.Println("Could not create testing directory, error: ", err) + t.FailNow() + } + + filePath := filepath.Join(path, "test.db") + + client, err := sql.Open("sqlite3", getDatabaseConn("test", "test", filePath)) + if err != nil { + fmt.Println("Could not connect to test database, error: ", err) + t.FailNow() + } + + if err := client.Ping(); err != nil { + fmt.Println("Could not ping test database, error: ", err) + client.Close() + t.FailNow() + } + + if err := client.Close(); err != nil { + fmt.Println("Could not close test database, error: ", err) + t.FailNow() + } + } +}