summaryrefslogtreecommitdiff
path: root/tests/server/misc.cpp
blob: bae84b17c569eb9061d1e15d2b85123144d3366f (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
/*
 * Copyright © 2012-2013 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 *
 */

#if HAVE_CONFIG_H
#include <config.h>
#endif

#include <fstream>

#include <xorg/gtest/xorg-gtest.h>
#include <X11/extensions/scrnsaver.h>
#include <X11/extensions/sync.h>

#include "xit-server-input-test.h"
#include "xit-event.h"
#include "device-interface.h"
#include "helpers.h"

using namespace xorg::testing;

class EventQueueTest : public XITServerInputTest,
                       public DeviceInterface {
public:
    /**
     * Initializes a standard mouse device.
     */
    virtual void SetUp() {
        SetDevice("mice/PIXART-USB-OPTICAL-MOUSE-HWHEEL.desc");
        XITServerInputTest::SetUp();
    }

    /**
     * Sets up an xorg.conf for a single evdev CoreKeyboard device based on
     * the evemu device. The input from GetParam() is used as XkbLayout.
     */
    virtual void SetUpConfigAndLog() {
        config.AddDefaultScreenWithDriver();
        config.AddInputSection("evdev", "--device--",
                               "Option \"CorePointer\" \"on\"\n"
                               "Option \"GrabDevice\" \"on\"\n"
                               "Option \"Device\" \"" + dev->GetDeviceNode() + "\"");
        /* add default keyboard device to avoid server adding our device again */
        config.AddInputSection("kbd", "kbd-device",
                               "Option \"CoreKeyboard\" \"on\"\n");
        config.WriteConfig();
    }
};

TEST_F(EventQueueTest, mieqOverflow)
{
    XORG_TESTCASE("Overflow the event queue and force a resize.\n"
                  "Search the server log for the error message.\n");

    for (int i = 0; i < 2048; i++)
        dev->PlayOne(EV_REL, REL_X, -1, true);
    XSync(Display(), False);
    ASSERT_EQ(server.GetState(), xorg::testing::Process::RUNNING);

    std::ifstream logfile(server.GetLogFilePath().c_str());
    std::string line;
    std::string bug_warn("EQ overflowing");
    bool found = false;
    ASSERT_TRUE(logfile.is_open());
    while(!found && getline(logfile, line))
        found = line.find(bug_warn);

    ASSERT_TRUE(found) << "Expected message '" << bug_warn << "' in the log "
                       << "but couldn't find it";
}

TEST(MiscServerTest, DoubleSegfault)
{
    XORG_TESTCASE("TESTCASE: SIGSEGV the server. The server must catch the "
                  "signal, clean up and then call abort().\n");

    XITServer server;
    server.Start();

    ASSERT_EQ(server.GetState(), xorg::testing::Process::RUNNING);

    /* We connect and run XSync, this usually takes long enough for some
       devices to come up. That way, input devices are part of the close
       down procedure */
    ::Display *dpy = XOpenDisplay(server.GetDisplayString().c_str());
    XSync(dpy, False);

    kill(server.Pid(), SIGSEGV);

    int status = 1;
    while(WIFEXITED(status) == false)
        if (waitpid(server.Pid(), &status, 0) == -1)
            break;

    ASSERT_TRUE(WIFSIGNALED(status));
    int termsig = WTERMSIG(status);
    ASSERT_EQ(termsig, SIGABRT)
        << "Expected SIGABRT, got " << termsig << " (" << strsignal(termsig) << ")";

    std::ifstream logfile(server.GetLogFilePath().c_str());
    std::string line;
    std::string signal_caught_msg("Caught signal");
    int signal_count = 0;
    ASSERT_TRUE(logfile.is_open());
    while(getline(logfile, line)) {
        if (line.find(signal_caught_msg) != std::string::npos)
            signal_count++;
    }

    ASSERT_EQ(signal_count, 1) << "Expected one signal to show up in the log\n";

    unlink(server.GetLogFilePath().c_str());

    /* The XServer destructor tries to terminate the server and prints
       warnings if it fails */
    std::cout << "Note: below warnings about server termination failures server are false positives\n";
    server.Terminate(); /* explicitly terminate instead of destructor to
                           only get one warning */
}

class ScreenSaverTest : public XITServerInputTest,
                        public DeviceInterface {
public:
    /**
     * Initializes a standard mouse device.
     */
    virtual void SetUp() {
        SetDevice("mice/PIXART-USB-OPTICAL-MOUSE-HWHEEL.desc");
        XITServerInputTest::SetUp();

        ASSERT_TRUE(XScreenSaverQueryExtension(Display(), &screensaver_opcode, &screensaver_error_base));
    }

    /**
     * Sets up an xorg.conf for a single evdev CoreKeyboard device based on
     * the evemu device. The input from GetParam() is used as XkbLayout.
     */
    virtual void SetUpConfigAndLog() {
        config.AddDefaultScreenWithDriver();
        config.AddInputSection("evdev", "--device--",
                               "Option \"CorePointer\" \"on\"\n"
                               "Option \"GrabDevice\" \"on\"\n"
                               "Option \"Device\" \"" + dev->GetDeviceNode() + "\"");
        /* add default keyboard device to avoid server adding our device again */
        config.AddInputSection("kbd", "kbd-device",
                               "Option \"CoreKeyboard\" \"on\"\n");
        config.WriteConfig();
    }

    int screensaver_opcode;
    int screensaver_error_base;
};


/* Screen saver activation takes a while, so this test does more things to
   avoid slowing down the test suite too much */
TEST_F(ScreenSaverTest, ScreenSaverActivateDeactivate)
{
    XORG_TESTCASE("Set screen saver timeout to 1 second.\n"
                  "Register for ScreensaverNotify events.\n"
                  "Wait 1 second\n"
                  "Ensure event is received with state on.\n"
                  "Query server for screensaver info, make sure saver is on\n"
                  "Force screen saver off\n"
                  "Ensure event is received with state off\n"
                  "Query server for screensaver info, make sure saver is off\n"
                  "https://bugs.freedesktop.org/show_bug.cgi?id=56649"
                  );

    ::Display *dpy = Display();
    XScreenSaverSelectInput(dpy, DefaultRootWindow(dpy), ScreenSaverNotifyMask);
    XSetScreenSaver(dpy, 1, 0, DontPreferBlanking, DefaultExposures);
    XSync(dpy, False);

    EXPECT_TRUE(xorg::testing::XServer::WaitForEventOfType(Display(),
                                                           screensaver_opcode + ScreenSaverNotify,
                                                           -1,
                                                           -1,
                                                           2000));
    XEvent ev;
    XNextEvent(dpy, &ev);
    XScreenSaverNotifyEvent *scev = reinterpret_cast<XScreenSaverNotifyEvent*>(&ev);
    EXPECT_EQ(scev->state, ScreenSaverOn);

    XScreenSaverInfo info;
    XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), &info);
    ASSERT_EQ(info.state, ScreenSaverOn);
    EXPECT_GT(info.til_or_since, 0U);

    XResetScreenSaver(dpy);
    XSync(dpy, False);

    EXPECT_TRUE(xorg::testing::XServer::WaitForEventOfType(Display(),
                                                           screensaver_opcode + ScreenSaverNotify,
                                                           -1,
                                                           -1,
                                                           2000));
    XNextEvent(dpy, &ev);
    EXPECT_EQ(scev->state, ScreenSaverOff);

    XScreenSaverQueryInfo(dpy, DefaultRootWindow(dpy), &info);
    ASSERT_EQ(info.state, ScreenSaverOff);
    EXPECT_LE(info.til_or_since, 1000U);
    EXPECT_LT(info.idle, 1000U);
}


