-
Notifications
You must be signed in to change notification settings - Fork 129
/
request_test.go
151 lines (120 loc) · 5.71 KB
/
request_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package winrm
import (
"strings"
"testing"
"github.com/ChrisTrenkamp/goxpath"
"github.com/ChrisTrenkamp/goxpath/tree"
"github.com/ChrisTrenkamp/goxpath/tree/xmltree"
"github.com/masterzen/simplexml/dom"
"github.com/masterzen/winrm/soap"
. "gopkg.in/check.v1"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) { TestingT(t) }
type WinRMSuite struct{}
var _ = Suite(&WinRMSuite{})
func (s *WinRMSuite) TestOpenShellRequest(c *C) {
openShell := NewOpenShellRequest("http://localhost", nil)
defer openShell.Free()
assertXPath(c, openShell.Doc(), "//a:Action", "http://schemas.xmlsoap.org/ws/2004/09/transfer/Create")
assertXPath(c, openShell.Doc(), "//a:To", "http://localhost")
assertXPath(c, openShell.Doc(), "//env:Body/rsp:Shell/rsp:InputStreams", "stdin")
assertXPath(c, openShell.Doc(), "//env:Body/rsp:Shell/rsp:OutputStreams", "stdout stderr")
}
func (s *WinRMSuite) TestDeleteShellRequest(c *C) {
request := NewDeleteShellRequest("http://localhost", "SHELLID", nil)
defer request.Free()
assertXPath(c, request.Doc(), "//a:Action", "http://schemas.xmlsoap.org/ws/2004/09/transfer/Delete")
assertXPath(c, request.Doc(), "//a:To", "http://localhost")
assertXPath(c, request.Doc(), "//w:Selector[@Name=\"ShellId\"]", "SHELLID")
}
func (s *WinRMSuite) TestExecuteCommandRequest(c *C) {
request := NewExecuteCommandRequest("http://localhost", "SHELLID", "ipconfig /all", []string{}, nil)
defer request.Free()
assertXPath(c, request.Doc(), "//a:Action", "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command")
assertXPath(c, request.Doc(), "//a:To", "http://localhost")
assertXPath(c, request.Doc(), "//w:Selector[@Name=\"ShellId\"]", "SHELLID")
assertXPath(c, request.Doc(), "//w:Option[@Name=\"WINRS_CONSOLEMODE_STDIN\"]", "TRUE")
assertXPath(c, request.Doc(), "//rsp:CommandLine/rsp:Command", "ipconfig /all")
assertXPathNil(c, request.Doc(), "//rsp:CommandLine/rsp:Arguments")
}
func (s *WinRMSuite) TestExecuteCommandWithArgumentsRequest(c *C) {
args := []string{"/p", "C:\\test.txt"}
request := NewExecuteCommandRequest("http://localhost", "SHELLID", "del", args, nil)
defer request.Free()
assertXPath(c, request.Doc(), "//a:Action", "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command")
assertXPath(c, request.Doc(), "//a:To", "http://localhost")
assertXPath(c, request.Doc(), "//w:Selector[@Name=\"ShellId\"]", "SHELLID")
assertXPath(c, request.Doc(), "//w:Option[@Name=\"WINRS_CONSOLEMODE_STDIN\"]", "TRUE")
assertXPath(c, request.Doc(), "//rsp:CommandLine/rsp:Command", "del")
assertXPath(c, request.Doc(), "//rsp:CommandLine/rsp:Arguments", "/p")
assertXPath(c, request.Doc(), "//rsp:CommandLine/rsp:Arguments", "C:\\test.txt")
}
func (s *WinRMSuite) TestGetOutputRequest(c *C) {
request := NewGetOutputRequest("http://localhost", "SHELLID", "COMMANDID", "stdout stderr", nil)
defer request.Free()
assertXPath(c, request.Doc(), "//a:Action", "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive")
assertXPath(c, request.Doc(), "//a:To", "http://localhost")
assertXPath(c, request.Doc(), "//w:Selector[@Name=\"ShellId\"]", "SHELLID")
assertXPath(c, request.Doc(), "//rsp:Receive/rsp:DesiredStream[@CommandId=\"COMMANDID\"]", "stdout stderr")
}
func (s *WinRMSuite) TestSendInputRequest(c *C) {
request := NewSendInputRequest("http://localhost", "SHELLID", "COMMANDID", []byte{31, 32}, true, nil)
defer request.Free()
assertXPath(c, request.Doc(), "//a:Action", "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send")
assertXPath(c, request.Doc(), "//a:To", "http://localhost")
assertXPath(c, request.Doc(), "//w:Selector[@Name=\"ShellId\"]", "SHELLID")
assertXPath(c, request.Doc(), "//rsp:Send/rsp:Stream[@CommandId=\"COMMANDID\"]", "HyA=")
assertXPath(c, request.Doc(), "//rsp:Send/rsp:Stream[@CommandId=\"COMMANDID\"]/@End", "true")
}
func (s *WinRMSuite) TestSendInputRequestNoEOF(c *C) {
request := NewSendInputRequest("http://localhost", "SHELLID", "COMMANDID", []byte{31, 32}, false, nil)
defer request.Free()
assertXPath(c, request.Doc(), "//rsp:Send/rsp:Stream[@CommandId=\"COMMANDID\"]", "HyA=")
assertXPathNil(c, request.Doc(), "//rsp:Send/rsp:Stream[@CommandId=\"COMMANDID\"]/@End")
}
func (s *WinRMSuite) TestSignalRequest(c *C) {
request := NewSignalRequest("http://localhost", "SHELLID", "COMMANDID", nil)
defer request.Free()
assertXPath(c, request.Doc(), "//a:Action", "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal")
assertXPath(c, request.Doc(), "//a:To", "http://localhost")
assertXPath(c, request.Doc(), "//w:Selector[@Name=\"ShellId\"]", "SHELLID")
assertXPath(c, request.Doc(), "//rsp:Signal[@CommandId=\"COMMANDID\"]/rsp:Code", "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/signal/terminate")
}
func assertXPath(c *C, doc *dom.Document, request string, expected string) {
nodes, err := parseXPath(doc, request)
if err != nil {
c.Fatalf("Xpath %s gives error %s", request, err)
}
c.Assert(len(nodes), Not(Equals), 0)
var foundValue string
for _, i := range nodes {
foundValue = i.ResValue()
if foundValue == expected {
break
}
}
if foundValue != expected {
c.Errorf("Should have found '%s', but found '%s' instead", expected, foundValue)
}
}
func assertXPathNil(c *C, doc *dom.Document, request string) {
nodes, err := parseXPath(doc, request)
if err != nil {
c.Fatalf("Xpath %s gives error %s", request, err)
}
c.Assert(len(nodes), Equals, 0)
}
func parseXPath(doc *dom.Document, request string) (tree.NodeSet, error) {
content := strings.NewReader(doc.String())
body, err := xmltree.ParseXML(content)
if err != nil {
return nil, err
}
xpExec := goxpath.MustParse(request)
nodes, err := xpExec.ExecNode(body, soap.GetAllXPathNamespaces())
if err != nil {
return nil, err
}
return nodes, nil
}