aboutsummaryrefslogtreecommitdiff
path: root/tests/test-cutils.c
diff options
context:
space:
mode:
authorMarkus Armbruster <armbru@redhat.com>2017-02-21 21:13:57 +0100
committerMarkus Armbruster <armbru@redhat.com>2017-02-23 20:35:36 +0100
commit0b742797aaada3a2e243175a69d542d2ed997aac (patch)
tree9a24db8ae7340c5ab5dc8ff8f8a124981a0be045 /tests/test-cutils.c
parenta6b4373fa257fdd7139a74187851575709a5ecb6 (diff)
downloadqemu-0b742797aaada3a2e243175a69d542d2ed997aac.zip
qemu-0b742797aaada3a2e243175a69d542d2ed997aac.tar.gz
qemu-0b742797aaada3a2e243175a69d542d2ed997aac.tar.bz2
test-cutils: Cover qemu_strtosz() around range limits
Signed-off-by: Markus Armbruster <armbru@redhat.com> Reviewed-by: Eric Blake <eblake@redhat.com> Message-Id: <1487708048-2131-14-git-send-email-armbru@redhat.com>
Diffstat (limited to 'tests/test-cutils.c')
-rw-r--r--tests/test-cutils.c61
1 files changed, 59 insertions, 2 deletions
diff --git a/tests/test-cutils.c b/tests/test-cutils.c
index 1773f15..df6c330 100644
--- a/tests/test-cutils.c
+++ b/tests/test-cutils.c
@@ -1371,16 +1371,52 @@ static void test_qemu_strtou64_full_max(void)
static void test_qemu_strtosz_simple(void)
{
- const char *str = "12345M";
+ const char *str;
char *endptr = NULL;
int64_t res;
+ str = "0";
+ res = qemu_strtosz(str, &endptr);
+ g_assert_cmpint(res, ==, 0);
+ g_assert(endptr == str + 1);
+
+ str = "12345M";
res = qemu_strtosz(str, &endptr);
g_assert_cmpint(res, ==, 12345 * M_BYTE);
g_assert(endptr == str + 6);
res = qemu_strtosz(str, NULL);
g_assert_cmpint(res, ==, 12345 * M_BYTE);
+
+ /* Note: precision is 53 bits since we're parsing with strtod() */
+
+ str = "9007199254740991"; /* 2^53-1 */
+ res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
+ g_assert_cmpint(res, ==, 0x1fffffffffffff);
+ g_assert(endptr == str + 16);
+
+ str = "9007199254740992"; /* 2^53 */
+ res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
+ g_assert_cmpint(res, ==, 0x20000000000000);
+ g_assert(endptr == str + 16);
+
+ str = "9007199254740993"; /* 2^53+1 */
+ res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
+ g_assert_cmpint(res, ==, 0x20000000000000); /* rounded to 53 bits */
+ g_assert(endptr == str + 16);
+
+ str = "9223372036854774784"; /* 0x7ffffffffffffc00 (53 msbs set) */
+ res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
+ g_assert_cmpint(res, ==, 0x7ffffffffffffc00);
+ g_assert(endptr == str + 19);
+
+ str = "9223372036854775295"; /* 0x7ffffffffffffdff */
+ res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
+ g_assert_cmpint(res, ==, 0x7ffffffffffffc00); /* rounded to 53 bits */
+ g_assert(endptr == str + 19);
+
+ /* 0x7ffffffffffffe00..0x7fffffffffffffff get rounded to
+ * 0x8000000000000000, thus -ERANGE; see test_qemu_strtosz_erange() */
}
static void test_qemu_strtosz_units(void)
@@ -1482,10 +1518,31 @@ static void test_qemu_strtosz_trailing(void)
static void test_qemu_strtosz_erange(void)
{
- const char *str = "10E";
+ const char *str;
char *endptr = NULL;
int64_t res;
+ str = "-1";
+ res = qemu_strtosz(str, &endptr);
+ g_assert_cmpint(res, ==, -ERANGE);
+ g_assert(endptr == str + 2);
+
+ str = "9223372036854775296"; /* 0x7ffffffffffffe00 */
+ res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
+ g_assert_cmpint(res, ==, -ERANGE);
+ g_assert(endptr == str + 19);
+
+ str = "9223372036854775807"; /* 2^63-1 */
+ res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
+ g_assert_cmpint(res, ==, -ERANGE);
+ g_assert(endptr == str + 19);
+
+ str = "9223372036854775808"; /* 2^63 */
+ res = qemu_strtosz_suffix(str, &endptr, QEMU_STRTOSZ_DEFSUFFIX_B);
+ g_assert_cmpint(res, ==, -ERANGE);
+ g_assert(endptr == str + 19);
+
+ str = "10E";
res = qemu_strtosz(str, &endptr);
g_assert_cmpint(res, ==, -ERANGE);
g_assert(endptr == str + 3);