class XSyncTest : public XITServerInputTest {
    public:
    virtual void SetUp() {
        XITServerInputTest::SetUp();
        QuerySyncExtension(Display());
    }

    virtual void QuerySyncExtension(::Display *dpy) {
        ASSERT_TRUE(XSyncQueryExtension (dpy, &sync_event, &sync_error));
        ASSERT_TRUE(XSyncInitialize (dpy, &sync_major, &sync_minor));
    }

    virtual XSyncAlarm SetAbsoluteAlarm(::Display *dpy, XSyncCounter counter,
                                        int threshold, XSyncTestType direction,
                                        bool need_events = true) {
        XSyncAlarm alarm;
        int flags;
        XSyncAlarmAttributes attr;
        XSyncValue delta, interval;

        XSyncIntToValue(&delta, 0);
        XSyncIntToValue(&interval, threshold);

        attr.trigger.counter = counter;
        attr.trigger.test_type = direction;;
        attr.trigger.value_type = XSyncAbsolute;
        attr.trigger.wait_value = interval;
        attr.delta = delta;
        attr.events = need_events;

        flags = XSyncCACounter | XSyncCAValueType | XSyncCATestType |
                XSyncCAValue | XSyncCADelta| XSyncCAEvents;

        alarm = XSyncCreateAlarm(dpy, flags, &attr);
        XSync(dpy, False);
        return alarm;
    }

