Back to home page

LXR

 
 

    


0001 /*
0002  * Test cases for lib/string_helpers.c module.
0003  */
0004 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0005 
0006 #include <linux/init.h>
0007 #include <linux/kernel.h>
0008 #include <linux/slab.h>
0009 #include <linux/module.h>
0010 #include <linux/random.h>
0011 #include <linux/string.h>
0012 #include <linux/string_helpers.h>
0013 
0014 static __init bool test_string_check_buf(const char *name, unsigned int flags,
0015                      char *in, size_t p,
0016                      char *out_real, size_t q_real,
0017                      char *out_test, size_t q_test)
0018 {
0019     if (q_real == q_test && !memcmp(out_test, out_real, q_test))
0020         return true;
0021 
0022     pr_warn("Test '%s' failed: flags = %u\n", name, flags);
0023 
0024     print_hex_dump(KERN_WARNING, "Input: ", DUMP_PREFIX_NONE, 16, 1,
0025                in, p, true);
0026     print_hex_dump(KERN_WARNING, "Expected: ", DUMP_PREFIX_NONE, 16, 1,
0027                out_test, q_test, true);
0028     print_hex_dump(KERN_WARNING, "Got: ", DUMP_PREFIX_NONE, 16, 1,
0029                out_real, q_real, true);
0030 
0031     return false;
0032 }
0033 
0034 struct test_string {
0035     const char *in;
0036     const char *out;
0037     unsigned int flags;
0038 };
0039 
0040 static const struct test_string strings[] __initconst = {
0041     {
0042         .in = "\\f\\ \\n\\r\\t\\v",
0043         .out = "\f\\ \n\r\t\v",
0044         .flags = UNESCAPE_SPACE,
0045     },
0046     {
0047         .in = "\\40\\1\\387\\0064\\05\\040\\8a\\110\\777",
0048         .out = " \001\00387\0064\005 \\8aH?7",
0049         .flags = UNESCAPE_OCTAL,
0050     },
0051     {
0052         .in = "\\xv\\xa\\x2c\\xD\\x6f2",
0053         .out = "\\xv\n,\ro2",
0054         .flags = UNESCAPE_HEX,
0055     },
0056     {
0057         .in = "\\h\\\\\\\"\\a\\e\\",
0058         .out = "\\h\\\"\a\e\\",
0059         .flags = UNESCAPE_SPECIAL,
0060     },
0061 };
0062 
0063 static void __init test_string_unescape(const char *name, unsigned int flags,
0064                     bool inplace)
0065 {
0066     int q_real = 256;
0067     char *in = kmalloc(q_real, GFP_KERNEL);
0068     char *out_test = kmalloc(q_real, GFP_KERNEL);
0069     char *out_real = kmalloc(q_real, GFP_KERNEL);
0070     int i, p = 0, q_test = 0;
0071 
0072     if (!in || !out_test || !out_real)
0073         goto out;
0074 
0075     for (i = 0; i < ARRAY_SIZE(strings); i++) {
0076         const char *s = strings[i].in;
0077         int len = strlen(strings[i].in);
0078 
0079         /* Copy string to in buffer */
0080         memcpy(&in[p], s, len);
0081         p += len;
0082 
0083         /* Copy expected result for given flags */
0084         if (flags & strings[i].flags) {
0085             s = strings[i].out;
0086             len = strlen(strings[i].out);
0087         }
0088         memcpy(&out_test[q_test], s, len);
0089         q_test += len;
0090     }
0091     in[p++] = '\0';
0092 
0093     /* Call string_unescape and compare result */
0094     if (inplace) {
0095         memcpy(out_real, in, p);
0096         if (flags == UNESCAPE_ANY)
0097             q_real = string_unescape_any_inplace(out_real);
0098         else
0099             q_real = string_unescape_inplace(out_real, flags);
0100     } else if (flags == UNESCAPE_ANY) {
0101         q_real = string_unescape_any(in, out_real, q_real);
0102     } else {
0103         q_real = string_unescape(in, out_real, q_real, flags);
0104     }
0105 
0106     test_string_check_buf(name, flags, in, p - 1, out_real, q_real,
0107                   out_test, q_test);
0108 out:
0109     kfree(out_real);
0110     kfree(out_test);
0111     kfree(in);
0112 }
0113 
0114 struct test_string_1 {
0115     const char *out;
0116     unsigned int flags;
0117 };
0118 
0119 #define TEST_STRING_2_MAX_S1        32
0120 struct test_string_2 {
0121     const char *in;
0122     struct test_string_1 s1[TEST_STRING_2_MAX_S1];
0123 };
0124 
0125 #define TEST_STRING_2_DICT_0        NULL
0126 static const struct test_string_2 escape0[] __initconst = {{
0127     .in = "\f\\ \n\r\t\v",
0128     .s1 = {{
0129         .out = "\\f\\ \\n\\r\\t\\v",
0130         .flags = ESCAPE_SPACE,
0131     },{
0132         .out = "\\f\\134\\040\\n\\r\\t\\v",
0133         .flags = ESCAPE_SPACE | ESCAPE_OCTAL,
0134     },{
0135         .out = "\\f\\x5c\\x20\\n\\r\\t\\v",
0136         .flags = ESCAPE_SPACE | ESCAPE_HEX,
0137     },{
0138         /* terminator */
0139     }},
0140 },{
0141     .in = "\\h\\\"\a\e\\",
0142     .s1 = {{
0143         .out = "\\\\h\\\\\"\\a\\e\\\\",
0144         .flags = ESCAPE_SPECIAL,
0145     },{
0146         .out = "\\\\\\150\\\\\\042\\a\\e\\\\",
0147         .flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
0148     },{
0149         .out = "\\\\\\x68\\\\\\x22\\a\\e\\\\",
0150         .flags = ESCAPE_SPECIAL | ESCAPE_HEX,
0151     },{
0152         /* terminator */
0153     }},
0154 },{
0155     .in = "\eb \\C\007\"\x90\r]",
0156     .s1 = {{
0157         .out = "\eb \\C\007\"\x90\\r]",
0158         .flags = ESCAPE_SPACE,
0159     },{
0160         .out = "\\eb \\\\C\\a\"\x90\r]",
0161         .flags = ESCAPE_SPECIAL,
0162     },{
0163         .out = "\\eb \\\\C\\a\"\x90\\r]",
0164         .flags = ESCAPE_SPACE | ESCAPE_SPECIAL,
0165     },{
0166         .out = "\\033\\142\\040\\134\\103\\007\\042\\220\\015\\135",
0167         .flags = ESCAPE_OCTAL,
0168     },{
0169         .out = "\\033\\142\\040\\134\\103\\007\\042\\220\\r\\135",
0170         .flags = ESCAPE_SPACE | ESCAPE_OCTAL,
0171     },{
0172         .out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\015\\135",
0173         .flags = ESCAPE_SPECIAL | ESCAPE_OCTAL,
0174     },{
0175         .out = "\\e\\142\\040\\\\\\103\\a\\042\\220\\r\\135",
0176         .flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_OCTAL,
0177     },{
0178         .out = "\eb \\C\007\"\x90\r]",
0179         .flags = ESCAPE_NP,
0180     },{
0181         .out = "\eb \\C\007\"\x90\\r]",
0182         .flags = ESCAPE_SPACE | ESCAPE_NP,
0183     },{
0184         .out = "\\eb \\C\\a\"\x90\r]",
0185         .flags = ESCAPE_SPECIAL | ESCAPE_NP,
0186     },{
0187         .out = "\\eb \\C\\a\"\x90\\r]",
0188         .flags = ESCAPE_SPACE | ESCAPE_SPECIAL | ESCAPE_NP,
0189     },{
0190         .out = "\\033b \\C\\007\"\\220\\015]",
0191         .flags = ESCAPE_OCTAL | ESCAPE_NP,
0192     },{
0193         .out = "\\033b \\C\\007\"\\220\\r]",
0194         .flags = ESCAPE_SPACE | ESCAPE_OCTAL | ESCAPE_NP,
0195     },{
0196         .out = "\\eb \\C\\a\"\\220\\r]",
0197         .flags = ESCAPE_SPECIAL | ESCAPE_SPACE | ESCAPE_OCTAL |
0198              ESCAPE_NP,
0199     },{
0200         .out = "\\x1bb \\C\\x07\"\\x90\\x0d]",
0201         .flags = ESCAPE_NP | ESCAPE_HEX,
0202     },{
0203         /* terminator */
0204     }},
0205 },{
0206     /* terminator */
0207 }};
0208 
0209 #define TEST_STRING_2_DICT_1        "b\\ \t\r"
0210 static const struct test_string_2 escape1[] __initconst = {{
0211     .in = "\f\\ \n\r\t\v",
0212     .s1 = {{
0213         .out = "\f\\134\\040\n\\015\\011\v",
0214         .flags = ESCAPE_OCTAL,
0215     },{
0216         .out = "\f\\x5c\\x20\n\\x0d\\x09\v",
0217         .flags = ESCAPE_HEX,
0218     },{
0219         /* terminator */
0220     }},
0221 },{
0222     .in = "\\h\\\"\a\e\\",
0223     .s1 = {{
0224         .out = "\\134h\\134\"\a\e\\134",
0225         .flags = ESCAPE_OCTAL,
0226     },{
0227         /* terminator */
0228     }},
0229 },{
0230     .in = "\eb \\C\007\"\x90\r]",
0231     .s1 = {{
0232         .out = "\e\\142\\040\\134C\007\"\x90\\015]",
0233         .flags = ESCAPE_OCTAL,
0234     },{
0235         /* terminator */
0236     }},
0237 },{
0238     /* terminator */
0239 }};
0240 
0241 static __init const char *test_string_find_match(const struct test_string_2 *s2,
0242                          unsigned int flags)
0243 {
0244     const struct test_string_1 *s1 = s2->s1;
0245     unsigned int i;
0246 
0247     if (!flags)
0248         return s2->in;
0249 
0250     /* Test cases are NULL-aware */
0251     flags &= ~ESCAPE_NULL;
0252 
0253     /* ESCAPE_OCTAL has a higher priority */
0254     if (flags & ESCAPE_OCTAL)
0255         flags &= ~ESCAPE_HEX;
0256 
0257     for (i = 0; i < TEST_STRING_2_MAX_S1 && s1->out; i++, s1++)
0258         if (s1->flags == flags)
0259             return s1->out;
0260     return NULL;
0261 }
0262 
0263 static __init void
0264 test_string_escape_overflow(const char *in, int p, unsigned int flags, const char *esc,
0265                 int q_test, const char *name)
0266 {
0267     int q_real;
0268 
0269     q_real = string_escape_mem(in, p, NULL, 0, flags, esc);
0270     if (q_real != q_test)
0271         pr_warn("Test '%s' failed: flags = %u, osz = 0, expected %d, got %d\n",
0272             name, flags, q_test, q_real);
0273 }
0274 
0275 static __init void test_string_escape(const char *name,
0276                       const struct test_string_2 *s2,
0277                       unsigned int flags, const char *esc)
0278 {
0279     size_t out_size = 512;
0280     char *out_test = kmalloc(out_size, GFP_KERNEL);
0281     char *out_real = kmalloc(out_size, GFP_KERNEL);
0282     char *in = kmalloc(256, GFP_KERNEL);
0283     int p = 0, q_test = 0;
0284     int q_real;
0285 
0286     if (!out_test || !out_real || !in)
0287         goto out;
0288 
0289     for (; s2->in; s2++) {
0290         const char *out;
0291         int len;
0292 
0293         /* NULL injection */
0294         if (flags & ESCAPE_NULL) {
0295             in[p++] = '\0';
0296             out_test[q_test++] = '\\';
0297             out_test[q_test++] = '0';
0298         }
0299 
0300         /* Don't try strings that have no output */
0301         out = test_string_find_match(s2, flags);
0302         if (!out)
0303             continue;
0304 
0305         /* Copy string to in buffer */
0306         len = strlen(s2->in);
0307         memcpy(&in[p], s2->in, len);
0308         p += len;
0309 
0310         /* Copy expected result for given flags */
0311         len = strlen(out);
0312         memcpy(&out_test[q_test], out, len);
0313         q_test += len;
0314     }
0315 
0316     q_real = string_escape_mem(in, p, out_real, out_size, flags, esc);
0317 
0318     test_string_check_buf(name, flags, in, p, out_real, q_real, out_test,
0319                   q_test);
0320 
0321     test_string_escape_overflow(in, p, flags, esc, q_test, name);
0322 
0323 out:
0324     kfree(in);
0325     kfree(out_real);
0326     kfree(out_test);
0327 }
0328 
0329 #define string_get_size_maxbuf 16
0330 #define test_string_get_size_one(size, blk_size, exp_result10, exp_result2)    \
0331     do {                                                                   \
0332         BUILD_BUG_ON(sizeof(exp_result10) >= string_get_size_maxbuf);  \
0333         BUILD_BUG_ON(sizeof(exp_result2) >= string_get_size_maxbuf);   \
0334         __test_string_get_size((size), (blk_size), (exp_result10),     \
0335                        (exp_result2));                         \
0336     } while (0)
0337 
0338 
0339 static __init void test_string_get_size_check(const char *units,
0340                           const char *exp,
0341                           char *res,
0342                           const u64 size,
0343                           const u64 blk_size)
0344 {
0345     if (!memcmp(res, exp, strlen(exp) + 1))
0346         return;
0347 
0348     res[string_get_size_maxbuf - 1] = '\0';
0349 
0350     pr_warn("Test 'test_string_get_size' failed!\n");
0351     pr_warn("string_get_size(size = %llu, blk_size = %llu, units = %s)\n",
0352         size, blk_size, units);
0353     pr_warn("expected: '%s', got '%s'\n", exp, res);
0354 }
0355 
0356 static __init void __test_string_get_size(const u64 size, const u64 blk_size,
0357                       const char *exp_result10,
0358                       const char *exp_result2)
0359 {
0360     char buf10[string_get_size_maxbuf];
0361     char buf2[string_get_size_maxbuf];
0362 
0363     string_get_size(size, blk_size, STRING_UNITS_10, buf10, sizeof(buf10));
0364     string_get_size(size, blk_size, STRING_UNITS_2, buf2, sizeof(buf2));
0365 
0366     test_string_get_size_check("STRING_UNITS_10", exp_result10, buf10,
0367                    size, blk_size);
0368 
0369     test_string_get_size_check("STRING_UNITS_2", exp_result2, buf2,
0370                    size, blk_size);
0371 }
0372 
0373 static __init void test_string_get_size(void)
0374 {
0375     /* small values */
0376     test_string_get_size_one(0, 512, "0 B", "0 B");
0377     test_string_get_size_one(1, 512, "512 B", "512 B");
0378     test_string_get_size_one(1100, 1, "1.10 kB", "1.07 KiB");
0379 
0380     /* normal values */
0381     test_string_get_size_one(16384, 512, "8.39 MB", "8.00 MiB");
0382     test_string_get_size_one(500118192, 512, "256 GB", "238 GiB");
0383     test_string_get_size_one(8192, 4096, "33.6 MB", "32.0 MiB");
0384 
0385     /* weird block sizes */
0386     test_string_get_size_one(3000, 1900, "5.70 MB", "5.44 MiB");
0387 
0388     /* huge values */
0389     test_string_get_size_one(U64_MAX, 4096, "75.6 ZB", "64.0 ZiB");
0390     test_string_get_size_one(4096, U64_MAX, "75.6 ZB", "64.0 ZiB");
0391 }
0392 
0393 static int __init test_string_helpers_init(void)
0394 {
0395     unsigned int i;
0396 
0397     pr_info("Running tests...\n");
0398     for (i = 0; i < UNESCAPE_ANY + 1; i++)
0399         test_string_unescape("unescape", i, false);
0400     test_string_unescape("unescape inplace",
0401                  get_random_int() % (UNESCAPE_ANY + 1), true);
0402 
0403     /* Without dictionary */
0404     for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
0405         test_string_escape("escape 0", escape0, i, TEST_STRING_2_DICT_0);
0406 
0407     /* With dictionary */
0408     for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
0409         test_string_escape("escape 1", escape1, i, TEST_STRING_2_DICT_1);
0410 
0411     /* Test string_get_size() */
0412     test_string_get_size();
0413 
0414     return -EINVAL;
0415 }
0416 module_init(test_string_helpers_init);
0417 MODULE_LICENSE("Dual BSD/GPL");