summaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/kvm/include/test_util.h
diff options
context:
space:
mode:
authorVipin Sharma <vipinsh@google.com>2022-11-03 12:17:18 -0700
committerSean Christopherson <seanjc@google.com>2022-11-16 10:03:24 -0800
commit0001725d0f9b5d749540021befb67c117d566416 (patch)
tree5789cf5ac497191de06a10ac0c22e577a382285f /tools/testing/selftests/kvm/include/test_util.h
parentc15bdebb32ddc73faac5e5180d6997b360e81619 (diff)
downloadlinux-0001725d0f9b5d749540021befb67c117d566416.tar.bz2
KVM: selftests: Add atoi_positive() and atoi_non_negative() for input validation
Many KVM selftests take command line arguments which are supposed to be positive (>0) or non-negative (>=0). Some tests do these validation and some missed adding the check. Add atoi_positive() and atoi_non_negative() to validate inputs in selftests before proceeding to use those values. Signed-off-by: Vipin Sharma <vipinsh@google.com> Reviewed-by: Sean Christopherson <seanjc@google.com> Link: https://lore.kernel.org/r/20221103191719.1559407-7-vipinsh@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
Diffstat (limited to 'tools/testing/selftests/kvm/include/test_util.h')
-rw-r--r--tools/testing/selftests/kvm/include/test_util.h16
1 files changed, 16 insertions, 0 deletions
diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testing/selftests/kvm/include/test_util.h
index feae42863759..3be98e81189a 100644
--- a/tools/testing/selftests/kvm/include/test_util.h
+++ b/tools/testing/selftests/kvm/include/test_util.h
@@ -154,4 +154,20 @@ static inline void *align_ptr_up(void *x, size_t size)
int atoi_paranoid(const char *num_str);
+static inline uint32_t atoi_positive(const char *name, const char *num_str)
+{
+ int num = atoi_paranoid(num_str);
+
+ TEST_ASSERT(num > 0, "%s must be greater than 0, got '%s'", name, num_str);
+ return num;
+}
+
+static inline uint32_t atoi_non_negative(const char *name, const char *num_str)
+{
+ int num = atoi_paranoid(num_str);
+
+ TEST_ASSERT(num >= 0, "%s must be non-negative, got '%s'", name, num_str);
+ return num;
+}
+
#endif /* SELFTEST_KVM_TEST_UTIL_H */