|
| 1 | +/* -------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All Rights Reserved. |
| 3 | + * See 'LICENSE' in the project root for license information. |
| 4 | + * ------------------------------------------------------------------------------------------ */ |
| 5 | + |
| 6 | +import { deepStrictEqual, strictEqual } from 'assert'; |
| 7 | +import { describe, it } from 'mocha'; |
| 8 | +import { splitArgs, sshCommandToConfig } from '../../src/SSH/sshCommandToConfig'; |
| 9 | + |
| 10 | +// eslint-disable-next-line import/no-unassigned-import |
| 11 | +require('source-map-support/register'); |
| 12 | + |
| 13 | +describe('splitArgs', () => { |
| 14 | + // [description, input, expected tokens] |
| 15 | + const cases: [string, string, string[]][] = [ |
| 16 | + ['empty string', '', []], |
| 17 | + ['whitespace only', ' \t ', []], |
| 18 | + ['simple words', 'ssh user@host', ['ssh', 'user@host']], |
| 19 | + ['collapses runs of whitespace', 'ssh \t user@host', ['ssh', 'user@host']], |
| 20 | + ['trims leading/trailing whitespace', ' ssh user@host ', ['ssh', 'user@host']], |
| 21 | + |
| 22 | + // Windows paths: backslashes must stay literal (the original bug). |
| 23 | + ['bare Windows path', 'ssh -i C:\\Users\\me\\key user@host', ['ssh', '-i', 'C:\\Users\\me\\key', 'user@host']], |
| 24 | + ['double-quoted Windows path with spaces', 'ssh -i "C:\\Program Files\\me\\key" user@host', ['ssh', '-i', 'C:\\Program Files\\me\\key', 'user@host']], |
| 25 | + ['single-quoted Windows path with spaces', "ssh -i 'C:\\Program Files\\me\\key' user@host", ['ssh', '-i', 'C:\\Program Files\\me\\key', 'user@host']], |
| 26 | + ['single-quoted Windows path without spaces', "ssh -i 'C:\\Users\\me\\key' user@host", ['ssh', '-i', 'C:\\Users\\me\\key', 'user@host']], |
| 27 | + |
| 28 | + // Quote handling. |
| 29 | + ['strips double quotes', '"a b" c', ['a b', 'c']], |
| 30 | + ['strips single quotes', "'a b' c", ['a b', 'c']], |
| 31 | + ['quotes joined to adjacent text', 'a"b c"d', ['ab cd']], |
| 32 | + ['single quotes inside double quotes are literal', '"it\'s here"', ["it's here"]], |
| 33 | + ['double quotes inside single quotes are literal', "'say \"hi\"'", ['say "hi"']], |
| 34 | + ['empty double-quoted token is preserved', 'a "" b', ['a', '', 'b']], |
| 35 | + ['empty single-quoted token is preserved', "a '' b", ['a', '', 'b']], |
| 36 | + |
| 37 | + // Forward-slash (POSIX-style) paths are unaffected. |
| 38 | + ['forward-slash path', 'ssh -i /home/me/.ssh/id_rsa user@host', ['ssh', '-i', '/home/me/.ssh/id_rsa', 'user@host']], |
| 39 | + |
| 40 | + // An unquoted backslash escapes a following whitespace character (POSIX behavior), so a |
| 41 | + // Unix path with an escaped space stays a single argument. |
| 42 | + ['backslash escapes a space', 'ssh -i /home/me/my\\ key user@host', ['ssh', '-i', '/home/me/my key', 'user@host']], |
| 43 | + ['backslash escapes multiple spaces', 'ssh -i /home/me/key\\ with\\ spaces user@host', ['ssh', '-i', '/home/me/key with spaces', 'user@host']], |
| 44 | + ['backslash escapes a tab', 'a\\\tb', ['a\tb']], |
| 45 | + ['trailing backslash is literal', 'foo\\', ['foo\\']], |
| 46 | + ['backslash before a letter stays literal (Windows path)', 'C:\\Users\\me', ['C:\\Users\\me']], |
| 47 | + ['UNC path keeps doubled backslashes', '\\\\server\\share', ['\\\\server\\share']], |
| 48 | + // A backslash that ends a quoted segment is literal and must not escape the following |
| 49 | + // separator (only an unquoted backslash directly before whitespace escapes). |
| 50 | + ['quoted path ending in backslash is not joined to the next arg', '"C:\\Program Files\\" next', ['C:\\Program Files\\', 'next']], |
| 51 | + |
| 52 | + // Lenient handling of an unterminated quote: runs to end of string. |
| 53 | + ['unterminated double quote runs to end', 'ssh -i "C:\\Users\\me', ['ssh', '-i', 'C:\\Users\\me']], |
| 54 | + ['unterminated single quote runs to end', "ssh -i 'C:\\Users\\me", ['ssh', '-i', 'C:\\Users\\me']] |
| 55 | + ]; |
| 56 | + |
| 57 | + for (const [description, input, expected] of cases) { |
| 58 | + it(`${description}: ${JSON.stringify(input)}`, () => { |
| 59 | + deepStrictEqual(splitArgs(input), expected); |
| 60 | + }); |
| 61 | + } |
| 62 | +}); |
| 63 | + |
| 64 | +describe('sshCommandToConfig', () => { |
| 65 | + it('preserves a bare Windows identity-file path', () => { |
| 66 | + const config = sshCommandToConfig('ssh -i C:\\Users\\me\\.ssh\\id_rsa user@host'); |
| 67 | + strictEqual(config.IdentityFile, 'C:\\Users\\me\\.ssh\\id_rsa'); |
| 68 | + strictEqual(config.HostName, 'host'); |
| 69 | + strictEqual(config.User, 'user'); |
| 70 | + }); |
| 71 | + |
| 72 | + it('preserves a single-quoted Windows identity-file path with spaces', () => { |
| 73 | + const config = sshCommandToConfig("ssh -i 'C:\\Program Files\\me\\key' user@host"); |
| 74 | + strictEqual(config.IdentityFile, 'C:\\Program Files\\me\\key'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('preserves a double-quoted Windows identity-file path with spaces', () => { |
| 78 | + const config = sshCommandToConfig('ssh -i "C:\\Program Files\\me\\key" user@host'); |
| 79 | + strictEqual(config.IdentityFile, 'C:\\Program Files\\me\\key'); |
| 80 | + }); |
| 81 | + |
| 82 | + it('preserves a Unix identity-file path with a backslash-escaped space', () => { |
| 83 | + const config = sshCommandToConfig('ssh -i /home/me/my\\ key user@host'); |
| 84 | + strictEqual(config.IdentityFile, '/home/me/my key'); |
| 85 | + strictEqual(config.HostName, 'host'); |
| 86 | + strictEqual(config.User, 'user'); |
| 87 | + }); |
| 88 | + |
| 89 | + it('parses host, user, and port from the connection string', () => { |
| 90 | + const config = sshCommandToConfig('ssh -p 2222 user@host'); |
| 91 | + strictEqual(config.HostName, 'host'); |
| 92 | + strictEqual(config.User, 'user'); |
| 93 | + strictEqual(config.Port, '2222'); |
| 94 | + }); |
| 95 | +}); |
0 commit comments