-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
auth_test.go
57 lines (50 loc) · 1.28 KB
/
auth_test.go
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
// Copyright 2021 Roxy Light
// SPDX-License-Identifier: ISC
package sqlite_test
import (
"testing"
"zombiezen.com/go/sqlite"
)
func TestSetAuthorizer(t *testing.T) {
c, err := sqlite.OpenConn(":memory:", 0)
if err != nil {
t.Fatal(err)
}
defer func() {
if err := c.Close(); err != nil {
t.Error(err)
}
}()
authResult := sqlite.AuthResult(0)
var lastAction sqlite.Action
auth := sqlite.AuthorizeFunc(func(action sqlite.Action) sqlite.AuthResult {
lastAction = action
return authResult
})
c.SetAuthorizer(auth)
t.Run("Allowed", func(t *testing.T) {
authResult = sqlite.AuthResultOK
stmt, _, err := c.PrepareTransient("SELECT 1;")
if err != nil {
t.Fatal(err)
}
stmt.Finalize()
if lastAction.Type() != sqlite.OpSelect {
t.Errorf("action = %v; want %v", lastAction, sqlite.OpSelect)
}
})
t.Run("Denied", func(t *testing.T) {
authResult = sqlite.AuthResultDeny
stmt, _, err := c.PrepareTransient("SELECT 1;")
if err == nil {
stmt.Finalize()
t.Fatal("PrepareTransient did not return an error")
}
if got, want := sqlite.ErrCode(err), sqlite.ResultAuth; got != want {
t.Errorf("sqlite.ErrCode(err) = %v; want %v", got, want)
}
if lastAction.Type() != sqlite.OpSelect {
t.Errorf("action = %v; want %v", lastAction, sqlite.OpSelect)
}
})
}