OLD | NEW |
| (Empty) |
1 # Copyright (c) 2011 The Native Client Authors. All rights reserved. | |
2 # Use of this source code is governed by a BSD-style license that can be | |
3 # found in the LICENSE file. | |
4 | |
5 import os | |
6 import struct | |
7 import subprocess | |
8 import tempfile | |
9 import unittest | |
10 | |
11 import naclimc | |
12 import nacl_launcher | |
13 | |
14 | |
15 def MakeFileHandlePair(): | |
16 # Make read/write file handle pair. This is like creating a pipe | |
17 # FD pair, but without a pipe buffer limit. | |
18 fd, filename = tempfile.mkstemp(prefix="shell_test_") | |
19 try: | |
20 write_fh = os.fdopen(fd, "w", 0) | |
21 read_fh = open(filename, "r") | |
22 finally: | |
23 os.unlink(filename) | |
24 return write_fh, read_fh | |
25 | |
26 | |
27 class TestStartupMessage(unittest.TestCase): | |
28 | |
29 def assertIn(self, x, y): | |
30 if x not in y: | |
31 raise AssertionError("%r not in %r" % (x, y)) | |
32 | |
33 def test_args_message(self): | |
34 lib_dir = os.environ["NACL_LIBRARY_DIR"] | |
35 write_fh, read_fh = MakeFileHandlePair() | |
36 proc, [fd1, fd2] = nacl_launcher.SpawnSelLdrWithSockets( | |
37 ["-s", "-a", | |
38 "--", os.path.join(lib_dir, "runnable-ld.so"), | |
39 # The remaining arguments are not used, because they are | |
40 # overridden by the message we send: | |
41 "this-arg-is-not-used"], | |
42 [nacl_launcher.NACL_PLUGIN_BOUND_SOCK, | |
43 nacl_launcher.NACL_PLUGIN_ASYNC_TO_CHILD_FD], | |
44 stdout=write_fh) | |
45 argv = ["unused-argv0", "--library-path", lib_dir, | |
46 os.environ["HELLO_WORLD_PROG"]] | |
47 envv = [] | |
48 fd2.imc_sendmsg(nacl_launcher.PackArgsMessage(argv, envv), tuple([])) | |
49 self.assertEquals(proc.wait(), 0) | |
50 self.assertEquals(read_fh.read(), "Hello, World!\n") | |
51 | |
52 def _TryStartupMessage(self, message, expected_line): | |
53 lib_dir = os.environ["NACL_LIBRARY_DIR"] | |
54 write_fh, read_fh = MakeFileHandlePair() | |
55 proc, [fd1, fd2] = nacl_launcher.SpawnSelLdrWithSockets( | |
56 ["-s", "-a", | |
57 "--", os.path.join(lib_dir, "runnable-ld.so"), | |
58 "this-arg-is-not-used"], | |
59 [nacl_launcher.NACL_PLUGIN_BOUND_SOCK, | |
60 nacl_launcher.NACL_PLUGIN_ASYNC_TO_CHILD_FD], | |
61 stderr=write_fh) | |
62 if message is not None: | |
63 fd2.imc_sendmsg(message, tuple([])) | |
64 del fd2 | |
65 self.assertEquals(proc.wait(), 127) | |
66 output_lines = read_fh.read().split("\n") | |
67 self.assertIn(expected_line, output_lines) | |
68 | |
69 def test_error_on_eof(self): | |
70 self._TryStartupMessage(None, "Error receiving startup message (5)") | |
71 | |
72 def test_error_on_missing_tag(self): | |
73 self._TryStartupMessage( | |
74 "", "Startup message (0 bytes) lacks the expected tag") | |
75 | |
76 def test_error_on_missing_body(self): | |
77 self._TryStartupMessage("ARGS", "Startup message too small (4 bytes)") | |
78 | |
79 def test_error_on_truncated_body(self): | |
80 data = nacl_launcher.PackArgsMessage(["a", "b", "c"], []) | |
81 self._TryStartupMessage( | |
82 data[:-1], "Unterminated argument string in startup message") | |
83 data = nacl_launcher.PackArgsMessage(["a", "b", "c"], ["e", "f"]) | |
84 self._TryStartupMessage( | |
85 data[:-1], "Unterminated environment string in startup message") | |
86 | |
87 def test_error_on_excess_data(self): | |
88 data = nacl_launcher.PackArgsMessage(["a", "b"], ["c", "d"]) | |
89 self._TryStartupMessage( | |
90 data + "x", "Excess data (1 bytes) in startup message body") | |
91 | |
92 def test_error_on_overly_large_array_sizes(self): | |
93 self._TryStartupMessage( | |
94 struct.pack("4sII", "ARGS", 0x20000, 0), | |
95 "Unterminated argument string in startup message") | |
96 self._TryStartupMessage( | |
97 struct.pack("4sII", "ARGS", 0x20000, -0x18000 & 0xffffffff), | |
98 "Unterminated argument string in startup message") | |
99 | |
100 | |
101 if __name__ == "__main__": | |
102 unittest.main() | |
OLD | NEW |