File: SSH2AgentTest.php

package info (click to toggle)
php-phpseclib3 3.0.46-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,528 kB
  • sloc: php: 30,756; xml: 392; makefile: 21
file content (58 lines) | stat: -rw-r--r-- 1,603 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php

/**
 * @author    Andreas Fischer <bantu@phpbb.com>
 * @copyright 2014 Andreas Fischer
 * @license   http://www.opensource.org/licenses/mit-license.html  MIT License
 */

namespace phpseclib3\Tests\Functional\Net;

use phpseclib3\Net\SSH2;
use phpseclib3\System\SSH\Agent;
use phpseclib3\Tests\PhpseclibFunctionalTestCase;
use PHPUnit\Framework\Attributes\Depends;

class SSH2AgentTest extends PhpseclibFunctionalTestCase
{
    public static function setUpBeforeClass(): void
    {
        if (!isset($_SERVER['SSH_AUTH_SOCK'])) {
            self::markTestSkipped(
                'This test requires an SSH Agent (SSH_AUTH_SOCK env variable).'
            );
        }
        parent::setUpBeforeClass();
    }

    public function testAgentLogin()
    {
        $ssh = new SSH2($this->getEnv('SSH_HOSTNAME'));
        $agent = new Agent();

        $this->assertTrue(
            $ssh->login($this->getEnv('SSH_USERNAME'), $agent),
            'SSH2 login using Agent failed.'
        );

        return ['ssh' => $ssh, 'ssh-agent' => $agent];
    }

    /** @depends testAgentLogin */
    #[Depends('testAgentLogin')]
    public function testAgentForward($args)
    {
        $ssh = $args['ssh'];
        $agent = $args['ssh-agent'];

        $hostname = $this->getEnv('SSH_HOSTNAME');
        $username = $this->getEnv('SSH_USERNAME');

        $this->assertEquals($username, trim($ssh->exec('whoami')));

        $agent->startSSHForwarding($ssh);
        $this->assertEquals($username, trim($ssh->exec("ssh " . $username . "@" . $hostname . ' \'whoami\'')));

        return $args;
    }
}