forked from mirror/go-sqlite3
Add Tests for UserAuth Omit
* Add test * Fix AuthEnabled() * Add AuthEnabled() to TestUserAuthCreateDatabase()
This commit is contained in:
parent
dc9d5d0e99
commit
de6249ddc9
|
@ -132,8 +132,12 @@ func (c *SQLiteConn) authUserDelete(username string) int {
|
|||
|
||||
// AuthEnabled checks if the database is protected by user authentication
|
||||
func (c *SQLiteConn) AuthEnabled() (exists bool) {
|
||||
// NOOP
|
||||
return false
|
||||
rv := c.authEnabled()
|
||||
if rv == 1 {
|
||||
exists = true
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// authEnabled perform the actual check for user authentication.
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
// 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.
|
||||
|
||||
// +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 {
|
||||
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 {
|
||||
t.Fatal("UserAuthOmit Failure")
|
||||
}
|
||||
|
||||
if conn.Authenticate("", ""); err != nil {
|
||||
t.Fatal("UserAuthOmit Failure")
|
||||
}
|
||||
if ok := conn.authenticate("", ""); ok != 0 {
|
||||
t.Fatal("UserAuthOmit Failure")
|
||||
}
|
||||
|
||||
if conn.AuthUserAdd("", "", true); err != nil {
|
||||
t.Fatal("UserAuthOmit Failure")
|
||||
}
|
||||
if ok := conn.authUserAdd("", "", 1); ok != 0 {
|
||||
t.Fatal("UserAuthOmit Failure")
|
||||
}
|
||||
|
||||
if conn.AuthUserChange("", "", true); err != nil {
|
||||
t.Fatal("UserAuthOmit Failure")
|
||||
}
|
||||
if ok := conn.authUserChange("", "", 1); ok != 0 {
|
||||
t.Fatal("UserAuthOmit Failure")
|
||||
}
|
||||
|
||||
if conn.AuthUserDelete(""); err != nil {
|
||||
t.Fatal("UserAuthOmit Failure")
|
||||
}
|
||||
if ok := conn.authUserDelete(""); ok != 0 {
|
||||
t.Fatal("UserAuthOmit Failure")
|
||||
}
|
||||
if enabled := conn.AuthEnabled(); enabled {
|
||||
t.Fatal("UserAuthOmit Failure")
|
||||
}
|
||||
}
|
|
@ -162,6 +162,10 @@ func TestUserAuthCreateDatabase(t *testing.T) {
|
|||
if !a {
|
||||
t.Fatal("UserAuth: User is not administrator")
|
||||
}
|
||||
|
||||
if enabled := c.AuthEnabled(); !enabled {
|
||||
t.Fatal("UserAuth Not Enabled")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserAuthLogin(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue