9 # From http://lackingrhoticity.blogspot.com/2008/11/tempdirtestcase-python-unittest-helper.html
10 class TempDirTestCase(unittest
.TestCase
):
13 self
._on
_teardown
= []
15 def make_temp_dir(self
):
16 temp_dir
= tempfile
.mkdtemp(prefix
="tmp-%s-" % self
.__class
__.__name
__)
18 shutil
.rmtree(temp_dir
)
19 self
._on
_teardown
.append(tear_down
)
23 for func
in reversed(self
._on
_teardown
):
27 def write_file(filename
, data
):
28 fh
= open(filename
, "w")
38 printf("%s", ""); /* Tests making a function call */
44 class GccWrapperTest(TempDirTestCase
):
46 def test_default_output_file_for_linking(self
):
47 temp_dir
= self
.make_temp_dir()
48 write_file(os
.path
.join(temp_dir
, "foo.c"), example_prog
)
49 subprocess
.check_call(["nacl-glibc-gcc", "-Wall", "foo.c"],
51 self
.assertEquals(sorted(os
.listdir(temp_dir
)), ["a.out", "foo.c"])
52 # Nops should have been written. Executable should be runnable.
53 subprocess
.check_call([os
.path
.join(temp_dir
, "a.out")])
55 def test_default_output_file_for_c(self
):
56 temp_dir
= self
.make_temp_dir()
57 write_file(os
.path
.join(temp_dir
, "foo.c"), example_prog
)
58 subprocess
.check_call(["nacl-glibc-gcc", "-Wall", "-c", "foo.c"],
60 subprocess
.check_call(["gcc", "foo.o", "-o", "foo"], cwd
=temp_dir
)
61 # Nops should have been written. Executable should be runnable.
62 subprocess
.check_call([os
.path
.join(temp_dir
, "foo")])
64 def test_explicit_output_file(self
):
65 temp_dir
= self
.make_temp_dir()
66 write_file(os
.path
.join(temp_dir
, "foo.c"), example_prog
)
67 subprocess
.check_call(["nacl-glibc-gcc", "-Wall", "foo.c", "-o", "foo"],
69 # Nops should have been written. Executable should be runnable.
70 subprocess
.check_call([os
.path
.join(temp_dir
, "foo")])
72 def test_building_against_installed_libraries(self
):
73 # Assumes libreadline5-dev is installed.
74 temp_dir
= self
.make_temp_dir()
75 write_file(os
.path
.join(temp_dir
, "foo.c"), """
76 #include <readline/readline.h>
82 subprocess
.check_call(
83 ["nacl-glibc-gcc", "-Wall", "foo.c", "-lreadline", "-o", "foo"],
85 subprocess
.check_call([os
.path
.join(temp_dir
, "foo")])
88 class GlibcTest(TempDirTestCase
):
90 def test_linking_with_libpthread(self
):
91 # Just tests that libpthread initialises OK.
92 temp_dir
= self
.make_temp_dir()
93 write_file(os
.path
.join(temp_dir
, "foo.c"), example_prog
)
94 subprocess
.check_call(
95 ["nacl-glibc-gcc", "-Wall", "foo.c", "-lpthread", "-o", "foo"],
97 subprocess
.check_call([os
.path
.join(temp_dir
, "foo")])
99 def test_linking_with_dubious_options(self
):
100 # Python links _sqlite.so with -L/usr/lib which causes the
101 # linker to drag in the static /usr/lib/libc.a.
102 temp_dir
= self
.make_temp_dir()
103 write_file(os
.path
.join(temp_dir
, "foo.c"), example_prog
)
104 subprocess
.check_call(
105 ["nacl-glibc-gcc", "-Wall", "foo.c", "-lpthread",
106 "-L/usr/lib", "-o", "foo"],
108 subprocess
.check_call([os
.path
.join(temp_dir
, "foo")])
109 # TODO: Validate resulting code. Otherwise broken output is
110 # noisy but doesn't signal failure.
112 def _check_fstat(self
, suffix
):
113 temp_dir
= self
.make_temp_dir()
114 write_file(os
.path
.join(temp_dir
, "test.c"), r
"""
120 #include <sys/stat.h>
123 struct stat%(suffix)s st;
124 int fd = open("test", O_RDONLY);
129 memset(&st, 0xee, sizeof(st));
130 if(fstat%(suffix)s(fd, &st) < 0) {
135 #define FIELD(name) printf("('%%s', %%li),\n", #name, (long) st.name)
151 """ % {"suffix": suffix
})
152 subprocess
.check_call(["nacl-glibc-gcc", "test.c", "-o", "test"], cwd
=temp_dir
)
153 subprocess
.check_call(["ncrewrite", "test"], cwd
=temp_dir
)
155 "-d", os
.path
.join(os
.getcwd(), "install-stubout/lib/ld-linux.so.2"),
156 "--", "--library-path", os
.path
.join(os
.getcwd(), "install-stubout/lib"),
158 proc
= subprocess
.Popen(args
, stdout
=subprocess
.PIPE
,
159 stderr
=open(os
.devnull
, "w"),
161 stdout
= proc
.communicate()[0]
162 self
.assertEquals(proc
.wait(), 0)
163 fields
= dict(eval("[%s]" % stdout
, {}))
164 real_stat
= os
.stat(os
.path
.join(temp_dir
, "test"))
165 # Some of these values are dummy values filled out by NaCl.
166 self
.assertEquals(fields
["st_dev"], 0)
167 # As a hack, a new st_ino is returned each time by NaCl but we
168 # expect it to be small but non-zero.
169 assert 0 < fields
["st_ino"] < 100, fields
["st_ino"]
170 # NaCl clears the "group" and "other" permission bits.
171 self
.assertEquals("%o" % fields
["st_mode"],
172 "%o" % (real_stat
.st_mode
& ~
0077))
173 self
.assertEquals(fields
["st_nlink"], real_stat
.st_nlink
)
174 self
.assertEquals(fields
["st_uid"], -1)
175 self
.assertEquals(fields
["st_gid"], -1)
176 self
.assertEquals(fields
["st_rdev"], 0)
177 self
.assertEquals(fields
["st_size"], real_stat
.st_size
)
178 self
.assertEquals(fields
["st_blksize"], 0)
179 self
.assertEquals(fields
["st_blocks"], 0)
180 self
.assertEquals(fields
["st_atime"], real_stat
.st_atime
)
181 self
.assertEquals(fields
["st_mtime"], real_stat
.st_mtime
)
182 self
.assertEquals(fields
["st_ctime"], real_stat
.st_ctime
)
184 def test_fstat(self
):
185 self
._check
_fstat
(suffix
="")
187 def test_fstat64(self
):
188 self
._check
_fstat
(suffix
="64")
191 if __name__
== "__main__":