Changeset View
Changeset View
Standalone View
Standalone View
src/xsprintf/__tests__/PhutilCsprintfTestCase.php
| Show All 33 Lines | public function testPowershell() { | ||||
| $cmd->setEscapingMode(PhutilCommandString::MODE_POWERSHELL); | $cmd->setEscapingMode(PhutilCommandString::MODE_POWERSHELL); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| '"`n"', | '"`n"', | ||||
| (string)$cmd); | (string)$cmd); | ||||
| } | } | ||||
| public function testNoPowershell() { | public function testNoPowershell() { | ||||
| if (!phutil_is_windows()) { | $cmd = csprintf('%s', '#"'); | ||||
| $cmd = csprintf('%s', '#'); | |||||
| $cmd->setEscapingMode(PhutilCommandString::MODE_DEFAULT); | $cmd->setEscapingMode(PhutilCommandString::MODE_DEFAULT); | ||||
| $this->assertEqual( | $this->assertEqual( | ||||
| '\'#\'', | phutil_is_windows() ? '^"#\^"^"' : '\'#"\'', | ||||
BYK: Well, this is not really accurate. I should probably branch and use the expected Windows… | |||||
| (string)$cmd); | (string)$cmd | ||||
| } | ); | ||||
| } | } | ||||
| public function testPasswords() { | public function testPasswords() { | ||||
| // Normal "%s" doesn't do anything special. | // Normal "%s" doesn't do anything special. | ||||
| $command = csprintf('echo %s', 'hunter2trustno1'); | $command = csprintf('echo %s', 'hunter2trustno1'); | ||||
| $this->assertTrue(strpos($command, 'hunter2trustno1') !== false); | $this->assertTrue(strpos($command, 'hunter2trustno1') !== false); | ||||
| // "%P" takes a PhutilOpaqueEnvelope. | // "%P" takes a PhutilOpaqueEnvelope. | ||||
| Show All 13 Lines | public function testPasswords() { | ||||
| // Executing the command works as expected. | // Executing the command works as expected. | ||||
| list($out) = execx('%C', $command); | list($out) = execx('%C', $command); | ||||
| $this->assertTrue(strpos($out, 'hunter2trustno1') !== false); | $this->assertTrue(strpos($out, 'hunter2trustno1') !== false); | ||||
| } | } | ||||
| public function testEscapingIsRobust() { | public function testEscapingIsRobust() { | ||||
| if (phutil_is_windows()) { | if (phutil_is_windows()) { | ||||
| // NOTE: The reason we can't run this test on Windows is two fold: | |||||
| // 1. We need to use both `argv` escaping and `cmd` escaping | |||||
| // when running commands on Windows because of the CMD proxy | |||||
| // 2. After the first `argv` escaping, you only need CMD escaping | |||||
| // but we need a new `%x` thing to signal this which is probably | |||||
| // not worth the added complexity. | |||||
| $this->assertSkipped(pht("This test doesn't work on Windows.")); | $this->assertSkipped(pht("This test doesn't work on Windows.")); | ||||
| } | } | ||||
| // Escaping should be robust even when used to escape commands which take | // Escaping should be robust even when used to escape commands which take | ||||
| // other commands. | // other commands. | ||||
| list($out) = execx( | list($out) = execx( | ||||
| 'sh -c %s', | 'sh -c %s', | ||||
| csprintf( | csprintf( | ||||
| 'sh -c %s', | 'sh -c %s', | ||||
| csprintf( | csprintf( | ||||
| 'sh -c %s', | 'sh -c %s', | ||||
| csprintf( | csprintf( | ||||
| 'echo %P', | 'echo %P', | ||||
| new PhutilOpaqueEnvelope('!@#$%^&*()'))))); | new PhutilOpaqueEnvelope('!@#$%^&*()'))))); | ||||
| $this->assertTrue(strpos($out, '!@#$%^&*()') !== false); | $this->assertTrue(strpos($out, '!@#$%^&*()') !== false); | ||||
| } | } | ||||
| public function testEdgeCases() { | |||||
epriestleyUnsubmitted Not Done Inline ActionsThese test cases do not test what they intend to test. In PHP, '\0' is the string literal "backslash, zero", not a null byte. Likewise, '\n' and '\r' are not newline characters. When these strings are changed to use double quotes, the test cases fail. epriestley: These test cases do not test what they intend to test.
In PHP, `'\0'` is the string literal… | |||||
BYKAuthorUnsubmitted Not Done Inline ActionsAh, okay I see. Will fix. BYK: Ah, okay I see. Will fix. | |||||
BYKAuthorUnsubmitted Not Done Inline ActionsTurns out it is not possible to pass a NULL BYTE through the command line anyways so gonna drop that case. Working on the new lines which are the other two problems. BYK: Turns out it is not possible to pass a NULL BYTE through the command line anyways so gonna drop… | |||||
| $edgeCases = array( | |||||
| '\0', // null byte | |||||
| '\\', | |||||
| '%', | |||||
| '%%', | |||||
| ' ', // space | |||||
| '', // empty string | |||||
| '-', | |||||
| '/flag', | |||||
| '\\\^\%\\\'\ \\', | |||||
| '%PATH%', | |||||
| '%XYZ%', | |||||
| '%%HOMEDIR%', | |||||
| '\n', // newline | |||||
| '\r', // newline | |||||
| 'a b', | |||||
| '"a b"', | |||||
| '"%%$HOMEDIR%^^"', | |||||
| '\'a b\'', | |||||
| '^%HO ^"M\'EDIR^%^%\'', | |||||
| '"\'a\0\r\nb%PATH%%`\'"\'`\'`\'', | |||||
| ); | |||||
| foreach ($edgeCases as $edgeCase) { | |||||
| list($output) = execx('php -r %s -- %s', 'echo $argv[1];', $edgeCase); | |||||
Not Done Inline ActionsShould probably do list($output) = execx('php -r %s -- %s', 'echo $argv[1];', $edgeCase);instead. BYK: Should probably do
list($output) = execx('php -r %s -- %s', 'echo $argv[1];', $edgeCase)… | |||||
| $this->assertEqual($edgeCase, $output); | |||||
| } | |||||
| } | |||||
| } | } | ||||
Well, this is not really accurate. I should probably branch and use the expected Windows version at this point. What do you think?