Commit Graph

71 Commits

Author SHA1 Message Date
rillig d13846be6a tests/snprintb: clean up tests
Always write the value to be formatted in hexadecimal, to make it easily
distinguishable from the buffer size and maximum line length.

Use consistent wording in the comments of the test cases. Be stricter
about mistakes in a format.

Fix some wrong numbers in the snprintb_m tests for limited line length.
2024-02-24 13:00:00 +00:00
rillig e7ed3a67af snprintb: allow non-ASCII descriptions on all platforms
Previously, these descriptions were only allowed on platforms where
plain 'char' is unsigned. On platforms where plain 'char' is signed,
they invoked undefined behavior or terminated the output early.
2024-02-24 12:40:00 +00:00
rillig 69c5b3b0c1 snprintb: always null-terminate output
Always null-terminate the output in the buffer, even in error cases. The
wording in the manual page has been promising this since 2008. For
snprintb_m, ensure that the output is terminated with two null
characters, to gracefully handle situations in which the caller does not
check whether snprintb returned an error.

If the buffer size is zero, allow the buffer to be a null pointer,
analogous to snprintf.

Fix an out-of-bounds memory read if the bitfmt ends with a '*' directive
(since today).

In the tests, merge the helper functions for snprintb, snprintb_m, as
they were similar enough.

Fix a few 'line_max exceeded' tests, ensuring that they output a '#'
marker, and that the 'complete' tests don't.
2024-02-22 21:04:23 +00:00
rillig d101133e9b snprintb: error out on out-of-bounds bit shifts
Previously, these invoked undefined behavior, now they lead to an early
return. An example of out-of-bounds bit number is in SCZ_PCICTRL_BITS.
Bit fields that extend beyond the msb are still allowed.

Allow 'f' and 'F' to have fields that are 64 bits wide. This only makes
sense when the field starts at bit 0.

Remove the unused 'val_len', it was only needed before snprintb.c 1.20.
2024-02-22 18:26:15 +00:00
rillig c235936e8d tests/snprintb: test '=' and '*' directives that can never match 2024-02-20 21:45:36 +00:00
rillig 12034681d8 snprintb_m: limit length of generated strings
The manual page promises that the 'max' argument limits the length of
the generated strings, so don't generate any strings that are longer
than that limit. Mark incomplete strings with a trailing '#' to make
them easily recognizable.
2024-02-20 20:31:56 +00:00
rillig c38e738e50 tests/snprintb: fix out-of-bounds memory read (since 2024-02-16)
Before t_snprintb.c 1.20, the buffer size was required to be greater
than zero. Allowing the buffer size to be zero led to buf[-1] being
checked. On amd64, that byte happened to be 0, on i386 it didn't.

Fixes PR lib/57951.
2024-02-20 19:49:10 +00:00
rillig 8f391120f6 snprintb: fix how named bit-fields are split into multiple lines 2024-02-19 23:30:56 +00:00
rillig db5059c208 tests/snprintb: add more tests, especially for snprintb_m 2024-02-19 23:22:03 +00:00
rillig 0b94bf4814 snprintb: eliminate two local variables, allow zero-size buffer
Like snprintf, the buffer size may be zero. Unlike snprintf, the buffer
must not be NULL.
2024-02-16 19:53:40 +00:00
rillig c277ab1882 snprintb: fix integer overflow when writing past a small buffer
Previously, snprintb returned -1 in this case, assuming that the
snprintf used in FMTSTR validates against an overly large buffer size.
2024-02-16 19:20:38 +00:00
rillig 9ec1abff9d tests/snprintb: fix test for 8-bit field value
The previous version tested the behavior on a platform with signed
'char' before the portability fix.
2024-02-16 18:13:47 +00:00
rillig 889b1738f6 snprintb: fix '=' and ':' for 8-bit values on platforms with signed char
Previously, '=' and ':' worked only on platforms where plain 'char' is
unsigned. On platforms where plain 'char' is signed, only 7-bit values
worked.
2024-02-16 18:09:15 +00:00
rillig 935bb26983 tests/snprintb: clean up existing tests, add more tests
Due to the check that any bytes beyond the expected output must be
unmodified, there's no need anymore to explicitly write the "ZZZ" at the
end of the expected output. While here, remove the redundant trailing
"\0".

