1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "test_engine_impl.h"
4
5#include <mos/lib/cmdline.h>
6
7MOS_TEST_CASE(simple_cmdline)
8{
9 char cmdline[256] = "one two three four five six seven eight nine ten";
10 const char *cmdlines[11] = { 0 };
11 size_t cmdlines_count = 0;
12 cmdline_parse_inplace(inbuf: cmdline, length: strlen(str: cmdline), cmdline_max: 10, cmdlines_count: &cmdlines_count, cmdlines);
13 MOS_TEST_CHECK(cmdlines_count, 10);
14 MOS_TEST_CHECK_STRING(cmdlines[0], "one");
15 MOS_TEST_CHECK_STRING(cmdlines[1], "two");
16 MOS_TEST_CHECK_STRING(cmdlines[2], "three");
17 MOS_TEST_CHECK_STRING(cmdlines[3], "four");
18 MOS_TEST_CHECK_STRING(cmdlines[4], "five");
19 MOS_TEST_CHECK_STRING(cmdlines[5], "six");
20 MOS_TEST_CHECK_STRING(cmdlines[6], "seven");
21 MOS_TEST_CHECK_STRING(cmdlines[7], "eight");
22 MOS_TEST_CHECK_STRING(cmdlines[8], "nine");
23 MOS_TEST_CHECK_STRING(cmdlines[9], "ten");
24 MOS_TEST_CHECK(cmdlines[10], NULL);
25}
26
27MOS_TEST_CASE(one_arg_with_an_option)
28{
29 char cmdline[256] = "one=nana";
30 const char *cmdlines[2] = { 0 };
31 size_t cmdlines_count = 0;
32 cmdline_parse_inplace(inbuf: cmdline, length: strlen(str: cmdline), cmdline_max: 1, cmdlines_count: &cmdlines_count, cmdlines);
33 MOS_TEST_CHECK(cmdlines_count, 1);
34 MOS_TEST_CHECK_STRING(cmdlines[0], "one=nana");
35 MOS_TEST_CHECK(cmdlines[1], NULL);
36}
37
38MOS_TEST_CASE(one_arg_with_multiple_options)
39{
40 char cmdline[256] = "one=nana1,nana2,nana3,nana4,false";
41 const char *cmdlines[2] = { 0 };
42 size_t cmdlines_count = 0;
43 cmdline_parse_inplace(inbuf: cmdline, length: strlen(str: cmdline), cmdline_max: 1, cmdlines_count: &cmdlines_count, cmdlines);
44 MOS_TEST_CHECK(cmdlines_count, 1);
45 MOS_TEST_CHECK_STRING(cmdlines[0], "one=nana1,nana2,nana3,nana4,false");
46 MOS_TEST_CHECK(cmdlines[1], NULL);
47}
48
49MOS_TEST_CASE(multi_args_with_multiple_options)
50{
51 char cmdline[256] = "one=nana1,nana2,nana3,nana4,false two=nana1,nana2,nana3,nana4,false three=nana1,nana2,nana3,nana4,true";
52 const char *cmdlines[4] = { 0 };
53 size_t cmdlines_count = 0;
54 cmdline_parse_inplace(inbuf: cmdline, length: strlen(str: cmdline), cmdline_max: 3, cmdlines_count: &cmdlines_count, cmdlines);
55 MOS_TEST_CHECK(cmdlines_count, 3);
56 MOS_TEST_CHECK_STRING(cmdlines[0], "one=nana1,nana2,nana3,nana4,false");
57 MOS_TEST_CHECK_STRING(cmdlines[1], "two=nana1,nana2,nana3,nana4,false");
58 MOS_TEST_CHECK_STRING(cmdlines[2], "three=nana1,nana2,nana3,nana4,true");
59 MOS_TEST_CHECK(cmdlines[3], NULL);
60}
61
62MOS_TEST_CASE(quoted_args)
63{
64 char cmdline[256] = "one=\"nana1,nana2,nana3,nana4,false\" two=\"nana1,nana2,nana3,nana4,false\" three=\"nana1,nana2,nana3,nana4,true\"";
65 const char *cmdlines[4] = { 0 };
66 size_t cmdlines_count = 0;
67 cmdline_parse_inplace(inbuf: cmdline, length: strlen(str: cmdline), cmdline_max: 3, cmdlines_count: &cmdlines_count, cmdlines);
68 MOS_TEST_CHECK(cmdlines_count, 3);
69 MOS_TEST_CHECK_STRING(cmdlines[0], "one=\"nana1,nana2,nana3,nana4,false\"");
70 MOS_TEST_CHECK_STRING(cmdlines[1], "two=\"nana1,nana2,nana3,nana4,false\"");
71 MOS_TEST_CHECK_STRING(cmdlines[2], "three=\"nana1,nana2,nana3,nana4,true\"");
72 MOS_TEST_CHECK(cmdlines[3], NULL);
73}
74
75MOS_TEST_CASE(quoted_args_with_spaces)
76{
77 char cmdline[256] = "one=\"nana1 nana2 nana3 nana4 false\" two=\"nana1 nana2 nana3 nana4 false\" three=\"nana1 nana2 nana3 nana4 true\"";
78 const char *cmdlines[4] = { 0 };
79 size_t cmdlines_count = 0;
80 cmdline_parse_inplace(inbuf: cmdline, length: strlen(str: cmdline), cmdline_max: 3, cmdlines_count: &cmdlines_count, cmdlines);
81 MOS_TEST_CHECK(cmdlines_count, 3);
82 MOS_TEST_CHECK_STRING(cmdlines[0], "one=\"nana1 nana2 nana3 nana4 false\"");
83 MOS_TEST_CHECK_STRING(cmdlines[1], "two=\"nana1 nana2 nana3 nana4 false\"");
84 MOS_TEST_CHECK_STRING(cmdlines[2], "three=\"nana1 nana2 nana3 nana4 true\"");
85 MOS_TEST_CHECK(cmdlines[3], NULL);
86}
87
88MOS_TEST_CASE(quoted_args_with_spaces_and_commas)
89{
90 char cmdline[256] = "one=\"nana1 nana2,nana3 nana4 false\" two=\"nana1 nana2,nana3 nana4 false\" three=\"nana1 nana2,nana3 nana4 true\"";
91 const char *cmdlines[4] = { 0 };
92 size_t cmdlines_count = 0;
93 cmdline_parse_inplace(inbuf: cmdline, length: strlen(str: cmdline), cmdline_max: 3, cmdlines_count: &cmdlines_count, cmdlines);
94 MOS_TEST_CHECK(cmdlines_count, 3);
95 MOS_TEST_CHECK_STRING(cmdlines[0], "one=\"nana1 nana2,nana3 nana4 false\"");
96 MOS_TEST_CHECK_STRING(cmdlines[1], "two=\"nana1 nana2,nana3 nana4 false\"");
97 MOS_TEST_CHECK_STRING(cmdlines[2], "three=\"nana1 nana2,nana3 nana4 true\"");
98 MOS_TEST_CHECK(cmdlines[3], NULL);
99}
100
101MOS_TEST_CASE(quoted_args_with_spaces_and_commas_and_equals)
102{
103 char cmdline[256] = "one=\"nana1=nana2,nana3=nana4 false\" two=\"nana1=nana2,nana3=nana4 false\" three=\"nana1=nana2,nana3=nana4 true\"";
104 const char *cmdlines[4] = { 0 };
105 size_t cmdlines_count = 0;
106 cmdline_parse_inplace(inbuf: cmdline, length: strlen(str: cmdline), cmdline_max: 3, cmdlines_count: &cmdlines_count, cmdlines);
107 MOS_TEST_CHECK(cmdlines_count, 3);
108 MOS_TEST_CHECK_STRING(cmdlines[0], "one=\"nana1=nana2,nana3=nana4 false\"");
109 MOS_TEST_CHECK_STRING(cmdlines[1], "two=\"nana1=nana2,nana3=nana4 false\"");
110 MOS_TEST_CHECK_STRING(cmdlines[2], "three=\"nana1=nana2,nana3=nana4 true\"");
111 MOS_TEST_CHECK(cmdlines[3], NULL);
112}
113
114MOS_TEST_CASE(quotation_with_escaped_quotation_marks)
115{
116 char cmdline[256] =
117 "one=\"nana1=\\\"nana2\\\",nana3=\\\"nana4\\\" false\" two=\"nana1=\\\"nana2\\\",nana3=\\\"nana4\\\" false\" three=\"nana1=\\\"nana2\\\",nana3=\\\"nana4\\\" true\"";
118 const char *cmdlines[4] = { 0 };
119 size_t cmdlines_count = 0;
120 cmdline_parse_inplace(inbuf: cmdline, length: strlen(str: cmdline), cmdline_max: 3, cmdlines_count: &cmdlines_count, cmdlines);
121 MOS_TEST_CHECK(cmdlines_count, 3);
122 MOS_TEST_CHECK_STRING(cmdlines[0], "one=\"nana1=\\\"nana2\\\",nana3=\\\"nana4\\\" false\"");
123 MOS_TEST_CHECK_STRING(cmdlines[1], "two=\"nana1=\\\"nana2\\\",nana3=\\\"nana4\\\" false\"");
124 MOS_TEST_CHECK_STRING(cmdlines[2], "three=\"nana1=\\\"nana2\\\",nana3=\\\"nana4\\\" true\"");
125 MOS_TEST_CHECK(cmdlines[3], NULL);
126}
127