summaryrefslogtreecommitdiffstats
path: root/tests/applicationinstaller/tst_applicationinstaller.cpp
blob: 62fefa8beee3ed1d1c722a57fc99dc113e7f0d6c (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
/****************************************************************************
**
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Pelagicore Application Manager.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore>
#include <QtTest>

#include <functional>

#include "applicationinstaller.h"
#include "applicationmanager.h"
#include "application.h"
#include "sudo.h"
#include "utilities.h"
#include "error.h"
#include "private/package_p.h"
#include "runtimefactory.h"
#include "qmlinprocessruntime.h"
#include "package.h"

#ifdef Q_OS_LINUX
#  include <signal.h>
#  include <linux/loop.h>
#  include <sys/ioctl.h>
#endif

#include "../sudo-cleanup.h"
#include "../error-checking.h"

QT_USE_NAMESPACE_AM

static bool startedSudoServer = false;
static QString sudoServerError;

static int spyTimeout = 5000; // shorthand for specifying QSignalSpy timeouts

// RAII to reset the global attribute
class AllowInstallations
{
public:
    enum Type {
        AllowUnsinged,
        RequireDevSigned,
        RequireStoreSigned
    };

    AllowInstallations(Type t)
        : m_oldUnsigned(ApplicationInstaller::instance()->allowInstallationOfUnsignedPackages())
        , m_oldDevMode(ApplicationInstaller::instance()->developmentMode())
    {
        switch (t) {
        case AllowUnsinged:
            ApplicationInstaller::instance()->setAllowInstallationOfUnsignedPackages(true);
            ApplicationInstaller::instance()->setDevelopmentMode(false);
            break;
        case RequireDevSigned:
            ApplicationInstaller::instance()->setAllowInstallationOfUnsignedPackages(false);
            ApplicationInstaller::instance()->setDevelopmentMode(true);
            break;
        case RequireStoreSigned:
            ApplicationInstaller::instance()->setAllowInstallationOfUnsignedPackages(false);
            ApplicationInstaller::instance()->setDevelopmentMode(false);
            break;
        }
    }
    ~AllowInstallations()
    {
        ApplicationInstaller::instance()->setAllowInstallationOfUnsignedPackages(m_oldUnsigned);
        ApplicationInstaller::instance()->setDevelopmentMode(m_oldDevMode);
    }
private:
    bool m_oldUnsigned;
    bool m_oldDevMode;
};

class tst_ApplicationInstaller : public QObject
{
    Q_OBJECT

public:
    tst_ApplicationInstaller(QObject *parent = nullptr);
    ~tst_ApplicationInstaller();

private slots:
    void initTestCase();
    void cleanupTestCase();

    void init();
    void cleanup();

    //TODO: test AI::cleanupBrokenInstallations() before calling cleanup() the first time!

    void installationLocations();

    void packageActivation();

    void packageInstallation_data();
    void packageInstallation();

    void removeAppOnMissingSDCard();

    void simulateErrorConditions_data();
    void simulateErrorConditions();

    void cancelPackageInstallation_data();
    void cancelPackageInstallation();

    void parallelPackageInstallation();

    void validateDnsName_data();
    void validateDnsName();

    void compareVersions_data();
    void compareVersions();

public:
    enum PathLocation {
        TemporaryMount = 0,
        Manifests,
        ImageMounts,
        Internal0,
        Documents0,
        Internal1,
        Documents1,
        SDCard0,
        SDCard0Images,
        SDCard1,
        SDCard1Images,

        PathLocationCount
    };

private:
    QString pathTo(const QString &sub = QString())
    {
        return pathTo(PathLocationCount, sub);
    }

    QString pathTo(PathLocation pathLocation, const QString &sub = QString())
    {
        QString base;
        switch (pathLocation) {
        case Manifests:      base = "manifests"; break;
        case TemporaryMount: base = "mnt"; break;
        case ImageMounts:    base = "image-mounts"; break;
        case Internal0:      base = "internal-0"; break;
        case Documents0:     base = "documents-0"; break;
        case Internal1:      base = "internal-1"; break;
        case Documents1:     base = "documents-1"; break;
        case SDCard0:        base = "sdcard0"; break;
        case SDCard0Images:  base = "sdcard0/" + m_hardwareId; break;
        case SDCard1:        base = "sdcard1"; break;
        case SDCard1Images:  base = "sdcard1/" + m_hardwareId; break;
        default: break;
        }

        QDir workDir(m_workDir.path());

        if (base.isEmpty() && sub.isEmpty())
            base = workDir.absolutePath();
        else if (sub.isEmpty())
            base = workDir.absoluteFilePath(base);
        else if (base.isEmpty())
            base = workDir.absoluteFilePath(sub);
        else
            base = workDir.absoluteFilePath(base + '/' + sub);

        if (QDir(base).exists())
            return base + '/';
        else
            return base;
    }

    void clearSignalSpies()
    {
        m_startedSpy->clear();
        m_requestingInstallationAcknowledgeSpy->clear();
        m_blockingUntilInstallationAcknowledgeSpy->clear();
        m_progressSpy->clear();
        m_finishedSpy->clear();
        m_failedSpy->clear();
        m_packageActivatedSpy->clear();
        m_packageDeactivatedSpy->clear();
    }

    bool checkPackageActivation(const QString &appId, int waitmsec = 0)
    {
        return internalCheckActivation(m_packageActivatedSpy, appId, waitmsec);
    }

    bool checkPackageDeactivation(const QString &appId, int waitmsec = 0)
    {
        return internalCheckActivation(m_packageDeactivatedSpy, appId, waitmsec);
    }

    bool internalCheckActivation(QSignalSpy *spy, const QString &appId, int waitmsec)
    {
        auto check = [spy, appId]() -> bool
        {
            for (int i = 0; i < spy->count(); ++i) {
                const QVariantList &paramList = spy->at(i);
                if ((paramList.size() == 2) && (paramList.at(0).toString() == appId))
                    return paramList.at(1).toBool();
            }
            return false;
        };

        forever {
            bool result = check();
            if (!result && waitmsec) {
                QTest::qWait(100);
                waitmsec -= qMin(100, waitmsec);
            } else {
                return result;
            }
        }
    }

    static bool isDataTag(const char *tag)
    {
        return !qstrcmp(tag, QTest::currentDataTag());
    }

private:
    SudoClient *m_sudo = nullptr;
    bool m_fakeSudo = false;

    QTemporaryDir m_workDir;
    QString m_hardwareId;
    QString m_loopbackForSDCard[2];
    QVector<InstallationLocation> m_installationLocations;
    ApplicationInstaller *m_ai = nullptr;
    QSignalSpy *m_startedSpy = nullptr;
    QSignalSpy *m_requestingInstallationAcknowledgeSpy = nullptr;
    QSignalSpy *m_blockingUntilInstallationAcknowledgeSpy = nullptr;
    QSignalSpy *m_progressSpy = nullptr;
    QSignalSpy *m_finishedSpy = nullptr;
    QSignalSpy *m_failedSpy = nullptr;
    QSignalSpy *m_packageActivatedSpy = nullptr;
    QSignalSpy *m_packageDeactivatedSpy = nullptr;
};


tst_ApplicationInstaller::tst_ApplicationInstaller(QObject *parent)
    : QObject(parent)
{ }

tst_ApplicationInstaller::~tst_ApplicationInstaller()
{
    if (m_workDir.isValid()) {
        detachLoopbacksAndUnmount(m_sudo, m_workDir.path());
        if (m_sudo)
            m_sudo->removeRecursive(m_workDir.path());
        else
            recursiveOperation(m_workDir.path(), safeRemove);
    }

    delete m_packageDeactivatedSpy;
    delete m_packageActivatedSpy;
    delete m_failedSpy;
    delete m_finishedSpy;
    delete m_progressSpy;
    delete m_blockingUntilInstallationAcknowledgeSpy;
    delete m_requestingInstallationAcknowledgeSpy;
    delete m_startedSpy;

    delete m_ai;
}

void tst_ApplicationInstaller::initTestCase()
{
    if (!QDir(qL1S(AM_TESTDATA_DIR "/packages")).exists())
        QSKIP("No test packages available in the data/ directory");

    bool verbose = qEnvironmentVariableIsSet("VERBOSE_TEST");
    if (!verbose)
        QLoggingCategory::setFilterRules("am.installer.debug=false");
    qInfo() << "Verbose mode is" << (verbose ? "on" : "off") << "(changed by (un)setting $VERBOSE_TEST)";

    spyTimeout *= timeoutFactor();

    QVERIFY(Package::checkCorrectLocale());
    QVERIFY2(startedSudoServer, qPrintable(sudoServerError));
    m_sudo = SudoClient::instance();
    QVERIFY(m_sudo);
    m_fakeSudo = m_sudo->isFallbackImplementation();

    // we need a (dummy) ApplicationManager for the installer, since the installer will
    // notify the manager during installations
    QVERIFY(ApplicationManager::createInstance(true));

    // create a temporary dir (plus sub-dirs) for everything created by this test run

    QVERIFY(m_workDir.isValid());

    // make sure we have a valid hardware-id
    m_hardwareId = "foobar";

    for (int i = 0; i < PathLocationCount; ++i)
        QVERIFY(QDir().mkdir(pathTo(PathLocation(i))));

    if (!m_fakeSudo) {
        // we have support for loopback devices, so we create two FAT images that simulate SDCards
        // (a lambda would have been perfect here, but QVERIFY/QCOMPARE do not work inside lambdas).

        for (int i = 0; i < 2; ++i) {
            QFile f(pathTo(QString::fromLatin1("sdcard-image-%1").arg(i)));
            QVERIFY2(f.open(QFile::WriteOnly | QFile::Truncate), qPrintable(f.errorString()));
            QVERIFY2(f.resize(3*1024*1024), qPrintable(f.errorString()));
            f.close();
            QCOMPARE(f.size(), 3*1024*1024);

            m_loopbackForSDCard[i] = m_sudo->attachLoopback(f.fileName());
            QVERIFY2(!m_loopbackForSDCard[i].isEmpty(), qPrintable(m_sudo->lastError()));
            QVERIFY2(m_sudo->mkfs(m_loopbackForSDCard[i], "vfat"), qPrintable(m_sudo->lastError()));
            QVERIFY2(m_sudo->mount(m_loopbackForSDCard[i], pathTo(i == 0 ? SDCard0 : SDCard1), false, "vfat"), qPrintable(m_sudo->lastError()));

            // those paths have been hidden due to the mount, so recreate them
            QVERIFY(QDir().mkdir(pathTo(i == 0 ? SDCard0Images : SDCard1Images)));
        }
    }

    // define some installation locations for testing

    QVariantList iloc = QVariantList {
            QVariantMap {
                { "isDefault", true },
                { "id", "internal-0" },
                { "installationPath", pathTo(Internal0) },
                { "documentPath", pathTo(Documents0) }
            },
            QVariantMap {
                { "id", "internal-1" },
                { "installationPath", pathTo(Internal1) },
                { "documentPath", pathTo(Documents1) },
            }
    };
    if (!m_fakeSudo) {
        iloc += QVariantList {
                QVariantMap {
                    { "id", "removable-0" },
                    { "mountPoint", pathTo(SDCard0) },
                    { "installationPath", pathTo(SDCard0, "@HARDWARE-ID@") },
                    { "documentPath", pathTo(Documents0) },
                },
                QVariantMap {
                    { "id", "removable-1" },
                    { "mountPoint", pathTo(SDCard1) },
                    { "installationPath", pathTo(SDCard1, "@HARDWARE-ID@") },
                    { "documentPath", pathTo(Documents1) },
                }
        };
    }

    m_installationLocations = InstallationLocation::parseInstallationLocations(iloc, m_hardwareId);

    QCOMPARE(m_installationLocations.size(), m_fakeSudo ? 2 : 4);
    if (!m_fakeSudo)
        QCOMPARE(m_installationLocations.at(2).installationPath(), pathTo(SDCard0, m_hardwareId));

    // finally, instantiate the ApplicationInstaller and a bunch of signal-spies for its signals

    QString installerError;
    m_ai = ApplicationInstaller::createInstance(m_installationLocations, pathTo(Manifests), pathTo(ImageMounts), m_hardwareId, &installerError);
    QVERIFY2(m_ai, qPrintable(installerError));

    m_startedSpy = new QSignalSpy(m_ai, &ApplicationInstaller::taskStarted);
    m_requestingInstallationAcknowledgeSpy = new QSignalSpy(m_ai, &ApplicationInstaller::taskRequestingInstallationAcknowledge);
    m_blockingUntilInstallationAcknowledgeSpy = new QSignalSpy(m_ai, &ApplicationInstaller::taskBlockingUntilInstallationAcknowledge);
    m_progressSpy = new QSignalSpy(m_ai, &ApplicationInstaller::taskProgressChanged);
    m_finishedSpy = new QSignalSpy(m_ai, &ApplicationInstaller::taskFinished);
    m_failedSpy = new QSignalSpy(m_ai, &ApplicationInstaller::taskFailed);
    m_packageActivatedSpy = new QSignalSpy(m_ai, &ApplicationInstaller::packageActivated);
    m_packageDeactivatedSpy = new QSignalSpy(m_ai, &ApplicationInstaller::packageDeactivated);

    // crypto stuff - we need to load the root CA and developer CA certificates

    QFile devcaFile(AM_TESTDATA_DIR "certificates/devca.crt");
    QFile storecaFile(AM_TESTDATA_DIR "certificates/store.crt");
    QFile caFile(AM_TESTDATA_DIR "certificates/ca.crt");
    QVERIFY2(devcaFile.open(QIODevice::ReadOnly), qPrintable(devcaFile.errorString()));
    QVERIFY2(storecaFile.open(QIODevice::ReadOnly), qPrintable(storecaFile.errorString()));
    QVERIFY2(caFile.open(QIODevice::ReadOnly), qPrintable(caFile.errorString()));

    QList<QByteArray> chainOfTrust;
    chainOfTrust << devcaFile.readAll() << caFile.readAll();
    QVERIFY(!chainOfTrust.at(0).isEmpty());
    QVERIFY(!chainOfTrust.at(1).isEmpty());
    m_ai->setCACertificates(chainOfTrust);

    // we do not require valid store signatures for this test run

    m_ai->setDevelopmentMode(true);

    // make sure we have a valid runtime available. The important part is
    // that we have a runtime called "native" - the functionality does not matter.
    RuntimeFactory::instance()->registerRuntime(new QmlInProcessRuntimeManager(qSL("native")));
}

void tst_ApplicationInstaller::cleanupTestCase()
{
    if (m_ai && m_ai->isPackageActivated("com.pelagicore.test")) {
        QVERIFY(m_ai->deactivatePackage("com.pelagicore.test"));
        QTRY_VERIFY(checkPackageDeactivation("com.pelagicore.test"));
    }
    // the real cleanup happens in ~tst_ApplicationInstaller, since we also need
    // to call this cleanup from the crash handler
}

void tst_ApplicationInstaller::init()
{
    // start with a fresh App1 dir on each test run

    if (!QDir(pathTo(Internal0)).exists())
        QVERIFY(QDir().mkdir(pathTo(Internal0)));
}

void tst_ApplicationInstaller::cleanup()
{
    // this helps with reducing the amount of cleanup work required
    // at the end of each test

    try {
        m_ai->cleanupBrokenInstallations();
    } catch (const Exception &e) {
        QFAIL(e.what());
    }

    clearSignalSpies();
    recursiveOperation(pathTo(Internal0), safeRemove);
}

void tst_ApplicationInstaller::installationLocations()
{
    QCOMPARE(InstallationLocation().type(), InstallationLocation::Invalid);

    for (int i = InstallationLocation::Removable; i >= InstallationLocation::Invalid; --i) {
        QString s = InstallationLocation::typeToString((InstallationLocation::Type) i);
        QVERIFY(!s.isEmpty());
        QVERIFY(InstallationLocation::typeFromString(s) == i);
    }

    const QVector<InstallationLocation> loclist = m_ai->installationLocations();

    QCOMPARE(loclist.size(), m_installationLocations.size());
    for (const InstallationLocation &loc : loclist) {
        QVERIFY(m_installationLocations.contains(loc));

        QCOMPARE(loc.id(), InstallationLocation::typeToString(loc.type()) + "-" + QString::number(loc.index()));
    }

    QVector<InstallationLocation> locationList = InstallationLocation::parseInstallationLocations(QVariantList {
        QVariantMap {
            { "id", "internal-0" },
            { "installationPath", QDir::tempPath() },
            { "documentPath", QDir::tempPath() },
            { "isDefault", true }
        },
    }, m_hardwareId);
    QCOMPARE(locationList.size(), 1);
    InstallationLocation &tmp = locationList.first();
    QVariantMap map = tmp.toVariantMap();
    QVERIFY(!map.isEmpty());

    QCOMPARE(map.value("id").toString(), tmp.id());
    QCOMPARE(map.value("type").toString(), InstallationLocation::typeToString(tmp.type()));
    QCOMPARE(map.value("index").toInt(), tmp.index());
    QCOMPARE(map.value("installationPath").toString(), tmp.installationPath());
    QCOMPARE(map.value("documentPath").toString(), tmp.documentPath());
    QCOMPARE(map.value("isDefault").toBool(), tmp.isDefault());
    QCOMPARE(map.value("isRemovable").toBool(), tmp.isRemovable());
    QCOMPARE(map.value("isMounted").toBool(), tmp.isMounted());
    QVERIFY(map.value("installationDeviceSize").toLongLong() > 0);
    QVERIFY(map.value("installationDeviceFree").toLongLong() > 0);
    QVERIFY(map.value("documentDeviceSize").toLongLong() > 0);
    QVERIFY(map.value("documentDeviceFree").toLongLong() > 0);
}


void tst_ApplicationInstaller::packageActivation()
{
#ifndef Q_OS_LINUX
    QSKIP("Cannot run SD-Card tests on non-Linux systems");
#else
    if (m_fakeSudo)
        QSKIP("Cannot run SD-Card tests without root privileges");
#endif


    QVERIFY(!m_ai->activatePackage(QString()));
    QVERIFY(!m_ai->activatePackage("does.not.exist"));
    QVERIFY(!m_ai->deactivatePackage(QString()));
    QVERIFY(!m_ai->deactivatePackage("does.not.exist"));

    // install package to sdcard

    QString taskId = m_ai->startPackageInstallation("removable-0", QUrl::fromLocalFile(AM_TESTDATA_DIR "packages/test-dev-signed.appkg"));
    QVERIFY(!taskId.isEmpty());
    m_ai->acknowledgePackageInstallation(taskId);

    // check received signals

    QVERIFY(m_finishedSpy->wait(spyTimeout));
    QCOMPARE(m_finishedSpy->first()[0].toString(), taskId);
    clearSignalSpies();

    // activate the package

    QString name = "com.pelagicore.test";

    QVERIFY(m_ai->doesPackageNeedActivation(name));
    QVERIFY(!m_ai->isPackageActivated(name));
    QVERIFY(m_ai->activatePackage(name));

    QVERIFY(checkPackageActivation(name, 3000));
    QVERIFY(m_ai->isPackageActivated(name));
    QVERIFY(!m_ai->activatePackage(name));
    clearSignalSpies();

    // check actual file-system mount

    QString mountPoint = QDir(pathTo(ImageMounts, name)).canonicalPath();
    QVERIFY(mountedDirectories().value(mountPoint).startsWith("/dev/loop"));
    QFile testFile(mountPoint + "/test");
    QVERIFY(testFile.exists());
    QVERIFY(!testFile.open(QFile::ReadWrite));

    // deactivate the package again

    QVERIFY(m_ai->deactivatePackage(name));
    QVERIFY(checkPackageDeactivation(name, 3000));
    QVERIFY(!m_ai->isPackageActivated(name));
    QVERIFY(!m_ai->deactivatePackage(name));
    clearSignalSpies();

    // check that the actual file-system mount is gone

    QVERIFY2(!mountedDirectories().contains(mountPoint),
             qPrintable(mountedDirectories().value(mountPoint)));
    QVERIFY(!testFile.exists());

    // remove package again

    taskId = m_ai->removePackage(name, false);
    QVERIFY(!taskId.isEmpty());
    QVERIFY(m_finishedSpy->wait(spyTimeout));
    QCOMPARE(m_finishedSpy->first()[0].toString(), taskId);
}


void tst_ApplicationInstaller::packageInstallation_data()
{
    QTest::addColumn<QString>("packageName");
    QTest::addColumn<QString>("installationLocationId");
    QTest::addColumn<QString>("updatePackageName");
    QTest::addColumn<QString>("updateInstallationLocationId");
    QTest::addColumn<bool>("devSigned");
    QTest::addColumn<bool>("storeSigned");
    QTest::addColumn<bool>("expectedSuccess");
    QTest::addColumn<bool>("updateExpectedSuccess");
    QTest::addColumn<QVariantMap>("extraMetaData");
    QTest::addColumn<QString>("errorString"); // start with ~ to create a RegExp

    QVariantMap nomd { }; // no meta-data
    QVariantMap extramd = QVariantMap {
        { "extra", QVariantMap {
            { "array", QVariantList { 1, 2 } },
            { "foo", "bar" },
            { "foo2","bar2" },
            { "key", "value" } } },
        { "extraSigned", QVariantMap {
            { "sfoo", "sbar" },
            { "sfoo2", "sbar2" },
            { "signed-key", "signed-value" },
            { "signed-object", QVariantMap { { "k1", "v1" }, { "k2", "v2" } } }
        } }
    };

    QTest::newRow("normal") \
            << "test.appkg" << "internal-0" << "test-update.appkg" << "internal-0"
            << false << false << true << true << nomd<< "";
    QTest::newRow("no-dev-signed") \
            << "test.appkg" << "internal-0" << "" << ""
            << true << false << false << false << nomd << "cannot install unsigned packages";
    QTest::newRow("dev-signed") \
            << "test-dev-signed.appkg" << "internal-0" << "test-update-dev-signed.appkg" << "internal-0"
            << true << false << true << true << nomd << "";
    QTest::newRow("no-store-signed") \
            << "test.appkg" << "internal-0" << "" << ""
            << false << true << false << false << nomd << "cannot install unsigned packages";
    QTest::newRow("no-store-but-dev-signed") \
            << "test-dev-signed.appkg" << "internal-0" << "" << ""
            << false << true << false << false << nomd << "cannot install development packages on consumer devices";
    QTest::newRow("store-signed") \
            << "test-store-signed.appkg" << "internal-0" << "" << ""
            << false << true << true << false << nomd << "";
    QTest::newRow("extra-metadata") \
            << "test-extra.appkg" << "internal-0" << "" << ""
            << false << false << true << false << extramd << "";
    QTest::newRow("extra-metadata-dev-signed") \
            << "test-extra-dev-signed.appkg" << "internal-0" << "" << ""
            << true << false << true << false << extramd << "";
    QTest::newRow("update-to-different-location") \
            << "test.appkg" << "internal-0" << "test-update.appkg" << "internal-1"
            << false << false << true << false << nomd << "the application com.pelagicore.test cannot be installed to internal-1, since it is already installed to internal-0";
    QTest::newRow("invalid-location") \
            << "test.appkg" << "internal-42" << "" << ""
            << false << false << false << false << nomd << "invalid installation location";
    QTest::newRow("invalid-file-order") \
            << "test-invalid-file-order.appkg" << "internal-0" << "" << ""
            << false << false << false << false << nomd << "The application icon (as stated in info.yaml) must be the second file in the package. Expected 'icon.png', got 'test'";
    QTest::newRow("invalid-header-format") \
            << "test-invalid-header-formatversion.appkg" << "internal-0" << "" << ""
            << false << false << false << false << nomd << "metadata has an invalid format specification: wrong formatVersion header: expected 1, got 2";
    QTest::newRow("invalid-header-diskspaceused") \
            << "test-invalid-header-diskspaceused.appkg" << "internal-0" << "" << ""
            << false << false << false << false << nomd << "metadata has an invalid diskSpaceUsed field (0)";
    QTest::newRow("invalid-header-id") \
            << "test-invalid-header-id.appkg" << "internal-0" << "" << ""
            << false << false << false << false << nomd << "metadata has an invalid applicationId field (:invalid)";
    QTest::newRow("non-matching-header-id") \
            << "test-non-matching-header-id.appkg" << "internal-0" << "" << ""
            << false << false << false << false << nomd << "the application identifiers in --PACKAGE-HEADER--' and info.yaml do not match";
    QTest::newRow("tampered-extra-signed-header") \
            << "test-tampered-extra-signed-header.appkg" << "internal-0" << "" << ""
            << false << false << false << false << nomd << "~package digest mismatch.*";
    QTest::newRow("invalid-info.yaml") \
            << "test-invalid-info.appkg" << "internal-0" << "" << ""
            << false << false << false << false << nomd << "~.*YAML parse error at line \\d+, column \\d+: did not find expected key";
    QTest::newRow("invalid-info.yaml-id") \
            << "test-invalid-info-id.appkg" << "internal-0" << "" << ""
            << false << false << false << false << nomd << "~.*the identifier \\(:invalid\\) is not a valid application-id: must consist of printable ASCII characters only, except any of .*";
    QTest::newRow("invalid-footer-signature") \
            << "test-invalid-footer-signature.appkg" << "internal-0" << "" << ""
            << true << false << false << false << nomd << "could not verify the package's developer signature";

    if (!m_fakeSudo) {
        QTest::newRow("sdcard") \
                << "test.appkg" << "removable-0" << "test-update.appkg" << "removable-0"
                << false << false << true << true << nomd << "";
        QTest::newRow("sdcard-dev-signed") \
                << "test-dev-signed.appkg" << "removable-0" << "test-update-dev-signed.appkg" << "removable-0"
                << true << false << true << true << nomd << "";
        QTest::newRow("sdcard-no-space") \
                << "bigtest-dev-signed.appkg" << "removable-0" << "" << ""
                << true << false << false << false << nomd << "~not enough storage space left on removable-0: [0-9.]+ MB available, but [0-9.]+ MB needed";
    }
}

// this test function is a bit of a kitchen sink, but the basic boiler plate
// code of testing the results of an installation is the biggest part and it
// is always the same.
void tst_ApplicationInstaller::packageInstallation()
{
    QFETCH(QString, packageName);
    QFETCH(QString, installationLocationId);
    QFETCH(QString, updatePackageName);
    QFETCH(QString, updateInstallationLocationId);
    QFETCH(bool, devSigned);
    QFETCH(bool, storeSigned);
    QFETCH(bool, expectedSuccess);
    QFETCH(bool, updateExpectedSuccess);
    QFETCH(QVariantMap, extraMetaData);
    QFETCH(QString, errorString);

#if !defined(Q_OS_LINUX)
    if (installationLocationId.startsWith("removable-"))
        QSKIP("no removable installation locations on this platform");
#else
    if (m_fakeSudo)
        QSKIP("removable installation locations cannot be tested without root privileges");
#endif

    AllowInstallations allow(storeSigned ? AllowInstallations::RequireStoreSigned
                                         : (devSigned ? AllowInstallations::RequireDevSigned
                                                      : AllowInstallations::AllowUnsinged));

    int lastPass = (updatePackageName.isEmpty() ? 1 : 2);
    // pass 1 is the installation / pass 2 is the update (if needed)
    for (int pass = 1; pass <= lastPass; ++pass) {
        const InstallationLocation &il = m_ai->installationLocationFromId(pass == 1 ? installationLocationId : updateInstallationLocationId);

        // this makes the results a bit ugly to look at, but it helps with debugging a lot
        if (pass > 1)
            qInfo("Pass %d", pass);

        // install (or update) the package

        QUrl url = QUrl::fromLocalFile(AM_TESTDATA_DIR "packages/" + (pass == 1 ? packageName : updatePackageName));
        QString taskId = m_ai->startPackageInstallation(il.id(), url);
        QVERIFY(!taskId.isEmpty());
        m_ai->acknowledgePackageInstallation(taskId);

        // check received signals...

        if (pass == 1 ? !expectedSuccess : !updateExpectedSuccess) {
            // ...in case of expected failure

            QVERIFY(m_failedSpy->wait(spyTimeout));
            QCOMPARE(m_failedSpy->first()[0].toString(), taskId);

            AM_CHECK_ERRORSTRING(m_failedSpy->first()[2].toString(), errorString);
        } else {
            // ...in case of expected success

            QVERIFY(m_finishedSpy->wait(spyTimeout));
            QCOMPARE(m_finishedSpy->first()[0].toString(), taskId);
            QVERIFY(!m_progressSpy->isEmpty());
            QCOMPARE(m_progressSpy->last()[0].toString(), taskId);
            QCOMPARE(m_progressSpy->last()[1].toDouble(), double(1));

            // check files

            QVERIFY(QFile::exists(pathTo(Manifests, "com.pelagicore.test/installation-report.yaml")));
            QVERIFY(QDir(pathTo(il.documentPath() + "/com.pelagicore.test")).exists());

            // if we installed to an SDCard, we need to mount the generate image first
            // in order to check its contents

            QString loopbackDevice;
            QString fileCheckPath;
            if (il.isRemovable()) {
                QString imgPath = il.installationPath() + "/com.pelagicore.test.appimg";

                QVERIFY(QFile::exists(imgPath));

                loopbackDevice = m_sudo->attachLoopback(imgPath, true);
                QVERIFY2(!loopbackDevice.isEmpty(), qPrintable(m_sudo->lastError()));
                QVERIFY2(m_sudo->mount(loopbackDevice, pathTo(TemporaryMount), true), qPrintable(m_sudo->lastError()));
                fileCheckPath = pathTo(TemporaryMount);
            } else {
                fileCheckPath = il.installationPath() + "/com.pelagicore.test";
            }

            // now check the installed files

            QStringList files = QDir(fileCheckPath).entryList(QDir::AllEntries | QDir::NoDotAndDotDot);
            files.removeAll("lost+found"); // no way to get rid of this completely
            files.sort();
            QVERIFY2(files == QStringList({ "icon.png", "info.yaml", "test", QString::fromUtf8("t\xc3\xa4st") }), qPrintable(files.join(", ")));

            QFile f(fileCheckPath + "/test");
            QVERIFY(f.open(QFile::ReadOnly));
            QCOMPARE(f.readAll(), QByteArray(pass == 1 ? "test\n" : "test update\n"));
            f.close();

            // remove loopback and unmount in case of an SDCard installation

            if (il.isRemovable()) {
                QVERIFY2(m_sudo->unmount(pathTo(TemporaryMount)), qPrintable(m_sudo->lastError()));
                QVERIFY2(m_sudo->detachLoopback(loopbackDevice), qPrintable(m_sudo->lastError()));
            }

            // check metadata
            QCOMPARE(m_requestingInstallationAcknowledgeSpy->count(), 1);
            QVariantMap extra = m_requestingInstallationAcknowledgeSpy->first()[2].toMap();
            QVariantMap extraSigned = m_requestingInstallationAcknowledgeSpy->first()[3].toMap();
            if (extraMetaData.value("extra").toMap() != extra) {
                qDebug() << "Actual: " << extra;
                qDebug() << "Expected: " << extraMetaData.value("extra").toMap();
                QVERIFY(extraMetaData == extra);
            }
            if (extraMetaData.value("extraSigned").toMap() != extraSigned) {
                qDebug() << "Actual: " << extraSigned;
                qDebug() << "Expected: " << extraMetaData.value("extraSigned").toMap();
                QVERIFY(extraMetaData == extraSigned);
            }

            // check if the meta-data was saved to the installation report correctly
            QVERIFY2(m_ai->installedApplicationExtraMetaData("com.pelagicore.test") == extra,
                     "Extra meta-data was not correctly saved to installation report");
            QVERIFY2(m_ai->installedApplicationExtraSignedMetaData("com.pelagicore.test") == extraSigned,
                     "Extra signed meta-data was not correctly saved to installation report");
        }
        if (pass == lastPass && expectedSuccess) {
            // remove package again

            clearSignalSpies();
            taskId = m_ai->removePackage("com.pelagicore.test", false);
            QVERIFY(!taskId.isEmpty());

            // check signals

            QVERIFY(m_finishedSpy->wait(spyTimeout));
            QCOMPARE(m_finishedSpy->first()[0].toString(), taskId);
        }
        clearSignalSpies();
    }
    // check that all files are gone

    for (PathLocation pl: { Manifests, Internal0, Internal1, Documents0, Documents1, SDCard0Images, SDCard1Images }) {
        QStringList entries = QDir(pathTo(pl)).entryList({ "com.pelagicore.test*" });
        QVERIFY2(entries.isEmpty(), qPrintable(pathTo(pl) + ": " + entries.join(", ")));
    }
}


void tst_ApplicationInstaller::removeAppOnMissingSDCard()
{
#ifndef Q_OS_LINUX
    QSKIP("Cannot run SD-Card tests on non-Linux systems");
#else
    if (m_fakeSudo)
        QSKIP("Cannot run SD-Card tests without root privileges");
#endif

    // install package to sdcard

    QString taskId = m_ai->startPackageInstallation("removable-0", QUrl::fromLocalFile(AM_TESTDATA_DIR "packages/test-dev-signed.appkg"));
    QVERIFY(!taskId.isEmpty());
    m_ai->acknowledgePackageInstallation(taskId);

    // check received signals

    QVERIFY(m_finishedSpy->wait(spyTimeout));
    QCOMPARE(m_finishedSpy->first()[0].toString(), taskId);
    clearSignalSpies();

    // check files

    QVERIFY(QFile::exists(pathTo(Manifests, "com.pelagicore.test/installation-report.yaml")));
    QVERIFY(QDir(pathTo(Documents0, "com.pelagicore.test")).exists());
    QVERIFY(QFile::exists(pathTo(SDCard0Images, "com.pelagicore.test.appimg")));

    // simulate removal of SD-Card

    QVERIFY2(m_sudo->unmount(pathTo(SDCard0), true), qPrintable(m_sudo->lastError()));

    // try to remove the package

    taskId = m_ai->removePackage("com.pelagicore.test", false);
    QVERIFY(!taskId.isEmpty());
    QVERIFY(m_failedSpy->wait(spyTimeout));
    QCOMPARE(m_failedSpy->first()[0].toString(), taskId);
    QCOMPARE(m_failedSpy->first()[2].toString(), QString::fromLatin1("cannot delete application com.pelagicore.test without the removable medium it was installed on"));
    clearSignalSpies();

    QVERIFY(QFile::exists(pathTo(Manifests, "com.pelagicore.test/installation-report.yaml")));
    QVERIFY(QDir(pathTo(Documents0, "com.pelagicore.test")).exists());

    // now force remove it

    taskId = m_ai->removePackage("com.pelagicore.test", false, true /*force*/);
    QVERIFY(!taskId.isEmpty());
    QVERIFY(m_finishedSpy->wait(spyTimeout));
    QCOMPARE(m_finishedSpy->first()[0].toString(), taskId);
    clearSignalSpies();

    // check if files that are not on SD-Card are gone

    QVERIFY(!QDir(pathTo(Manifests, "com.pelagicore.test")).exists());
    QVERIFY(!QDir(pathTo(Documents0, "com.pelagicore.test")).exists());

    // "re-insert" the SD-Card

    QVERIFY(m_sudo->mount(m_loopbackForSDCard[0], pathTo(SDCard0), false, "vfat"));
    QVERIFY(QFile::exists(pathTo(SDCard0Images, "com.pelagicore.test.appimg")));
    QVERIFY(QFile::remove(pathTo(SDCard0Images, "com.pelagicore.test.appimg")));

    //TODO: now that AI::cleanupBrokenInstallations() works, call it here and check the results
}


