Debian Codegraph @

sqlite3_opt_userauth_test.go

raw
1 // Copyright (C) 2018 G.J.R. Timmer <[email protected]>.
2 //
3 // Use of this source code is governed by an MIT-style
4 // license that can be found in the LICENSE file.
5
6 //go:build sqlite_userauth
7
8 package sqlite3
9
10 import (
11 "database/sql"
12 "errors"
13 "fmt"
14 "os"
15 "testing"
16 )
17
18 var (
19 conn *SQLiteConn
20 connect func(t *testing.T, f string, username, password string) (file string, db *sql.DB, c *SQLiteConn, err error)
21 )
22
23 func init() {
24 // Create database connection
25 sql.Register("sqlite3_with_conn",
26 &SQLiteDriver{
27 ConnectHook: func(c *SQLiteConn) error {
28 conn = c
29 return nil
30 },
31 })
32
33 connect = func(t *testing.T, f string, username, password string) (file string, db *sql.DB, c *SQLiteConn, err error) {
34 conn = nil // Clear connection
35 file = f // Copy provided file (f) => file
36 if file == "" {
37 // Create dummy file
38 file = TempFilename(t)
39 }
40
41 params := "?_auth"
42 if len(username) > 0 {
43 params = fmt.Sprintf("%s&_auth_user=%s", params, username)
44 }
45 if len(password) > 0 {
46 params = fmt.Sprintf("%s&_auth_pass=%s", params, password)
47 }
48 db, err = sql.Open("sqlite3_with_conn", "file:"+file+params)
49 if err != nil {
50 defer os.Remove(file)
51 return file, nil, nil, err
52 }
53
54 // Dummy query to force connection and database creation
55 // Will return errUserAuthNoLongerSupported if user authentication fails
56 if _, err = db.Exec("SELECT 1;"); err != nil {
57 defer os.Remove(file)
58 defer db.Close()
59 return file, nil, nil, err
60 }
61 c = conn
62
63 return
64 }
65 }
66
67 func TestUserAuth(t *testing.T) {
68 _, _, _, err := connect(t, "", "admin", "admin")
69 if err == nil {
70 t.Fatalf("UserAuth not enabled: %s", err)
71 }
72 if !errors.Is(err, errUserAuthNoLongerSupported) {
73 t.Fatal(err)
74 }
75 }