cmdline_kunit.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Test cases for API provided by cmdline.c
  4. */
  5. #include <kunit/test.h>
  6. #include <linux/kernel.h>
  7. #include <linux/random.h>
  8. #include <linux/string.h>
  9. static const char *cmdline_test_strings[] = {
  10. "\"\"", "" , "=" , "\"-", "," , "-," , ",-" , "-" ,
  11. "+," , "--", ",,", "''" , "\"\",", "\",\"", "-\"\"", "\"",
  12. };
  13. static const int cmdline_test_values[] = {
  14. 1, 1, 1, 1, 2, 3, 2, 3,
  15. 1, 3, 2, 1, 1, 1, 3, 1,
  16. };
  17. static_assert(ARRAY_SIZE(cmdline_test_strings) == ARRAY_SIZE(cmdline_test_values));
  18. static const char *cmdline_test_range_strings[] = {
  19. "-7" , "--7" , "-1-2" , "7--9",
  20. "7-" , "-7--9", "7-9," , "9-7" ,
  21. "5-a", "a-5" , "5-8" , ",8-5",
  22. "+,1", "-,4" , "-3,0-1,6", "4,-" ,
  23. " +2", " -9" , "0-1,-3,6", "- 9" ,
  24. };
  25. static const int cmdline_test_range_values[][16] = {
  26. { 1, -7, }, { 0, -0, }, { 4, -1, 0, +1, 2, }, { 0, 7, },
  27. { 0, +7, }, { 0, -7, }, { 3, +7, 8, +9, 0, }, { 0, 9, },
  28. { 0, +5, }, { 0, -0, }, { 4, +5, 6, +7, 8, }, { 0, 0, },
  29. { 0, +0, }, { 0, -0, }, { 4, -3, 0, +1, 6, }, { 1, 4, },
  30. { 0, +0, }, { 0, -0, }, { 4, +0, 1, -3, 6, }, { 0, 0, },
  31. };
  32. static_assert(ARRAY_SIZE(cmdline_test_range_strings) == ARRAY_SIZE(cmdline_test_range_values));
  33. static void cmdline_do_one_test(struct kunit *test, const char *in, int rc, int offset)
  34. {
  35. const char *fmt = "Pattern: %s";
  36. const char *out = in;
  37. int dummy;
  38. int ret;
  39. ret = get_option((char **)&out, &dummy);
  40. KUNIT_EXPECT_EQ_MSG(test, ret, rc, fmt, in);
  41. KUNIT_EXPECT_PTR_EQ_MSG(test, out, in + offset, fmt, in);
  42. }
  43. static void cmdline_test_noint(struct kunit *test)
  44. {
  45. unsigned int i = 0;
  46. do {
  47. const char *str = cmdline_test_strings[i];
  48. int rc = 0;
  49. int offset;
  50. /* Only first and leading '-' will advance the pointer */
  51. offset = !!(*str == '-');
  52. cmdline_do_one_test(test, str, rc, offset);
  53. } while (++i < ARRAY_SIZE(cmdline_test_strings));
  54. }
  55. static void cmdline_test_lead_int(struct kunit *test)
  56. {
  57. unsigned int i = 0;
  58. char in[32];
  59. do {
  60. const char *str = cmdline_test_strings[i];
  61. int rc = cmdline_test_values[i];
  62. int offset;
  63. sprintf(in, "%u%s", get_random_u8(), str);
  64. /* Only first '-' after the number will advance the pointer */
  65. offset = strlen(in) - strlen(str) + !!(rc == 2);
  66. cmdline_do_one_test(test, in, rc, offset);
  67. } while (++i < ARRAY_SIZE(cmdline_test_strings));
  68. }
  69. static void cmdline_test_tail_int(struct kunit *test)
  70. {
  71. unsigned int i = 0;
  72. char in[32];
  73. do {
  74. const char *str = cmdline_test_strings[i];
  75. /* When "" or "-" the result will be valid integer */
  76. int rc = strcmp(str, "") ? (strcmp(str, "-") ? 0 : 1) : 1;
  77. int offset;
  78. sprintf(in, "%s%u", str, get_random_u8());
  79. /*
  80. * Only first and leading '-' not followed by integer
  81. * will advance the pointer.
  82. */
  83. offset = rc ? strlen(in) : !!(*str == '-');
  84. cmdline_do_one_test(test, in, rc, offset);
  85. } while (++i < ARRAY_SIZE(cmdline_test_strings));
  86. }
  87. static void cmdline_do_one_range_test(struct kunit *test, const char *in,
  88. unsigned int n, const int *e)
  89. {
  90. unsigned int i;
  91. int r[16];
  92. int *p;
  93. memset(r, 0, sizeof(r));
  94. get_options(in, ARRAY_SIZE(r), r);
  95. KUNIT_EXPECT_EQ_MSG(test, r[0], e[0], "in test %u (parsed) expected %d numbers, got %d",
  96. n, e[0], r[0]);
  97. for (i = 1; i < ARRAY_SIZE(r); i++)
  98. KUNIT_EXPECT_EQ_MSG(test, r[i], e[i], "in test %u at %u", n, i);
  99. memset(r, 0, sizeof(r));
  100. get_options(in, 0, r);
  101. KUNIT_EXPECT_EQ_MSG(test, r[0], e[0], "in test %u (validated) expected %d numbers, got %d",
  102. n, e[0], r[0]);
  103. p = memchr_inv(&r[1], 0, sizeof(r) - sizeof(r[0]));
  104. KUNIT_EXPECT_PTR_EQ_MSG(test, p, NULL, "in test %u at %u out of bound", n, p - r);
  105. }
  106. static void cmdline_test_range(struct kunit *test)
  107. {
  108. unsigned int i = 0;
  109. do {
  110. const char *str = cmdline_test_range_strings[i];
  111. const int *e = cmdline_test_range_values[i];
  112. cmdline_do_one_range_test(test, str, i, e);
  113. } while (++i < ARRAY_SIZE(cmdline_test_range_strings));
  114. }
  115. static struct kunit_case cmdline_test_cases[] = {
  116. KUNIT_CASE(cmdline_test_noint),
  117. KUNIT_CASE(cmdline_test_lead_int),
  118. KUNIT_CASE(cmdline_test_tail_int),
  119. KUNIT_CASE(cmdline_test_range),
  120. {}
  121. };
  122. static struct kunit_suite cmdline_test_suite = {
  123. .name = "cmdline",
  124. .test_cases = cmdline_test_cases,
  125. };
  126. kunit_test_suite(cmdline_test_suite);
  127. MODULE_LICENSE("GPL");