<?php

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

namespace phpseclib4\Tests\Functional\Net;

use phpseclib4\Net\SCP;
use phpseclib4\Tests\PhpseclibFunctionalTestCase;

class SCPSSH2UserStoryTest extends PhpseclibFunctionalTestCase
{
    protected static $remoteFile;
    protected static $exampleData;
    protected static $exampleDataLength;

    public static function setUpBeforeClass(): void
    {
        parent::setUpBeforeClass();
        self::$remoteFile = uniqid('phpseclib-scp-ssh2-') . '.txt';
        self::$exampleData = str_repeat('abscp12345', 1000);
        self::$exampleDataLength = 10000;
    }

    public function testConstructor()
    {
        $scp = new SCP($this->getEnv('SSH_HOSTNAME'));
        $this->assertTrue(
            $scp->login(
                $this->getEnv('SSH_USERNAME'),
                $this->getEnv('SSH_PASSWORD')
            )
        );
        $this->assertTrue(
            is_object($scp),
            'Could not construct \phpseclib\Net\SCP object.'
        );
        return $scp;
    }

    #[\PHPUnit\Framework\Attributes\Depends('testConstructor')]
    public function testPutGetString($scp)
    {
        $scp->put(self::$remoteFile, self::$exampleData);
        $content = $scp->get(self::$remoteFile);
        $this->assertSame(
            strlen($content),
            self::$exampleDataLength,
            'Failed asserting that string length matches expected length.'
        );
        $this->assertSame(
            $content,
            self::$exampleData,
            'Failed asserting that string content matches expected content.'
        );
        return $scp;
    }

    #[\PHPUnit\Framework\Attributes\Depends('testPutGetString')]
    public function testGetFile($scp)
    {
        $localFilename = $this->createTempFile();
        $scp->get(self::$remoteFile, $localFilename);
        $this->assertContains(
            filesize($localFilename),
            array(self::$exampleDataLength, self::$exampleDataLength + 1),
            'Failed asserting that filesize matches expected data size.'
        );
        $this->assertContains(
            file_get_contents($localFilename),
            array(self::$exampleData, self::$exampleData . "\0"),
            'Failed asserting that file content matches expected content.'
        );
        return $scp;
    }

    /**
     * @group github873
     */
    #[\PHPUnit\Framework\Attributes\Depends('testGetFile')]
    public function testGetBadFilePutGet($scp)
    {
        $scp->exec('rm ' . self::$remoteFile);
        try {
            $scp->get(self::$remoteFile);
            $this->assertTrue(
                false,
                'Failed asserting that get() on a non-existant file failed'
            );
        } catch (\Exception) {
        }
        $scp->put(self::$remoteFile, self::$exampleData);
        $content = $scp->get(self::$remoteFile);
        $this->assertSame(
            $content,
            self::$exampleData,
            'Failed asserting that string content matches expected content.'
        );
        return $scp;
    }
}
