/
request_test.go
97 lines (78 loc) · 3.81 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
package winrm
import (
"github.com/masterzen/simplexml/dom"
"github.com/masterzen/xmlpath"
"github.com/sneal/go-winrm/soap"
. "gopkg.in/check.v1"
"strings"
"testing"
)
// 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", 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\"")
}
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}, 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=")
}
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/Command")
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, node *dom.Document, request string, expected string) {
content := strings.NewReader(node.String())
path, err := xmlpath.CompileWithNamespace(request, soap.GetAllNamespaces())
if err != nil {
c.Fatalf("Xpath %s gives error %s", request, err)
}
var root *xmlpath.Node
root, err = xmlpath.Parse(content)
if err != nil {
c.Fatalf("Xpath %s gives error %s", request, err)
}
var e string
var ok bool
e, ok = path.String(root)
c.Assert(ok, Equals, true)
c.Assert(e, Equals, expected)
}