public function testGetPublicSshKeyFromPrivateKeyOk()
 {
     $keyFileName = __DIR__ . '/../resource/private_key.txt';
     $this->shellExecServiceMock->expects($this->at(0))->method('shellExec')->with($this->equalTo('which ssh-keygen'))->will($this->returnValue(new ShellExecResult(0, array())));
     $this->configServiceMock->expects($this->once())->method('getPrivateKeyFilename')->will($this->returnValue($keyFileName));
     $this->shellExecServiceMock->expects($this->at(1))->method('shellExec')->with($this->equalTo("ssh-keygen -P '' -q -y -f '{$keyFileName}'"))->will($this->returnValue(new ShellExecResult(0, array('ssh-rsa AAAAB'))));
     $publicKey = $this->cut->getPublicSshKeyFromPrivateKey();
     $this->assertEquals('ssh-rsa AAAAB', $publicKey);
 }
 /**
  * @ControllerManaged
  * @NoCSRFRequired
  *
  * Upload SSH private key file
  */
 protected function uploadSshKey()
 {
     $file = $this->request->getUploadedFile('easybackup_sshKeyFile');
     if (!$file || !file_exists($file['tmp_name'])) {
         throw new EasyBackupException('Uploaded file not found');
     }
     $key = file_get_contents($file['tmp_name']);
     if ($key === false || strlen($key) == 0) {
         throw new EasyBackupException('Uploaded file is empty');
     }
     if (!$this->backupService->validatePrivateSshKey($key)) {
         throw new EasyBackupException('Key is not well-formed');
     }
     $filename = $this->configService->getPrivateKeyFilename();
     if (!file_put_contents($filename, $key)) {
         throw new \Exception('Could not store private key in ' . $filename);
     }
     chmod($filename, 0600);
     $publicKey = $this->backupService->getPublicSshKeyFromPrivateKey();
     $this->configService->setPublicKey($publicKey);
     return array('preconditionsHtml' => $this->renderPreconditionsHtml(), 'publicKeyHtml' => $this->renderPublicKeyHtml());
 }