Issues (16)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

tests/unit/TelnetClientTest.php (12 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Graze\TelnetClient\Test\Unit;
4
5
use Exception;
6
use Graze\TelnetClient\Exception\TelnetExceptionInterface;
7
use Graze\TelnetClient\InterpretAsCommand;
8
use Graze\TelnetClient\PromptMatcher;
9
use Graze\TelnetClient\TelnetClient;
10
use Graze\TelnetClient\TelnetResponseInterface;
11
use Mockery as m;
12
use PHPUnit_Framework_TestCase;
13
use Socket\Raw\Factory as SocketFactory;
14
use Socket\Raw\Socket;
15
16
class TelnetClientTest extends PHPUnit_Framework_TestCase
17
{
18
    /**
19
     * @dataProvider dataProviderConnect
20
     *
21
     * @param string $prompt
22
     * @param string $promptError
23
     * @param string $lineEnding
24
     * @param float|null $timeout
25
     *
26
     * @return void
27
     */
28
    public function testConnect($prompt = null, $promptError = null, $lineEnding = null, $timeout = null)
29
    {
30
        $dsn = 'localhost:80';
31
        $socket = m::mock(Socket::class);
32
        $socketFactory = m::mock(SocketFactory::class)
33
            ->shouldReceive('createClient')
34
            ->with($dsn, $timeout)
35
            ->andReturn($socket)
36
            ->once()
37
            ->getMock();
38
39
        $telnetClient = m::mock(
40
            TelnetClient::class,
41
            [$socketFactory, m::mock(PromptMatcher::class), m::mock(InterpretAsCommand::class)]
42
        )
43
            ->shouldReceive('setSocket')
44
            ->with($socket)
45
            ->once();
46
47
        if ($prompt !== null) {
48
            $telnetClient
49
                ->shouldReceive('setPrompt')
50
                ->with($prompt)
51
                ->once();
52
        } else {
53
            $telnetClient
54
                ->shouldReceive('setPrompt')
55
                ->never();
56
        }
57
58
        if ($promptError !== null) {
59
            $telnetClient
60
                ->shouldReceive('setPromptError')
61
                ->with($promptError)
62
                ->once();
63
        } else {
64
            $telnetClient
65
                ->shouldReceive('setPromptError')
66
                ->never();
67
        }
68
69
        if ($lineEnding !== null) {
70
            $telnetClient
71
                ->shouldReceive('setLineEnding')
72
                ->with($lineEnding)
73
                ->once();
74
        } else {
75
            $telnetClient
76
                ->shouldReceive('setLineEnding')
77
                ->never();
78
        }
79
80
        $telnetClient = $telnetClient->getMock()->makePartial();
81
        $telnetClient->connect($dsn, $prompt, $promptError, $lineEnding, $timeout);
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function dataProviderConnect()
88
    {
89
        return [
90
            ['PROMPT', 'PROMPTERROR', 'LINENDING'],
91
            ['PROMPT', 'PROMPTERROR', 'LINENDING', 5],
92
            [null, null, null]
93
        ];
94
    }
95
    /**
96
     * @dataProvider dataProviderExecute
97
     *
98
     * @param string $command
99
     * @param array $promptMatches
100
     * @param bool $isError
101
     * @param string $promptResponse
102
     * @param string $promptGlobal
103
     * @param string $promptLocal
104
     * @param string $promptError
105
     * @param string $lineEnding
106
     *
107
     * @return void
108
     */
109 View Code Duplication
    public function testExecute(
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
110
        $command,
111
        array $promptMatches,
112
        $isError,
113
        $promptResponse,
114
        $promptGlobal = null,
115
        $promptLocal = null,
116
        $promptError = null,
117
        $lineEnding = null
118
    ) {
119
        $lineEnding = $lineEnding ?: "\n";
120
        $socket = m::mock(Socket::class)
121
            ->shouldReceive('write')
122
            ->with($command.$lineEnding)
123
            ->once();
124
125
        // echo the command back when reading each byte one-by-one from the socket
126
        $commandResponse = str_split($command.$lineEnding.$promptResponse.$lineEnding);
127
        $socket = $socket
128
            ->shouldReceive('read')
129
            ->with(1);
130
        call_user_func_array([$socket, 'andReturn'], $commandResponse);
131
        $socket = $socket
132
            ->times(count($commandResponse))
133
            ->shouldReceive('close')
134
            ->once()
135
            ->getMock();
136
137
        /** @var SocketFactory $socketFactory */
138
        $socketFactory = m::mock(SocketFactory::class)
139
            ->shouldReceive('createClient')
140
            ->andReturn($socket)
141
            ->once()
142
            ->getMock();
143
144
        $promptMatcher = new PromptMatcher();
145
146
        /** @var InterpretAsCommand $interpretAsCommand */
147
        $interpretAsCommand = m::mock(InterpretAsCommand::class)
148
            ->shouldReceive('interpret')
149
            ->times(count($commandResponse))
150
            ->andReturn(false)
151
            ->getMock();
152
153
        $telnetClient = new TelnetClient($socketFactory, $promptMatcher, $interpretAsCommand);
154
        $telnetClient->connect('127.0.0.1:23', $promptGlobal, $promptError, $lineEnding);
155
156
        $response = $telnetClient->execute($command, $promptLocal);
157
        $this->assertInstanceOf(TelnetResponseInterface::class, $response);
158
        $this->assertEquals($isError, $response->isError());
159
        $this->assertEquals($command, $response->getResponseText());
160
        $this->assertEquals($promptMatches, $response->getPromptMatches());
161
    }
162
    
163
    /**
164
     * @dataProvider dataProviderExecute
165
     *
166
     * @param string $command
167
     * @param array $promptMatches
168
     * @param bool $isError
169
     * @param string $promptResponse
170
     * @param string $promptGlobal
171
     * @param string $promptLocal
172
     * @param string $promptError
173
     * @param string $lineEnding
174
     *
175
     * @return void
176
     */
177 View Code Duplication
    public function testExecutePromptError(
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
178
        $command,
179
        array $promptMatches,
180
        $isError,
181
        $promptResponse,
182
        $promptGlobal = null,
183
        $promptLocal = null,
184
        $promptError = null,
185
        $lineEnding = null
186
    ) {
187
        $lineEnding = $lineEnding ?: "\n";
188
        $socket = m::mock(Socket::class)
189
            ->shouldReceive('write')
190
            ->with($command.$lineEnding)
191
            ->once();
192
193
        // echo the command back when reading each byte one-by-one from the socket
194
        $commandResponse = str_split($command.$lineEnding.$promptResponse.$lineEnding);
195
        $socket = $socket
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
196
            ->shouldReceive('read')
197
            ->with(1);
198
        call_user_func_array([$socket, 'andReturn'], $commandResponse);
199
        $socket = $socket
200
            ->times(count($commandResponse))
201
            ->shouldReceive('close')
202
            ->once()
203
            ->getMock();
204
205
        /** @var SocketFactory $socketFactory */
206
        $socketFactory = m::mock(SocketFactory::class)
207
            ->shouldReceive('createClient')
208
            ->andReturn($socket)
209
            ->once()
210
            ->getMock();
211
212
        $promptMatcher = new PromptMatcher();
213
214
        /** @var InterpretAsCommand $interpretAsCommand */
215
        $interpretAsCommand = m::mock(InterpretAsCommand::class)
216
            ->shouldReceive('interpret')
217
            ->times(count($commandResponse))
218
            ->andReturn(false)
219
            ->getMock();
220
221
        $telnetClient = new TelnetClient($socketFactory, $promptMatcher, $interpretAsCommand);
222
        $telnetClient->connect('127.0.0.1:23', $promptGlobal, null, $lineEnding);
223
224
        $response = $telnetClient->execute($command, $promptLocal, $promptError);
225
        $this->assertInstanceOf(TelnetResponseInterface::class, $response);
226
        $this->assertEquals($isError, $response->isError());
227
        $this->assertEquals($command, $response->getResponseText());
228
        $this->assertEquals($promptMatches, $response->getPromptMatches());
229
    }
230
231
    /**
232
     * @return array
233
     */
234
    public function dataProviderExecute()
235
    {
236
        return [
237
            ['party', ['$'], false, "\$"], // success
238
            ['party', ['OK'], false, 'OK', 'OK'], // success custom global prompt
239
            ['party', ['OK'], false, 'OK', null, 'OK'], // success custom local prompt
240
            ['party', ['ERROR'], true, 'ERROR'], // error
241
             // error custom prompt
242
            ['party', ['DAMN 123', 'DAMN', '123'], true, 'DAMN 123', null, null, '(DAMN) ([0-9]{3})'],
243
            ['party', ['$'], false, "\$", null, null, null, 'LOL'], // success custom line ending
244
            ['party', ['ERROR'], true, "ERROR", null, null, null, 'LOL'], // error custom line ending
245
        ];
246
    }
247
248
    public function testExecuteException()
249
    {
250
        $this->setExpectedException(TelnetExceptionInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
251
252
        $client = m::mock(TelnetClient::class)->makePartial();
253
        $client->execute('aCommand');
254
    }
255
256 View Code Duplication
    public function testWriteException()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
257
    {
258
        $this->setExpectedException(TelnetExceptionInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
259
260
        $client = m::mock(TelnetClient::class)->makePartial();
261
262
        $socket = m::mock(Socket::class)
263
            ->shouldReceive('write')
264
            ->andThrow(Exception::class)
265
            ->shouldReceive('close')
266
            ->getMock();
267
268
        $client->setSocket($socket);
269
        $client->execute('aCommand');
270
    }
271
272 View Code Duplication
    public function testReadException()
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
273
    {
274
        $this->setExpectedException(TelnetExceptionInterface::class, 'failed reading from socket');
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
275
276
        $client = m::mock(TelnetClient::class)->makePartial();
277
278
        $socket = m::mock(Socket::class)
279
            ->shouldReceive('write')
280
            ->shouldReceive('close')
281
            ->shouldReceive('read')
282
            ->andThrow(Exception::class)
283
            ->getMock();
284
285
        $client->setSocket($socket);
286
        $client->execute('aCommand');
287
    }
288
289
    public function testMaxBytesReadException()
290
    {
291
        $this->setExpectedException(
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
292
            TelnetExceptionInterface::class, 
293
            'Maximum number of bytes read (20), the last bytes were 0123456789'
294
        );
295
296
        $socket = m::mock(Socket::class)
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
297
            ->shouldReceive('write')
298
            ->shouldReceive('close')
299
            ->shouldReceive('read');
300
301
        call_user_func_array([$socket, 'andReturn'], str_split('0123456789012345678912'));
302
        $socket = $socket->getMock();
303
304
        $socketFactory = m::mock(SocketFactory::class)
305
            ->shouldReceive('createClient')
306
            ->andReturn($socket)
307
            ->once()
308
            ->getMock();
309
310
        $promptMatcher = new PromptMatcher();
311
312
        $interpretAsCommand = m::mock(InterpretAsCommand::class)
313
            ->shouldReceive('interpret')
314
            ->andReturn(false)
315
            ->getMock();
316
317
        $client = new TelnetClient($socketFactory, $promptMatcher, $interpretAsCommand);
318
        $client->connect('dsn');
319
        $client->setMaxBytesRead(20);
320
321
        $client->execute('aCommand');
322
    }
323
324
    public function testFactory()
325
    {
326
        $client = TelnetClient::factory();
327
        $this->assertInstanceOf(TelnetClient::class, $client);
328
    }
329
330
    /**
331
     * @dataProvider setReadTimeoutDataProvider
332
     * @param float $timeout
333
     * @param int $expectedSec
334
     * @param int $expectedUsec
335
     */
336
    public function testSetReadTimeout($timeout, $expectedSec, $expectedUsec)
337
    {
338
        $client = m::mock(TelnetClient::class)->makePartial();
339
340
        $socket = m::mock(Socket::class)
0 ignored issues
show
The method shouldReceive() does not seem to exist on object<Mockery\Expectation>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
341
            ->shouldReceive('setOption')
342
            ->with(SOL_SOCKET, SO_RCVTIMEO, ['sec' => $expectedSec, 'usec' => $expectedUsec])
343
            ->shouldReceive('close')
344
            ->getMock();
345
346
        $client->setSocket($socket);
347
348
        $client->setReadTimeout($timeout);
349
    }
350
351
    /**
352
     * @return mixed[]
353
     */
354
    public function setReadTimeoutDataProvider()
355
    {
356
        return [
357
            [1, 1, 0],
358
            [0.5, 0, 500000],
359
            [2.345, 2, 345000],
360
        ];
361
    }
362
363
    public function testSetReadTimeoutException()
364
    {
365
        $this->setExpectedException(TelnetExceptionInterface::class);
0 ignored issues
show
Deprecated Code introduced by
The method PHPUnit_Framework_TestCase::setExpectedException() has been deprecated with message: Method deprecated since Release 5.2.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
366
367
        $client = m::mock(TelnetClient::class)->makePartial();
368
369
        $client->setReadTimeout(1);
370
    }
371
}
372