bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody/* always pads with leading zeros to a size of 9 digits */
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmodystatic int crappy_uintmax_to_str(char *into, uintmax_t val)
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody len = crappy_uintmax_to_str(into, val/BIGBASE);
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody unsigned int i=0;
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek int randrange = i_rand_minmax(1, 15); /* when 1, will max out on 1s */
62461eb609e1d852e027cf4e07d30d51288678a2Aki Tuomi uintmax_t value = 0, valbase = i_rand() * 1000ull;
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody char buff[50]; /* totally assumes < 159 bits */
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek value -= i_rand_limit(randrange); /* don't always test the same numbers */
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody /* test with trailing noise */
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody buff[len] = 'x'; /* don't even null-terminate, let's be evil */
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody test_assert_idx(value_back == 0x1234567890123456, i);
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody ret = str_parse_uintmax(buff, &value_back, &endp);
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody /* not knowing exactly how large a uintmax_t is, we have to construct
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody the troublesome near-10/9*MAX strings manually by appending digits
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody to a MAX/9 string which we can easily create. Do a wider range
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody of 30 rather than the obvious 10, just in case - all are too large.*/
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody test_begin("str_to_uintmax overflow corner case");
629e96c5e2d4724b713ca7d62e59ed033107edcdPhil Carmody for(i = 0; i <= 30; ++i) {
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody test_assert_idx(ret < 0 && value == valbase + i, i);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch/* always pads with leading zeros to a size of 9 digits */
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschstatic int crappy_uintmax_to_str_hex(char *into, uintmax_t val)
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch len = crappy_uintmax_to_str_hex(into, val/BIGBASE);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch unsigned int i=0;
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek int randrange = i_rand_minmax(1, 15); /* when 1, will max out on 1s */
62461eb609e1d852e027cf4e07d30d51288678a2Aki Tuomi uintmax_t value = 0, valbase = i_rand() * 1000ull;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch char buff[52]; /* totally assumes < 200 bits */
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek value -= i_rand_limit(randrange); /* don't always test the same numbers */
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch /* test with trailing noise */
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch buff[len] = 'x'; /* don't even null-terminate, let's be evil */
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch test_assert_idx(value_back == 0x1234567890123456, i);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch ret = str_parse_uintmax_hex(buff, &value_back, &endp);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch /* not knowing exactly how large a uintmax_t is, we have to construct
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch the troublesome near-0x10/0x0F*MAX strings manually by appending digits
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch to a MAX/0x0f string which we can easily create. Do a wider range
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch of 0x30 rather than the obvious 0x10, just in case - all are too large.*/
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch test_begin("str_to_uintmax_hex overflow corner case");
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch for(i = 0; i <= 0x30; ++i) {
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody test_assert_idx(ret < 0 && value == valbase + i, i);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch/* always pads with leading zeros to a size of 9 digits */
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Boschstatic int crappy_uintmax_to_str_oct(char *into, uintmax_t val)
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch len = crappy_uintmax_to_str_oct(into, val/BIGBASE);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch unsigned int i=0;
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek int randrange = i_rand_minmax(1, 15); /* when 1, will max out on 1s */
62461eb609e1d852e027cf4e07d30d51288678a2Aki Tuomi uintmax_t value = 0, valbase = i_rand() * 1000ull;
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch char buff[69]; /* totally assumes < 200 bits */
191153d1a5b0eb0c129139570e3aa5212f28d2acJosef 'Jeff' Sipek value -= i_rand_limit(randrange); /* don't always test the same numbers */
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch /* test with trailing noise */
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch buff[len] = 'x'; /* don't even null-terminate, let's be evil */
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch test_assert_idx(value_back == 0x1234567890123456, i);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch ret = str_parse_uintmax_oct(buff, &value_back, &endp);
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch /* not knowing exactly how large a uintmax_t is, we have to construct
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch the troublesome near-010/007*MAX strings manually by appending digits
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch to a MAX/007 string which we can easily create. Do a wider range
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch of 030 rather than the obvious 010, just in case - all are too large.*/
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch test_begin("str_to_uintmax_oct overflow corner case");
4a272f5b8bacf2852c2e53f3aa8e899e0d5c604fStephan Bosch for(i = 0; i <= 030; ++i) {
d6bbf85809664a810726b5c711c7213874d8df57Phil Carmody test_assert_idx(ret < 0 && value == valbase + i, i);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody unsigned int i;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody const struct {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody VALID(000000000000000000000000000000000000000000000000000000000000000),
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody { "000000000000000000000000000000000000000000000000000001000000001", 0, 1000000001 },
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody { "18446744073709551615", 0, 18446744073709551615ULL },
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody INVALID(20496382304121724010), /* 2^64*10/9 doesn't wrap */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody INVALID(20496382304121724017), /* 2^64*10/9 wraps only after addition */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody INVALID(20496382304121724020), /* 2^64*10/9 wraps on multiply*/
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody int ret = str_to_uint64(u64tests[i].input, &val);
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmody const char *longer = t_strconcat(u64tests[i].input, "x", NULL);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody unsigned int i;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody const struct {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody int ret = str_to_uint32(u32tests[i].input, &val);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody/* Assumes long long is 64 bit, 2's complement */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody unsigned int i;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody const struct {
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody { "-9223372036854775808", 0, -9223372036854775807-1 },
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody int ret = str_to_llong(i64tests[i].input, &val);
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody/* Assumes int is 32 bit, 2's complement */
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody unsigned int i;
42826d96c8d0bba9eddc85b01bf70d7db571ae7fPhil Carmody const struct {
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi /* accepts integer */
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi test_begin("str_is_float refuses invalid values");
d3a430481a1e072fb55fee8803c16d075bf7bd91Aki Tuomi test_assert(!str_is_float("i am not a float at all",'\0'));
47ca7a853a116b1be3d73b46301d97e572f2f806Phil Carmody /* If the above isn't true, then we do expect some failures possibly */