Q_DECLARE_METATYPE(std::function<bool()>)
typedef QMultiMap<QString, std::function<bool()>> FunctionMap;
Q_DECLARE_METATYPE(FunctionMap)

void tst_ApplicationInstaller::simulateErrorConditions_data()
{
     QTest::addColumn<QString>("installationLocation");
     QTest::addColumn<bool>("testUpdate");
     QTest::addColumn<QString>("errorString");
     QTest::addColumn<FunctionMap>("functions");

#ifdef Q_OS_LINUX
     QTest::newRow("manifests-dir-read-only") \
             << "internal-0" << false << "~could not create manifest sub-directory .*" \
             << FunctionMap { { "before-start", [this]() { return chmod(pathTo(Manifests).toLocal8Bit(), 0000) == 0; } },
                              { "after-failed", [this]() { return chmod(pathTo(Manifests).toLocal8Bit(), 0777) == 0; } } };

     QTest::newRow("applications-dir-read-only") \
             << "internal-0" << false << "~could not create installation directory .*" \
             << FunctionMap { { "before-start", [this]() { return chmod(pathTo(Internal0).toLocal8Bit(), 0000) == 0; } },
                              { "after-failed", [this]() { return chmod(pathTo(Internal0).toLocal8Bit(), 0777) == 0; } } };

     QTest::newRow("documents-dir-read-only") \
             << "internal-0" << false << "~could not create the document directory .*" \
             << FunctionMap { { "before-start", [this]() { return chmod(pathTo(Documents0).toLocal8Bit(), 0000) == 0; } },
                              { "after-failed", [this]() { return chmod(pathTo(Documents0).toLocal8Bit(), 0777) == 0; } } };

     QTest::newRow("image-still-activated") \
             << "removable-0" << true << "~existing application image .* is still mounted" \
             << FunctionMap { { "before-start", [this]() { return m_ai->activatePackage("com.pelagicore.test")
                                                                      && checkPackageActivation("com.pelagicore.test", 3000); } },
                              { "after-failed", [this]() { return m_ai->deactivatePackage("com.pelagicore.test")
                                                                      && checkPackageDeactivation("com.pelagicore.test", 3000); } } };

     QTest::newRow("sdcard-not-mounted") \
             << "removable-0" << false << "installation medium removable-0 is not mounted" \
             << FunctionMap { { "before-start", [this]() { return m_sudo->unmount(pathTo(SDCard0), true); } },
                              { "after-failed", [this]() { return m_sudo->mount(m_loopbackForSDCard[0], pathTo(SDCard0), false, "vfat"); } } };

     QTest::newRow("sdcard-unmounted-while-installing") \
             << "removable-0" << false << "removable medium removable-0 is not mounted" \
             << FunctionMap { { "after-start", [this]() { return m_requestingInstallationAcknowledgeSpy->wait(spyTimeout)
                                                                     && m_blockingUntilInstallationAcknowledgeSpy->wait(spyTimeout)
                                                                     && m_sudo->unmount(pathTo(SDCard0), true); } },
                              { "after-failed", [this]() { return m_sudo->mount(m_loopbackForSDCard[0], pathTo(SDCard0), false, "vfat"); } } };

#endif
}