    int sync_event;
    int sync_error;
    int sync_major;
    int sync_minor;
};

class IdletimerTest : public XSyncTest,
                      public DeviceInterface {
public:
    virtual void SetUp() {
        SetDevice("mice/PIXART-USB-OPTICAL-MOUSE-HWHEEL.desc");
        XSyncTest::SetUp();
    }

    virtual void SetUpConfigAndLog() {
        config.AddDefaultScreenWithDriver();
        config.AddInputSection("evdev", "--device--",
                               "Option \"CorePointer\" \"on\"\n"
                               "Option \"GrabDevice\" \"on\"\n"
                               "Option \"Device\" \"" + dev->GetDeviceNode() + "\"");
        /* add default keyboard device to avoid server adding our device again */
        config.AddInputSection("kbd", "kbd-device",
                               "Option \"CoreKeyboard\" \"on\"\n");
        config.WriteConfig();
    }

    virtual XSyncCounter GetIdletimeCounter(::Display *dpy) {
        int ncounters;
        XSyncSystemCounter *counters;
        XSyncCounter counter = None;

        counters = XSyncListSystemCounters (dpy, &ncounters);
        for (int i = 0; i < ncounters; i++) {
            if (!strcmp(counters[i].name, "IDLETIME")) {
                counter = counters[i].counter;
            }
        }

        XSyncFreeSystemCounterList(counters);

        if (!counter)
            ADD_FAILURE() << "IDLETIME counter not found.";
        return counter;
    }

    /**
     * This function is necessary because in this test setup the server may
     * hang when we're only sending one event. Event gets processed, but
     * ProcessInputEvent() is never called so we just hang waiting for
     * something to happen and the test fails.
     * This is a server bug, but won't be triggered on a normal desktop
     * since there's always something to process (client request, timer,
     * etc.).
     */
    virtual void WaitForEvent(::Display *dpy) {
        XEvent ev;
        XSelectInput(dpy, DefaultRootWindow(dpy), PointerMotionMask);
        while (!XCheckMaskEvent(dpy, PointerMotionMask, &ev)) {
            dev->PlayOne(EV_REL, REL_X, 10, true);
            XSync(dpy, False);
            usleep(10000);
        }
    }
};

