winrm 2.0.3 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +10 -10
  3. data/.rubocop.yml +26 -26
  4. data/.travis.yml +11 -11
  5. data/Gemfile +3 -3
  6. data/README.md +260 -233
  7. data/Rakefile +34 -34
  8. data/Vagrantfile +6 -6
  9. data/WinrmAppveyor.psm1 +31 -31
  10. data/appveyor.yml +51 -51
  11. data/changelog.md +104 -101
  12. data/lib/winrm.rb +39 -39
  13. data/lib/winrm/connection.rb +83 -82
  14. data/lib/winrm/connection_opts.rb +91 -91
  15. data/lib/winrm/exceptions.rb +76 -76
  16. data/lib/winrm/http/response_handler.rb +96 -96
  17. data/lib/winrm/http/transport.rb +424 -424
  18. data/lib/winrm/http/transport_factory.rb +68 -68
  19. data/lib/winrm/output.rb +59 -59
  20. data/lib/winrm/psrp/create_pipeline.xml.erb +167 -167
  21. data/lib/winrm/psrp/fragment.rb +70 -70
  22. data/lib/winrm/psrp/init_runspace_pool.xml.erb +224 -224
  23. data/lib/winrm/psrp/message.rb +130 -130
  24. data/lib/winrm/psrp/message_data.rb +42 -42
  25. data/lib/winrm/psrp/message_data/base.rb +49 -49
  26. data/lib/winrm/psrp/message_data/error_record.rb +68 -68
  27. data/lib/winrm/psrp/message_data/pipeline_host_call.rb +32 -32
  28. data/lib/winrm/psrp/message_data/pipeline_output.rb +49 -49
  29. data/lib/winrm/psrp/message_data/pipeline_state.rb +40 -40
  30. data/lib/winrm/psrp/message_data/runspacepool_host_call.rb +32 -32
  31. data/lib/winrm/psrp/message_data/runspacepool_state.rb +39 -39
  32. data/lib/winrm/psrp/message_data/session_capability.rb +36 -36
  33. data/lib/winrm/psrp/message_defragmenter.rb +62 -62
  34. data/lib/winrm/psrp/message_factory.rb +75 -75
  35. data/lib/winrm/psrp/message_fragmenter.rb +60 -60
  36. data/lib/winrm/psrp/powershell_output_decoder.rb +139 -139
  37. data/lib/winrm/psrp/receive_response_reader.rb +97 -97
  38. data/lib/winrm/psrp/session_capability.xml.erb +7 -7
  39. data/lib/winrm/psrp/uuid.rb +40 -40
  40. data/lib/winrm/shells/base.rb +180 -175
  41. data/lib/winrm/shells/cmd.rb +65 -65
  42. data/lib/winrm/shells/power_shell.rb +202 -202
  43. data/lib/winrm/shells/retryable.rb +45 -45
  44. data/lib/winrm/shells/shell_factory.rb +58 -50
  45. data/lib/winrm/version.rb +7 -7
  46. data/lib/winrm/wsmv/base.rb +59 -59
  47. data/lib/winrm/wsmv/cleanup_command.rb +61 -61
  48. data/lib/winrm/wsmv/close_shell.rb +50 -50
  49. data/lib/winrm/wsmv/command.rb +101 -101
  50. data/lib/winrm/wsmv/command_output.rb +76 -76
  51. data/lib/winrm/wsmv/command_output_decoder.rb +55 -55
  52. data/lib/winrm/wsmv/configuration.rb +46 -46
  53. data/lib/winrm/wsmv/create_pipeline.rb +66 -66
  54. data/lib/winrm/wsmv/create_shell.rb +119 -119
  55. data/lib/winrm/wsmv/header.rb +203 -203
  56. data/lib/winrm/wsmv/init_runspace_pool.rb +95 -95
  57. data/lib/winrm/wsmv/iso8601_duration.rb +60 -60
  58. data/lib/winrm/wsmv/keep_alive.rb +68 -68
  59. data/lib/winrm/wsmv/receive_response_reader.rb +126 -126
  60. data/lib/winrm/wsmv/send_data.rb +68 -68
  61. data/lib/winrm/wsmv/soap.rb +51 -51
  62. data/lib/winrm/wsmv/wql_query.rb +79 -79
  63. data/lib/winrm/wsmv/write_stdin.rb +88 -88
  64. data/tests/integration/auth_timeout_spec.rb +18 -18
  65. data/tests/integration/cmd_spec.rb +131 -110
  66. data/tests/integration/config-example.yml +16 -16
  67. data/tests/integration/issue_59_spec.rb +26 -26
  68. data/tests/integration/powershell_spec.rb +165 -165
  69. data/tests/integration/spec_helper.rb +65 -65
  70. data/tests/integration/transport_spec.rb +99 -99
  71. data/tests/integration/wql_spec.rb +16 -16
  72. data/tests/matchers.rb +60 -60
  73. data/tests/spec/configuration_spec.rb +184 -184
  74. data/tests/spec/connection_spec.rb +39 -39
  75. data/tests/spec/exception_spec.rb +50 -50
  76. data/tests/spec/http/transport_factory_spec.rb +68 -68
  77. data/tests/spec/http/transport_spec.rb +44 -44
  78. data/tests/spec/output_spec.rb +127 -127
  79. data/tests/spec/psrp/fragment_spec.rb +62 -62
  80. data/tests/spec/psrp/message_data/base_spec.rb +13 -13
  81. data/tests/spec/psrp/message_data/error_record_spec.rb +41 -41
  82. data/tests/spec/psrp/message_data/pipeline_host_call_spec.rb +25 -25
  83. data/tests/spec/psrp/message_data/pipeline_output_spec.rb +32 -32
  84. data/tests/spec/psrp/message_data/pipeline_state_spec.rb +40 -40
  85. data/tests/spec/psrp/message_data/runspace_pool_host_call_spec.rb +25 -25
  86. data/tests/spec/psrp/message_data/runspacepool_state_spec.rb +16 -16
  87. data/tests/spec/psrp/message_data/session_capability_spec.rb +30 -30
  88. data/tests/spec/psrp/message_data_spec.rb +35 -35
  89. data/tests/spec/psrp/message_defragmenter_spec.rb +47 -47
  90. data/tests/spec/psrp/message_fragmenter_spec.rb +105 -105
  91. data/tests/spec/psrp/powershell_output_decoder_spec.rb +100 -100
  92. data/tests/spec/psrp/psrp_message_spec.rb +70 -70
  93. data/tests/spec/psrp/recieve_response_reader_spec.rb +172 -172
  94. data/tests/spec/psrp/uuid_spec.rb +28 -28
  95. data/tests/spec/response_handler_spec.rb +61 -61
  96. data/tests/spec/shells/base_spec.rb +202 -202
  97. data/tests/spec/shells/cmd_spec.rb +75 -75
  98. data/tests/spec/shells/powershell_spec.rb +175 -175
  99. data/tests/spec/spec_helper.rb +47 -47
  100. data/tests/spec/stubs/clixml/error_record.xml.erb +84 -84
  101. data/tests/spec/stubs/clixml/pipeline_state.xml.erb +88 -88
  102. data/tests/spec/stubs/responses/get_command_output_response.xml.erb +13 -13
  103. data/tests/spec/stubs/responses/get_command_output_response_not_done.xml.erb +10 -10
  104. data/tests/spec/stubs/responses/get_powershell_keepalive_response.xml.erb +10 -10
  105. data/tests/spec/stubs/responses/get_powershell_output_response.xml.erb +12 -12
  106. data/tests/spec/stubs/responses/get_powershell_output_response_not_done.xml.erb +9 -9
  107. data/tests/spec/stubs/responses/open_shell_v1.xml +19 -19
  108. data/tests/spec/stubs/responses/open_shell_v2.xml +20 -20
  109. data/tests/spec/stubs/responses/soap_fault_v1.xml +36 -36
  110. data/tests/spec/stubs/responses/soap_fault_v2.xml +42 -42
  111. data/tests/spec/stubs/responses/wmi_error_v2.xml +41 -41
  112. data/tests/spec/wsmv/cleanup_command_spec.rb +22 -22
  113. data/tests/spec/wsmv/close_shell_spec.rb +17 -17
  114. data/tests/spec/wsmv/command_output_decoder_spec.rb +37 -37
  115. data/tests/spec/wsmv/command_output_spec.rb +45 -45
  116. data/tests/spec/wsmv/command_spec.rb +19 -19
  117. data/tests/spec/wsmv/configuration_spec.rb +17 -17
  118. data/tests/spec/wsmv/create_pipeline_spec.rb +31 -31
  119. data/tests/spec/wsmv/create_shell_spec.rb +38 -38
  120. data/tests/spec/wsmv/init_runspace_pool_spec.rb +36 -36
  121. data/tests/spec/wsmv/keep_alive_spec.rb +21 -21
  122. data/tests/spec/wsmv/receive_response_reader_spec.rb +123 -123
  123. data/tests/spec/wsmv/send_data_spec.rb +30 -30
  124. data/tests/spec/wsmv/wql_query_spec.rb +13 -13
  125. data/tests/spec/wsmv/write_stdin_spec.rb +22 -22
  126. data/winrm.gemspec +42 -42
  127. metadata +2 -2