void tst_ApplicationInstaller::simulateErrorConditions()
{
#ifndef Q_OS_LINUX
    QSKIP("Cannot run SD-Card tests on non-Linux systems");
#else
    if (m_fakeSudo)
        QSKIP("Cannot run SD-Card tests without root privileges");
#endif

    QFETCH(QString, installationLocation);
    QFETCH(bool, testUpdate);
    QFETCH(QString, errorString);
    QFETCH(FunctionMap, functions);

    QString taskId;

    if (testUpdate) {
        // the check will run when updating a package, so we need to install it first

        taskId = m_ai->startPackageInstallation(installationLocation, QUrl::fromLocalFile(AM_TESTDATA_DIR "packages/test-dev-signed.appkg"));
        QVERIFY(!taskId.isEmpty());
        m_ai->acknowledgePackageInstallation(taskId);
        QVERIFY(m_finishedSpy->wait(spyTimeout));
        QCOMPARE(m_finishedSpy->first()[0].toString(), taskId);
        clearSignalSpies();
    }

    foreach (const auto &f, functions.values("before-start"))
        QVERIFY(f());

    taskId = m_ai->startPackageInstallation(installationLocation, QUrl::fromLocalFile(AM_TESTDATA_DIR "packages/test-dev-signed.appkg"));

    foreach (const auto &f, functions.values("after-start"))
        QVERIFY(f());

    m_ai->acknowledgePackageInstallation(taskId);

    QVERIFY(m_failedSpy->wait(spyTimeout));
    QCOMPARE(m_failedSpy->first()[0].toString(), taskId);
    AM_CHECK_ERRORSTRING(m_failedSpy->first()[2].toString(), errorString);
    clearSignalSpies();

    foreach (const auto &f, functions.values("after-failed"))
        QVERIFY(f());

    if (testUpdate) {
        taskId = m_ai->removePackage("com.pelagicore.test", false);

        QVERIFY(m_finishedSpy->wait(spyTimeout));
        QCOMPARE(m_finishedSpy->first()[0].toString(), taskId);
    }
}


