File: sqlite3_opt_userauth_test.go

package info (click to toggle)
golang-github-mattn-go-sqlite3 1.14.32~ds1-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 772 kB
  • sloc: cpp: 1,132; ansic: 728; makefile: 60
file content (76 lines) | stat: -rw-r--r-- 1,757 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (C) 2018 G.J.R. Timmer <gjr.timmer@gmail.com>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

//go:build sqlite_userauth
// +build sqlite_userauth

package sqlite3

import (
	"database/sql"
	"errors"
	"fmt"
	"os"
	"testing"
)

var (
	conn    *SQLiteConn
	connect func(t *testing.T, f string, username, password string) (file string, db *sql.DB, c *SQLiteConn, err error)
)

func init() {
	// Create database connection
	sql.Register("sqlite3_with_conn",
		&SQLiteDriver{
			ConnectHook: func(c *SQLiteConn) error {
				conn = c
				return nil
			},
		})

	connect = func(t *testing.T, f string, username, password string) (file string, db *sql.DB, c *SQLiteConn, err error) {
		conn = nil // Clear connection
		file = f   // Copy provided file (f) => file
		if file == "" {
			// Create dummy file
			file = TempFilename(t)
		}

		params := "?_auth"
		if len(username) > 0 {
			params = fmt.Sprintf("%s&_auth_user=%s", params, username)
		}
		if len(password) > 0 {
			params = fmt.Sprintf("%s&_auth_pass=%s", params, password)
		}
		db, err = sql.Open("sqlite3_with_conn", "file:"+file+params)
		if err != nil {
			defer os.Remove(file)
			return file, nil, nil, err
		}

		// Dummy query to force connection and database creation
		// Will return errUserAuthNoLongerSupported if user authentication fails
		if _, err = db.Exec("SELECT 1;"); err != nil {
			defer os.Remove(file)
			defer db.Close()
			return file, nil, nil, err
		}
		c = conn

		return
	}
}

func TestUserAuth(t *testing.T) {
	_, _, _, err := connect(t, "", "admin", "admin")
	if err == nil {
		t.Fatalf("UserAuth not enabled: %s", err)
	}
	if !errors.Is(err, errUserAuthNoLongerSupported) {
		t.Fatal(err)
	}
}