git.iko.soy/typeable/nixpkgs
on master help sign in
nixosTests.chromium: Port to Python
commit2e7d406b11d48832e88d3aefd81e84e1cf512dbd authorJacek Galowicz <jacek.galowicz@cyberus-technology.de> dateThu 6 Feb 2020, 14:50 (7 years ago) parent1013c2e
1 file changed+216−151
nixos/tests/chromium.nix+216−151
nixos/tests/chromium.nix+216−151
@@ -8,7 +8,7 @@
88 }
99}:
1010
11−with import ../lib/testing.nix { inherit system pkgs; };
11+with import ../lib/testing-python.nix { inherit system pkgs; };
1212with pkgs.lib;
1313
1414mapAttrs (channel: chromiumPkg: makeTest rec {
@@ -21,9 +21,11 @@ mapAttrs (channel: chromiumPkg: makeTest rec {
2121
2222 enableOCR = true;
2323
24+ user = "alice";
25+
2426 machine.imports = [ ./common/user-account.nix ./common/x11.nix ];
2527 machine.virtualisation.memorySize = 2047;
26− machine.test-support.displayManager.auto.user = "alice";
28+ machine.test-support.displayManager.auto.user = user;
2729 machine.environment.systemPackages = [ chromiumPkg ];
2830
2931 startupHTML = pkgs.writeText "chromium-startup.html" ''
@@ -47,155 +49,218 @@ mapAttrs (channel: chromiumPkg: makeTest rec {
4749 xdoScript = pkgs.writeText "${name}.xdo" text;
4850 in "${pkgs.xdotool}/bin/xdotool '${xdoScript}'";
4951 in ''
52+ import shlex
53+ from contextlib import contextmanager, _GeneratorContextManager
54+
55+
5056 # Run as user alice
51− sub ru ($) {
52− my $esc = $_[0] =~ s/'/'\\${"'"}'/gr;
53− return "su - alice -c '$esc'";
54− }
55−
56− sub createNewWin {
57− $machine->nest("creating a new Chromium window", sub {
58− $machine->execute(ru "${xdo "new-window" ''
59− search --onlyvisible --name "startup done"
60− windowfocus --sync
61− windowactivate --sync
62− ''}");
63− $machine->execute(ru "${xdo "new-window" ''
64− key Ctrl+n
65− ''}");
66− });
67− }
68−
69− sub closeWin {
70− Machine::retry sub {
71− $machine->execute(ru "${xdo "close-window" ''
72− search --onlyvisible --name "new tab"
73− windowfocus --sync
74− windowactivate --sync
75− ''}");
76− $machine->execute(ru "${xdo "close-window" ''
77− key Ctrl+w
78− ''}");
79− for (1..20) {
80− my ($status, $out) = $machine->execute(ru "${xdo "wait-for-close" ''
81− search --onlyvisible --name "new tab"
82− ''}");
83− return 1 if $status != 0;
84− $machine->sleep(1);
85− }
86− }
87− }
88−
89− sub waitForNewWin {
90− my $ret = 0;
91− $machine->nest("waiting for new Chromium window to appear", sub {
92− for (1..20) {
93− my ($status, $out) = $machine->execute(ru "${xdo "wait-for-window" ''
94− search --onlyvisible --name "new tab"
95− windowfocus --sync
96− windowactivate --sync
97− ''}");
98− if ($status == 0) {
99− $ret = 1;
100−
101− # XXX: Somehow Chromium is not accepting keystrokes for a few
102− # seconds after a new window has appeared, so let's wait a while.
103− $machine->sleep(10);
104−
105− last;
106− }
107− $machine->sleep(1);
108− }
109− });
110− return $ret;
111− }
112−
113− sub createAndWaitForNewWin {
114− for (1..3) {
115− createNewWin;
116− return 1 if waitForNewWin;
117− }
118− die "new window didn't appear within 60 seconds";
119− }
120−
121− sub testNewWin {
122− my ($desc, $code) = @_;
123− createAndWaitForNewWin;
124− subtest($desc, $code);
125− closeWin;
126− }
127−
128− $machine->waitForX;
129−
130− my $url = "file://${startupHTML}";
131− $machine->execute(ru "ulimit -c unlimited; chromium \"$url\" & disown");
132− $machine->waitForText(qr/startup done/);
133− $machine->waitUntilSucceeds(ru "${xdo "check-startup" ''
134− search --sync --onlyvisible --name "startup done"
135− # close first start help popup
136− key -delay 1000 Escape
137− windowfocus --sync
138− windowactivate --sync
139− ''}");
140−
141− createAndWaitForNewWin;
142− $machine->screenshot("empty_windows");
143− closeWin;
144−
145− $machine->screenshot("startup_done");
146−
147− testNewWin "check sandbox", sub {
148− $machine->succeed(ru "${xdo "type-url" ''
149− search --sync --onlyvisible --name "new tab"
150− windowfocus --sync
151− type --delay 1000 "chrome://sandbox"
152− ''}");
153−
154− $machine->succeed(ru "${xdo "submit-url" ''
155− search --sync --onlyvisible --name "new tab"
156− windowfocus --sync
157− key --delay 1000 Return
158− ''}");
159−
160− $machine->screenshot("sandbox_info");
161−
162− $machine->succeed(ru "${xdo "find-window" ''
163− search --sync --onlyvisible --name "sandbox status"
164− windowfocus --sync
165− ''}");
166− $machine->succeed(ru "${xdo "copy-sandbox-info" ''
167− key --delay 1000 Ctrl+a Ctrl+c
168− ''}");
169−
170− my $clipboard = $machine->succeed(ru "${pkgs.xclip}/bin/xclip -o");
171− die "sandbox not working properly: $clipboard"
172− unless $clipboard =~ /layer 1 sandbox.*namespace/mi
173− && $clipboard =~ /pid namespaces.*yes/mi
174− && $clipboard =~ /network namespaces.*yes/mi
175− && $clipboard =~ /seccomp.*sandbox.*yes/mi
176− && $clipboard =~ /you are adequately sandboxed/mi;
177−
178− $machine->sleep(1);
179− $machine->succeed(ru "${xdo "find-window-after-copy" ''
180− search --onlyvisible --name "sandbox status"
181− ''}");
182−
183− my $clipboard = $machine->succeed(ru "echo void | ${pkgs.xclip}/bin/xclip -i");
184− $machine->succeed(ru "${xdo "copy-sandbox-info" ''
185− key --delay 1000 Ctrl+a Ctrl+c
186− ''}");
187−
188− my $clipboard = $machine->succeed(ru "${pkgs.xclip}/bin/xclip -o");
189− die "copying twice in a row does not work properly: $clipboard"
190− unless $clipboard =~ /layer 1 sandbox.*namespace/mi
191− && $clipboard =~ /pid namespaces.*yes/mi
192− && $clipboard =~ /network namespaces.*yes/mi
193− && $clipboard =~ /seccomp.*sandbox.*yes/mi
194− && $clipboard =~ /you are adequately sandboxed/mi;
195−
196− $machine->screenshot("afer_copy_from_chromium");
197− };
198−
199− $machine->shutdown;
57+ def ru(cmd):
58+ return "su - ${user} -c " + shlex.quote(cmd)
59+
60+
61+ def create_new_win():
62+ with machine.nested("Creating a new Chromium window"):
63+ machine.execute(
64+ ru(
65+ "${xdo "new-window" ''
66+ search --onlyvisible --name "startup done"
67+ windowfocus --sync
68+ windowactivate --sync
69+ ''}"
70+ )
71+ )
72+ machine.execute(
73+ ru(
74+ "${xdo "new-window" ''
75+ key Ctrl+n
76+ ''}"
77+ )
78+ )
79+
80+
81+ def close_win():
82+ def try_close(_):
83+ machine.execute(
84+ ru(
85+ "${xdo "close-window" ''
86+ search --onlyvisible --name "new tab"
87+ windowfocus --sync
88+ windowactivate --sync
89+ ''}"
90+ )
91+ )
92+ machine.execute(
93+ ru(
94+ "${xdo "close-window" ''
95+ key Ctrl+w
96+ ''}"
97+ )
98+ )
99+ for _ in range(1, 20):
100+ status, out = machine.execute(
101+ ru(
102+ "${xdo "wait-for-close" ''
103+ search --onlyvisible --name "new tab"
104+ ''}"
105+ )
106+ )
107+ if status != 0:
108+ return True
109+ machine.sleep(1)
110+ return False
111+
112+ retry(try_close)
113+
114+
115+ def wait_for_new_win():
116+ ret = False
117+ with machine.nested("Waiting for new Chromium window to appear"):
118+ for _ in range(1, 20):
119+ status, out = machine.execute(
120+ ru(
121+ "${xdo "wait-for-window" ''
122+ search --onlyvisible --name "new tab"
123+ windowfocus --sync
124+ windowactivate --sync
125+ ''}"
126+ )
127+ )
128+ if status == 0:
129+ ret = True
130+ machine.sleep(10)
131+ break
132+ machine.sleep(1)
133+ return ret
134+
135+
136+ def create_and_wait_for_new_win():
137+ for _ in range(1, 3):
138+ create_new_win()
139+ if wait_for_new_win():
140+ return True
141+ assert False, "new window did not appear within 60 seconds"
142+
143+
144+ @contextmanager
145+ def test_new_win(description):
146+ create_and_wait_for_new_win()
147+ with machine.nested(description):
148+ yield
149+ close_win()
150+
151+
152+ machine.wait_for_x()
153+
154+ url = "file://${startupHTML}"
155+ machine.succeed(ru(f'ulimit -c unlimited; chromium "{url}" & disown'))
156+ machine.wait_for_text("startup done")
157+ machine.wait_until_succeeds(
158+ ru(
159+ "${xdo "check-startup" ''
160+ search --sync --onlyvisible --name "startup done"
161+ # close first start help popup
162+ key -delay 1000 Escape
163+ windowfocus --sync
164+ windowactivate --sync
165+ ''}"
166+ )
167+ )
168+
169+ create_and_wait_for_new_win()
170+ machine.screenshot("empty_windows")
171+ close_win()
172+
173+ machine.screenshot("startup_done")
174+
175+ with test_new_win("check sandbox"):
176+ machine.succeed(
177+ ru(
178+ "${xdo "type-url" ''
179+ search --sync --onlyvisible --name "new tab"
180+ windowfocus --sync
181+ type --delay 1000 "chrome://sandbox"
182+ ''}"
183+ )
184+ )
185+
186+ machine.succeed(
187+ ru(
188+ "${xdo "submit-url" ''
189+ search --sync --onlyvisible --name "new tab"
190+ windowfocus --sync
191+ key --delay 1000 Return
192+ ''}"
193+ )
194+ )
195+
196+ machine.screenshot("sandbox_info")
197+
198+ machine.succeed(
199+ ru(
200+ "${xdo "find-window" ''
201+ search --sync --onlyvisible --name "sandbox status"
202+ windowfocus --sync
203+ ''}"
204+ )
205+ )
206+ machine.succeed(
207+ ru(
208+ "${xdo "copy-sandbox-info" ''
209+ key --delay 1000 Ctrl+a Ctrl+c
210+ ''}"
211+ )
212+ )
213+
214+ clipboard = machine.succeed(
215+ ru("${pkgs.xclip}/bin/xclip -o")
216+ )
217+
218+ filters = [
219+ "layer 1 sandbox.*namespace",
220+ "pid namespaces.*yes",
221+ "network namespaces.*yes",
222+ "seccomp.*sandbox.*yes",
223+ "you are adequately sandboxed",
224+ ]
225+ if not all(
226+ re.search(filter, clipboard, flags=re.DOTALL | re.IGNORECASE)
227+ for filter in filters
228+ ):
229+ assert False, f"sandbox not working properly: {clipboard}"
230+
231+ machine.sleep(1)
232+ machine.succeed(
233+ ru(
234+ "${xdo "find-window-after-copy" ''
235+ search --onlyvisible --name "sandbox status"
236+ ''}"
237+ )
238+ )
239+
240+ clipboard = machine.succeed(
241+ ru(
242+ "echo void | ${pkgs.xclip}/bin/xclip -i"
243+ )
244+ )
245+ machine.succeed(
246+ ru(
247+ "${xdo "copy-sandbox-info" ''
248+ key --delay 1000 Ctrl+a Ctrl+c
249+ ''}"
250+ )
251+ )
252+
253+ clipboard = machine.succeed(
254+ ru("${pkgs.xclip}/bin/xclip -o")
255+ )
256+ if not all(
257+ re.search(filter, clipboard, flags=re.DOTALL | re.IGNORECASE)
258+ for filter in filters
259+ ):
260+ assert False, f"copying twice in a row does not work properly: {clipboard}"
261+
262+ machine.screenshot("after_copy_from_chromium")
263+
264+ machine.shutdown()
200265 '';
201266}) channelMap