summaryrefslogtreecommitdiffstats
path: root/test/units/testsuite-73.sh
blob: 022a70862f0c1b4bbc9b38bc372d9179080a7ae0 (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
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later

set -eux
set -o pipefail

# shellcheck source=test/units/test-control.sh
. "$(dirname "$0")"/test-control.sh
# shellcheck source=test/units/util.sh
. "$(dirname "$0")"/util.sh

enable_debug() {
    mkdir -p /run/systemd/system/systemd-localed.service.d
    cat >>/run/systemd/system/systemd-localed.service.d/override.conf <<EOF
[Service]
Environment=SYSTEMD_LOG_LEVEL=debug
EOF

    mkdir -p /run/systemd/system/systemd-vconsole-setup.service.d
    cat >>/run/systemd/system/systemd-vconsole-setup.service.d/override.conf <<EOF
[Unit]
StartLimitIntervalSec=0

[Service]
Environment=SYSTEMD_LOG_LEVEL=debug
EOF

    systemctl daemon-reload
}

restore_locale() {
    if [[ -d /usr/lib/locale/xx_XX.UTF-8 ]]; then
        rmdir /usr/lib/locale/xx_XX.UTF-8
    fi

    if [[ -f /tmp/locale.conf.bak ]]; then
        mv /tmp/locale.conf.bak /etc/locale.conf
    else
        rm -f /etc/locale.conf
    fi

    if [[ -f /tmp/default-locale.bak ]]; then
        mv /tmp/default-locale.bak /etc/default/locale
    else
        rm -f /etc/default/locale
        rmdir --ignore-fail-on-non-empty /etc/default
    fi

    if [[ -f /tmp/locale.gen.bak ]]; then
        mv /tmp/locale.gen.bak /etc/locale.gen
    else
        rm -f /etc/locale.gen
    fi
}

testcase_locale() {
    local i output

    if [[ -f /etc/locale.conf ]]; then
        cp /etc/locale.conf /tmp/locale.conf.bak
    fi

    # Debian/Ubuntu specific file
    if [[ -f /etc/default/locale ]]; then
        cp /etc/default/locale /tmp/default-locale.bak
    fi

    if [[ -f /etc/locale.gen ]]; then
        cp /etc/locale.gen /tmp/locale.gen.bak
    fi

    # remove locale.conf to make /etc/default/locale used by Debian/Ubuntu
    rm -f /etc/locale.conf
    # also remove /etc/default/locale
    rm -f /etc/default/locale
    # and create /etc/default to make /etc/default/locale created by localed
    mkdir -p /etc/default

    trap restore_locale RETURN

    if command -v locale-gen >/dev/null 2>&1 &&
           ! localectl list-locales | grep -F "en_US.UTF-8"; then
        # ensure at least one utf8 locale exist
        echo "en_US.UTF-8 UTF-8" >/etc/locale.gen
        locale-gen en_US.UTF-8
    fi

    # create invalid locale
    mkdir -p /usr/lib/locale/xx_XX.UTF-8
    assert_not_in "xx_XX.UTF-8" "$(localectl list-locales)"

    if [[ -z "$(localectl list-locales)" ]]; then
        echo "No locale installed, skipping test."
        return
    fi

    # start with a known default environment and make sure to also give a
    # default value to LC_CTYPE= since we're about to also set/unset it. We
    # also reload PID1 configuration to make sure that PID1 environment itself
    # is updated as it's not always been the case.
    assert_rc 0 localectl set-locale "LANG=en_US.UTF-8" "LC_CTYPE=C"
    systemctl daemon-reload
    output=$(localectl)
    assert_in "System Locale: LANG=en_US.UTF-8" "$output"
    assert_in "LC_CTYPE=C" "$output"
    output=$(systemctl show-environment)
    assert_in "LANG=en_US.UTF-8" "$output"
    assert_in "LC_CTYPE=C" "$output"

    # warn when kernel command line has locale settings
    output=$(SYSTEMD_PROC_CMDLINE="locale.LANG=C.UTF-8 locale.LC_CTYPE=ja_JP.UTF-8" localectl 2>&1)
    assert_in "Warning:" "$output"
    assert_in "Command Line: LANG=C.UTF-8" "$output"
    assert_in "LC_CTYPE=ja_JP.UTF-8" "$output"
    assert_in "System Locale:" "$output"

    # change locale
    for i in $(localectl list-locales); do
        assert_rc 0 localectl set-locale "LANG=C" "LC_CTYPE=$i"
        if [[ -f /etc/default/locale ]]; then
            assert_eq "$(cat /etc/default/locale)" "LANG=C
LC_CTYPE=$i"
        else
            assert_eq "$(cat /etc/locale.conf)" "LANG=C
LC_CTYPE=$i"
        fi
        output=$(localectl)
        assert_in "System Locale: LANG=C" "$output"
        assert_in "LC_CTYPE=$i" "$output"
        output=$(systemctl show-environment)
        assert_in "LANG=C" "$output"
        assert_in "LC_CTYPE=$i" "$output"

        assert_rc 0 localectl set-locale "$i"
        if [[ -f /etc/default/locale ]]; then
            assert_eq "$(cat /etc/default/locale)" "LANG=$i"
        else
            assert_eq "$(cat /etc/locale.conf)" "LANG=$i"
        fi
        output=$(localectl)
        assert_in "System Locale: LANG=$i" "$output"
        assert_not_in "LC_CTYPE=" "$output"
        output=$(systemctl show-environment)
        assert_in "LANG=$i" "$output"
        assert_not_in "LC_CTYPE=" "$output"
    done

    # test if localed auto-runs locale-gen
    if command -v locale-gen >/dev/null 2>&1 &&
           ! localectl list-locales | grep -F "de_DE.UTF-8"; then

        # clear previous locale
        systemctl stop systemd-localed.service
        rm -f /etc/locale.conf /etc/default/locale

        # change locale
        assert_rc 0 localectl set-locale de_DE.UTF-8
        if [[ -f /etc/default/locale ]]; then
            assert_eq "$(cat /etc/default/locale)" "LANG=de_DE.UTF-8"
        else
            assert_eq "$(cat /etc/locale.conf)" "LANG=de_DE.UTF-8"
        fi
        assert_in "System Locale: LANG=de_DE.UTF-8" "$(localectl)"
        assert_in "LANG=de_DE.UTF-8" "$(systemctl show-environment)"

        # ensure tested locale exists and works now
        assert_in "de_DE.UTF-8" "$(localectl list-locales)"
    fi
}

backup_keymap() {
    if [[ -f /etc/vconsole.conf ]]; then
        cp /etc/vconsole.conf /tmp/vconsole.conf.bak
    fi

    if [[ -f /etc/X11/xorg.conf.d/00-keyboard.conf ]]; then
        cp /etc/X11/xorg.conf.d/00-keyboard.conf /tmp/00-keyboard.conf.bak
    fi

    # Debian/Ubuntu specific file
    if [[ -f /etc/default/keyboard ]]; then
        cp /etc/default/keyboard /tmp/default-keyboard.bak
    fi

    mkdir -p /etc/default
}

restore_keymap() {
    if [[ -f /tmp/vconsole.conf.bak ]]; then
        mv /tmp/vconsole.conf.bak /etc/vconsole.conf
    else
        rm -f /etc/vconsole.conf
    fi

    if [[ -f /tmp/00-keyboard.conf.bak ]]; then
        mv /tmp/00-keyboard.conf.bak /etc/X11/xorg.conf.d/00-keyboard.conf
    else
        rm -f /etc/X11/xorg.conf.d/00-keyboard.conf
    fi

    if [[ -f /tmp/default-keyboard.bak ]]; then
        mv /tmp/default-keyboard.bak /etc/default/keyboard
    else
        rm -f /etc/default/keyboard
        rmdir --ignore-fail-on-non-empty /etc/default
    fi
}

wait_vconsole_setup() {
    local i ss
    for i in {1..20}; do
        (( i > 1 )) && sleep 0.5
        ss="$(systemctl --property SubState --value show systemd-vconsole-setup.service)"
        if [[ "$ss" == "exited" || "$ss" == "dead" || "$ss" == "condition" ]]; then
            return 0
        elif [[ "$ss" == "failed" ]]; then
            echo "WARNING: systemd-vconsole-setup.service failed, ignoring." >&2
            systemctl reset-failed systemd-vconsole-setup.service
            return 0
        fi
    done

    systemctl status systemd-vconsole-setup.service
    return 1
}

testcase_vc_keymap() {
    local i output vc

    if [[ -z "$(localectl list-keymaps)" ]]; then
        echo "No vconsole keymap installed, skipping test."
        return
    fi

    backup_keymap
    trap restore_keymap RETURN

    # should activate daemon and work
    assert_in "VC Keymap:" "$(localectl)"

    for i in $(localectl list-keymaps); do
        # clear previous conversion from VC -> X11 keymap
        systemctl stop systemd-localed.service
        wait_vconsole_setup
        rm -f /etc/vconsole.conf /etc/X11/xorg.conf.d/00-keyboard.conf /etc/default/keyboard

        # set VC keymap
        assert_rc 0 localectl set-keymap "$i"
        output=$(localectl)

        # check VC keymap
        vc=$(cat /etc/vconsole.conf)
        assert_in "KEYMAP=$i" "$vc"
        assert_in "VC Keymap: $i" "$output"

        # check VC -> X11 keymap conversion
        if [[ "$i" == "us" ]]; then
            assert_in "X11 Layout: us" "$output"
            assert_in "X11 Model: pc105\+inet" "$output"
            assert_not_in "X11 Variant:" "$output"
            assert_in "X11 Options: terminate:ctrl_alt_bksp" "$output"

            assert_in "XKBLAYOUT=us" "$vc"
            assert_in "XKBMODEL=pc105\+inet" "$vc"
            assert_not_in "XKBVARIANT" "$vc"
            assert_in "XKBOPTIONS=terminate:ctrl_alt_bksp" "$vc"
        elif [[ "$i" == "us-acentos" ]]; then
            assert_in "X11 Layout: us" "$output"
            assert_in "X11 Model: pc105" "$output"
            assert_in "X11 Variant: intl" "$output"
            assert_in "X11 Options: terminate:ctrl_alt_bksp" "$output"

            assert_in "XKBLAYOUT=us" "$vc"
            assert_in "XKBMODEL=pc105" "$vc"
            assert_in "XKBVARIANT=intl" "$vc"
            assert_in "XKBOPTIONS=terminate:ctrl_alt_bksp" "$vc"
        elif [[ "$i" =~ ^us-.* ]]; then
            assert_in "X11 Layout: us" "$output"
            assert_in "X11 Model: microsoftpro" "$output"
            assert_in "X11 Variant:" "$output"
            assert_in "X11 Options: terminate:ctrl_alt_bksp" "$output"

            assert_in "XKBLAYOUT=us" "$vc"
            assert_in "XKBMODEL=microsoftpro" "$vc"
            assert_in "XKBVARIANT=" "$vc"
            assert_in "XKBOPTIONS=terminate:ctrl_alt_bksp" "$vc"
        fi
    done

    # gets along without config file
    systemctl stop systemd-localed.service
    wait_vconsole_setup
    rm -f /etc/vconsole.conf
    assert_in "VC Keymap: .unset." "$(localectl)"
}

testcase_x11_keymap() {
    local output

    if [[ -z "$(localectl list-x11-keymap-layouts)" ]]; then
        echo "No x11 keymap installed, skipping test."
        return
    fi

    backup_keymap
    trap restore_keymap RETURN

    # should activate daemon and work
    assert_in "X11 Layout:" "$(localectl)"

    # set x11 keymap (layout, model, variant, options)
    assert_rc 0 localectl set-x11-keymap us pc105+inet intl terminate:ctrl_alt_bksp

    if [[ -f /etc/default/keyboard ]]; then
        assert_eq "$(cat /etc/default/keyboard)" "XKBLAYOUT=us
XKBMODEL=pc105+inet
XKBVARIANT=intl
XKBOPTIONS=terminate:ctrl_alt_bksp"
    else
        output=$(cat /etc/X11/xorg.conf.d/00-keyboard.conf)
        assert_in 'Option "XkbLayout" "us"' "$output"
        assert_in 'Option "XkbModel" "pc105\+inet"' "$output"
        assert_in 'Option "XkbVariant" "intl"' "$output"
        assert_in 'Option "XkbOptions" "terminate:ctrl_alt_bksp"' "$output"

        output=$(cat /etc/vconsole.conf)
        assert_in 'XKBLAYOUT=us' "$output"
        assert_in 'XKBMODEL=pc105\+inet' "$output"
        assert_in 'XKBVARIANT=intl' "$output"
        assert_in 'XKBOPTIONS=terminate:ctrl_alt_bksp' "$output"
    fi

    output=$(localectl)
    assert_in "X11 Layout: us" "$output"
    assert_in "X11 Model: pc105\+inet" "$output"
    assert_in "X11 Variant: intl" "$output"
    assert_in "X11 Options: terminate:ctrl_alt_bksp" "$output"

    # Debian/Ubuntu patch is buggy, unspecified settings are not cleared
    rm -f /etc/default/keyboard

    # set x11 keymap (layout, model, variant)
    assert_rc 0 localectl set-x11-keymap us pc105+inet intl

    if [[ -f /etc/default/keyboard ]]; then
        assert_eq "$(cat /etc/default/keyboard)" "XKBLAYOUT=us
XKBMODEL=pc105+inet
XKBVARIANT=intl"
    else
        output=$(cat /etc/X11/xorg.conf.d/00-keyboard.conf)
        assert_in 'Option "XkbLayout" "us"' "$output"
        assert_in 'Option "XkbModel" "pc105\+inet"' "$output"
        assert_in 'Option "XkbVariant" "intl"' "$output"
        assert_not_in 'Option "XkbOptions"' "$output"

        output=$(cat /etc/vconsole.conf)
        assert_in 'XKBLAYOUT=us' "$output"
        assert_in 'XKBMODEL=pc105\+inet' "$output"
        assert_in 'XKBVARIANT=intl' "$output"
        assert_not_in 'XKBOPTIONS' "$output"
    fi

    output=$(localectl)
    assert_in "X11 Layout: us" "$output"
    assert_in "X11 Model: pc105\+inet" "$output"
    assert_in "X11 Variant: intl" "$output"
    assert_not_in "X11 Options:" "$output"

    # Debian/Ubuntu patch is buggy, unspecified settings are not cleared
    rm -f /etc/default/keyboard

    # set x11 keymap (layout, model)
    assert_rc 0 localectl set-x11-keymap us pc105+inet

    if [[ -f /etc/default/keyboard ]]; then
        assert_eq "$(cat /etc/default/keyboard)" "XKBLAYOUT=us
XKBMODEL=pc105+inet"
    else
        output=$(cat /etc/X11/xorg.conf.d/00-keyboard.conf)
        assert_in 'Option "XkbLayout" "us"' "$output"
        assert_in 'Option "XkbModel" "pc105\+inet"' "$output"
        assert_not_in 'Option "XkbVariant"' "$output"
        assert_not_in 'Option "XkbOptions"' "$output"

        output=$(cat /etc/vconsole.conf)
        assert_in 'XKBLAYOUT=us' "$output"
        assert_in 'XKBMODEL=pc105\+inet' "$output"
        assert_not_in 'XKBVARIANT' "$output"
        assert_not_in 'XKBOPTIONS' "$output"
    fi

    output=$(localectl)
    assert_in "X11 Layout: us" "$output"
    assert_in "X11 Model: pc105\+inet" "$output"
    assert_not_in "X11 Variant:" "$output"
    assert_not_in "X11 Options:" "$output"

    # Debian/Ubuntu patch is buggy, unspecified settings are not cleared
    rm -f /etc/default/keyboard

    # set x11 keymap (layout)
    assert_rc 0 localectl set-x11-keymap us

    if [[ -f /etc/default/keyboard ]]; then
        assert_eq "$(cat /etc/default/keyboard)" "XKBLAYOUT=us"
    else
        output=$(cat /etc/X11/xorg.conf.d/00-keyboard.conf)
        assert_in 'Option "XkbLayout" "us"' "$output"
        assert_not_in 'Option "XkbModel"' "$output"
        assert_not_in 'Option "XkbVariant"' "$output"
        assert_not_in 'Option "XkbOptions"' "$output"

        output=$(cat /etc/vconsole.conf)
        assert_in 'XKBLAYOUT=us' "$output"
        assert_not_in 'XKBMODEL' "$output"
        assert_not_in 'XKBVARIANT' "$output"
        assert_not_in 'XKBOPTIONS' "$output"
    fi

    output=$(localectl)
    assert_in "X11 Layout: us" "$output"
    assert_not_in "X11 Model:" "$output"
    assert_not_in "X11 Variant:" "$output"
    assert_not_in "X11 Options:" "$output"

    # gets along without config file
    systemctl stop systemd-localed.service
    rm -f /etc/vconsole.conf /etc/X11/xorg.conf.d/00-keyboard.conf /etc/default/keyboard
    output=$(localectl)
    assert_in "X11 Layout: .unset." "$output"
    assert_not_in "X11 Model:" "$output"
    assert_not_in "X11 Variant:" "$output"
    assert_not_in "X11 Options:" "$output"
}

testcase_convert() {
    if [[ -z "$(localectl list-keymaps)" ]]; then
        echo "No vconsole keymap installed, skipping test."
        return
    fi

    if [[ -z "$(localectl list-x11-keymap-layouts)" ]]; then
        echo "No x11 keymap installed, skipping test."
        return
    fi

    backup_keymap
    trap restore_keymap RETURN

    # clear previous settings
    systemctl stop systemd-localed.service
    wait_vconsole_setup
    rm -f /etc/vconsole.conf /etc/X11/xorg.conf.d/00-keyboard.conf /etc/default/keyboard

    # set VC keymap without conversion
    assert_rc 0 localectl --no-convert set-keymap us
    output=$(localectl)

    # check VC keymap
    vc=$(cat /etc/vconsole.conf)
    assert_in "KEYMAP=us" "$vc"
    assert_in "VC Keymap: us" "$output"

    # check VC -> X11 keymap conversion (nothing set)
    assert_in     "X11 Layout: .unset."  "$output"
    assert_not_in "X11 Model:"           "$output"
    assert_not_in "X11 Variant:"         "$output"
    assert_not_in "X11 Options:"         "$output"

    assert_not_in "XKBLAYOUT="  "$vc"
    assert_not_in "XKBMODEL="   "$vc"
    assert_not_in "XKBVARIANT=" "$vc"
    assert_not_in "XKBOPTIONS=" "$vc"

    # set VC keymap with conversion
    assert_rc 0 localectl set-keymap us
    output=$(localectl)

    # check VC keymap
    vc=$(cat /etc/vconsole.conf)
    assert_in "KEYMAP=us" "$vc"
    assert_in "VC Keymap: us" "$output"

    # check VC -> X11 keymap conversion
    assert_in     "X11 Layout: us"                       "$output"
    assert_in     "X11 Model: pc105\+inet"               "$output"
    assert_not_in "X11 Variant:"                         "$output"
    assert_in     "X11 Options: terminate:ctrl_alt_bksp" "$output"

    assert_in     "XKBLAYOUT=us"                       "$vc"
    assert_in     "XKBMODEL=pc105\+inet"               "$vc"
    assert_not_in "XKBVARIANT"                         "$vc"
    assert_in     "XKBOPTIONS=terminate:ctrl_alt_bksp" "$vc"

    # clear previous settings
    systemctl stop systemd-localed.service
    wait_vconsole_setup
    rm -f /etc/vconsole.conf /etc/X11/xorg.conf.d/00-keyboard.conf /etc/default/keyboard

    # set x11 keymap (layout) without conversion
    assert_rc 0 localectl --no-convert set-x11-keymap us

    assert_not_in "KEYMAP=" "$(cat /etc/vconsole.conf)"
    assert_in "VC Keymap: .unset." "$(localectl)"

    if [[ -f /etc/default/keyboard ]]; then
        assert_eq "$(cat /etc/default/keyboard)" "XKBLAYOUT=us"
    else
        output=$(cat /etc/X11/xorg.conf.d/00-keyboard.conf)
        assert_in     'Option "XkbLayout" "us"' "$output"
        assert_not_in 'Option "XkbModel"'       "$output"
        assert_not_in 'Option "XkbVariant"'     "$output"
        assert_not_in 'Option "XkbOptions"'     "$output"

        output=$(cat /etc/vconsole.conf)
        assert_in     'XKBLAYOUT=us' "$output"
        assert_not_in 'XKBMODEL='    "$output"
        assert_not_in 'XKBVARIANT='  "$output"
        assert_not_in 'XKBOPTIONS='  "$output"
    fi

    output=$(localectl)
    assert_in     "X11 Layout: us" "$output"
    assert_not_in "X11 Model:"     "$output"
    assert_not_in "X11 Variant:"   "$output"
    assert_not_in "X11 Options:"   "$output"

    # set x11 keymap (layout, model) with conversion
    assert_rc 0 localectl set-x11-keymap us

    assert_in "KEYMAP=us" "$(cat /etc/vconsole.conf)"
    assert_in "VC Keymap: us" "$(localectl)"

    if [[ -f /etc/default/keyboard ]]; then
        assert_eq "$(cat /etc/default/keyboard)" "XKBLAYOUT=us"
    else
        output=$(cat /etc/X11/xorg.conf.d/00-keyboard.conf)
        assert_in     'Option "XkbLayout" "us"' "$output"
        assert_not_in 'Option "XkbModel"'       "$output"
        assert_not_in 'Option "XkbVariant"'     "$output"
        assert_not_in 'Option "XkbOptions"'     "$output"

        output=$(cat /etc/vconsole.conf)
        assert_in     'XKBLAYOUT=us' "$output"
        assert_not_in 'XKBMODEL='    "$output"
        assert_not_in 'XKBVARIANT='  "$output"
        assert_not_in 'XKBOPTIONS='  "$output"
    fi

    output=$(localectl)
    assert_in     "X11 Layout: us" "$output"
    assert_not_in "X11 Model:"     "$output"
    assert_not_in "X11 Variant:"   "$output"
    assert_not_in "X11 Options:"   "$output"
}

testcase_validate() {
    if [[ -z "$(localectl list-keymaps)" ]]; then
        echo "No vconsole keymap installed, skipping test."
        return
    fi

    if [[ -z "$(localectl list-x11-keymap-layouts)" ]]; then
        echo "No x11 keymap installed, skipping test."
        return
    fi

    backup_keymap
    trap restore_keymap RETURN

    # clear previous settings
    systemctl stop systemd-localed.service
    wait_vconsole_setup
    rm -f /etc/X11/xorg.conf.d/00-keyboard.conf /etc/default/keyboard

    # create invalid configs
    cat >/etc/vconsole.conf <<EOF
KEYMAP=foobar
XKBLAYOUT=hogehoge
EOF

    # confirm that the invalid settings are not shown
    output=$(localectl)
    assert_in "VC Keymap: .unset."  "$output"
    if [[ "$output" =~ "X11 Layout: hogehoge" ]]; then
        # Debian/Ubuntu build systemd without xkbcommon.
        echo "systemd built without xkbcommon, skipping test."
        return
    fi
    assert_in "X11 Layout: .unset." "$output"

    # only update the virtual console keymap
    assert_rc 0 localectl --no-convert set-keymap us

    output=$(localectl)
    assert_in "VC Keymap: us"       "$output"
    assert_in "X11 Layout: .unset." "$output"

    output=$(cat /etc/vconsole.conf)
    assert_in     "KEYMAP=us"  "$output"
    assert_not_in "XKBLAYOUT=" "$output"

    # clear previous settings
    systemctl stop systemd-localed.service
    wait_vconsole_setup
    rm -f /etc/X11/xorg.conf.d/00-keyboard.conf /etc/default/keyboard

    # create invalid configs
    cat >/etc/vconsole.conf <<EOF
KEYMAP=foobar
XKBLAYOUT=hogehoge
EOF

    # confirm that the invalid settings are not shown
    output=$(localectl)
    assert_in "VC Keymap: .unset."  "$output"
    assert_in "X11 Layout: .unset." "$output"

    # only update the X11 keyboard layout
    assert_rc 0 localectl --no-convert set-x11-keymap us

    output=$(localectl)
    assert_in "VC Keymap: .unset."  "$output"
    assert_in "X11 Layout: us"      "$output"

    output=$(cat /etc/vconsole.conf)
    assert_not_in "KEYMAP="      "$output"
    assert_in     "XKBLAYOUT=us" "$output"

    # clear previous settings
    systemctl stop systemd-localed.service
    wait_vconsole_setup
    rm -f /etc/X11/xorg.conf.d/00-keyboard.conf /etc/default/keyboard

    # create invalid configs
    cat >/etc/vconsole.conf <<EOF
KEYMAP=foobar
XKBLAYOUT=hogehoge
EOF

    # update the virtual console keymap with conversion
    assert_rc 0 localectl set-keymap us

    output=$(localectl)
    assert_in "VC Keymap: us"  "$output"
    assert_in "X11 Layout: us" "$output"

    output=$(cat /etc/vconsole.conf)
    assert_in "KEYMAP=us"    "$output"
    assert_in "XKBLAYOUT=us" "$output"
}

locale_gen_cleanup() {
    # Some running apps might keep the mount point busy, hence the lazy unmount
    mountpoint -q /usr/lib/locale && umount --lazy /usr/lib/locale
    [[ -e /tmp/locale.gen.bak ]] && mv -f /tmp/locale.gen.bak /etc/locale.gen

    return 0
}

# Issue: https://github.com/systemd/systemd/pull/27179
testcase_locale_gen_leading_space() {
    if ! command -v locale-gen >/dev/null; then
        echo "No locale-gen support, skipping test."
        return 0
    fi

    [[ -e /etc/locale.gen ]] && cp -f /etc/locale.gen /tmp/locale.gen.bak
    trap locale_gen_cleanup RETURN
    # Overmount the existing locale-gen database with an empty directory
    # to force it to regenerate locales
    mount -t tmpfs tmpfs /usr/lib/locale

    {
        echo -e "en_US.UTF-8 UTF-8"
        echo -e " en_US.UTF-8 UTF-8"
        echo -e "\ten_US.UTF-8 UTF-8"
        echo -e " \t en_US.UTF-8 UTF-8 \t"
    } >/etc/locale.gen

    localectl set-locale de_DE.UTF-8
    localectl set-locale en_US.UTF-8
}

: >/failed

# Make sure the content of kbd-model-map is the one that the tests expect
# regardless of the version installed on the distro where the testsuite is
# running on.
export SYSTEMD_KBD_MODEL_MAP=/usr/lib/systemd/tests/testdata/test-keymap-util/kbd-model-map

enable_debug
run_testcases

touch /testok
rm /failed