-
Notifications
You must be signed in to change notification settings - Fork 0
/
creds_test.go
115 lines (104 loc) · 3.26 KB
/
creds_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package oracle
import (
"fmt"
"math/rand"
"reflect"
"regexp"
"testing"
"testing/quick"
"github.com/hashicorp/vault/builtin/logical/database/dbplugin"
"github.com/hashicorp/vault/plugins/helper/database/credsutil"
)
type testUsername string
func (tu testUsername) Generate(rand *rand.Rand, size int) reflect.Value {
credsProducer := &oracleCredentialsProducer{
credsutil.SQLCredentialsProducer{
DisplayNameLen: oracleDisplayNameMaxLength,
RoleNameLen: oracleDisplayNameMaxLength,
UsernameLen: oracleUsernameLength,
Separator: "_",
},
}
usernameConfig := dbplugin.UsernameConfig{
DisplayName: "display",
RoleName: "role",
}
if username, err := credsProducer.GenerateUsername(usernameConfig); err != nil {
panic(err)
} else {
return reflect.ValueOf(testUsername(username))
}
}
func TestUsernameShouldMatchOracleRequirements(t *testing.T) {
assertion := func(username testUsername) bool {
result, _ := regexp.MatchString(fmt.Sprintf("^[[:lower:]][_[:lower:][:digit:]]{%d}$", oracleUsernameLength-1), string(username))
return result
}
if err := quick.Check(assertion, nil); err != nil {
t.Error(err)
}
}
func TestShouldReplaceHyphenInDisplayNameAndRoleName(t *testing.T) {
credsProducer := &oracleCredentialsProducer{
credsutil.SQLCredentialsProducer{
DisplayNameLen: oracleDisplayNameMaxLength,
RoleNameLen: oracleDisplayNameMaxLength,
UsernameLen: oracleUsernameLength,
Separator: "_",
},
}
usernameConfig := dbplugin.UsernameConfig{
DisplayName: "a-b-c",
RoleName: "d-e-f",
}
if username, err := credsProducer.GenerateUsername(usernameConfig); err != nil {
t.Errorf("err: %s", err)
} else {
if match, err := regexp.MatchString("a_b_c_d_e_f", username); err != nil {
t.Errorf("err: %s", err)
} else if !match {
t.Errorf("does not match expected name. was '%s', expected to match '%s'", username, "a_b_c_d_e_f")
}
}
}
func TestShouldReplaceDotInDisplayNameAndRoleName(t *testing.T) {
credsProducer := &oracleCredentialsProducer{
credsutil.SQLCredentialsProducer{
DisplayNameLen: oracleDisplayNameMaxLength,
RoleNameLen: oracleDisplayNameMaxLength,
UsernameLen: oracleUsernameLength,
Separator: "_",
},
}
usernameConfig := dbplugin.UsernameConfig{
DisplayName: "a.b.c",
RoleName: "d.e.f",
}
if username, err := credsProducer.GenerateUsername(usernameConfig); err != nil {
t.Errorf("err: %s", err)
} else {
if match, err := regexp.MatchString("a_b_c_d_e_f", username); err != nil {
t.Errorf("err: %s", err)
} else if !match {
t.Errorf("does not match expected name. was '%s', expected to match '%s'", username, "a_b_c_d_e_f")
}
}
}
type testPassword string
func (tp testPassword) Generate(rand *rand.Rand, size int) reflect.Value {
credsProducer := &oracleCredentialsProducer{}
if password, err := credsProducer.GeneratePassword(); err != nil {
panic(err)
} else {
return reflect.ValueOf(testPassword(password))
}
}
func TestPasswordShouldMatchOracleRequirements(t *testing.T) {
assertion := func(password testPassword) bool {
result, _ := regexp.MatchString(fmt.Sprintf("^[[:lower:]][_[:lower:][:digit:]]{%d}$", oraclePasswordLength-1), string(password))
return result
}
if err := quick.Check(assertion, nil); err != nil {
t.Error(err)
}
}