2024-04-10 16:18:52 +10:00
RSpec . describe Msf :: Sessions :: CommandShellWindows do
describe 'to_cmd processing' do
it 'should not do anything for simple args' do
2024-10-03 22:00:26 +10:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ ] ) ) . to eq ( 'test.exe' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'basic' , 'args' ] ) ) . to eq ( 'test.exe basic args' )
2024-04-10 16:18:52 +10:00
end
it 'should quote spaces' do
2024-10-03 22:00:26 +10:00
expect ( described_class . to_cmd ( [ 'C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE' ] + [ ] ) ) . to eq ( '"C:\\Program Files\\Microsoft Office\\root\\Office16\\WINWORD.EXE"' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'with space' ] ) ) . to eq ( 'test.exe "with space"' )
2024-04-10 16:18:52 +10:00
end
it 'should escape logical operators' do
2024-10-03 22:00:26 +10:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '&&' , 'echo' , 'words' ] ) ) . to eq ( 'test.exe "&&" echo words' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '||' , 'echo' , 'words' ] ) ) . to eq ( 'test.exe "||" echo words' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '&echo' , 'words' ] ) ) . to eq ( 'test.exe "&echo" words' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'run&echo' , 'words' ] ) ) . to eq ( 'test.exe "run&echo" words' )
2024-04-10 16:18:52 +10:00
end
it 'should escape redirectors' do
2024-10-03 22:00:26 +10:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '>' , 'out.txt' ] ) ) . to eq ( 'test.exe ">" out.txt' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '<' , 'in.txt' ] ) ) . to eq ( 'test.exe "<" in.txt' )
2024-04-10 16:18:52 +10:00
end
it 'should escape carets' do
2024-10-03 22:00:26 +10:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'with^caret' ] ) ) . to eq ( 'test.exe "with^caret"' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'with^^carets' ] ) ) . to eq ( 'test.exe "with^^carets"' )
2024-04-10 16:18:52 +10:00
end
it 'should not expand env vars' do
2024-10-03 22:00:26 +10:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '%temp%' ] ) ) . to eq ( 'test.exe ^%temp^%' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'env' , 'var' , 'is' , '%temp%' ] ) ) . to eq ( 'test.exe env var is ^%temp^%' )
2024-04-10 16:18:52 +10:00
end
2024-04-15 09:27:26 +10:00
it 'should handle the weird backslash escaping behaviour in front of quotes' do
2024-10-16 15:46:35 +11:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'quote\\\\"' ] ) ) . to eq ( 'test.exe "quote\\\\\\\\""' )
2024-10-03 22:00:26 +10:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'will be quoted\\\\' ] ) ) . to eq ( 'test.exe "will be quoted\\\\\\\\"' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'will be quoted\\\\ ' ] ) ) . to eq ( 'test.exe "will be quoted\\\\ "' ) # Should not be doubled up
2024-10-16 10:13:16 +11:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '"test"' , 'test\\"' , 'test\\\\"' , 'test words\\\\\\\\' , 'test words\\\\\\' , '\\\\' ] ) ) . to eq ( 'test.exe """test""" "test\\\\"" "test\\\\\\\\"" "test words\\\\\\\\\\\\\\\\" "test words\\\\\\\\\\\\" \\\\' )
2024-04-15 09:27:26 +10:00
end
2024-04-10 16:18:52 +10:00
it 'should handle combinations of quoting and percent-escaping' do
2024-10-03 22:00:26 +10:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'env var is %temp%' ] ) ) . to eq ( 'test.exe "env var is "^%temp^%' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ 'env var is %temp%, yes, %TEMP%' ] ) ) . to eq ( 'test.exe "env var is "^%temp^%", yes, "^%TEMP^%' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '%temp%found at the start shouldn\'t %temp% be quoted' ] ) ) . to eq ( 'test.exe ^%temp^%"found at the start shouldn\'t "^%temp^%" be quoted"' )
2024-04-10 16:18:52 +10:00
end
it 'should handle single percents' do
2024-10-03 22:00:26 +10:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '%single percent' ] ) ) . to eq ( 'test.exe ^%"single percent"' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '100%' ] ) ) . to eq ( 'test.exe 100^%' )
2024-04-10 16:18:52 +10:00
end
2024-04-15 09:27:26 +10:00
it 'should handle empty args' do
2024-10-03 22:00:26 +10:00
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '' ] ) ) . to eq ( 'test.exe ""' )
expect ( described_class . to_cmd ( [ 'test.exe' ] + [ '' , '' ] ) ) . to eq ( 'test.exe "" ""' )
2024-04-15 09:27:26 +10:00
end
end
describe 'argv_to_commandline processing' do
it 'should not do anything for simple args' do
2024-04-18 14:04:53 +10:00
expect ( described_class . argv_to_commandline ( [ ] ) ) . to eq ( '' )
expect ( described_class . argv_to_commandline ( [ 'basic' , 'args' ] ) ) . to eq ( 'basic args' )
expect ( described_class . argv_to_commandline ( [ '!@#$%^&*(){}><.,\'' ] ) ) . to eq ( '!@#$%^&*(){}><.,\'' )
2024-04-15 09:27:26 +10:00
end
it 'should quote space characters' do
2024-04-18 14:04:53 +10:00
expect ( described_class . argv_to_commandline ( [ ] ) ) . to eq ( '' )
expect ( described_class . argv_to_commandline ( [ 'basic' , 'args' ] ) ) . to eq ( 'basic args' )
2024-04-15 09:27:26 +10:00
end
it 'should escape double-quote characters' do
2024-04-18 14:04:53 +10:00
expect ( described_class . argv_to_commandline ( [ '"one' , '"two"' ] ) ) . to eq ( '\\"one \\"two\\"' )
expect ( described_class . argv_to_commandline ( [ '"one "two"' ] ) ) . to eq ( '"\\"one \\"two\\""' )
2024-04-15 09:27:26 +10:00
end
it 'should handle the weird backslash escaping behaviour in front of quotes' do
2024-04-18 14:04:53 +10:00
expect ( described_class . argv_to_commandline ( [ '\\\\"' ] ) ) . to eq ( '\\\\\\\\\\"' )
expect ( described_class . argv_to_commandline ( [ 'space \\\\' ] ) ) . to eq ( '"space \\\\\\\\"' )
2024-10-16 10:13:16 +11:00
expect ( described_class . argv_to_commandline ( [ '"test"' , 'test\\"' , 'test\\\\"' , 'test words\\\\\\\\' , 'test words\\\\\\' , '\\\\' ] ) ) . to eq ( '\"test\" test\\\\\\" test\\\\\\\\\\" "test words\\\\\\\\\\\\\\\\" "test words\\\\\\\\\\\\" \\\\' )
2024-04-15 09:27:26 +10:00
end
it 'should handle empty args' do
2024-04-18 14:04:53 +10:00
expect ( described_class . argv_to_commandline ( [ '' ] ) ) . to eq ( '""' )
expect ( described_class . argv_to_commandline ( [ '' , '' ] ) ) . to eq ( '"" ""' )
2024-04-15 09:27:26 +10:00
end
2024-04-10 16:18:52 +10:00
end
end