void tst_ApplicationInstaller::cancelPackageInstallation_data()
{
    QTest::addColumn<bool>("expectedResult");

    // please note that the data tag names are used in the actual test function below!

    QTest::newRow("before-started-signal") << true;
    QTest::newRow("after-started-signal") << true;
    QTest::newRow("after-blocking-until-installation-acknowledge-signal") << true;
    QTest::newRow("after-finished-signal") << false;
}

void tst_ApplicationInstaller::cancelPackageInstallation()
{
    QFETCH(bool, expectedResult);

    QString taskId = m_ai->startPackageInstallation("internal-0", QUrl::fromLocalFile(AM_TESTDATA_DIR "packages/test-dev-signed.appkg"));
    QVERIFY(!taskId.isEmpty());

    if (isDataTag("before-started-signal")) {
        QCOMPARE(m_ai->cancelTask(taskId), expectedResult);
    } else if (isDataTag("after-started-signal")) {
        QVERIFY(m_startedSpy->wait(spyTimeout));
        QCOMPARE(m_startedSpy->first()[0].toString(), taskId);
        QCOMPARE(m_ai->cancelTask(taskId), expectedResult);
    } else if (isDataTag("after-blocking-until-installation-acknowledge-signal")) {
        QVERIFY(m_blockingUntilInstallationAcknowledgeSpy->wait(spyTimeout));
        QCOMPARE(m_blockingUntilInstallationAcknowledgeSpy->first()[0].toString(), taskId);
        QCOMPARE(m_ai->cancelTask(taskId), expectedResult);
    } else if (isDataTag("after-finished-signal")) {
        m_ai->acknowledgePackageInstallation(taskId);
        QVERIFY(m_finishedSpy->wait(spyTimeout));
        QCOMPARE(m_finishedSpy->first()[0].toString(), taskId);
        QCOMPARE(m_ai->cancelTask(taskId), expectedResult);
    }

    if (expectedResult) {
        if (!m_startedSpy->isEmpty()) {
            QVERIFY(m_failedSpy->wait(spyTimeout));
            QCOMPARE(m_failedSpy->first()[0].toString(), taskId);
            QCOMPARE(m_failedSpy->first()[1].toInt(), int(Error::Canceled));
        }
    } else {
        clearSignalSpies();

        taskId = m_ai->removePackage("com.pelagicore.test", false);
        QVERIFY(!taskId.isEmpty());
        QVERIFY(m_finishedSpy->wait(spyTimeout));
        QCOMPARE(m_finishedSpy->first()[0].toString(), taskId);
    }
    clearSignalSpies();
}

