go-sqlite3/driver/opt_userauth_omit_test.go

76 lines
1.7 KiB
Go
Raw Normal View History

2018-06-20 23:50:53 +03:00
// Copyright (C) 2018 The Go-SQLite3 Authors.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
2018-06-20 23:50:53 +03:00
// +build cgo
// +build !sqlite_userauth
package sqlite3
import (
"database/sql"
"os"
"testing"
)
// This file is included for code coverage
func TestUserAuthOmit(t *testing.T) {
var conn *SQLiteConn
sql.Register("sqlite3_with_conn",
&SQLiteDriver{
ConnectHook: func(c *SQLiteConn) error {
conn = c
return nil
},
})
file := TempFilename(t)
defer os.Remove(file)
db, err := sql.Open("sqlite3_with_conn", "file:"+file+"?_auth&_auth_user=admin&_auth_pass=admin")
if err != nil {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
defer db.Close()
// Dummy query to force connection and database creation
// Will return ErrUnauthorized (SQLITE_AUTH) if user authentication fails
if _, err := db.Exec("SELECT 1;"); err != nil {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
if conn.Authenticate("", ""); err != nil {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
if ok := conn.authenticate("", ""); ok != 0 {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
if conn.AuthUserAdd("", "", true); err != nil {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
if ok := conn.authUserAdd("", "", 1); ok != 0 {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
if conn.AuthUserChange("", "", true); err != nil {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
if ok := conn.authUserChange("", "", 1); ok != 0 {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
if conn.AuthUserDelete(""); err != nil {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
if ok := conn.authUserDelete(""); ok != 0 {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
if enabled := conn.AuthEnabled(); enabled {
2018-07-22 23:32:19 +03:00
t.Fatal("UserAuthOmit failure")
}
}