From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 988631FF0AF for ; Thu, 08 Oct 2026 15:52:44 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 9B3732162D; Thu, 08 Oct 2026 15:52:29 +0200 (CEST) From: Dominik Csapak To: pve-devel@lists.proxmox.com Subject: [PATCH common 2/3] json schema: add tests for numeric behavior Date: Thu, 8 Oct 2026 15:48:12 +0200 Message-ID: <20261008135224.3323985-3-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261008135224.3323985-1-d.csapak@proxmox.com> References: <20261008135224.3323985-1-d.csapak@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL 0.375 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: A4OZQTFBPOCPZD2CQOEKUGXXO7Q47N2S X-Message-ID-Hash: A4OZQTFBPOCPZD2CQOEKUGXXO7Q47N2S X-MailFrom: d.csapak@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Signed-off-by: Dominik Csapak --- test/json-schema-test.pl | 166 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 166 insertions(+) diff --git a/test/json-schema-test.pl b/test/json-schema-test.pl index f1c968f..c5014e9 100755 --- a/test/json-schema-test.pl +++ b/test/json-schema-test.pl @@ -22,6 +22,10 @@ use Test::More; # in - input of the sub-test # out - expected output of the sub-test, falls back to outer `out` # must_fail - if set the test must fail and the error must match the regex defined here. + +# exceeds the range of a double, so it numifies to infinity +my $huge_int = '1' . ('0' x 400); + my $property_string_tests = [ { name => 'default-key-with-type-boolean', @@ -99,6 +103,38 @@ my $property_string_tests = [ }, ], }, + { + name => 'huge-numeric-values', + format => { + int => { type => 'integer', optional => 1 }, + num => { type => 'number', optional => 1 }, + 'int-ranged' => { type => 'integer', optional => 1, minimum => -10, maximum => 10 }, + 'num-ranged' => { type => 'number', optional => 1, minimum => -1, maximum => 1 }, + }, + subtests => [ + { in => "int=$huge_int", out => { int => $huge_int } }, + { in => "int=-$huge_int", out => { int => "-$huge_int" } }, + { in => "num=$huge_int", out => { num => $huge_int } }, + { in => "num=1e400", out => { num => '1e400' } }, + { in => "num=-1e400", out => { num => '-1e400' } }, + { + in => "int-ranged=$huge_int", + must_fail => qr/int-ranged: value must have a maximum value of 10/, + }, + { + in => "int-ranged=-$huge_int", + must_fail => qr/int-ranged: value must have a minimum value of -10/, + }, + { + in => "num-ranged=1e400", + must_fail => qr/num-ranged: value must have a maximum value of 1/, + }, + { + in => "num-ranged=-1e400", + must_fail => qr/num-ranged: value must have a minimum value of -1/, + }, + ], + }, # TODO: more tests, like complex formats and ranges and the like ]; @@ -863,4 +899,134 @@ for my $test ($check_one_of->@*) { }; } +# check numeric type and range checks with values exceeding the range of a double +# +# Properties of a test: +# +# name - describes the test +# schema - the schema of the property 'value' +# subtests - list of: +# name - describes the sub-test +# in - the value to check +# must_fail - regex that must match the error of the property +my $numeric_tests = [ + { + name => 'huge integer', + schema => { type => 'integer' }, + subtests => [ + { name => 'positive', in => "100" }, + { name => 'negative', in => "-100" }, + { name => 'explicit plus sign', in => "+100" }, + { + name => 'exponent notation', + in => '1e2', + must_fail => qr/^type check \('integer'\) failed - got '1e2'/, + }, + { + name => 'not a number', + in => 'foo', + must_fail => qr/^type check \('integer'\) failed/, + }, + ], + }, + { + name => 'huge integer', + schema => { type => 'integer' }, + subtests => [ + { name => 'positive', in => $huge_int }, + { name => 'negative', in => "-$huge_int" }, + { name => 'explicit plus sign', in => "+$huge_int" }, + { + name => 'exponent notation', + in => '1e400', + must_fail => qr/^type check \('integer'\) failed/, + }, + { + name => 'fractional part', + in => "$huge_int.0", + must_fail => qr/^type check \('integer'\) failed/, + }, + ], + }, + { + name => 'huge integer with range', + schema => { type => 'integer', minimum => -10, maximum => 10 }, + subtests => [ + { + name => 'above maximum', + in => $huge_int, + must_fail => qr/^value must have a maximum value of 10$/, + }, + { + name => 'below minimum', + in => "-$huge_int", + must_fail => qr/^value must have a minimum value of -10$/, + }, + ], + }, + { + name => 'huge number', + schema => { type => 'number' }, + subtests => [ + { name => 'integer notation', in => $huge_int }, + { name => 'negative integer notation', in => "-$huge_int" }, + { name => 'fractional part', in => "$huge_int.5" }, + { name => 'exponent notation', in => '1e400' }, + { name => 'negative exponent notation', in => '-1e400' }, + { name => 'tiny exponent notation', in => '1e-400' }, + { + name => 'infinity literal', + in => 'inf', + must_fail => qr/^type check \('number'\) failed/, + }, + ], + }, + { + name => 'huge number with range', + schema => { type => 'number', minimum => 0, maximum => 1 }, + subtests => [ + { + name => 'above maximum', + in => '1e400', + must_fail => qr/^value must have a maximum value of 1$/, + }, + { + name => 'above maximum in integer notation', + in => $huge_int, + must_fail => qr/^value must have a maximum value of 1$/, + }, + { + name => 'below minimum', + in => '-1e400', + must_fail => qr/^value must have a minimum value of 0$/, + }, + { name => 'tiny value within range', in => '1e-400' }, + ], + }, +]; + +for my $test ($numeric_tests->@*) { + subtest $test->{name}, sub { + for my $subtest ($test->{subtests}->@*) { + my $name = $subtest->{name}; + my $schema = { + type => 'object', + properties => { value => $test->{schema} }, + }; + my $value = { value => $subtest->{in} }; + + my $errors = {}; + PVE::JSONSchema::check_prop($value, $schema, undef, $errors); + + if (my $must_fail = $subtest->{must_fail}) { + like(delete($errors->{value}) // '', $must_fail, "$name - failed as expected"); + } + my $err_str = join("\n", map { "$_: $errors->{$_}" } sort keys %$errors); + is($err_str, '', "$name - no unexpected errors"); + is($value->{value}, $subtest->{in}, "$name - value remains unchanged"); + } + done_testing(); + }; +} + done_testing(); -- 2.47.3