void tst_ApplicationInstaller::parallelPackageInstallation()
{
    QString task1Id = m_ai->startPackageInstallation("internal-0", QUrl::fromLocalFile(AM_TESTDATA_DIR "packages/test-dev-signed.appkg"));
    QVERIFY(!task1Id.isEmpty());
    QVERIFY(m_blockingUntilInstallationAcknowledgeSpy->wait(spyTimeout));
    QCOMPARE(m_blockingUntilInstallationAcknowledgeSpy->first()[0].toString(), task1Id);

    QString task2Id = m_ai->startPackageInstallation("internal-0", QUrl::fromLocalFile(AM_TESTDATA_DIR "packages/bigtest-dev-signed.appkg"));
    QVERIFY(!task2Id.isEmpty());
    m_ai->acknowledgePackageInstallation(task2Id);
    QVERIFY(m_finishedSpy->wait(spyTimeout));
    QCOMPARE(m_finishedSpy->first()[0].toString(), task2Id);

    clearSignalSpies();
    m_ai->acknowledgePackageInstallation(task1Id);
    QVERIFY(m_finishedSpy->wait(spyTimeout));
    QCOMPARE(m_finishedSpy->first()[0].toString(), task1Id);

    clearSignalSpies();
}


static tst_ApplicationInstaller *tstApplicationInstaller = nullptr;

