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 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
package winrm
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"strings"
"sync"
"time"
"github.com/masterzen/winrm/soap"
. "gopkg.in/check.v1"
)
func (s *WinRMSuite) TestExecuteCommand(c *C) {
endpoint := NewEndpoint("localhost", 5985, false, false, nil, nil, nil, 0)
client, err := NewClient(endpoint, "Administrator", "v3r1S3cre7")
c.Assert(err, IsNil)
shell := &Shell{client: client, id: "67A74734-DD32-4F10-89DE-49A060483810"}
count := 0
r := Requester{}
r.http = func(client *Client, message *soap.SoapMessage) (string, error) {
switch count {
case 0:
{
c.Assert(message.String(), Contains, "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command")
count = 1
return executeCommandResponse, nil
}
case 1:
{
c.Assert(message.String(), Contains, "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive")
count = 2
return outputResponse, nil
}
default:
{
return doneCommandResponse, nil
}
}
}
client.http = r
command, _ := shell.Execute("ipconfig /all")
var stdout, stderr bytes.Buffer
var wg sync.WaitGroup
f := func(b *bytes.Buffer, r *commandReader) {
wg.Add(1)
defer wg.Done()
io.Copy(b, r)
}
go f(&stdout, command.Stdout)
go f(&stderr, command.Stderr)
command.Wait()
wg.Wait()
c.Assert(stdout.String(), Equals, "That's all folks!!!")
c.Assert(stderr.String(), Equals, "This is stderr, I'm pretty sure!")
}
func (s *WinRMSuite) TestStdinCommand(c *C) {
endpoint := NewEndpoint("localhost", 5985, false, false, nil, nil, nil, 0)
client, err := NewClient(endpoint, "Administrator", "v3r1S3cre7")
c.Assert(err, IsNil)
shell := &Shell{
client: client,
id: "67A74734-DD32-4F10-89DE-49A060483810",
}
count := 0
r := Requester{}
r.http = func(client *Client, message *soap.SoapMessage) (string, error) {
if strings.Contains(message.String(), "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Send") {
c.Assert(message.String(), Contains, "c3RhbmRhcmQgaW5wdXQ=")
return "", nil
} else {
if strings.Contains(message.String(), "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command") {
return executeCommandResponse, nil
} else if count != 1 && strings.Contains(message.String(), "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive") {
count = 1
return outputResponse, nil
} else {
return doneCommandResponse, nil
}
}
}
client.http = r
command, _ := shell.Execute("ipconfig /all")
command.Stdin.Write([]byte("standard input"))
// slurp output from command
var outWriter, errWriter bytes.Buffer
go io.Copy(&outWriter, command.Stdout)
go io.Copy(&errWriter, command.Stderr)
command.Wait()
}
func (s *WinRMSuite) TestCommandExitCode(c *C) {
endpoint := NewEndpoint("localhost", 5985, false, false, nil, nil, nil, 0)
client, err := NewClient(endpoint, "Administrator", "v3r1S3cre7")
c.Assert(err, IsNil)
shell := &Shell{
client: client,
id: "67A74734-DD32-4F10-89DE-49A060483810",
}
count := 0
r := Requester{}
r.http = func(client *Client, message *soap.SoapMessage) (string, error) {
defer func() { count++ }()
switch count {
case 0:
return executeCommandResponse, nil
case 1:
return doneCommandResponse, nil
default:
c.Log("Mimicking some observed Windows behavior where only the first 'done' response has the actual exit code and 0 afterwards")
return doneCommandExitCode0Response, nil
}
}
client.http = r
command, _ := shell.Execute("ipconfig /all")
command.Wait()
<-time.After(time.Second) // to make the test fail if fetchOutput races to re-set the exit code
c.Assert(command.ExitCode(), Equals, 123)
}
func (s *WinRMSuite) TestCloseCommandStopsFetch(c *C) {
endpoint := NewEndpoint("localhost", 5985, false, false, nil, nil, nil, 0)
client, err := NewClient(endpoint, "Administrator", "v3r1S3cre7")
c.Assert(err, IsNil)
shell := &Shell{client: client, id: "67A74734-DD32-4F10-89DE-49A060483810"}
httpChan := make(chan string)
r := Requester{}
r.http = func(client *Client, message *soap.SoapMessage) (string, error) {
switch {
case strings.Contains(message.String(), "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Receive"):
c.Log("Request for command output received by server")
r := <-httpChan
c.Log("Returning command output")
return r, nil
case strings.Contains(message.String(), "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Command"):
return executeCommandResponse, nil
case strings.Contains(message.String(), "http://schemas.microsoft.com/wbem/wsman/1/windows/shell/Signal"):
c.Log("Signal message received by server")
return "", nil // response is not used
default:
c.Logf("Unexpected message: %s", message)
return "", nil
}
}
client.http = r
command, _ := shell.Execute("ipconfig /all")
// need to be reading Stdout/Stderr, otherwise, the writes to these are blocking...
go ioutil.ReadAll(command.Stdout)
go ioutil.ReadAll(command.Stderr)
httpChan <- outputResponse // wait for command to enter fetch/slurp
command.Close()
select {
case httpChan <- outputResponse: // return to fetch from slurp
c.Log("Fetch loop 'drained' one last reponse before realizing that the command is now closed")
case <-time.After(1 * time.Second):
c.Log("no poll within one second, fetch may have stopped")
}
select {
case httpChan <- outputResponse:
c.Log("Fetch loop is still polling after command.Close()")
c.FailNow()
case <-time.After(1 * time.Second):
c.Log("no poll within one second, assuming fetch has stopped")
}
}
func (s *WinRMSuite) TestConnectionTimeout(c *C) {
count := 0
ts, host, port, err := StartTestServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(2 * time.Second)
w.Header().Set("Content-Type", "application/soap+xml")
switch count {
case 0:
{
count = 1
fmt.Fprintln(w, executeCommandResponse)
}
case 1:
{
count = 2
fmt.Fprintln(w, outputResponse)
}
default:
{
fmt.Fprintln(w, doneCommandResponse)
}
}
}))
defer ts.Close()
if err != nil {
c.Error(err)
}
endpoint := NewEndpoint(host, port, false, false, nil, nil, nil, 1*time.Second)
client, err := NewClient(endpoint, "Administrator", "v3r1S3cre7")
c.Assert(err, IsNil)
shell := &Shell{client: client, id: "67A74734-DD32-4F10-89DE-49A060483810"}
_, err = shell.Execute("ipconfig /all")
c.Assert(err, ErrorMatches, ".*timeout.*")
}
func (s *WinRMSuite) TestOperationTimeoutSupport(c *C) {
count := 0
ts, host, port, err := StartTestServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/soap+xml")
switch count {
case 0:
{
count = 1
fmt.Fprintln(w, executeCommandResponse)
}
case 1:
{
count = 2
w.WriteHeader(500)
fmt.Fprintln(w, operationTimeoutResponse)
}
case 2:
{
count = 3
fmt.Fprintln(w, outputResponse)
}
default:
{
fmt.Fprintln(w, doneCommandResponse)
}
}
}))
defer ts.Close()
if err != nil {
c.Error(err)
}
endpoint := NewEndpoint(host, port, false, false, nil, nil, nil, 0)
client, err := NewClient(endpoint, "Administrator", "v3r1S3cre7")
c.Assert(err, IsNil)
shell := &Shell{client: client, id: "67A74734-DD32-4F10-89DE-49A060483810"}
command, _ := shell.Execute("ipconfig /all")
var stdout, stderr bytes.Buffer
var wg sync.WaitGroup
f := func(b *bytes.Buffer, r *commandReader) {
wg.Add(1)
defer wg.Done()
io.Copy(b, r)
}
go f(&stdout, command.Stdout)
go f(&stderr, command.Stderr)
command.Wait()
wg.Wait()
c.Assert(stdout.String(), Equals, "That's all folks!!!")
c.Assert(stderr.String(), Equals, "This is stderr, I'm pretty sure!")
}
func (s *WinRMSuite) TestEOFError(c *C) {
count := 0
endpoint := NewEndpoint("localhost", 5985, false, false, nil, nil, nil, 0)
client, err := NewClient(endpoint, "Administrator", "v3r1S3cre7")
c.Assert(err, IsNil)
r := Requester{}
// simulating a dropped client connection
r.http = func(client *Client, message *soap.SoapMessage) (string, error) {
defer func() { count++ }()
switch count {
case 0:
return executeCommandResponse, nil
case 1:
return "", fmt.Errorf("http response error: 200 - /wsman EOF")
default:
return doneCommandExitCode0Response, nil
}
}
client.http = r
shell := &Shell{client: client, id: "67A74734-DD32-4F10-89DE-49A060483810"}
command, _ := shell.Execute("ipconfig /all")
command.Wait()
c.Assert(command.exitCode, Equals, 16001)
c.Assert(command.err.Error(), Contains, "EOF")
}
|