GCC Code Coverage Report


Directory: ./
File: autotests/client/filter.cpp
Date: 2024-01-22 17:25:27
Exec Total Coverage
Lines: 63 63 100.0%
Branches: 69 132 52.3%

Line Branch Exec Source
1 /********************************************************************
2 Copyright 2017 David Edmundson <davidedmundson@kde.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) version 3, or any
8 later version accepted by the membership of KDE e.V. (or its
9 successor approved by the membership of KDE e.V.), which shall
10 act as a proxy defined in Section 6 of version 3 of the license.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library. If not, see <http://www.gnu.org/licenses/>.
19 *********************************************************************/
20 #include <QtTest>
21
22 #include "../../src/client/blur.h"
23 #include "../../src/client/compositor.h"
24 #include "../../src/client/connection_thread.h"
25 #include "../../src/client/event_queue.h"
26 #include "../../src/client/region.h"
27 #include "../../src/client/registry.h"
28 #include "../../src/client/surface.h"
29
30 #include "../../server/blur.h"
31 #include "../../server/client.h"
32 #include "../../server/compositor.h"
33 #include "../../server/display.h"
34 #include "../../server/filtered_display.h"
35 #include "../../server/region.h"
36
37 #include "../../tests/globals.h"
38
39 #include <wayland-server.h>
40
41 using namespace Wrapland::Client;
42
43 class TestDisplay;
44
45 class TestFilter : public QObject
46 {
47 Q_OBJECT
48 public:
49 explicit TestFilter(QObject* parent = nullptr);
50 private Q_SLOTS:
51 void init();
52 void cleanup();
53 void testFilter_data();
54 void testFilter();
55
56 private:
57 std::unique_ptr<TestDisplay> m_display;
58 Wrapland::Server::globals globals;
59 };
60
61 constexpr auto socket_name{"wrapland-test-wayland-blur-0"};
62
63 // The following non-realistic class allows only clients in the m_allowedClients list to access the
64 // blur interface all other interfaces are allowed
65 class TestDisplay : public Wrapland::Server::FilteredDisplay
66 {
67 public:
68 TestDisplay();
69 bool allowInterface(Wrapland::Server::Client* client, QByteArray const& interfaceName) override;
70 QList<wl_client*> m_allowedClients;
71 };
72
73 4 TestDisplay::TestDisplay()
74 2 : Wrapland::Server::FilteredDisplay()
75 2 {
76 2 }
77
78 6 bool TestDisplay::allowInterface(Wrapland::Server::Client* client, QByteArray const& interfaceName)
79 {
80
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 if (interfaceName == "org_kde_kwin_blur_manager") {
81 3 return m_allowedClients.contains(client->native());
82 }
83 3 return true;
84 6 }
85
86
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
2 TestFilter::TestFilter(QObject* parent)
87 1 : QObject(parent)
88 1 {
89 1 }
90
91 2 void TestFilter::init()
92 {
93 using namespace Wrapland::Server;
94 2 m_display = std::make_unique<TestDisplay>();
95
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 m_display->set_socket_name(socket_name);
96 2 m_display->start();
97
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 QVERIFY(m_display->running());
98
99 2 globals.compositor = std::make_unique<Wrapland::Server::Compositor>(m_display.get());
100 2 globals.blur_manager = std::make_unique<Wrapland::Server::BlurManager>(m_display.get());
101 2 }
102
103 2 void TestFilter::cleanup()
104 {
105 2 }
106
107 1 void TestFilter::testFilter_data()
108 {
109 1 QTest::addColumn<bool>("accessAllowed");
110 1 QTest::newRow("granted") << true;
111 1 QTest::newRow("denied") << false;
112 1 }
113
114 2 void TestFilter::testFilter()
115 {
116 2 QFETCH(bool, accessAllowed);
117
118 // setup connection
119 2 std::unique_ptr<Wrapland::Client::ConnectionThread> connection(
120
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 new Wrapland::Client::ConnectionThread());
121
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 QSignalSpy connectedSpy(connection.get(), &ConnectionThread::establishedChanged);
122
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 QVERIFY(connectedSpy.isValid());
123
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 connection->setSocketName(socket_name);
124
125
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 std::unique_ptr<QThread> thread(new QThread(this));
126
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 connection->moveToThread(thread.get());
127
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 thread->start();
128
129
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 connection->establishConnection();
130
6/12
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
2 QVERIFY(connectedSpy.count() || connectedSpy.wait());
131
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 QCOMPARE(connectedSpy.count(), 1);
132
133 // use low level API as Server::Display::connections only lists connections which have
134 // been previous fetched via getConnection()
135
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (accessAllowed) {
136 wl_client* clientConnection;
137
10/18
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 2 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 14 taken 1 times.
✗ Branch 15 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
2 wl_client_for_each(clientConnection, wl_display_get_client_list(m_display->native()))
138 {
139
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 m_display->m_allowedClients << clientConnection;
140 1 }
141 1 }
142
143
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Wrapland::Client::EventQueue queue;
144
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 queue.setup(connection.get());
145
146
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 Registry registry;
147
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 QSignalSpy registryDoneSpy(&registry, &Registry::interfacesAnnounced);
148
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 QSignalSpy compositorSpy(&registry, &Registry::compositorAnnounced);
149
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 QSignalSpy blurSpy(&registry, &Registry::blurAnnounced);
150
151
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 registry.setEventQueue(&queue);
152
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 registry.create(connection->display());
153
3/6
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
2 QVERIFY(registry.isValid());
154
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 registry.setup();
155
156
5/10
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 2 times.
2 QVERIFY(registryDoneSpy.wait());
157
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 QVERIFY(compositorSpy.count() == 1);
158
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 QVERIFY(blurSpy.count() == accessAllowed ? 1 : 0);
159
160
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 thread->quit();
161
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 thread->wait();
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 }
163
164
4/8
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 1 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 1 times.
✗ Branch 7 not taken.
1 QTEST_GUILESS_MAIN(TestFilter)
165 #include "filter.moc"
166