int main(int argc, char **argv)
{
    Package::ensureCorrectLocale();

    try {
        Sudo::forkServer(Sudo::DropPrivilegesPermanently);
        startedSudoServer = true;
    } catch (...) { }

    QCoreApplication a(argc, argv);
    tstApplicationInstaller = new tst_ApplicationInstaller(&a);

#ifdef Q_OS_LINUX
    auto crashHandler = [](int sigNum) -> void {
        // we are doing very unsafe things from a within a signal handler, but
        // we've crashed anyway at this point and the alternative is that we are
        // leaking mounts and attached loopback devices.

        tstApplicationInstaller->~tst_ApplicationInstaller();

        if (sigNum != -1)
            exit(1);
    };

    signal(SIGABRT, crashHandler);
    signal(SIGSEGV, crashHandler);
    signal(SIGINT, crashHandler);
#endif // Q_OS_LINUX

    return QTest::qExec(tstApplicationInstaller, argc, argv);
}

void tst_ApplicationInstaller::validateDnsName_data()
{
    QTest::addColumn<QString>("dnsName");
    QTest::addColumn<int>("minParts");
    QTest::addColumn<bool>("valid");

    // passes
    QTest::newRow("normal") << "com.pelagicore.test" << 3 << true;
    QTest::newRow("shortest") << "c.p.t" << 3 << true;
    QTest::newRow("valid-chars") << "1-2.c-d.3.z" << 3 << true;
    QTest::newRow("longest-part") << "com.012345678901234567890123456789012345678901234567890123456789012.test" << 3 << true;
    QTest::newRow("longest-name") << "com.012345678901234567890123456789012345678901234567890123456789012.012345678901234567890123456789012345678901234567890123456789012.0123456789012.test" << 3 << true;
    QTest::newRow("max-part-cnt") << "a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.0.1.2.3.4.5.6.7.8.9.a.b.c.d.e.f.g.h.i.j.k.l.m.n.o.p.q.r.s.t.u.v.w.x.y.z.0.1.2.3.4.5.6.7.8.9.a.0.12" << 3 << true;
    QTest::newRow("one-part-only") << "c" << 1 << true;

    // failures
    QTest::newRow("too-few-parts") << "com.pelagicore" << 3 << false;
    QTest::newRow("empty-part") << "com..test" << 3 << false;
    QTest::newRow("empty") << "" << 3 << false;
    QTest::newRow("dot-only") << "." << 3 << false;
    QTest::newRow("invalid-char1") << "com.pelagi_core.test" << 3 << false;
    QTest::newRow("invalid-char2") << "com.pelagi#core.test" << 3 << false;
    QTest::newRow("invalid-char3") << "com.pelagi$core.test" << 3 << false;
    QTest::newRow("invalid-char3") << "com.pelagi@core.test" << 3 << false;
    QTest::newRow("unicode-char") << QString::fromUtf8("c\xc3\xb6m.pelagicore.test") << 3 << false;
    QTest::newRow("upper-case") << "com.Pelagicore.test" << 3 << false;
    QTest::newRow("dash-at-start") << "com.-pelagicore.test" << 3 << false;
    QTest::newRow("dash-at-end") << "com.pelagicore-.test" << 3 << false;
    QTest::newRow("part-too-long") << "com.x012345678901234567890123456789012345678901234567890123456789012.test" << 3 << false;
}