TEST_F(IdletimerTest, NegativeTransitionHighThreshold)
{
    XORG_TESTCASE("Set up an alarm on negative transition for a high threshold\n"
                  "Wait past threshold, then move pointer\n"
                  "Expect event\n"
                  "Wait past threshold, then move pointer\n"
                  "Expect event\n"
                  "https://bugs.freedesktop.org/show_bug.cgi?id=59644");

    ::Display *dpy = Display();

    /* make sure server is ready to send events */
    WaitForEvent(dpy);
    XSync(dpy, True);

    XSyncAlarm neg_alarm;
    XSyncCounter idlecounter;

    /* bug: if the negative transition threshold fires but the idletime is
       below the threshold, it is never set up again */

    const int threshold = 1000; /* ms  */

    idlecounter = GetIdletimeCounter(dpy);
    ASSERT_GT(idlecounter, (XSyncCounter)None);
    neg_alarm = SetAbsoluteAlarm(dpy, idlecounter, threshold, XSyncNegativeTransition);
    ASSERT_GT(neg_alarm, (XSyncAlarm)None);

    usleep(threshold * 1.5 * 1000);
    WaitForEvent(dpy);

    usleep(threshold * 1.5 * 1000);
    WaitForEvent(dpy);

    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev1, dpy, sync_event + XSyncAlarmNotify);
    ASSERT_EQ(ev1->alarm, neg_alarm);
    ASSERT_EQ(ev1->state, XSyncAlarmActive);

    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev2, dpy, sync_event + XSyncAlarmNotify);
    ASSERT_EQ(ev2->alarm, neg_alarm);
    ASSERT_EQ(ev1->state, XSyncAlarmActive);
}

TEST_F(IdletimerTest, NegativeTransitionLowThreshold)
{
    XORG_TESTCASE("Set up an alarm on negative transition for a low threshold\n"
                  "Wait past threshold, then move pointer\n"
                  "Expect alarm event\n"
                  "Wait past threshold, then move pointer\n"
                  "Expect alarm event\n"
                  "------------------------------------------------\n"
                  "This alarm will generate false positives, the test \n"
                  "will only fail if the server is busy and the time to\n"
                  "idle timer query takes more than <threshold> ms\n"
                  "https://bugs.freedesktop.org/show_bug.cgi?id=70476");

    ::Display *dpy = Display();

    WaitForEvent(dpy);
    XSync(dpy, True);

    XSyncAlarm neg_alarm;
    XSyncCounter idlecounter;

    /* bug: if the negative transition threshold fires but the idletime may
       move past the threshold before the handler queries it */

    const int threshold = 1; /* ms  */

    usleep(threshold * 1000);

    idlecounter = GetIdletimeCounter(dpy);
    ASSERT_GT(idlecounter, (XSyncCounter)None);
    neg_alarm = SetAbsoluteAlarm(dpy, idlecounter, threshold, XSyncNegativeTransition);
    ASSERT_GT(neg_alarm, (XSyncAlarm)None);

    usleep(threshold * 1.5 * 1000);
    WaitForEvent(dpy);

    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev1, dpy, sync_event + XSyncAlarmNotify);
    ASSERT_EQ(ev1->alarm, neg_alarm);
    ASSERT_EQ(ev1->state, XSyncAlarmActive);
}

TEST_F(IdletimerTest, PositiveTransitionLowThreshold)
{
    XORG_TESTCASE("Set up an alarm on positive transition for a low threshold\n"
                  "Wait past threshold, then move pointer\n"
                  "Expect event when idletime passes threshold\n"
                  "Wait past threshold, then move pointer\n"
                  "Expect event when idletime passes threshold\n"
                  "https://bugs.freedesktop.org/show_bug.cgi?id=70476");

    ::Display *dpy = Display();

    /* make sure server is ready to send events */
    WaitForEvent(dpy);
    XSync(dpy, True);

    XSyncAlarm pos_alarm;
    XSyncCounter idlecounter;

    /* bug: if the postive transition threshold fires but the idletime is
       already above the threshold, it is never set up again */

    const int threshold = 1; /* ms  */

    usleep(threshold * 20000);

    idlecounter = GetIdletimeCounter(dpy);
    ASSERT_GT(idlecounter, (XSyncCounter)None);
    pos_alarm = SetAbsoluteAlarm(dpy, idlecounter, threshold, XSyncPositiveTransition);
    SetAbsoluteAlarm(dpy, idlecounter, threshold + 1, XSyncNegativeTransition, false);
    ASSERT_GT(pos_alarm, (XSyncAlarm)None);

    WaitForEvent(dpy);
    usleep(threshold * 2000);

    WaitForEvent(dpy);
    usleep(threshold * 2000);

    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev1, dpy, sync_event + XSyncAlarmNotify);
    ASSERT_EQ(ev1->alarm, pos_alarm);
    ASSERT_EQ(ev1->state, XSyncAlarmActive);

    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev2, dpy, sync_event + XSyncAlarmNotify);
    ASSERT_EQ(ev2->alarm, pos_alarm);
    ASSERT_EQ(ev1->state, XSyncAlarmActive);
}

