File: TestCMDLineAssembler.cc

package info (click to toggle)
j4-dmenu-desktop 3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,180 kB
  • sloc: cpp: 6,282; python: 473; sh: 81; makefile: 8
file content (211 lines) | stat: -rw-r--r-- 8,493 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
//
// This file is part of j4-dmenu-desktop.
//
// j4-dmenu-desktop is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// j4-dmenu-desktop is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with j4-dmenu-desktop.  If not, see <http://www.gnu.org/licenses/>.
//

#include <catch2/catch_message.hpp>
#include <catch2/catch_test_macros.hpp>
#include <catch2/generators/catch_generators.hpp>

#include <optional>
#include <string>
#include <string_view>
#include <vector>

#include "generated/tests_config.hh"

#include "Application.hh"
#include "CMDLineAssembler.hh"
#include "LineReader.hh"
#include "LocaleSuffixes.hh"
#include "ShellUnquote.hh"

bool test_quoting(std::string_view str) {
    auto quoted = CMDLineAssembly::sq_quote(str);
    auto unquoted = ShellUnquote::posix_shell_unqote(quoted);

    if (unquoted != str) {
        FAIL_CHECK("Quoted string doesn't produce the original string when "
                   "passed to the shell!\n"
                   "Original string:\n"
                   "================\n"
                   << str
                   << "\nQuoted string:\n"
                      "==============\n"
                   << quoted
                   << "\nResulting string:\n"
                      "=================\n"
                   << unquoted);
        return false;
    }
    return true;
}

TEST_CASE("Test quoting for POSIX shell", "[CMDLineAssembler]") {
    REQUIRE(test_quoting("simple"));
    REQUIRE(test_quoting("multiple words"));
    REQUIRE(test_quoting("@#$%^&*{}}{*&^%$%^&*&^%$#$%^&}"));
    REQUIRE(test_quoting("@#$%^&*{}}{*&^%$%^&*&^%$#$%^&}'"));
    REQUIRE(test_quoting("@#$%^&*{}}{*&^%$%^&*&^%$#$%^&}''"));
    REQUIRE(test_quoting("@#$%'^&*{}''}{*&^%$%^&*&^%''''''''$#$%^&}"));
    REQUIRE(test_quoting("@\t#$%'^&*{}'\n'}{*&^%$%^&*&^%'''\r'''\\n''$#$%^&}"));
    REQUIRE(test_quoting(""));
    REQUIRE(test_quoting("\\"));
    REQUIRE(test_quoting("'"));
    REQUIRE(test_quoting("testtesttest'"));
    REQUIRE(test_quoting("testte'sttest'"));
    REQUIRE(test_quoting("testte''sttest'"));
    REQUIRE(test_quoting("testte'''sttest'"));
}

