Skip to content

Commit 6b7684d

Browse files
committed
Refactor & move xsd-specific tests to xml-common
1 parent 0984792 commit 6b7684d

13 files changed

+219
-674
lines changed

src/Assert.php

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -311,46 +311,24 @@
311311
* @method static void nullOrThrows(Closure|null $expression, string $class, string $message = '', string $exception = '')
312312
* @method static void allThrows(Closure[] $expression, string $class, string $message = '', string $exception = '')
313313
*
314-
* @method static void validHexBinary(mixed $value, string $message = '', string $exception = '')
315-
* @method static void validNMToken(mixed $value, string $message = '', string $exception = '')
316-
* @method static void validNMTokens(mixed $value, string $message = '', string $exception = '')
317-
* @method static void validDuration(mixed $value, string $message = '', string $exception = '')
318-
* @method static void stringPlausibleBase64(mixed $value, string $message = '', string $exception = '')
319-
* @method static void validDateTime(mixed $value, string $message = '', string $exception = '')
314+
* @method static void validBase64(mixed $value, string $message = '', string $exception = '')
320315
* @method static void notInArray(mixed $value, array $values, string $message = '', string $exception = '')
321316
* @method static void validURN(mixed $value, string $message = '', string $exception = '')
322317
* @method static void validURI(mixed $value, string $message = '', string $exception = '')
323318
* @method static void validURL(mixed $value, string $message = '', string $exception = '')
324-
* @method static void validNCName(mixed $value, string $message = '', string $exception = '')
325-
* @method static void validQName(mixed $value, string $message = '', string $exception = '')
326-
* @method static void nullOrValidHexBinary(mixed $value, string $message = '', string $exception = '')
327-
* @method static void nullOrValidNMToken(mixed $value, string $message = '', string $exception = '')
328-
* @method static void nullOrValidNMTokens(mixed $value, string $message = '', string $exception = '')
329-
* @method static void nullOrValidDuration(mixed $value, string $message = '', string $exception = '')
330-
* @method static void nullOrStringPlausibleBase64(mixed $value, string $message = '', string $exception = '')
331-
* @method static void nullOrValidDateTime(mixed $value, string $message = '', string $exception = '')
319+
* @method static void nullOrValidBase64(mixed $value, string $message = '', string $exception = '')
332320
* @method static void nullOrNotInArray(mixed $value, array $values, string $message = '', string $exception = '')
333321
* @method static void nullOrValidURN(mixed $value, string $message = '', string $exception = '')
334322
* @method static void nullOrValidURI(mixed $value, string $message = '', string $exception = '')
335323
* @method static void nullOrValidURL(mixed $value, string $message = '', string $exception = '')
336-
* @method static void nullOrValidNCName(mixed $value, string $message = '', string $exception = '')
337-
* @method static void nullOrValidQName(mixed $value, string $message = '', string $exception = '')
338-
* @method static void allValidHexBinary(mixed $value, string $message = '', string $exception = '')
339-
* @method static void allValidNMToken(mixed $value, string $message = '', string $exception = '')
340-
* @method static void allValidNMTokens(mixed $value, string $message = '', string $exception = '')
341-
* @method static void allValidDuration(mixed $value, string $message = '', string $exception = '')
342-
* @method static void allStringPlausibleBase64(mixed $value, string $message = '', string $exception = '')
343-
* @method static void allValidDateTime(mixed $value, string $message = '', string $exception = '')
324+
* @method static void allValidBase64(mixed $value, string $message = '', string $exception = '')
344325
* @method static void allNotInArray(mixed $value, array $values, string $message = '', string $exception = '')
345-
* @method static void allValidURN(mixed $value, string $message = '', string $exception = '')
346-
* @method static void allValidURI(mixed $value, string $message = '', string $exception = '')
347-
* @method static void allValidURL(mixed $value, string $message = '', string $exception = '')
348-
* @method static void allValidNCName(mixed $value, string $message = '', string $exception = '')
349-
* @method static void allValidQName(mixed $value, string $message = '', string $exception = '')
350326
*/
351327
final class Assert
352328
{
353-
use CustomAssertionTrait;
329+
use Base64Trait;
330+
use NotInArrayTrait;
331+
use URITrait;
354332

355333

356334
/**

src/Base64Trait.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace SimpleSAML\Assert;
6+
7+
use InvalidArgumentException;
8+
9+
use function base64_decode;
10+
use function base64_encode;
11+
use function filter_var;
12+
use function sprintf;
13+
use function strlen;
14+
15+
/**
16+
* @package simplesamlphp/assert
17+
*/
18+
trait Base64Trait
19+
{
20+
/** @var string */
21+
private static string $base64_regex = '/^(?:[a-z0-9+\/]{4})*(?:[a-z0-9+\/]{2}==|[a-z0-9+\/]{3}=)?$/i';
22+
23+
24+
/***********************************************************************************
25+
* NOTE: Custom assertions may be added below this line. *
26+
* They SHOULD be marked as `protected` to ensure the call is forced *
27+
* through __callStatic(). *
28+
* Assertions marked `public` are called directly and will *
29+
* not handle any custom exception passed to it. *
30+
***********************************************************************************/
31+
32+
33+
/**
34+
* Note: This test is not bullet-proof but prevents a string containing illegal characters
35+
* from being passed and ensures the string roughly follows the correct format for a Base64 encoded string
36+
*
37+
* @param string $value
38+
* @param string $message
39+
*/
40+
protected static function validBase64(string $value, string $message = ''): void
41+
{
42+
$result = true;
43+
44+
if (filter_var($value, FILTER_VALIDATE_REGEXP, ['options' => ['regexp' => self::$base64_regex]]) === false) {
45+
$result = false;
46+
} elseif (strlen($value) % 4 !== 0) {
47+
$result = false;
48+
} else {
49+
$decoded = base64_decode($value, true);
50+
if (empty($decoded)) { // Invalid _or_ empty string
51+
$result = false;
52+
} elseif (base64_encode($decoded) !== $value) {
53+
$result = false;
54+
}
55+
}
56+
57+
if ($result === false) {
58+
throw new InvalidArgumentException(sprintf(
59+
$message ?: '\'%s\' is not a valid Base64 encoded string',
60+
$value,
61+
));
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)