void tst_ApplicationInstaller::validateDnsName()
{
    QFETCH(QString, dnsName);
    QFETCH(int, minParts);
    QFETCH(bool, valid);

    QString errorString;
    bool result = m_ai->validateDnsName(dnsName, minParts);

    QVERIFY2(valid == result, qPrintable(errorString));
}

void tst_ApplicationInstaller::compareVersions_data()
{
    QTest::addColumn<QString>("version1");
    QTest::addColumn<QString>("version2");
    QTest::addColumn<int>("result");


    QTest::newRow("1") << "" << "" << 0;
    QTest::newRow("2") << "0" << "0" << 0;
    QTest::newRow("3") << "foo" << "foo" << 0;
    QTest::newRow("4") << "1foo" << "1foo" << 0;
    QTest::newRow("5") << "foo1" << "foo1" << 0;
    QTest::newRow("6") << "13.403.51-alpha2+git" << "13.403.51-alpha2+git" << 0;
    QTest::newRow("7") << "1" << "2" << -1;
    QTest::newRow("8") << "2" << "1" << 1;
    QTest::newRow("9") << "1.0" << "2.0" << -1;
    QTest::newRow("10") << "1.99" << "2.0" << -1;
    QTest::newRow("11") << "1.9" << "11" << -1;
    QTest::newRow("12") << "9" << "10" << -1;
    QTest::newRow("12") << "9a" << "10" << -1;
    QTest::newRow("13") << "9-a" << "10" << -1;
    QTest::newRow("14") << "13.403.51-alpha2+gi" << "13.403.51-alpha2+git" << -1;
    QTest::newRow("15") << "13.403.51-alpha1+git" << "13.403.51-alpha2+git" << -1;
    QTest::newRow("16") << "13.403.51-alpha2+git" << "13.403.51-beta1+git" << -1;
    QTest::newRow("17") << "13.403.51-alpha2+git" << "13.403.52" << -1;
    QTest::newRow("18") << "13.403.51-alpha2+git" << "13.403.52-alpha2+git" << -1;
    QTest::newRow("19") << "13.403.51-alpha2+git" << "13.404" << -1;
    QTest::newRow("20") << "13.402" << "13.403.51-alpha2+git" << -1;
    QTest::newRow("21") << "12.403.51-alpha2+git" << "13.403.51-alpha2+git" << -1;
}

void tst_ApplicationInstaller::compareVersions()
{
    QFETCH(QString, version1);
    QFETCH(QString, version2);
    QFETCH(int, result);

    int cmp = m_ai->compareVersions(version1, version2);
    QCOMPARE(cmp, result);

    if (result) {
        cmp = m_ai->compareVersions(version2, version1);
        QCOMPARE(cmp, -result);
    }
}

#include "tst_applicationinstaller.moc"