/usr/share/gocode/src/github.com/masterzen/winrm/request_test.go is in golang-github-masterzen-winrm-dev 0.0~git20170601.0.1ca0ba6-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package winrm
import (
"strings"
"testing"
"github.com/masterzen/simplexml/dom"
"github.com/masterzen/winrm/soap"
"github.com/masterzen/xmlpath"
. "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}, 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/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) {
root, path, err := parseXPath(doc, request)
if err != nil {
c.Fatalf("Xpath %s gives error %s", request, err)
}
ok := path.Exists(root)
c.Assert(ok, Equals, true)
var foundValue string
iter := path.Iter(root)
for iter.Next() {
foundValue = iter.Node().String()
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) {
root, path, err := parseXPath(doc, request)
if err != nil {
c.Fatalf("Xpath %s gives error %s", request, err)
}
ok := path.Exists(root)
c.Assert(ok, Equals, false)
}
func parseXPath(doc *dom.Document, request string) (*xmlpath.Node, *xmlpath.Path, error) {
content := strings.NewReader(doc.String())
node, err := xmlpath.Parse(content)
if err != nil {
return nil, nil, err
}
path, err := xmlpath.CompileWithNamespace(request, soap.GetAllNamespaces())
if err != nil {
return nil, nil, err
}
return node, path, nil
}
|