Add more tests to cover possible situations where an out-of-bounds write
may have occurred. In some cases, the line length specified in
snprintb_m is exceeded.
2024-02-16 01:19:53 +00:00
rillig 26844513e8 snprintb: fix string termination (since today)
In the previous commit, I had accidentally only run the tests for
snprintb_m but not those for snprintb, thereby missing a newly
introduced bug that would not null-terminate the resulting strings.

Add more tests to cover similar situations in which the buffer is too
small to contain the complete output.
2024-02-15 23:48:51 +00:00
rillig 795d5f45da snprintb: fix out-of-bounds write 2024-02-15 22:37:10 +00:00
rillig 1fb4457e9f tests/libutil/snprintb_m: add tests for small line length 2024-02-14 20:51:17 +00:00
rillig fc60683a64 tests/libutil/snprintb: remove redundant integer constant suffixes
I added these because lint told me so. Lint was wrong though, as it had
not implemented the C99 rules for integer constants.
2024-01-27 21:42:29 +00:00
rillig 59290883cd tests/libutil/snprintb: fix test for 32-bit platforms 2024-01-27 10:32:03 +00:00
rillig c7aa2e0520 tests/libutil/snprintb: add more tests
Test all number bases (octal, decimal, hexadecimal), in both old-style
and new-style formats, as well as small buffer sizes.

Document working edge cases such as empty descriptions or descriptions
containing spaces, as well as situations that invoke undefined behavior.

Add comments to the individual test cases, as reading the actual format
strings takes more time.
2024-01-27 08:54:15 +00:00
rillig efceecc2b3 tests/libutil/snprintb: clean up and extend tests
In case of a failure, print the details of the test case, including file
and line number of the actual test data. Do not write the format strings
directly to the output, as they contain non-printable bytes and embedded
null bytes.

After a failed test case, continue with the others.

Lay out the format strings according to their structure, to make them
more readable. Remove redundant "\0" at the end of the new-style format
strings.

Fix an off-by-one error in the test data: 0xf is FIFTEEN, not SIXTEEN.

Add a test for performing a restricted subset of rot13 in the format
string, to explore the limits of snprintb formatting.

What's still missing are tests for edge cases and error cases.
2024-01-25 18:13:14 +00:00
msaitoh 175c89d22f Sync with if_dl.h rev. 1.31's change. 2022-11-07 08:34:30 +00:00
msaitoh 838c32b638 Revert for if_dl.h's change. 2022-10-27 12:59:59 +00:00
msaitoh 5b130b9d06 Sync with if_dl.h rev. 1.29's change. 2022-10-24 13:42:55 +00:00
christos 23ae5a70c4 explicitly set TZ=UTC so that we don't depend on the testing rig for it. 2022-05-02 19:57:50 +00:00
christos 7b280d165b 1. add check for December in MM/DD/YYYY
2. unsetenv("TZ") since tests assume UTC (all relative tests and one date test
   fail otherwise)
2022-04-23 13:08:49 +00:00
kre e12ce6c4ea Adapt for change to parsedate() which now follows POSIX and altered the
dividing line between 21st and 20th century conversions from 69/70 to
68/69.   Adapt the tests for this to conform.
2020-10-19 15:06:49 +00:00
christos ea19b263f1 Adjust tests for 0 hex values printing 0 instead of 0x0 2019-12-08 17:37:16 +00:00
christos 91fabc4486 Fix broken tests in big endian machines. Internally field values are
stored as uint64_t/uintmax_t so printing them with %d on big endian
ends up being 0.
2019-12-06 19:28:11 +00:00
kre 63e182f423 snprintb(3) says that, in the new(?) Torek format, all fields specs end with \0
The F spec is one of those, it should be terminated with \0 just like all
the others (irrelevant that it has no extra data to delimit).

