-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemp_client_test.go
More file actions
133 lines (115 loc) · 4.01 KB
/
semp_client_test.go
File metadata and controls
133 lines (115 loc) · 4.01 KB
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package solacevaultplugin
import (
"context"
"net/http"
"net/http/httptest"
"testing"
)
func TestSEMPClient_ChangePassword_Success(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Verify basic auth
user, pass, ok := r.BasicAuth()
if !ok || user != "admin" || pass != "adminpass" {
t.Errorf("bad auth: user=%q pass=%q ok=%v", user, pass, ok)
}
// Verify POST to /SEMP
if r.Method != http.MethodPost {
t.Errorf("method = %q, want POST", r.Method)
}
if r.URL.Path != "/SEMP" {
t.Errorf("path = %q, want /SEMP", r.URL.Path)
}
w.Header().Set("Content-Type", "application/xml")
w.Write([]byte(`<rpc-reply>
<execute-result code="ok"/>
</rpc-reply>`))
}))
defer server.Close()
client := &SEMPClient{
SEMPURL: server.URL,
AdminUsername: "admin",
AdminPassword: "adminpass",
SEMPVersion: "soltr/10_4",
HTTPClient: server.Client(),
}
err := client.ChangePassword(context.Background(), "testuser", "newpassword")
if err != nil {
t.Fatalf("ChangePassword: %v", err)
}
}
func TestSEMPClient_ChangePassword_SEMPError(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/xml")
w.Write([]byte(`<rpc-reply>
<execute-result code="fail"/>
<parse-error>Invalid username</parse-error>
</rpc-reply>`))
}))
defer server.Close()
client := &SEMPClient{
SEMPURL: server.URL,
AdminUsername: "admin",
AdminPassword: "adminpass",
HTTPClient: server.Client(),
}
err := client.ChangePassword(context.Background(), "testuser", "newpassword")
if err == nil {
t.Fatal("expected error for SEMP failure")
}
}
func TestSEMPClient_ChangePassword_HTTPError(t *testing.T) {
client := &SEMPClient{
SEMPURL: "http://127.0.0.1:1",
AdminUsername: "admin",
AdminPassword: "adminpass",
HTTPClient: http.DefaultClient,
}
err := client.ChangePassword(context.Background(), "testuser", "newpassword")
if err == nil {
t.Fatal("expected error for unreachable broker")
}
}
func TestBuildChangePasswordXML(t *testing.T) {
xml := buildChangePasswordXML("soltr/10_4", "myuser", "mypass")
expected := `<rpc semp-version="soltr/10_4"><username><name>myuser</name><change-password><password>mypass</password></change-password></username></rpc>`
if xml != expected {
t.Errorf("got:\n%s\nwant:\n%s", xml, expected)
}
}
func TestBuildChangePasswordXML_NoVersion(t *testing.T) {
xml := buildChangePasswordXML("", "myuser", "mypass")
expected := `<rpc><username><name>myuser</name><change-password><password>mypass</password></change-password></username></rpc>`
if xml != expected {
t.Errorf("got:\n%s\nwant:\n%s", xml, expected)
}
}
func TestSEMPClient_ChangePassword_RedirectBlocked(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "http://evil.example.com/steal", http.StatusFound)
}))
defer server.Close()
config := &BrokerConfig{
SEMPURL: server.URL,
AdminUsername: "admin",
AdminPassword: "adminpass",
}
client := NewSEMPClient(config)
err := client.ChangePassword(context.Background(), "testuser", "newpassword")
if err == nil {
t.Fatal("expected error when server returns redirect")
}
}
func TestBuildChangePasswordXML_EscapesXMLChars(t *testing.T) {
result := buildChangePasswordXML("", "user</name><inject>", "pass&word")
expected := `<rpc><username><name>user</name><inject></name><change-password><password>pass&word</password></change-password></username></rpc>`
if result != expected {
t.Errorf("got:\n%s\nwant:\n%s", result, expected)
}
}
func TestBuildChangePasswordXML_EscapesSEMPVersion(t *testing.T) {
result := buildChangePasswordXML(`ver"1.0`, "user", "pass")
expected := `<rpc semp-version="ver"1.0"><username><name>user</name><change-password><password>pass</password></change-password></username></rpc>`
if result != expected {
t.Errorf("got:\n%s\nwant:\n%s", result, expected)
}
}