TEST_F(IdletimerTest, NegativeTransition)
{
    XORG_TESTCASE("Set up an alarm on negative transition\n"
                  "Move pointer\n"
                  "Expect event\n"
                  "Wait for timeout\n"
                  "Move pointer again\n"
                  "Expect event\n");

    ::Display *dpy = Display();

    /* make sure server is ready to send events */
    WaitForEvent(dpy);
    XSync(dpy, True);

    XSyncAlarm neg_alarm;
    XSyncCounter idlecounter;

    /* bug: if the negative transition threshold fires but the idletime is
       below the threshold, it is never set up again */
    const int threshold = 1000;
    usleep(threshold * 1001);

    idlecounter = GetIdletimeCounter(dpy);
    ASSERT_GT(idlecounter, (XSyncCounter)None);
    neg_alarm = SetAbsoluteAlarm(dpy, idlecounter, threshold, XSyncNegativeTransition);
    ASSERT_GT(neg_alarm, (XSyncAlarm)None);

    dev->PlayOne(EV_REL, REL_X, 10, true);
    WaitForEvent(dpy);
    usleep(threshold * 1001);

    dev->PlayOne(EV_REL, REL_X, 10, true);
    WaitForEvent(dpy);

    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev1, dpy, sync_event + XSyncAlarmNotify);
    ASSERT_EQ(ev1->alarm, neg_alarm);
    ASSERT_EQ(ev1->state, XSyncAlarmActive);

    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev2, dpy, sync_event + XSyncAlarmNotify);
    ASSERT_EQ(ev2->alarm, neg_alarm);
    ASSERT_EQ(ev1->state, XSyncAlarmActive);
}

TEST_F(IdletimerTest, NegativeTransitionMultipleAlarms)
{
    XORG_TESTCASE("Set up an multiple alarm on negative transitions\n"
                  "Move pointer\n"
                  "Expect event\n"
                  "Wait for timeout\n"
                  "Move pointer again\n"
                  "Expect event\n");

    ::Display *dpy = Display();

    /* make sure server is ready to send events */
    XSelectInput(dpy, DefaultRootWindow(dpy), PointerMotionMask);
    WaitForEvent(dpy);
    XSync(dpy, True);

    XSyncAlarm neg_alarm1, neg_alarm2;
    XSyncCounter idlecounter;

    /* bug: if the negative transition threshold fires but the idletime is
       below the threshold, it is never set up again */
    const int threshold = 200;
    usleep(threshold * 2000);

    idlecounter = GetIdletimeCounter(dpy);
    ASSERT_GT(idlecounter, (XSyncCounter)None);
    neg_alarm2 = SetAbsoluteAlarm(dpy, idlecounter, threshold/2, XSyncNegativeTransition);
    neg_alarm1 = SetAbsoluteAlarm(dpy, idlecounter, threshold, XSyncNegativeTransition);
    ASSERT_GT(neg_alarm1, (XSyncAlarm)None);
    ASSERT_GT(neg_alarm2, (XSyncAlarm)None);

    WaitForEvent(dpy);
    usleep(threshold * 2000);

    WaitForEvent(dpy);

    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev1, dpy, sync_event + XSyncAlarmNotify);
    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev2, dpy, sync_event + XSyncAlarmNotify);
    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev3, dpy, sync_event + XSyncAlarmNotify);
    ASSERT_EVENT(XSyncAlarmNotifyEvent, ev4, dpy, sync_event + XSyncAlarmNotify);
}

int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}