Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/smokeping.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ function smokeping_cli_import_smokeping(array $options, $file, $apply, $confirm,
$service = smokeping_import_service();

foreach (array('device-template', 'poller', 'site', 'tree') as $name) {
if (isset($options[$name]) && !preg_match('/^[0-9]{1,9}$/', $options[$name])) {
if (isset($options[$name]) && !preg_match('/^[0-9]{1,9}$/D', $options[$name])) {
fwrite(STDERR, 'ERROR: --' . $name . ' must be a number' . PHP_EOL . $usage);

return 1;
Expand Down
2 changes: 1 addition & 1 deletion lib/ConfigSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public static function build(ConfigTree $tree): ConfigDocument {
* look like its type stays a string, and the check reports it.
*/
public static function coerce(string $type, string $text): int|bool|string {
if ($type === 'int' && preg_match('/^-?[0-9]{1,18}$/', $text) === 1) {
if ($type === 'int' && preg_match('/^-?[0-9]{1,18}$/D', $text) === 1) {
return (int) $text;
}

Expand Down
4 changes: 2 additions & 2 deletions lib/LossColors.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ enum LossColors: string {
* 2.007003; null for a version that is not in that form.
*/
public static function ofVersion(string $version): ?self {
if (preg_match('/^(\d+)\.(\d{3})\d{3}$/', $version, $m) !== 1) {
if (preg_match('/^(\d+)\.(\d{3})\d{3}$/D', $version, $m) !== 1) {
return null;
}

Expand All @@ -59,7 +59,7 @@ public static function resolve(string $setting, string $detectedVersion): self {
* 2.7.3; anything else unchanged.
*/
public static function displayVersion(string $version): string {
if (preg_match('/^(\d+)\.(\d{3})(\d{3})$/', $version, $m) !== 1) {
if (preg_match('/^(\d+)\.(\d{3})(\d{3})$/D', $version, $m) !== 1) {
return $version;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/SmokepingInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ private static function probeProperties(?SmokepingSection $probes, string $name)
}

private static function intOrNull(?string $value): ?int {
if ($value === null || preg_match('/^\d+$/', $value) !== 1) {
if ($value === null || preg_match('/^\d+$/D', $value) !== 1) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/SummaryScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function values(string $series): SummaryValues {
* @return array{target: int, location: string}|null
*/
public static function parse(string $series): ?array {
if (preg_match('/^([1-9][0-9]{0,9})~([-_.0-9a-zA-Z]{0,64})$/', $series, $match) !== 1) {
if (preg_match('/^([1-9][0-9]{0,9})~([-_.0-9a-zA-Z]{0,64})$/D', $series, $match) !== 1) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/YamlReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ private function quoted(string $text, int $start, int $no): array {
$digits = $e === 'x' ? 2 : ($e === 'u' ? 4 : 0);
$hex = $digits > 0 ? substr($text, $i + 1, $digits) : '';

if ($digits === 0 || strlen($hex) !== $digits || preg_match('/^[0-9A-Fa-f]+$/', $hex) !== 1) {
if ($digits === 0 || strlen($hex) !== $digits || preg_match('/^[0-9A-Fa-f]+$/D', $hex) !== 1) {
throw new ConfigException(sprintf('Unknown escape "\\%s" in a double-quoted scalar', $e), $no);
}

Expand Down
10 changes: 5 additions & 5 deletions lib/YamlScalar.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ public static function classify(string $text): array {
return [self::BOOL, false];
}

if (preg_match('/^(0|-?[1-9][0-9]{0,17})$/', $text) === 1) {
if (preg_match('/^(0|-?[1-9][0-9]{0,17})$/D', $text) === 1) {
return [self::INT, (int) $text];
}

if (
preg_match('/^(y|n|yes|no|on|off)$/i', $text) === 1
|| preg_match('/^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$/', $text) === 1
preg_match('/^(y|n|yes|no|on|off)$/iD', $text) === 1
|| preg_match('/^[-+]?(\.[0-9]+|[0-9]+(\.[0-9]*)?)([eE][-+]?[0-9]+)?$/D', $text) === 1
|| preg_match('/^[-+]?0[xXoObB]/', $text) === 1
|| preg_match('/^[-+]?\.(inf|nan)$/i', $text) === 1
|| preg_match('/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+(\.[0-9]*)?$/', $text) === 1
|| preg_match('/^[-+]?\.(inf|nan)$/iD', $text) === 1
|| preg_match('/^[-+]?[0-9][0-9_]*(:[0-5]?[0-9])+(\.[0-9]*)?$/D', $text) === 1
|| preg_match('/^[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}/', $text) === 1
|| $text === '='
|| $text === '<<'
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -498,3 +498,8 @@ public function bytes(int $length): string {

expect(fn () => $service->apply($document, $plan->id(), 1, 1))->toThrow(ConfigException::class, 'do not cover every device');
});

it('leaves integer text with a trailing newline as a string for the check to report', function () {
expect(Somethingwithproof\Smokeping\ConfigSchema::coerce('int', '42'))->toBe(42)
->and(Somethingwithproof\Smokeping\ConfigSchema::coerce('int', "42\n"))->toBe("42\n");
});
4 changes: 3 additions & 1 deletion tests/Unit/LossColorsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ function lc_dir(array $files): string {
['3.000000', LossColors::Current],
['', null],
['2.8.2', null],
["2.007003\n", null],
]);

it('follows the setting, else the detected version, else 2.8 and later', function () {
Expand All @@ -72,7 +73,8 @@ function lc_dir(array $files): string {
->and(LossColors::resolve('bogus', ''))->toBe(LossColors::Current)
->and(LossColors::Legacy->other())->toBe(LossColors::Current)
->and(LossColors::displayVersion('2.007003'))->toBe('2.7.3')
->and(LossColors::displayVersion('odd'))->toBe('odd');
->and(LossColors::displayVersion('odd'))->toBe('odd')
->and(LossColors::displayVersion("2.007003\n"))->toBe("2.007003\n");
});

it('names a graph template per ping count and color table', function () {
Expand Down
2 changes: 2 additions & 0 deletions tests/Unit/SummaryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ function summaryObservation(?float $loss, ?float $median, bool $present = true,
'space' => ['12~a b', null],
'shell' => ['12~$(id)', null],
'empty' => ['', null],
'trailing newline' => ["12~berlin\n", null],
'newline after separator' => ["12~\n", null],
]);

it('builds the key the data source stores', function () {
Expand Down
5 changes: 5 additions & 0 deletions tests/Unit/YamlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

use Somethingwithproof\Smokeping\ConfigException;
use Somethingwithproof\Smokeping\YamlReader;
use Somethingwithproof\Smokeping\YamlScalar;
use Somethingwithproof\Smokeping\YamlWriter;

function smokeping_yaml(string $text): array {
Expand Down Expand Up @@ -288,3 +289,7 @@
// Both outcomes must occur, or the corpus is not exercising the reader.
expect($ok)->toBeGreaterThan(100)->and($bad)->toBeGreaterThan(100);
});

it('classifies a scalar with a trailing newline as a plain string, not a number or ambiguous word', function (string $text) {

Check warning on line 293 in tests/Unit/YamlTest.php

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Split this 125 characters long line (which is greater than 120 authorized).

See more on https://sonarcloud.io/project/issues?id=somethingwithproof_plugin_smokeping&issues=AaH4Kju7Xf_8hDfniBii&open=AaH4Kju7Xf_8hDfniBii&pullRequest=117
expect(YamlScalar::classify($text)[0])->toBe(YamlScalar::STRING);
})->with(["12\n", "yes\n", "1.5\n", ".inf\n", "1:30\n"]);