source: calamares/trunk/fuentes/src/modules/shellprocess/Tests.cpp @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 13 months ago

sync with github

File size: 6.1 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2017, Adriaan de Groot <groot@kde.org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include "Tests.h"
20
21#include "GlobalStorage.h"
22#include "JobQueue.h"
23#include "Settings.h"
24
25#include "utils/CommandList.h"
26#include "utils/Logger.h"
27#include "utils/YamlUtils.h"
28
29#include <yaml-cpp/yaml.h>
30
31#include <QtTest/QtTest>
32
33#include <QFileInfo>
34#include <QStringList>
35
36QTEST_GUILESS_MAIN( ShellProcessTests )
37
38using CommandList = CalamaresUtils::CommandList;
39
40ShellProcessTests::ShellProcessTests()
41{
42}
43
44ShellProcessTests::~ShellProcessTests()
45{
46}
47
48void
49ShellProcessTests::initTestCase()
50{
51}
52
53void
54ShellProcessTests::testProcessListSampleConfig()
55{
56    YAML::Node doc;
57
58    QStringList dirs { "src/modules/shellprocess", "." };
59    for ( const auto& dir : dirs )
60    {
61        QString filename = dir + "/shellprocess.conf";
62        if ( QFileInfo::exists( filename ) )
63        {
64            doc = YAML::LoadFile( filename.toStdString() );
65            break;
66        }
67    }
68
69    CommandList cl(
70        CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
71    QVERIFY( !cl.isEmpty() );
72    QCOMPARE( cl.count(), 3 );
73    QCOMPARE( cl.at(0).timeout(), -1 );
74    QCOMPARE( cl.at(2).timeout(), 3600 );  // slowloris
75}
76
77void ShellProcessTests::testProcessListFromList()
78{
79    YAML::Node doc = YAML::Load( R"(---
80script:
81    - "ls /tmp"
82    - "ls /nonexistent"
83    - "/bin/false"
84)" );
85    CommandList cl(
86        CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
87    QVERIFY( !cl.isEmpty() );
88    QCOMPARE( cl.count(), 3 );
89
90    // Contains 1 bad element
91    doc = YAML::Load( R"(---
92script:
93    - "ls /tmp"
94    - false
95    - "ls /nonexistent"
96)" );
97    CommandList cl1(
98        CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
99    QVERIFY( !cl1.isEmpty() );
100    QCOMPARE( cl1.count(), 2 );  // One element ignored
101}
102
103void ShellProcessTests::testProcessListFromString()
104{
105    YAML::Node doc = YAML::Load( R"(---
106script: "ls /tmp"
107)" );
108    CommandList cl(
109        CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
110    QVERIFY( !cl.isEmpty() );
111    QCOMPARE( cl.count(), 1 );
112    QCOMPARE( cl.at(0).timeout(), 10 );
113    QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) );
114
115    // Not a string
116    doc = YAML::Load( R"(---
117script: false
118)" );
119    CommandList cl1(
120        CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
121    QVERIFY( cl1.isEmpty() );
122    QCOMPARE( cl1.count(), 0 );
123
124}
125
126void ShellProcessTests::testProcessFromObject()
127{
128    YAML::Node doc = YAML::Load( R"(---
129script:
130    command: "ls /tmp"
131    timeout: 20
132)" );
133    CommandList cl(
134        CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
135    QVERIFY( !cl.isEmpty() );
136    QCOMPARE( cl.count(), 1 );
137    QCOMPARE( cl.at(0).timeout(), 20 );
138    QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) );
139}
140
141void ShellProcessTests::testProcessListFromObject()
142{
143    YAML::Node doc = YAML::Load( R"(---
144script:
145    - command: "ls /tmp"
146      timeout: 12
147    - "-/bin/false"
148)" );
149    CommandList cl(
150        CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" ) );
151    QVERIFY( !cl.isEmpty() );
152    QCOMPARE( cl.count(), 2 );
153    QCOMPARE( cl.at(0).timeout(), 12 );
154    QCOMPARE( cl.at(0).command(), QStringLiteral( "ls /tmp" ) );
155    QCOMPARE( cl.at(1).timeout(), -1 );  // not set
156}
157
158void ShellProcessTests::testRootSubstitution()
159{
160    YAML::Node doc = YAML::Load( R"(---
161script:
162    - "ls /tmp"
163)" );
164    QVariant plainScript = CalamaresUtils::yamlMapToVariant( doc ).toMap().value( "script" );
165    QVariant rootScript = CalamaresUtils::yamlMapToVariant(
166        YAML::Load( R"(---
167script:
168    - "ls @@ROOT@@"
169)" ) ).toMap().value( "script" );
170    QVariant userScript = CalamaresUtils::yamlMapToVariant(
171        YAML::Load( R"(---
172script:
173    - mktemp -d @@ROOT@@/calatestXXXXXXXX
174    - "chown @@USER@@ @@ROOT@@/calatest*"
175    - rm -rf @@ROOT@@/calatest*
176)" ) ).toMap().value( "script" );
177
178    if ( !Calamares::JobQueue::instance() )
179        (void *)new Calamares::JobQueue( nullptr );
180    if ( !Calamares::Settings::instance() )
181        (void *)new Calamares::Settings( QString(), true );
182
183    Calamares::GlobalStorage* gs = Calamares::JobQueue::instance()->globalStorage();
184    QVERIFY( gs != nullptr );
185
186    qDebug() << "Expect WARNING, ERROR, WARNING";
187    // Doesn't use @@ROOT@@, so no failures
188    QVERIFY( bool(CommandList(plainScript, false, 10 ).run()) );
189
190    // Doesn't use @@ROOT@@, but does chroot, so fails
191    QVERIFY( !bool(CommandList(plainScript, true, 10 ).run()) );
192
193    // Does use @@ROOT@@, which is not set, so fails
194    QVERIFY( !bool(CommandList(rootScript, false, 10 ).run()) );
195    // .. fails for two reasons
196    QVERIFY( !bool(CommandList(rootScript, true, 10 ).run()) );
197
198    gs->insert( "rootMountPoint", "/tmp" );
199    // Now that the root is set, two variants work .. still can't
200    // chroot, unless the rootMountPoint contains a full system,
201    // *and* we're allowed to chroot (ie. running tests as root).
202    qDebug() << "Expect no output.";
203    QVERIFY( bool(CommandList(plainScript, false, 10 ).run()) );
204    QVERIFY( bool(CommandList(rootScript, false, 10 ).run()) );
205
206    qDebug() << "Expect ERROR";
207    // But no user set yet
208    QVERIFY( !bool(CommandList(userScript, false, 10 ).run()) );
209
210    // Now play dangerous games with shell expansion
211    gs->insert( "username", "`id -u`" );
212    QVERIFY( bool(CommandList(userScript, false, 10 ).run()) );
213}
Note: See TracBrowser for help on using the repository browser.