@@ -1,18 +1,18 @@
1
- # encoding: UTF-8
2
- require_relative 'spec_helper'
3
-
4
- # This test may only be meaningful with kerberos auth
5
- # Against server 2012, a kerberos connection will require reauth (get a 401)
6
- # if there are no requests for >= 15 seconds
7
-
8
- describe 'Verify kerberos will reauth when necessary', kerberos: true do
9
- before(:all) do
10
- @powershell = winrm_connection.shell(:powershell)
11
- end
12
-
13
- it 'work with a 18 second sleep' do
14
- ps_command = 'Start-Sleep -s 18'
15
- output = @powershell.run(ps_command)
16
- expect(output.exitcode).to eq(0)
17
- end
18
- end
1
+ # encoding: UTF-8
2
+ require_relative 'spec_helper'
3
+
4
+ # This test may only be meaningful with kerberos auth
5
+ # Against server 2012, a kerberos connection will require reauth (get a 401)
6
+ # if there are no requests for >= 15 seconds
7
+
8
+ describe 'Verify kerberos will reauth when necessary', kerberos: true do
9
+ before(:all) do
10
+ @powershell = winrm_connection.shell(:powershell)
11
+ end
12
+
13
+ it 'work with a 18 second sleep' do
14
+ ps_command = 'Start-Sleep -s 18'
15
+ output = @powershell.run(ps_command)
16
+ expect(output.exitcode).to eq(0)
17
+ end
18
+ end
@@ -1,110 +1,131 @@
1
- # encoding: UTF-8
2
- require_relative 'spec_helper'
3
-
4
- describe 'winrm client cmd' do
5
- before(:all) do
6
- @cmd_shell = winrm_connection.shell(:cmd)
7
- end
8
-
9
- describe 'empty string' do
10
- subject(:output) { @cmd_shell.run('') }
11
- it { should have_exit_code 0 }
12
- it { should have_no_stdout }
13
- it { should have_no_stderr }
14
- end
15
-
16
- describe 'ipconfig' do
17
- subject(:output) { @cmd_shell.run('ipconfig') }
18
- it { should have_exit_code 0 }
19
- it { should have_stdout_match(/Windows IP Configuration/) }
20
- it { should have_no_stderr }
21
- end
22
-
23
- describe 'echo \'hello world\' using apostrophes' do
24
- subject(:output) { @cmd_shell.run("echo 'hello world'") }
25
- it { should have_exit_code 0 }
26
- it { should have_stdout_match(/'hello world'/) }
27
- it { should have_no_stderr }
28
- end
29
-
30
- describe 'multi stream output from large file' do
31
- subject(:output) { @cmd_shell.run('type c:\windows\logs\dism\dism.log') }
32
- it { should have_exit_code 0 }
33
- it { should have_no_stderr }
34
- end
35
-
36
- describe 'echo "string with trailing \\" using double quotes' do
37
- # This is a regression test for #131. " is converted to " when serializing
38
- # the command to SOAP/XML. Any naive substitution performed on such a serialized
39
- # string can result in any \& sequence being interpreted as a back-substitution.
40
- subject(:output) { @cmd_shell.run('echo "string with trailing \\"') }
41
- it { should have_exit_code 0 }
42
- it { should have_stdout_match(/string with trailing \\/) }
43
- it { should have_no_stderr }
44
- end
45
-
46
- describe 'capturing output from stdout and stderr' do
47
- subject(:output) do
48
- # Note: Multiple lines doesn't work:
49
- # script = <<-eos
50
- # echo Hello
51
- # echo , world! 1>&2
52
- # eos
53
-
54
- script = 'echo Hello & echo , world! 1>&2'
55
-
56
- @captured_stdout = ''
57
- @captured_stderr = ''
58
- @cmd_shell.run(script) do |stdout, stderr|
59
- @captured_stdout << stdout if stdout
60
- @captured_stderr << stderr if stderr
61
- end
62
- end
63
-
64
- it 'should have stdout' do
65
- expect(output.stdout).to eq("Hello \r\n")
66
- expect(output.stdout).to eq(@captured_stdout)
67
- end
68
-
69
- it 'should have stderr' do
70
- expect(output.stderr).to eq(", world! \r\n")
71
- expect(output.stderr).to eq(@captured_stderr)
72
- end
73
-
74
- it 'should have output' do
75
- expect(output.output).to eq("Hello \r\n, world! \r\n")
76
- end
77
- end
78
-
79
- describe 'ipconfig with /all argument' do
80
- subject(:output) { @cmd_shell.run('ipconfig', %w(/all)) }
81
- it { should have_exit_code 0 }
82
- it { should have_stdout_match(/Windows IP Configuration/) }
83
- it { should have_no_stderr }
84
- end
85
-
86
- describe 'dir with incorrect argument /z' do
87
- subject(:output) { @cmd_shell.run('dir /z') }
88
- it { should have_exit_code 1 }
89
- it { should have_no_stdout }
90
- it { should have_stderr_match(/Invalid switch/) }
91
- end
92
-
93
- describe 'ipconfig && echo error 1>&2' do
94
- subject(:output) { @cmd_shell.run('ipconfig && echo error 1>&2') }
95
- it { should have_exit_code 0 }
96
- it { should have_stdout_match(/Windows IP Configuration/) }
97
- it { should have_stderr_match(/error/) }
98
- end
99
-
100
- describe 'ipconfig with a block' do
101
- subject(:stdout) do
102
- outvar = ''
103
- @cmd_shell.run('ipconfig') do |stdout, _stderr|
104
- outvar << stdout
105
- end
106
- outvar
107
- end
108
- it { should match(/Windows IP Configuration/) }
109
- end
110
- end
1
+ # encoding: UTF-8
2
+ require_relative 'spec_helper'
3
+
4
+ describe 'winrm client cmd' do
5
+ before(:all) do
6
+ @cmd_shell = winrm_connection.shell(:cmd)
7
+ end
8
+
9
+ describe 'empty string' do
10
+ subject(:output) { @cmd_shell.run('') }
11
+ it { should have_exit_code 0 }
12
+ it { should have_no_stdout }
13
+ it { should have_no_stderr }
14
+ end
15
+
16
+ describe 'ipconfig' do
17
+ subject(:output) { @cmd_shell.run('ipconfig') }
18
+ it { should have_exit_code 0 }
19
+ it { should have_stdout_match(/Windows IP Configuration/) }
20
+ it { should have_no_stderr }
21
+ end
22
+
23
+ describe 'codepage' do
24
+ let(:options) { Hash.new }
25
+ let(:shell) { winrm_connection.shell(:cmd, options) }
26
+
27
+ after { shell.close }
28
+
29
+ subject(:output) { shell.run('chcp') }
30
+
31
+ it 'should default to UTF-8 (65001)' do
32
+ should have_stdout_match(/Active code page: 65001/)
33
+ end
34
+
35
+ context 'when changing the codepage' do
36
+ let(:options) { { codepage: 437 } }
37
+
38
+ it 'sets the codepage to the one given' do
39
+ should have_stdout_match(/Active code page: 437/)
40
+ end
41
+ end
42
+ end
43
+
44
+ describe 'echo \'hello world\' using apostrophes' do
45
+ subject(:output) { @cmd_shell.run("echo 'hello world'") }
46
+ it { should have_exit_code 0 }
47
+ it { should have_stdout_match(/'hello world'/) }
48
+ it { should have_no_stderr }
49
+ end
50
+
51
+ describe 'multi stream output from large file' do
52
+ subject(:output) { @cmd_shell.run('type c:\windows\logs\dism\dism.log') }
53
+ it { should have_exit_code 0 }
54
+ it { should have_no_stderr }
55
+ end
56
+
57
+ describe 'echo "string with trailing \\" using double quotes' do
58
+ # This is a regression test for #131. " is converted to &quot; when serializing
59
+ # the command to SOAP/XML. Any naive substitution performed on such a serialized
60
+ # string can result in any \& sequence being interpreted as a back-substitution.
61
+ subject(:output) { @cmd_shell.run('echo "string with trailing \\"') }
62
+ it { should have_exit_code 0 }
63
+ it { should have_stdout_match(/string with trailing \\/) }
64
+ it { should have_no_stderr }
65
+ end
66
+
67
+ describe 'capturing output from stdout and stderr' do
68
+ subject(:output) do
69
+ # Note: Multiple lines doesn't work:
70
+ # script = <<-eos
71
+ # echo Hello
72
+ # echo , world! 1>&2
73
+ # eos
74
+
75
+ script = 'echo Hello & echo , world! 1>&2'
76
+
77
+ @captured_stdout = ''
78
+ @captured_stderr = ''
79
+ @cmd_shell.run(script) do |stdout, stderr|
80
+ @captured_stdout << stdout if stdout
81
+ @captured_stderr << stderr if stderr
82
+ end
83
+ end
84
+
85
+ it 'should have stdout' do
86
+ expect(output.stdout).to eq("Hello \r\n")
87
+ expect(output.stdout).to eq(@captured_stdout)
88
+ end
89
+
90
+ it 'should have stderr' do
91
+ expect(output.stderr).to eq(", world! \r\n")
92
+ expect(output.stderr).to eq(@captured_stderr)
93
+ end
94
+
95
+ it 'should have output' do
96
+ expect(output.output).to eq("Hello \r\n, world! \r\n")
97
+ end
98
+ end
99
+
100
+ describe 'ipconfig with /all argument' do
101
+ subject(:output) { @cmd_shell.run('ipconfig', %w(/all)) }
102
+ it { should have_exit_code 0 }
103
+ it { should have_stdout_match(/Windows IP Configuration/) }
104
+ it { should have_no_stderr }
105
+ end
106
+
107
+ describe 'dir with incorrect argument /z' do
108
+ subject(:output) { @cmd_shell.run('dir /z') }
109
+ it { should have_exit_code 1 }
110
+ it { should have_no_stdout }
111
+ it { should have_stderr_match(/Invalid switch/) }
112
+ end
113
+
114
+ describe 'ipconfig && echo error 1>&2' do
115
+ subject(:output) { @cmd_shell.run('ipconfig && echo error 1>&2') }
116
+ it { should have_exit_code 0 }
117
+ it { should have_stdout_match(/Windows IP Configuration/) }
118
+ it { should have_stderr_match(/error/) }
119
+ end
120
+
121
+ describe 'ipconfig with a block' do
122
+ subject(:stdout) do
123
+ outvar = ''
124
+ @cmd_shell.run('ipconfig') do |stdout, _stderr|
125
+ outvar << stdout
126
+ end
127
+ outvar
128
+ end
129
+ it { should match(/Windows IP Configuration/) }
130
+ end
131
+ end
@@ -1,16 +1,16 @@
1
- # Copy this file to config.yml and edit the settings below.
2
- # This should work out of the box for vagrant provisioned boxes.
3
-
4
- ## Kerberos
5
- # auth_type: kerberos
6
- # realm: "your.realm"
7
- # endpoint: "http://<yourserver>:5985/wsman"
8
-
9
- # If you are running this in a vagrant provisioned box using NAT,
10
- # this will be the forwarded WinRM HTTP port to your VM.
11
- # If you are running this on the VM, the default HTTP port is 5985.
12
- # See README.md#Troubleshooting.
13
-
14
- endpoint: "http://localhost:55985/wsman"
15
- user: vagrant
16
- password: vagrant
1
+ # Copy this file to config.yml and edit the settings below.
2
+ # This should work out of the box for vagrant provisioned boxes.
3
+
4
+ ## Kerberos
5
+ # auth_type: kerberos
6
+ # realm: "your.realm"
7
+ # endpoint: "http://<yourserver>:5985/wsman"
8
+
9
+ # If you are running this in a vagrant provisioned box using NAT,
10
+ # this will be the forwarded WinRM HTTP port to your VM.
11
+ # If you are running this on the VM, the default HTTP port is 5985.
12
+ # See README.md#Troubleshooting.
13
+
14
+ endpoint: "http://localhost:55985/wsman"
15
+ user: vagrant
16
+ password: vagrant
@@ -1,26 +1,26 @@
1
- # encoding: UTF-8
2
- require_relative 'spec_helper'
3
-
4
- describe 'issue 59' do
5
- describe 'long running script without output' do
6
- let(:logged_output) { StringIO.new }
7
- let(:logger) { Logging.logger(logged_output) }
8
-
9
- before do
10
- opts = connection_opts.dup
11
- opts[:operation_timeout] = 1
12
- conn = WinRM::Connection.new(opts)
13
- conn.logger = logger
14
- @powershell = conn.shell(:powershell)
15
- end
16
-
17
- it 'should not error' do
18
- out = @powershell.run('$ProgressPreference="SilentlyContinue";sleep 3; Write-Host "Hello"')
19
-
20
- expect(out).to have_exit_code 0
21
- expect(out).to have_stdout_match(/Hello/)
22
- expect(out).to have_no_stderr
23
- expect(logged_output.string).to match(/retrying receive request/)
24
- end
25
- end
26
- end
1
+ # encoding: UTF-8
2
+ require_relative 'spec_helper'
3
+
4
+ describe 'issue 59' do
5
+ describe 'long running script without output' do
6
+ let(:logged_output) { StringIO.new }
7
+ let(:logger) { Logging.logger(logged_output) }
8
+
9
+ before do
10
+ opts = connection_opts.dup
11
+ opts[:operation_timeout] = 1
12
+ conn = WinRM::Connection.new(opts)
13
+ conn.logger = logger
14
+ @powershell = conn.shell(:powershell)
15
+ end
16
+
17
+ it 'should not error' do
18
+ out = @powershell.run('$ProgressPreference="SilentlyContinue";sleep 3; Write-Host "Hello"')
19
+
20
+ expect(out).to have_exit_code 0
21
+ expect(out).to have_stdout_match(/Hello/)
22
+ expect(out).to have_no_stderr
23
+ expect(logged_output.string).to match(/retrying receive request/)
24
+ end
25
+ end
26
+ end
@@ -1,165 +1,165 @@
1
- # encoding: UTF-8
2
- require_relative 'spec_helper'
3
-
4
- describe 'winrm client powershell' do
5
- before(:all) do
6
- @powershell = winrm_connection.shell(:powershell)
7
- end
8
-
9
- describe 'ipconfig' do
10
- subject(:output) { @powershell.run('ipconfig') }
11
- it { should have_exit_code 0 }
12
- it { should have_stdout_match(/Windows IP Configuration/) }
13
- it { should have_no_stderr }
14
- end
15
-
16
- describe 'ipconfig with invalid args' do
17
- subject(:output) { @powershell.run('ipconfig blah') }
18
- it { should have_exit_code 1 }
19
- end
20
-
21
- describe 'throw' do
22
- subject(:output) { @powershell.run("throw 'an error occured'") }
23
- it { should have_exit_code 0 }
24
- it { should have_stderr_match(/an error occured/) }
25
- end
26
-
27
- describe 'exit' do
28
- subject(:output) { @powershell.run('exit 5') }
29
- it { should have_exit_code 5 }
30
- end
31
-
32
- describe 'echo \'hello world\' using apostrophes' do
33
- subject(:output) { @powershell.run("echo 'hello world'") }
34
- it { should have_exit_code 0 }
35
- it { should have_stdout_match(/hello world/) }
36
- it { should have_no_stderr }
37
- end
38
-
39
- describe 'handling special XML characters' do
40
- subject(:output) { @powershell.run("echo 'hello & <world>'") }
41
- it { should have_exit_code 0 }
42
- it { should have_stdout_match(/hello & <world>/) }
43
- it { should have_no_stderr }
44
- end
45
-
46
- describe 'dir with incorrect argument /z' do
47
- subject(:output) { @powershell.run('dir /z') }
48
- it { should have_stderr_match(/Cannot find path/) }
49
- it { should have_no_stdout }
50
- end
51
-
52
- describe 'Math area calculation' do
53
- subject(:output) do
54
- @powershell.run <<-EOH
55
- $diameter = 4.5
56
- $area = [Math]::pow([Math]::PI * ($diameter/2), 2)
57
- Write-Host $area
58
- EOH
59
- end
60
- it { should have_exit_code 0 }
61
- it { should have_stdout_match(/49.9648722805149/) }
62
- it { should have_no_stderr }
63
- end
64
-
65
- describe 'ipconfig with a block' do
66
- subject(:stdout) do
67
- outvar = ''
68
- @powershell.run('ipconfig') do |stdout, _stderr|
69
- outvar << stdout
70
- end
71
- outvar
72
- end
73
- it { should match(/Windows IP Configuration/) }
74
- end
75
-
76
- describe 'capturing output from Write-Host and Write-Error' do
77
- subject(:output) do
78
- script = <<-eos
79
- Write-Host 'Hello'
80
- $host.ui.WriteErrorLine(', world!')
81
- eos
82
-
83
- @captured_stdout = ''
84
- @captured_stderr = ''
85
- @powershell.run(script) do |stdout, stderr|
86
- @captured_stdout << stdout if stdout
87
- @captured_stderr << stderr if stderr
88
- end
89
- end
90
-
91
- it 'should have stdout' do
92
- expect(output.stdout).to eq("Hello\r\n")
93
- expect(output.stdout).to eq(@captured_stdout)
94
- end
95
-
96
- it 'should have stderr' do
97
- expect(output.stderr).to eq(", world!\r\n")
98
- expect(output.stderr).to eq(@captured_stderr)
99
- end
100
-
101
- it 'should have output' do
102
- expect(output.output).to eq("Hello\r\n, world!\r\n")
103
- end
104
- end
105
-
106
- describe 'capturing output from pipeline followed by Host' do
107
- subject(:output) do
108
- script = <<-eos
109
- Write-Output 'output'
110
- $host.UI.Writeline('host')
111
- eos
112
-
113
- @captured_stdout = ''
114
- @captured_stderr = ''
115
- @powershell.run(script) do |stdout, stderr|
116
- @captured_stdout << stdout if stdout
117
- @captured_stderr << stderr if stderr
118
- end
119
- end
120
-
121
- it 'should print from the pipeline first' do
122
- expect(output.stdout).to start_with("output\r\n")
123
- end
124
-
125
- it 'should write to host last' do
126
- expect(output.stdout).to end_with("host\r\n")
127
- end
128
- end
129
-
130
- describe 'it should handle utf-8 characters' do
131
- subject(:output) { @powershell.run('echo "✓1234-äöü"') }
132
- it { should have_exit_code 0 }
133
- it { should have_stdout_match(/✓1234-äöü/) }
134
- end
135
-
136
- describe 'output exceeds a single fragment' do
137
- subject(:output) { @powershell.run('Write-Output $("a"*600000)') }
138
- it { should have_exit_code 0 }
139
- it 'has assebled the output' do
140
- expect(output.stdout).to eq('a' * 600000 + "\r\n")
141
- end
142
- end
143
-
144
- describe 'command exceeds a single fragment' do
145
- subject(:output) { @powershell.run("$var='#{'a' * 600000}';Write-Output 'long var'") }
146
- it { should have_exit_code 0 }
147
- it 'has sent the output' do
148
- expect(output.stdout).to eq("long var\r\n")
149
- end
150
- end
151
-
152
- describe 'reading pipeline messages' do
153
- subject(:messages) { @powershell.send_pipeline_command('ipconfig') }
154
-
155
- it 'returns multiple messages' do
156
- expect(messages.length).to be > 1
157
- end
158
- it 'first message is pipeline output' do
159
- expect(messages.first.type).to eq(WinRM::PSRP::Message::MESSAGE_TYPES[:pipeline_output])
160
- end
161
- it 'last message is pipeline state' do
162
- expect(messages.last.type).to eq(WinRM::PSRP::Message::MESSAGE_TYPES[:pipeline_state])
163
- end
164
- end
165
- end
1
+ # encoding: UTF-8
2
+ require_relative 'spec_helper'
3
+
4
+ describe 'winrm client powershell' do
5
+ before(:all) do
6
+ @powershell = winrm_connection.shell(:powershell)
7
+ end
8
+
9
+ describe 'ipconfig' do
10
+ subject(:output) { @powershell.run('ipconfig') }
11
+ it { should have_exit_code 0 }
12
+ it { should have_stdout_match(/Windows IP Configuration/) }
13
+ it { should have_no_stderr }
14
+ end
15
+
16
+ describe 'ipconfig with invalid args' do
17
+ subject(:output) { @powershell.run('ipconfig blah') }
18
+ it { should have_exit_code 1 }
19
+ end
20
+
21
+ describe 'throw' do
22
+ subject(:output) { @powershell.run("throw 'an error occured'") }
23
+ it { should have_exit_code 0 }
24
+ it { should have_stderr_match(/an error occured/) }
25
+ end
26
+
27
+ describe 'exit' do
28
+ subject(:output) { @powershell.run('exit 5') }
29
+ it { should have_exit_code 5 }
30
+ end
31
+
32
+ describe 'echo \'hello world\' using apostrophes' do
33
+ subject(:output) { @powershell.run("echo 'hello world'") }
34
+ it { should have_exit_code 0 }
35
+ it { should have_stdout_match(/hello world/) }
36
+ it { should have_no_stderr }
37
+ end
38
+
39
+ describe 'handling special XML characters' do
40
+ subject(:output) { @powershell.run("echo 'hello & <world>'") }
41
+ it { should have_exit_code 0 }
42
+ it { should have_stdout_match(/hello & <world>/) }
43
+ it { should have_no_stderr }
44
+ end
45
+
46
+ describe 'dir with incorrect argument /z' do
47
+ subject(:output) { @powershell.run('dir /z') }
48
+ it { should have_stderr_match(/Cannot find path/) }
49
+ it { should have_no_stdout }
50
+ end
51
+
52
+ describe 'Math area calculation' do
53
+ subject(:output) do
54
+ @powershell.run <<-EOH
55
+ $diameter = 4.5
56
+ $area = [Math]::pow([Math]::PI * ($diameter/2), 2)
57
+ Write-Host $area
58
+ EOH
59
+ end
60
+ it { should have_exit_code 0 }
61
+ it { should have_stdout_match(/49.9648722805149/) }
62
+ it { should have_no_stderr }
63
+ end
64
+
65
+ describe 'ipconfig with a block' do
66
+ subject(:stdout) do
67
+ outvar = ''
68
+ @powershell.run('ipconfig') do |stdout, _stderr|
69
+ outvar << stdout
70
+ end
71
+ outvar
72
+ end
73
+ it { should match(/Windows IP Configuration/) }
74
+ end
75
+
76
+ describe 'capturing output from Write-Host and Write-Error' do
77
+ subject(:output) do
78
+ script = <<-eos
79
+ Write-Host 'Hello'
80
+ $host.ui.WriteErrorLine(', world!')
81
+ eos
82
+
83
+ @captured_stdout = ''
84
+ @captured_stderr = ''
85
+ @powershell.run(script) do |stdout, stderr|
86
+ @captured_stdout << stdout if stdout
87
+ @captured_stderr << stderr if stderr
88
+ end
89
+ end
90
+
91
+ it 'should have stdout' do
92
+ expect(output.stdout).to eq("Hello\r\n")
93
+ expect(output.stdout).to eq(@captured_stdout)
94
+ end
95
+
96
+ it 'should have stderr' do
97
+ expect(output.stderr).to eq(", world!\r\n")
98
+ expect(output.stderr).to eq(@captured_stderr)
99
+ end
100
+
101
+ it 'should have output' do
102
+ expect(output.output).to eq("Hello\r\n, world!\r\n")
103
+ end
104
+ end
105
+
106
+ describe 'capturing output from pipeline followed by Host' do
107
+ subject(:output) do
108
+ script = <<-eos
109
+ Write-Output 'output'
110
+ $host.UI.Writeline('host')
111
+ eos
112
+
113
+ @captured_stdout = ''
114
+ @captured_stderr = ''
115
+ @powershell.run(script) do |stdout, stderr|
116
+ @captured_stdout << stdout if stdout
117
+ @captured_stderr << stderr if stderr
118
+ end
119
+ end
120
+
121
+ it 'should print from the pipeline first' do
122
+ expect(output.stdout).to start_with("output\r\n")
123
+ end
124
+
125
+ it 'should write to host last' do
126
+ expect(output.stdout).to end_with("host\r\n")
127
+ end
128
+ end
129
+
130
+ describe 'it should handle utf-8 characters' do
131
+ subject(:output) { @powershell.run('echo "✓1234-äöü"') }
132
+ it { should have_exit_code 0 }
133
+ it { should have_stdout_match(/✓1234-äöü/) }
134
+ end
135
+
136
+ describe 'output exceeds a single fragment' do
137
+ subject(:output) { @powershell.run('Write-Output $("a"*600000)') }
138
+ it { should have_exit_code 0 }
139
+ it 'has assebled the output' do
140
+ expect(output.stdout).to eq('a' * 600000 + "\r\n")
141
+ end
142
+ end
143
+
144
+ describe 'command exceeds a single fragment' do
145
+ subject(:output) { @powershell.run("$var='#{'a' * 600000}';Write-Output 'long var'") }
146
+ it { should have_exit_code 0 }
147
+ it 'has sent the output' do
148
+ expect(output.stdout).to eq("long var\r\n")
149
+ end
150
+ end
151
+
152
+ describe 'reading pipeline messages' do
153
+ subject(:messages) { @powershell.send_pipeline_command('ipconfig') }
154
+
155
+ it 'returns multiple messages' do
156
+ expect(messages.length).to be > 1
157
+ end
158
+ it 'first message is pipeline output' do
159
+ expect(messages.first.type).to eq(WinRM::PSRP::Message::MESSAGE_TYPES[:pipeline_output])
160
+ end
161
+ it 'last message is pipeline state' do
162
+ expect(messages.last.type).to eq(WinRM::PSRP::Message::MESSAGE_TYPES[:pipeline_state])
163
+ end
164
+ end
165
+ end