Fix <sys/mman.h> to define the snprintb() format string correctly (include
the missing \0's).   Fix the copy of that definition included into
snprintb(3) to match the updated mman.h version (ride the date bump
from the day before yesterday .. this is the same change, just corrected).

Undo the previous snprintb.c change ("off by one" fix) which was an
attempt to make the broken mman.h usage work (and did, but not the way
it should be done).   Also, after using the new * format (instead of only
when something has already matched) skip the associated data so we don't
attempt to interpret it as more field specifiers.  This func needs lots of TLC!

Fix the ATF tests for snprintb() to not assume that F format is really
exactly like f format, and has data after the field specifier.  It doesn't.
Add several more tests (including testing the '*' field operator
recently added).
2019-04-29 07:55:38 +00:00
ryo 638374d825 add test case for new format 'f' and 'F' 2017-10-14 18:41:44 +00:00
kre 992d3e66d8 Fix typo in a comment. 2017-03-22 05:35:33 +00:00
kre 08844cd91f Add tests for "12 noon", "12 midnight". 2017-03-22 01:50:22 +00:00
kre f8f91559ae Since PR lib/52101 is now fixed, the tests for its bug should no longer fail. 2017-03-22 01:00:19 +00:00
kre 10db8f184b Fix a typo - it doesn't matter now, as the test fails anyway, but
it will matter when parsedate is fixed and the test is supposed to
succeed.
2017-03-21 21:42:18 +00:00
kre 0c8a4b80f9 PR lib/52101
Add 6 extra tests for the 12am/12pm bug - all currently expected to fail.
(That is, 6 subtests of the "times" test will fail, all new ones)>

While here, when parsedate() fails (returns -1) avoid converting that
failure value (-1) to a struct tm (1969-12-31T23:59:59 UTC) and then
comparing the values with those expected by the test, and complaining
about all of those (where ANY was not permitted) that don't match...
2017-03-21 20:06:27 +00:00
kre bd9b00916f Remove tests of "dawn" and "sunset" in preparation for removing
them from parsedate() itself sometime soon.
2016-06-22 15:01:38 +00:00
kre 25d58d24b2 Make the 9 months ago test, when reverting from late November to the
end of Feburary just a little less bogus (stupid cut & paste error).
2016-05-05 02:12:21 +00:00
gson 85c4af6b99 Reduce the step size, exposing another bug. 2016-05-04 18:36:46 +00:00
kre e4edd6e6c4 PR bin/50574 - make the tests conform to what parsedate() actually
does, so they should no longer fail.   This needs parsedate.y 1.28
in order to work properly.

OK christos@
2016-05-03 18:18:15 +00:00
kre 97c9ae31d2 Whitespace. No changes. OK christos@ 2016-05-03 18:10:38 +00:00
gson 73fec7e59e In the "relative" test case, run the tests multiple times relative to
various dates over a span of a few decades, instead of just once
relative to the current date.  This makes the test fail consistently
instead of sometimes failing and sometimes not depending on when
it is run.  Makes PR lib/50574 consistently reproducible.
2016-05-01 16:39:47 +00:00
dholland bb7bd59051 Fix REL_CHECK error output; it had "expected" and "obtained" swapped.
While here, improve the general legibility of the printout. Not
actually relevant to PR 50574, but anyone looking at that should be
aware of this change.
2015-12-31 10:56:13 +00:00
dholland 6970e57cc6 When evaluated on a Sunday, "next Sunday" means 7 days in the future,
not 14. When evaluated on a Monday, it apparently means 13 days in the
future. There's not exactly a spec for parsedate.y, so conform to the
implementation.

PR 50574.

XXX: to me at least this is an odd notion of "next Sunday", but whatever...
2015-12-31 10:18:00 +00:00
dholland 9775679d6b Fix the "Tuesday midnight" test to match what parsedate.y does; in
particular it doesn't add a week if evaluating that on Wednesday.

Whether that's right is an open question, but there's not exactly a
spec for parsedate.y and there's no point having the test fail one
day in seven.

PR 50574.
2015-12-31 10:10:15 +00:00
christos 8861b14877 Kill one more REL_DEBUG, from kre 2015-12-08 12:41:28 +00:00
christos da8d5d2ae7 there is no REL_DEBUG 2015-12-07 20:57:39 +00:00
christos 432cd4b9b7 new tests from kre 2015-12-07 20:52:46 +00:00
apb 4206688740 2013-07-09 was in British Summer TIme, so use tzoff=-60, not 0. 2014-10-08 17:23:03 +00:00
apb 5b0c63042a When tests fail, print all args, notjust the date string. 2014-10-08 17:21:40 +00:00