SshConfigFileConfigurationTest.php (4848B)
1 <?php 2 3 namespace Ssh; 4 5 class SshConfigFileConfigurationTest extends \PHPUnit_Framework_TestCase 6 { 7 public function testParseValidSshConfigFile() 8 { 9 $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'test'); 10 11 $this->assertAttributeEquals(array( 12 array( 13 'host' => 'hello', 14 'hostname' => 'hello.com', 15 'port' => '1234' 16 ), 17 array( 18 'host' => 'hello.com', 19 'hostname' => 'hello.com', 20 'port' => '1234' 21 ), 22 array( 23 'host' => 'test', 24 'hostname' => 'test.com' 25 ), 26 array( 27 'host' => 'testuser.com', 28 'user' => 'test', 29 'identityfile' => 'test' 30 ), 31 array( 32 'host' => 'tamp', 33 'hostname' => 'tamp.yo' 34 ), 35 array( 36 'host' => 'identity', 37 'user' => 'identity', 38 'identityfile' => '~/identity' 39 ), 40 array( 41 'host' => 'ta*', 42 'user' => 'bob', 43 'port' => '12345', 44 'hostname' => 'test.com' 45 ) 46 ), 'configs', $config); 47 } 48 49 /** 50 * @expectedException RuntimeException 51 * @expectedExceptionMessage Unable to find configuration for host 'notfound' 52 */ 53 public function testParseSshConfigFileHostNotFound() 54 { 55 $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'notfound'); 56 } 57 58 public function testParseInvalidSshConfigFile() 59 { 60 $exceptions = 0; 61 $file = __DIR__ . '/Fixtures/config_invalid'; 62 try { 63 new SshConfigFileConfiguration($file, 'test'); 64 } catch (\RuntimeException $e) { 65 $exceptions++; 66 $this->assertEquals("The file '$file' is not parsable at line '1'", $e->getMessage()); 67 } 68 $this->assertEquals(1, $exceptions); 69 } 70 71 public function testHostNameFromConfig() 72 { 73 $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'tamp'); 74 $this->assertAttributeEquals('tamp.yo', 'host', $config); 75 } 76 77 public function testPortFromConfig() 78 { 79 $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'tamp'); 80 $this->assertAttributeEquals('12345', 'port', $config); 81 } 82 83 /** 84 * @expectedException RuntimeException 85 * @expectedExceptionMessage The file 'fakefile' does not exist or is not readable 86 */ 87 public function testParseNonExsistantSshConfigFile() 88 { 89 new SshConfigFileConfiguration('fakefile', 'test'); 90 } 91 92 /** 93 * @covers \Ssh\SshConfigFileConfiguration 94 */ 95 public function testGetAuthentication() 96 { 97 $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'test'); 98 99 $identity = getenv('HOME') . "/.ssh/id_rsa"; 100 101 if (file_exists($identity)) { 102 $this->assertEquals(new Authentication\PublicKeyFile( 103 'test', 104 "{$identity}.pub", 105 $identity, 106 null 107 ), $config->getAuthentication(null, 'test')); 108 109 } else { 110 $this->assertEquals(new Authentication\None('test'), $config->getAuthentication(null, 'test')); 111 } 112 113 $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'testuser.com'); 114 115 $this->assertEquals( 116 new Authentication\PublicKeyFile( 117 'test', 118 'test.pub', 119 'test', 120 null 121 ), 122 $config->getAuthentication() 123 ); 124 125 $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'testuser.com'); 126 127 $this->assertEquals( 128 new Authentication\PublicKeyFile( 129 'otheruser', 130 'test.pub', 131 'test', 132 null 133 ), 134 $config->getAuthentication(null, 'otheruser') 135 ); 136 137 } 138 139 public function testIdentityFilePath() 140 { 141 $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'identity'); 142 $this->assertAttributeEquals(array( 143 'user' => 'identity', 144 'identityfile' => getenv('HOME') . '/identity' 145 ), 'config', $config); 146 } 147 148 /** 149 * @expectedException RuntimeException 150 * @expectedExceptionMessage Can not authenticate for 'test.com' could not find user to authenticate as 151 */ 152 public function testGetAuthenticationFailed() 153 { 154 $config = new SshConfigFileConfiguration(__DIR__ . '/Fixtures/config_valid', 'test'); 155 $config->getAuthentication(); 156 } 157 }