Skip to content

Commit 869a75f

Browse files
committed
Added Request::verify() and Query::verify(), which the Client runs before sending the request (fixes #8);
In relation to the above, Communicator::verifyLengthSupport() is now public; Unmarked the 3 byte length test, as it seems to work now for some reason (still not the 4 byte length one though); Fixed a ResponseCollection::orderBy() test.
1 parent 8c3dc13 commit 869a75f

File tree

7 files changed

+89
-19
lines changed

7 files changed

+89
-19
lines changed

src/PEAR2/Net/RouterOS/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ private static function _login(
282282
. pack('H*', $response->getProperty('ret'))
283283
)
284284
);
285-
$request->send($com);
285+
$request->verify($com)->send($com);
286286

287287
$response = new Response($com, false, $timeout);
288288
if ($response->getType() === Response::TYPE_FINAL) {
@@ -790,7 +790,7 @@ public function __destruct()
790790
*/
791791
protected function send(Request $request)
792792
{
793-
$request->send($this->com, $this->registry);
793+
$request->verify($this->com)->send($this->com, $this->registry);
794794
$this->pendingRequestsCount++;
795795
return $this;
796796
}

src/PEAR2/Net/RouterOS/Communicator.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ public function __construct(
134134
array(
135135
'ssl' => array(
136136
'ciphers' => 'ADH',
137-
'verify_peer' => false
137+
'verify_peer' => false,
138+
'verify_peer_name' => false
138139
)
139140
)
140141
);
@@ -482,7 +483,7 @@ public function sendWordFromStream($prefix, $stream)
482483
*
483484
* @return void
484485
*/
485-
protected static function verifyLengthSupport($length)
486+
public static function verifyLengthSupport($length)
486487
{
487488
if ($length > 0xFFFFFFFF) {
488489
throw new LengthException(

src/PEAR2/Net/RouterOS/Query.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,39 @@ private function _send(Communicator $com)
240240
return $bytes;
241241
}
242242

243+
/**
244+
* Verifies the query.
245+
*
246+
* Verifies the query against a communicator, i.e. whether the query
247+
* could successfully be sent (assuming the connection is still opened).
248+
*
249+
* @param Communicator $com The Communicator to check against.
250+
*
251+
* @return $this The query object itself.
252+
*
253+
* @throws LengthException If the resulting length of an API word is not
254+
* supported.
255+
*/
256+
public function verify(Communicator $com)
257+
{
258+
foreach ($this->words as $queryWord) {
259+
list($predicate, $value) = $queryWord;
260+
if (null === $value) {
261+
$com::verifyLengthSupport(strlen('?' . $predicate));
262+
} elseif (is_string($value)) {
263+
$com::verifyLengthSupport(
264+
strlen('?' . $predicate . '=' . $value)
265+
);
266+
} else {
267+
$com::verifyLengthSupport(
268+
strlen('?' . $predicate . '=') +
269+
$com::seekableStreamLength($value)
270+
);
271+
}
272+
}
273+
return $this;
274+
}
275+
243276
/**
244277
* Adds a condition.
245278
*

src/PEAR2/Net/RouterOS/Request.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,40 @@ private function _send(Communicator $com)
339339
return $bytes;
340340
}
341341

342+
/**
343+
* Verifies the request.
344+
*
345+
* Verifies the request against a communicator, i.e. whether the request
346+
* could successfully be sent (assuming the connection is still opened).
347+
*
348+
* @param Communicator $com The Communicator to check against.
349+
*
350+
* @return $this The request object itself.
351+
*
352+
* @throws LengthException If the resulting length of an API word is not
353+
* supported.
354+
*/
355+
public function verify(Communicator $com)
356+
{
357+
$com::verifyLengthSupport(strlen($this->getCommand()));
358+
$com::verifyLengthSupport(strlen('.tag=' . (string)$this->getTag()));
359+
foreach ($this->attributes as $name => $value) {
360+
if (is_string($value)) {
361+
$com::verifyLengthSupport(strlen('=' . $name . '=' . $value));
362+
} else {
363+
$com::verifyLengthSupport(
364+
strlen('=' . $name . '=') +
365+
$com::seekableStreamLength($value)
366+
);
367+
}
368+
}
369+
$query = $this->getQuery();
370+
if ($query instanceof Query) {
371+
$query->verify($com);
372+
}
373+
return $this;
374+
}
375+
342376
/**
343377
* Parses the arguments of a command.
344378
*

src/PEAR2/Net/RouterOS/Util.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ public static function parseValue($value)
182182
);
183183
//@codeCoverageIgnoreStart
184184
// See previous ignored section's note.
185-
//
185+
//
186186
// This section is added for backwards compatibility with current
187187
// PHP versions, when in the future sub-second support is added.
188188
// In that event, the test inputs for older versions will be

tests/Client/Safe.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,8 +1271,8 @@ public function testResponseCollectionOrderBy()
12711271
$sortedByMaxLimitDownloadFirst
12721272
);
12731273
$this->assertNotSame(
1274-
$sortedByMaxLimitFirst,
1275-
$sortedByMaxLimitDownloadFirst
1274+
$sortedByMaxLimitLast,
1275+
$sortedByMaxLimitDownloadLast
12761276
);
12771277
}
12781278

tests/Client/Unsafe.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,8 @@ public function testSendSyncReturningResponseStreamData()
120120

121121
public function testSendSyncReturningResponseLarge3bytesLength()
122122
{
123-
$this->markTestIncomplete(
124-
'For some reason, my RouterOS v5.6 doesn not work with this (bug?).'
125-
);
123+
$memoryLimit = ini_set('memory_limit', -1);
124+
126125
$systemResource = $this->object->sendSync(
127126
new Request('/system/resource/print')
128127
);
@@ -169,13 +168,18 @@ public function testSendSyncReturningResponseLarge3bytesLength()
169168
);
170169
}
171170
}
171+
172+
//Clearing out for other tests.
173+
ini_set('memory_limit', $memoryLimit);
172174
}
173175

174176
public function testSendSyncReturningResponseLarge4bytesLength()
175177
{
176178
$this->markTestIncomplete(
177-
'For some reason, my RouterOS v5.6 doesn not work with this (bug?).'
179+
'For some reason, my RouterOS v6.* doesn not work with this (bug?).'
178180
);
181+
$memoryLimit = ini_set('memory_limit', -1);
182+
179183
$systemResource = $this->object->sendSync(
180184
new Request('/system/resource/print')
181185
);
@@ -222,19 +226,19 @@ public function testSendSyncReturningResponseLarge4bytesLength()
222226
);
223227
}
224228
}
229+
230+
//Clearing out for other tests.
231+
ini_set('memory_limit', $memoryLimit);
225232
}
226233

227234
public function testSendSyncReturningResponseLargeDataException()
228235
{
229-
$this->markTestIncomplete(
230-
'TODO: A known issue; Requests with excessively long words "leak".'
231-
);
232236
//Required for this test
233237
$memoryLimit = ini_set('memory_limit', -1);
234238
try {
235239
$comment = fopen('php://temp', 'r+b');
236240
fwrite($comment, str_repeat('t', 0xFFFFFF));
237-
for ($i = 0; $i < 14; $i++) {
241+
for ($i = 0; $i < 255; $i++) {
238242
fwrite($comment, str_repeat('t', 0xFFFFFF));
239243
}
240244
fwrite(
@@ -243,14 +247,12 @@ public function testSendSyncReturningResponseLargeDataException()
243247
);
244248
rewind($comment);
245249

246-
$commentString = stream_get_contents($comment);
247-
$maxArgL = 0xFFFFFFF - strlen('=comment=');
250+
$maxArgL = 0xFFFFFFFF - strlen('=comment=');
248251
$this->assertGreaterThan(
249252
$maxArgL,
250-
strlen($commentString),
253+
Communicator::seekableStreamLength($comment),
251254
'$comment is not long enough.'
252255
);
253-
unset($commentString);
254256
rewind($comment);
255257
$addRequest = new Request('/queue/simple/add');
256258
$addRequest->setArgument('name', TEST_QUEUE_NAME)

0 commit comments

Comments
 (0)