forked from dylanmei/logspout-kafka
-
Notifications
You must be signed in to change notification settings - Fork 2
/
authen_test.go
100 lines (82 loc) · 2.6 KB
/
authen_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
package kafka
import (
"os"
"testing"
"github.com/Shopify/sarama"
)
func Test_load_SASL_SCRAM_SHA256(t *testing.T) {
os.Setenv("DEBUG", "1")
os.Setenv("KAFKA_COMPRESSION_CODEC", "gzip")
config := newConfig()
options := map[string]string{
"security.protocol": "SASL_PLAINTEXT",
"sasl.mechanism": sarama.SASLTypeSCRAMSHA256,
"sasl.user": "tutorial-producer",
"sasl.password": "tutorial-producer-password",
}
authen := NewSASLAuthentication(options)
if err := authen.SetConfig(config); err != nil {
t.Error(err)
}
if !config.Net.SASL.Enable {
t.Error("SASL option be not enabled")
}
if config.Net.SASL.User == "" {
t.Error("SASL User option be not set")
}
if config.Net.SASL.Password == "" {
t.Error("SASL Password option be not set")
}
if config.Net.SASL.Mechanism != sarama.SASLTypeSCRAMSHA256 {
t.Errorf("SASL Mechanism option be not %s", sarama.SASLTypeSCRAMSHA256)
}
if scramClient := config.Net.SASL.SCRAMClientGeneratorFunc(); scramClient == nil {
t.Error("SASL's SCRAM client generator function be no work!")
}
}
func Test_load_SASL_SCRAM_SHA512(t *testing.T) {
os.Setenv("DEBUG", "1")
os.Setenv("KAFKA_COMPRESSION_CODEC", "gzip")
config := newConfig()
options := map[string]string{
"security.protocol": "SASL_PLAINTEXT",
"sasl.mechanism": sarama.SASLTypeSCRAMSHA512,
"sasl.user": "tutorial-producer",
"sasl.password": "tutorial-producer-password",
}
authen := NewSASLAuthentication(options)
if err := authen.SetConfig(config); err != nil {
t.Error(err)
}
if !config.Net.SASL.Enable {
t.Error("SASL option be not enabled")
}
if config.Net.SASL.User == "" {
t.Error("SASL User option be not set")
}
if config.Net.SASL.Password == "" {
t.Error("SASL Password option be not set")
}
if config.Net.SASL.Mechanism != sarama.SASLTypeSCRAMSHA512 {
t.Errorf("SASL Mechanism option be not %s", sarama.SASLTypeSCRAMSHA512)
}
if scramClient := config.Net.SASL.SCRAMClientGeneratorFunc(); scramClient == nil {
t.Error("SASL's SCRAM client generator function be no work!")
}
}
func Test_catch_SASL_version_invalid(t *testing.T) {
os.Setenv("DEBUG", "1")
os.Setenv("KAFKA_COMPRESSION_CODEC", "gzip")
config := newConfig()
options := map[string]string{
"security.protocol": "SASL_PLAINTEXT",
"sasl.version": "1.0.0",
"sasl.mechanism": sarama.SASLTypeSCRAMSHA512,
"sasl.user": "tutorial-producer",
"sasl.password": "tutorial-producer-password",
}
authen := NewSASLAuthentication(options)
if err := authen.SetConfig(config); err == nil {
t.Error("Fail return error when options pass invalid SASL version")
}
}