TEST_CASE("Test converting Exec key to command array", "[CMDLineAssembler]") {
    using strvec = std::vector<std::string>;
    REQUIRE(CMDLineAssembly::convert_exec_to_command("command") ==
            strvec{"command"});
    REQUIRE(CMDLineAssembly::convert_exec_to_command(R"(command "a b c\"d")") ==
            strvec{"command", R"(a b c"d)"});
    REQUIRE(CMDLineAssembly::convert_exec_to_command(R"(command "a b c\"")") ==
            strvec{"command", R"(a b c")"});
    REQUIRE(CMDLineAssembly::convert_exec_to_command(R"("\`")") ==
            strvec{R"(`)"});
    REQUIRE(
        CMDLineAssembly::convert_exec_to_command(R"(command --arg "\$  \\")") ==
        strvec{"command", "--arg", R"($  \)"});
}

TEST_CASE("Test Exec key validation", "[CMDLineAssembler][!mayfail]") {
    REQUIRE(CMDLineAssembly::validate_exec_key(R"--(some string\)--"));
    REQUIRE(CMDLineAssembly::validate_exec_key(R"--("some string\)--"));
    REQUIRE(CMDLineAssembly::validate_exec_key(R"--(so\me string)--"));
    REQUIRE(CMDLineAssembly::validate_exec_key(R"--("so\me" string)--"));
    REQUIRE(CMDLineAssembly::validate_exec_key(
        R"--("string a""stringb" and "string c)--"));
    REQUIRE_FALSE(CMDLineAssembly::validate_exec_key(R"--(some string)--"));
    REQUIRE_FALSE(
        CMDLineAssembly::validate_exec_key(R"--("\"\`\$\\" valid escapes)--"));
}

// See #181
TEST_CASE("Test Wine generated desktop file failing",
          "[CMDLineAssembler][!mayfail]") {
    LocaleSuffixes ls("en_US");
    LineReader liner;

    std::optional<Application> app_optional;
    std::vector<std::string> arguments;
    // Omitting the `= false` here can lead to some compilers identifying this
    // variable as possibly being used without initialization, which produces
    // a compiler warning. This is not true, code below will always initialize
    // it, but it is also set here to silence the warnings.
    bool enable_compatibility_mode = false;

    // This test focuses on wine quirk. The multispace_unused variable makes
    // sure that the behavior of wine quirk has no correlation to the
    // multispace quirk (which is not relevant here).
    bool multispace_unused = GENERATE(true, false);

    SECTION("Wine generated desktop file from issue #174") {
        app_optional.emplace(TEST_FILES "applications/wine-#174.desktop", liner,
                             ls, std::vector<std::string>{});
        arguments = {
            "env", "WINEPREFIX=/home/baltazar/.wine", "wine",
            R"(C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Line 6\POD HD Pro X Edit\POD HD Pro X Edit.lnk)"};
        SECTION("Compatibility mode ON") {
            enable_compatibility_mode = true;
        }
        SECTION("Compatibility mode OFF") {
            enable_compatibility_mode = false;
        }
    }
    SECTION("Wine generated desktop file for PowerPoint viewer") {
        app_optional.emplace(TEST_FILES
                             "applications/wine-powerpoint-viewer.desktop",
                             liner, ls, std::vector<std::string>{});
        arguments = {
            "env", "WINEPREFIX=/home/meator/wine", "wine",
            R"(C:\users\meator\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Microsoft Office PowerPoint Viewer 2003.lnk)"};
        SECTION("Compatibility mode ON") {
            enable_compatibility_mode = true;
        }
        SECTION("Compatibility mode OFF") {
            enable_compatibility_mode = false;
        }
    }

    Application &app = *app_optional;

    if (enable_compatibility_mode) {
        auto commandline = CMDLineAssembly::convert_exec_to_command(
            app.exec, {true, multispace_unused});
        REQUIRE(commandline == arguments);
    } else
        REQUIRE_THROWS(CMDLineAssembly::convert_exec_to_command(
            app.exec, {enable_compatibility_mode, multispace_unused}));
}

// See #181
TEST_CASE("Test desktop files with superfluous whitespace in Exec",
          "[CMDLineAssembler]") {
    LocaleSuffixes ls("en_US");
    LineReader liner;

    Application app(TEST_FILES "applications/eagle-extra-spaces.desktop", liner,
                    ls, {});

    // This test focuses on multispace quirk. The wine_unused variable makes
    // sure that the behavior of multispace quirk has no correlation to the
    // wine quirk (which is not relevant here).
    bool wine_unused = GENERATE(true, false);

    auto collapse_extra_spaces =
        CMDLineAssembly::convert_exec_to_command(app.exec, {wine_unused, true});
    auto no_collapse_extra_spaces = CMDLineAssembly::convert_exec_to_command(
        app.exec, {wine_unused, false});

    REQUIRE(collapse_extra_spaces ==
            std::vector<std::string>{"eagle", "-style", "plastique"});
    REQUIRE(
        no_collapse_extra_spaces ==
        std::vector<std::string>{"eagle", "", "-style", "", "", "plastique"});
}

TEST_CASE("Test Distrobox compatibility", "[CMDLineAssembler]") {
    LocaleSuffixes ls("en_US");
    LineReader liner;

    Application app(TEST_FILES "applications/alpine-toolbox-latest-feh.desktop",
                    liner, ls, {});

    auto split_exec =
        CMDLineAssembly::convert_exec_to_command(app.exec, {false, true});
    REQUIRE(split_exec == std::vector<std::string>{
                              "/home/meator/distrobox-1.8.0/distrobox-enter",
                              "-n", "alpine-toolbox-latest", "--", "feh",
                              "--start-at", "%u"});
}

TEST_CASE("Test wine and multispace quirk", "[CMDLineAssembler]") {
    LocaleSuffixes ls("en_US");
    LineReader liner;

    Application app(TEST_FILES "applications/all-quirks.desktop", liner, ls,
                    {});

    auto result =
        CMDLineAssembly::convert_exec_to_command(app.exec, {true, true});

    REQUIRE(result == std::vector<std::string>{"eagle", "-style", "some flag",
                                               "plastique"});
}