summaryrefslogtreecommitdiff
path: root/spec/mixlib/shellout/windows_spec.rb
blob: 2b273a6c757656917d9f747765d2c4fa77b326c8 (plain)
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
require "spec_helper"

# FIXME: these are stubby enough unit tests that they almost run under unix, but the
# Mixlib::ShellOut object does not mixin the Windows behaviors when running on unix.
describe "Mixlib::ShellOut::Windows", :windows_only do

  describe "Utils" do
    describe ".should_run_under_cmd?" do
      subject { Mixlib::ShellOut.new.send(:should_run_under_cmd?, command) }

      def self.with_command(_command, &example)
        context "with command: #{_command}" do
          let(:command) { _command }
          it(&example)
        end
      end

      context "when unquoted" do
        with_command(%q{ruby -e 'prints "foobar"'}) { is_expected.not_to be_truthy }

        # https://github.com/chef/mixlib-shellout/pull/2#issuecomment-4825574
        with_command(%q{"C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe" /i "C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll"}) { is_expected.not_to be_truthy }

        with_command(%q{ruby -e 'exit 1' | ruby -e 'exit 0'}) { is_expected.to be_truthy }
        with_command(%q{ruby -e 'exit 1' > out.txt}) { is_expected.to be_truthy }
        with_command(%q{ruby -e 'exit 1' > out.txt 2>&1}) { is_expected.to be_truthy }
        with_command(%q{ruby -e 'exit 1' < in.txt}) { is_expected.to be_truthy }
        with_command(%q{ruby -e 'exit 1' || ruby -e 'exit 0'}) { is_expected.to be_truthy }
        with_command(%q{ruby -e 'exit 1' && ruby -e 'exit 0'}) { is_expected.to be_truthy }
        with_command(%q{@echo TRUE}) { is_expected.to be_truthy }

        with_command(%q{echo %PATH%}) { is_expected.to be_truthy }
        with_command(%q{run.exe %A}) { is_expected.to be_falsey }
        with_command(%q{run.exe B%}) { is_expected.to be_falsey }
        with_command(%q{run.exe %A B%}) { is_expected.to be_falsey }
        with_command(%q{run.exe %A B% %PATH%}) { is_expected.to be_truthy }
        with_command(%q{run.exe %A B% %_PATH%}) { is_expected.to be_truthy }
        with_command(%q{run.exe %A B% %PATH_EXT%}) { is_expected.to be_truthy }
        with_command(%q{run.exe %A B% %1%}) { is_expected.to be_falsey }
        with_command(%q{run.exe %A B% %PATH1%}) { is_expected.to be_truthy }
        with_command(%q{run.exe %A B% %_PATH1%}) { is_expected.to be_truthy }

        context "when outside quotes" do
          with_command(%q{ruby -e "exit 1" | ruby -e "exit 0"}) { is_expected.to be_truthy }
          with_command(%q{ruby -e "exit 1" > out.txt}) { is_expected.to be_truthy }
          with_command(%q{ruby -e "exit 1" > out.txt 2>&1}) { is_expected.to be_truthy }
          with_command(%q{ruby -e "exit 1" < in.txt}) { is_expected.to be_truthy }
          with_command(%q{ruby -e "exit 1" || ruby -e "exit 0"}) { is_expected.to be_truthy }
          with_command(%q{ruby -e "exit 1" && ruby -e "exit 0"}) { is_expected.to be_truthy }
          with_command(%q{@echo "TRUE"}) { is_expected.to be_truthy }

          context "with unclosed quote" do
            with_command(%q{ruby -e "exit 1" | ruby -e "exit 0}) { is_expected.to be_truthy }
            with_command(%q{ruby -e "exit 1" > "out.txt}) { is_expected.to be_truthy }
            with_command(%q{ruby -e "exit 1" > "out.txt 2>&1}) { is_expected.to be_truthy }
            with_command(%q{ruby -e "exit 1" < "in.txt}) { is_expected.to be_truthy }
            with_command(%q{ruby -e "exit 1" || "ruby -e "exit 0"}) { is_expected.to be_truthy }
            with_command(%q{ruby -e "exit 1" && "ruby -e "exit 0"}) { is_expected.to be_truthy }
            with_command(%q{@echo "TRUE}) { is_expected.to be_truthy }

            with_command(%q{echo "%PATH%}) { is_expected.to be_truthy }
            with_command(%q{run.exe "%A}) { is_expected.to be_falsey }
            with_command(%q{run.exe "B%}) { is_expected.to be_falsey }
            with_command(%q{run.exe "%A B%}) { is_expected.to be_falsey }
            with_command(%q{run.exe "%A B% %PATH%}) { is_expected.to be_truthy }
            with_command(%q{run.exe "%A B% %_PATH%}) { is_expected.to be_truthy }
            with_command(%q{run.exe "%A B% %PATH_EXT%}) { is_expected.to be_truthy }
            with_command(%q{run.exe "%A B% %1%}) { is_expected.to be_falsey }
            with_command(%q{run.exe "%A B% %PATH1%}) { is_expected.to be_truthy }
            with_command(%q{run.exe "%A B% %_PATH1%}) { is_expected.to be_truthy }
          end
        end
      end

      context "when quoted" do
        with_command(%q{run.exe "ruby -e 'exit 1' || ruby -e 'exit 0'"}) { is_expected.to be_falsey }
        with_command(%q{run.exe "ruby -e 'exit 1' > out.txt"}) { is_expected.to be_falsey }
        with_command(%q{run.exe "ruby -e 'exit 1' > out.txt 2>&1"}) { is_expected.to be_falsey }
        with_command(%q{run.exe "ruby -e 'exit 1' < in.txt"}) { is_expected.to be_falsey }
        with_command(%q{run.exe "ruby -e 'exit 1' || ruby -e 'exit 0'"}) { is_expected.to be_falsey }
        with_command(%q{run.exe "ruby -e 'exit 1' && ruby -e 'exit 0'"}) { is_expected.to be_falsey }
        with_command(%q{run.exe "%PATH%"}) { is_expected.to be_truthy }
        with_command(%q{run.exe "%A"}) { is_expected.to be_falsey }
        with_command(%q{run.exe "B%"}) { is_expected.to be_falsey }
        with_command(%q{run.exe "%A B%"}) { is_expected.to be_falsey }
        with_command(%q{run.exe "%A B% %PATH%"}) { is_expected.to be_truthy }
        with_command(%q{run.exe "%A B% %_PATH%"}) { is_expected.to be_truthy }
        with_command(%q{run.exe "%A B% %PATH_EXT%"}) { is_expected.to be_truthy }
        with_command(%q{run.exe "%A B% %1%"}) { is_expected.to be_falsey }
        with_command(%q{run.exe "%A B% %PATH1%"}) { is_expected.to be_truthy }
        with_command(%q{run.exe "%A B% %_PATH1%"}) { is_expected.to be_truthy }

        context "with unclosed quote" do
          with_command(%q{run.exe "ruby -e 'exit 1' || ruby -e 'exit 0'}) { is_expected.to be_falsey }
          with_command(%q{run.exe "ruby -e 'exit 1' > out.txt}) { is_expected.to be_falsey }
          with_command(%q{run.exe "ruby -e 'exit 1' > out.txt 2>&1}) { is_expected.to be_falsey }
          with_command(%q{run.exe "ruby -e 'exit 1' < in.txt}) { is_expected.to be_falsey }
          with_command(%q{run.exe "ruby -e 'exit 1' || ruby -e 'exit 0'}) { is_expected.to be_falsey }
          with_command(%q{run.exe "ruby -e 'exit 1' && ruby -e 'exit 0'}) { is_expected.to be_falsey }
          with_command(%q{run.exe "%PATH%}) { is_expected.to be_truthy }
          with_command(%q{run.exe "%A}) { is_expected.to be_falsey }
          with_command(%q{run.exe "B%}) { is_expected.to be_falsey }
          with_command(%q{run.exe "%A B%}) { is_expected.to be_falsey }
          with_command(%q{run.exe "%A B% %PATH%}) { is_expected.to be_truthy }
          with_command(%q{run.exe "%A B% %_PATH%}) { is_expected.to be_truthy }
          with_command(%q{run.exe "%A B% %PATH_EXT%}) { is_expected.to be_truthy }
          with_command(%q{run.exe "%A B% %1%}) { is_expected.to be_falsey }
          with_command(%q{run.exe "%A B% %PATH1%}) { is_expected.to be_truthy }
          with_command(%q{run.exe "%A B% %_PATH1%}) { is_expected.to be_truthy }
        end
      end
    end

    describe ".kill_process_tree" do
      let(:shell_out) { Mixlib::ShellOut.new }
      let(:wmi) { Object.new }
      let(:wmi_ole_object) { Object.new }
      let(:wmi_process) { Object.new }
      let(:logger) { Object.new }

      before do
        allow(wmi).to receive(:query).and_return([wmi_process])
        allow(wmi_process).to receive(:wmi_ole_object).and_return(wmi_ole_object)
        allow(logger).to receive(:debug)
      end

      context "with a protected system process in the process tree" do
        before do
          allow(wmi_ole_object).to receive(:name).and_return("csrss.exe")
          allow(wmi_ole_object).to receive(:processid).and_return(100)
        end

        it "does not attempt to kill csrss.exe" do
          expect(shell_out).to_not receive(:kill_process)
          shell_out.send(:kill_process_tree, 200, wmi, logger)
        end
      end

      context "with a non-system-critical process in the process tree" do
        before do
          allow(wmi_ole_object).to receive(:name).and_return("blah.exe")
          allow(wmi_ole_object).to receive(:processid).and_return(300)
        end

        it "does attempt to kill blah.exe" do
          expect(shell_out).to receive(:kill_process).with(wmi_process, logger)
          expect(shell_out).to receive(:kill_process_tree).with(200, wmi, logger).and_call_original
          expect(shell_out).to receive(:kill_process_tree).with(300, wmi, logger)
          shell_out.send(:kill_process_tree, 200, wmi, logger)
        end
      end
    end
  end

  # Caveat: Private API methods are subject to change without notice.
  # Monkeypatch at your own risk.
  context "#command_to_run" do

    describe "#command_to_run" do
      subject { shell_out.send(:command_to_run, command) }

      # @param cmd [String] command string
      # @param filename [String] the pathname to the executable that will be found (nil to have no pathname match)
      # @param search [Boolean] false: will setup expectation not to search PATH, true: will setup expectations that it searches the PATH
      # @param directory [Boolean] true: will setup an expectation that the search strategy will find a directory
      def self.with_command(cmd, filename: nil, search: false, directory: false, &example)
        context "with #{cmd}" do
          let(:shell_out) { Mixlib::ShellOut.new }
          let(:comspec) { 'C:\Windows\system32\cmd.exe' }
          let(:command) { cmd }
          before do
            if search
              expect(ENV).to receive(:[]).with("PATH").and_return('C:\Windows\system32')
            else
              expect(ENV).not_to receive(:[]).with("PATH")
            end
            allow(ENV).to receive(:[]).with("PATHEXT").and_return(".COM;.EXE;.BAT;.CMD")
            allow(ENV).to receive(:[]).with("COMSPEC").and_return(comspec)
            allow(File).to receive(:executable?).and_return(false)
            if filename
              expect(File).to receive(:executable?).with(filename).and_return(true)
              expect(File).to receive(:directory?).with(filename).and_return(false)
            end
            if directory
              expect(File).to receive(:executable?).with(cmd).and_return(true)
              expect(File).to receive(:directory?).with(cmd).and_return(true)
            end
          end
          it(&example)
        end
      end

      # quoted and unquoted commands that have correct bat and cmd extensions
      with_command("autoexec.bat", filename: "autoexec.bat") do
        is_expected.to eql([ comspec, 'cmd /c "autoexec.bat"'])
      end
      with_command("autoexec.cmd", filename: "autoexec.cmd") do
        is_expected.to eql([ comspec, 'cmd /c "autoexec.cmd"'])
      end
      with_command('"C:\Program Files\autoexec.bat"', filename: 'C:\Program Files\autoexec.bat') do
        is_expected.to eql([ comspec, 'cmd /c ""C:\Program Files\autoexec.bat""'])
      end
      with_command('"C:\Program Files\autoexec.cmd"', filename: 'C:\Program Files\autoexec.cmd') do
        is_expected.to eql([ comspec, 'cmd /c ""C:\Program Files\autoexec.cmd""'])
      end

      # lookups via PATHEXT
      with_command("autoexec", filename: "autoexec.BAT") do
        is_expected.to eql([ comspec, 'cmd /c "autoexec"'])
      end
      with_command("autoexec", filename: "autoexec.CMD") do
        is_expected.to eql([ comspec, 'cmd /c "autoexec"'])
      end

      # unquoted commands that have "bat" or "cmd" in the wrong place
      with_command("autoexecbat", filename: "autoexecbat") do
        is_expected.to eql(%w{autoexecbat autoexecbat})
      end
      with_command("autoexeccmd", filename: "autoexeccmd") do
        is_expected.to eql(%w{autoexeccmd autoexeccmd})
      end
      with_command("abattoir.exe", filename: "abattoir.exe") do
        is_expected.to eql([ "abattoir.exe", "abattoir.exe" ])
      end
      with_command("parse_cmd.exe", filename: "parse_cmd.exe") do
        is_expected.to eql([ "parse_cmd.exe", "parse_cmd.exe" ])
      end

      # quoted commands that have "bat" or "cmd" in the wrong place
      with_command('"C:\Program Files\autoexecbat"', filename: 'C:\Program Files\autoexecbat') do
        is_expected.to eql([ 'C:\Program Files\autoexecbat', '"C:\Program Files\autoexecbat"' ])
      end
      with_command('"C:\Program Files\autoexeccmd"', filename: 'C:\Program Files\autoexeccmd') do
        is_expected.to eql([ 'C:\Program Files\autoexeccmd', '"C:\Program Files\autoexeccmd"'])
      end
      with_command('"C:\Program Files\abattoir.exe"', filename: 'C:\Program Files\abattoir.exe') do
        is_expected.to eql([ 'C:\Program Files\abattoir.exe', '"C:\Program Files\abattoir.exe"' ])
      end
      with_command('"C:\Program Files\parse_cmd.exe"', filename: 'C:\Program Files\parse_cmd.exe') do
        is_expected.to eql([ 'C:\Program Files\parse_cmd.exe', '"C:\Program Files\parse_cmd.exe"' ])
      end

      # empty command
      with_command(" ") do
        expect { subject }.to raise_error(Mixlib::ShellOut::EmptyWindowsCommand)
      end

      # extensionless executable
      with_command("ping", filename: 'C:\Windows\system32/ping.EXE', search: true) do
        is_expected.to eql([ 'C:\Windows\system32/ping.EXE', "ping" ])
      end

      # it ignores directories
      with_command("ping", filename: 'C:\Windows\system32/ping.EXE', directory: true, search: true) do
        is_expected.to eql([ 'C:\Windows\system32/ping.EXE', "ping" ])
      end

      # https://github.com/chef/mixlib-shellout/pull/2 with bat file
      with_command('"C:\Program Files\Application\Start.bat"', filename: 'C:\Program Files\Application\Start.bat') do
        is_expected.to eql([ comspec, 'cmd /c ""C:\Program Files\Application\Start.bat""' ])
      end
      with_command('"C:\Program Files\Application\Start.bat" arguments', filename: 'C:\Program Files\Application\Start.bat') do
        is_expected.to eql([ comspec, 'cmd /c ""C:\Program Files\Application\Start.bat" arguments"' ])
      end
      with_command('"C:\Program Files\Application\Start.bat" /i "C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll"', filename: 'C:\Program Files\Application\Start.bat') do
        is_expected.to eql([ comspec, 'cmd /c ""C:\Program Files\Application\Start.bat" /i "C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll""' ])
      end

      # https://github.com/chef/mixlib-shellout/pull/2 with cmd file
      with_command('"C:\Program Files\Application\Start.cmd"', filename: 'C:\Program Files\Application\Start.cmd') do
        is_expected.to eql([ comspec, 'cmd /c ""C:\Program Files\Application\Start.cmd""' ])
      end
      with_command('"C:\Program Files\Application\Start.cmd" arguments', filename: 'C:\Program Files\Application\Start.cmd') do
        is_expected.to eql([ comspec, 'cmd /c ""C:\Program Files\Application\Start.cmd" arguments"' ])
      end
      with_command('"C:\Program Files\Application\Start.cmd" /i "C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll"', filename: 'C:\Program Files\Application\Start.cmd') do
        is_expected.to eql([ comspec, 'cmd /c ""C:\Program Files\Application\Start.cmd" /i "C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll""' ])
      end

      # https://github.com/chef/mixlib-shellout/pull/2 with unquoted exe file
      with_command('C:\RUBY192\bin\ruby.exe', filename: 'C:\RUBY192\bin\ruby.exe') do
        is_expected.to eql([ 'C:\RUBY192\bin\ruby.exe', 'C:\RUBY192\bin\ruby.exe' ])
      end
      with_command('C:\RUBY192\bin\ruby.exe arguments', filename: 'C:\RUBY192\bin\ruby.exe') do
        is_expected.to eql([ 'C:\RUBY192\bin\ruby.exe', 'C:\RUBY192\bin\ruby.exe arguments' ])
      end
      with_command('C:\RUBY192\bin\ruby.exe -e "print \'fee fie foe fum\'"', filename: 'C:\RUBY192\bin\ruby.exe') do
        is_expected.to eql([ 'C:\RUBY192\bin\ruby.exe', 'C:\RUBY192\bin\ruby.exe -e "print \'fee fie foe fum\'"' ])
      end

      # https://github.com/chef/mixlib-shellout/pull/2 with quoted exe file
      exe_with_spaces = 'C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe'
      with_command("\"#{exe_with_spaces}\"", filename: exe_with_spaces) do
        is_expected.to eql([ exe_with_spaces, "\"#{exe_with_spaces}\"" ])
      end
      with_command("\"#{exe_with_spaces}\" arguments", filename: exe_with_spaces) do
        is_expected.to eql([ exe_with_spaces, "\"#{exe_with_spaces}\" arguments" ])
      end
      long_options = "/i \"C:\Program Files (x86)\NUnit 2.6\bin\framework\nunit.framework.dll\""
      with_command("\"#{exe_with_spaces}\" #{long_options}", filename: exe_with_spaces) do
        is_expected.to eql([ exe_with_spaces, "\"#{exe_with_spaces}\" #{long_options}" ])
      end

      # shell built in
      with_command("copy thing1.txt thing2.txt", search: true) do
        is_expected.to eql([ comspec, 'cmd /c "copy thing1.txt thing2.txt"' ])
      end
    end
  end

  context "#combine_args" do
    let(:shell_out) { Mixlib::ShellOut.new }
    subject { shell_out.send(:combine_args, *largs) }

    def self.with_args(*args, &example)
      context "with command #{args}" do
        let(:largs) { args }
        it(&example)
      end
    end

    with_args("echo", "%PATH%") do
      is_expected.to eql(%q{echo %PATH%})
    end

    with_args("echo %PATH%") do
      is_expected.to eql(%q{echo %PATH%})
    end

    # Note carefully for the following that single quotes in ruby support '\\' as an escape sequence for a single
    # literal backslash.  It is not mandatory to always use this since '\d' does not escape the 'd' and is literally
    # a backlash followed by an 'd'.  However, in the following all backslashes are escaped for consistency.  Otherwise
    # it becomes prohibitively confusing to track when you need and do not need the escape the backslash (particularly
    # when the literal string has a trailing backslash such that '\\' must be used instead of '\' which would escape
    # the intended terminating single quote or %q{\} which escapes the terminating delimiter).

    with_args("child.exe", "argument1", "argument 2", '\\some\\path with\\spaces') do
      is_expected.to eql('child.exe argument1 "argument 2" "\\some\\path with\\spaces"')
    end

    with_args("child.exe", "argument1", 'she said, "you had me at hello"', '\\some\\path with\\spaces') do
      is_expected.to eql('child.exe argument1 "she said, \\"you had me at hello\\"" "\\some\\path with\\spaces"')
    end

    with_args("child.exe", "argument1", 'argument\\\\"2\\\\"', "argument3", "argument4") do
      is_expected.to eql('child.exe argument1 "argument\\\\\\\\\\"2\\\\\\\\\\"" argument3 argument4')
    end

    with_args("child.exe", '\\some\\directory with\\spaces\\', "argument2") do
      is_expected.to eql('child.exe "\\some\\directory with\\spaces\\\\" argument2')
    end

    with_args("child.exe", '\\some\\directory with\\\\\\spaces\\\\\\', "argument2") do
      is_expected.to eql('child.exe "\\some\\directory with\\\\\\spaces\\\\\\\\\\\\" argument2')
    end
  end

  context "#run_command" do
    let(:shell_out) { Mixlib::ShellOut.new(*largs) }
    subject { shell_out.send(:run_command) }

    def self.with_args(*args, &example)
      context "with command #{args}" do
        let(:largs) { args }
        it(&example)
      end
    end

    with_args("echo", "FOO") do
      is_expected.not_to be_error
    end

    # Test box is going to have to have c:\program files on it, which is perhaps brittle in principle, but
    # I don't know enough windows to come up with a less brittle test.  Fix it if you've got a better idea.
    # The tests need to fail though if the argument is not quoted correctly so using `echo` would be poor
    # because `echo FOO BAR` and `echo "FOO BAR"` aren't any different.

    with_args('dir c:\\program files') do
      is_expected.to be_error
    end

    with_args('dir "c:\\program files"') do
      is_expected.not_to be_error
    end

    with_args("dir", 'c:\\program files') do
      is_expected.not_to be_error
    end

    with_args("dir", 'c:\\program files\\') do
      is_expected.not_to be_error
    end
  end
end