| 1 |
import py.test |
| 2 |
from rpython.rtyper.test.tool import BaseRtypingTest |
| 3 |
from rpython.rtyper.annlowlevel import hlstr |
| 4 |
from rpython.tool.udir import udir |
| 5 |
from rpython.rlib.rarithmetic import is_valid_int |
| 6 |
|
| 7 |
import os, sys |
| 8 |
exec('import %s as posix' % os.name) |
| 9 |
|
| 10 |
def setup_module(module): |
| 11 |
testf = udir.join('test.txt') |
| 12 |
module.path = testf.strpath |
| 13 |
|
| 14 |
def skip_if_os_x(): |
| 15 |
if sys.platform == 'darwin': |
| 16 |
py.test.skip("Bogus test on OS X") |
| 17 |
|
| 18 |
class TestPosix(BaseRtypingTest): |
| 19 |
|
| 20 |
def setup_method(self, meth): |
| 21 |
|
| 22 |
testfile = open(path, 'wb') |
| 23 |
testfile.write('This is a test') |
| 24 |
testfile.close() |
| 25 |
|
| 26 |
def test_open(self): |
| 27 |
def f(): |
| 28 |
ff = posix.open(path, posix.O_RDONLY, 0777) |
| 29 |
return ff |
| 30 |
func = self.interpret(f, []) |
| 31 |
assert is_valid_int(func) |
| 32 |
|
| 33 |
def test_fstat(self): |
| 34 |
def fo(fi): |
| 35 |
g = posix.fstat(fi) |
| 36 |
return g |
| 37 |
fi = os.open(path,os.O_RDONLY,0777) |
| 38 |
func = self.interpret(fo,[fi]) |
| 39 |
stat = os.fstat(fi) |
| 40 |
for i in range(len(stat)): |
| 41 |
|
| 42 |
if stat[i] != 0: |
| 43 |
assert long(getattr(func, 'item%d' % i)) == stat[i] |
| 44 |
|
| 45 |
|
| 46 |
def test_stat(self): |
| 47 |
def fo(): |
| 48 |
g = posix.stat(path) |
| 49 |
return g |
| 50 |
func = self.interpret(fo,[]) |
| 51 |
stat = os.stat(path) |
| 52 |
for i in range(len(stat)): |
| 53 |
assert long(getattr(func, 'item%d' % i)) == stat[i] |
| 54 |
|
| 55 |
def test_stat_exception(self): |
| 56 |
def fo(): |
| 57 |
try: |
| 58 |
posix.stat('I/do/not/exist') |
| 59 |
except OSError: |
| 60 |
return True |
| 61 |
else: |
| 62 |
return False |
| 63 |
res = self.interpret(fo,[]) |
| 64 |
assert res |
| 65 |
|
| 66 |
def test_times(self): |
| 67 |
py.test.skip("llinterp does not like tuple returns") |
| 68 |
from rpython.rtyper.test.test_llinterp import interpret |
| 69 |
times = interpret(lambda: posix.times(), ()) |
| 70 |
assert isinstance(times, tuple) |
| 71 |
assert len(times) == 5 |
| 72 |
for value in times: |
| 73 |
assert is_valid_int(value) |
| 74 |
|
| 75 |
|
| 76 |
def test_lseek(self): |
| 77 |
def f(fi, pos): |
| 78 |
posix.lseek(fi, pos, 0) |
| 79 |
fi = os.open(path, os.O_RDONLY, 0777) |
| 80 |
func = self.interpret(f, [fi, 5]) |
| 81 |
res = os.read(fi, 2) |
| 82 |
assert res =='is' |
| 83 |
|
| 84 |
def test_isatty(self): |
| 85 |
def f(fi): |
| 86 |
posix.isatty(fi) |
| 87 |
fi = os.open(path, os.O_RDONLY, 0777) |
| 88 |
func = self.interpret(f, [fi]) |
| 89 |
assert not func |
| 90 |
os.close(fi) |
| 91 |
func = self.interpret(f, [fi]) |
| 92 |
assert not func |
| 93 |
|
| 94 |
def test_getcwd(self): |
| 95 |
def f(): |
| 96 |
return posix.getcwd() |
| 97 |
res = self.interpret(f,[]) |
| 98 |
cwd = os.getcwd() |
| 99 |
|
| 100 |
assert self.ll_to_string(res) == cwd |
| 101 |
|
| 102 |
def test_write(self): |
| 103 |
def f(fi): |
| 104 |
if fi > 0: |
| 105 |
text = 'This is a test' |
| 106 |
else: |
| 107 |
text = '333' |
| 108 |
return posix.write(fi,text) |
| 109 |
fi = os.open(path,os.O_WRONLY,0777) |
| 110 |
text = 'This is a test' |
| 111 |
func = self.interpret(f,[fi]) |
| 112 |
os.close(fi) |
| 113 |
fi = os.open(path,os.O_RDONLY,0777) |
| 114 |
res = os.read(fi,20) |
| 115 |
assert res == text |
| 116 |
|
| 117 |
def test_read(self): |
| 118 |
def f(fi,len): |
| 119 |
return posix.read(fi,len) |
| 120 |
fi = os.open(path,os.O_WRONLY,0777) |
| 121 |
text = 'This is a test' |
| 122 |
os.write(fi,text) |
| 123 |
os.close(fi) |
| 124 |
fi = os.open(path,os.O_RDONLY,0777) |
| 125 |
res = self.interpret(f,[fi,20]) |
| 126 |
assert self.ll_to_string(res) == text |
| 127 |
|
| 128 |
@py.test.mark.skipif("not hasattr(os, 'chown')") |
| 129 |
def test_chown(self): |
| 130 |
f = open(path, "w") |
| 131 |
f.write("xyz") |
| 132 |
f.close() |
| 133 |
def f(): |
| 134 |
try: |
| 135 |
posix.chown(path, os.getuid(), os.getgid()) |
| 136 |
return 1 |
| 137 |
except OSError: |
| 138 |
return 2 |
| 139 |
|
| 140 |
assert self.interpret(f, []) == 1 |
| 141 |
os.unlink(path) |
| 142 |
assert self.interpret(f, []) == 2 |
| 143 |
|
| 144 |
def test_close(self): |
| 145 |
def f(fi): |
| 146 |
return posix.close(fi) |
| 147 |
fi = os.open(path,os.O_WRONLY,0777) |
| 148 |
text = 'This is a test' |
| 149 |
os.write(fi,text) |
| 150 |
res = self.interpret(f,[fi]) |
| 151 |
py.test.raises( OSError, os.fstat, fi) |
| 152 |
|
| 153 |
@py.test.mark.skipif("not hasattr(os, 'ftruncate')") |
| 154 |
def test_ftruncate(self): |
| 155 |
def f(fi,len): |
| 156 |
os.ftruncate(fi,len) |
| 157 |
fi = os.open(path,os.O_RDWR,0777) |
| 158 |
func = self.interpret(f,[fi,6]) |
| 159 |
assert os.fstat(fi).st_size == 6 |
| 160 |
|
| 161 |
@py.test.mark.skipif("not hasattr(os, 'getuid')") |
| 162 |
def test_getuid(self): |
| 163 |
def f(): |
| 164 |
return os.getuid() |
| 165 |
assert self.interpret(f, []) == f() |
| 166 |
|
| 167 |
@py.test.mark.skipif("not hasattr(os, 'getgid')") |
| 168 |
def test_getgid(self): |
| 169 |
def f(): |
| 170 |
return os.getgid() |
| 171 |
assert self.interpret(f, []) == f() |
| 172 |
|
| 173 |
@py.test.mark.skipif("not hasattr(os, 'setuid')") |
| 174 |
def test_os_setuid(self): |
| 175 |
def f(): |
| 176 |
os.setuid(os.getuid()) |
| 177 |
return os.getuid() |
| 178 |
assert self.interpret(f, []) == f() |
| 179 |
|
| 180 |
@py.test.mark.skipif("not hasattr(os, 'sysconf')") |
| 181 |
def test_os_sysconf(self): |
| 182 |
def f(i): |
| 183 |
return os.sysconf(i) |
| 184 |
assert self.interpret(f, [13]) == f(13) |
| 185 |
|
| 186 |
@py.test.mark.skipif("not hasattr(os, 'confstr')") |
| 187 |
def test_os_confstr(self): |
| 188 |
def f(i): |
| 189 |
try: |
| 190 |
return os.confstr(i) |
| 191 |
except OSError: |
| 192 |
return "oooops!!" |
| 193 |
some_value = os.confstr_names.values()[-1] |
| 194 |
res = self.interpret(f, [some_value]) |
| 195 |
assert hlstr(res) == f(some_value) |
| 196 |
res = self.interpret(f, [94781413]) |
| 197 |
assert hlstr(res) == "oooops!!" |
| 198 |
|
| 199 |
@py.test.mark.skipif("not hasattr(os, 'pathconf')") |
| 200 |
def test_os_pathconf(self): |
| 201 |
def f(i): |
| 202 |
return os.pathconf("/tmp", i) |
| 203 |
i = os.pathconf_names["PC_NAME_MAX"] |
| 204 |
some_value = self.interpret(f, [i]) |
| 205 |
assert some_value >= 31 |
| 206 |
|
| 207 |
@py.test.mark.skipif("not hasattr(os, 'chroot')") |
| 208 |
def test_os_chroot(self): |
| 209 |
def f(): |
| 210 |
try: |
| 211 |
os.chroot('!@$#!#%$#^#@!#!$$#^') |
| 212 |
except OSError: |
| 213 |
return 1 |
| 214 |
return 0 |
| 215 |
|
| 216 |
assert self.interpret(f, []) == 1 |
| 217 |
|
| 218 |
def test_os_wstar(self): |
| 219 |
from rpython.rlib import rposix |
| 220 |
for name in rposix.WAIT_MACROS: |
| 221 |
if not hasattr(os, name): |
| 222 |
continue |
| 223 |
def fun(s): |
| 224 |
return getattr(os, name)(s) |
| 225 |
|
| 226 |
for value in [0, 1, 127, 128, 255]: |
| 227 |
res = self.interpret(fun, [value]) |
| 228 |
assert res == fun(value) |
| 229 |
|
| 230 |
@py.test.mark.skipif("not hasattr(os, 'getgroups')") |
| 231 |
def test_getgroups(self): |
| 232 |
skip_if_os_x() |
| 233 |
def f(): |
| 234 |
return os.getgroups() |
| 235 |
ll_a = self.interpret(f, []) |
| 236 |
assert self.ll_to_list(ll_a) == f() |
| 237 |
|
| 238 |
@py.test.mark.skipif("not hasattr(os, 'setgroups')") |
| 239 |
def test_setgroups(self): |
| 240 |
skip_if_os_x() |
| 241 |
def f(): |
| 242 |
try: |
| 243 |
os.setgroups(os.getgroups()) |
| 244 |
except OSError: |
| 245 |
pass |
| 246 |
self.interpret(f, []) |
| 247 |
|
| 248 |
@py.test.mark.skipif("not hasattr(os, 'initgroups')") |
| 249 |
def test_initgroups(self): |
| 250 |
skip_if_os_x() |
| 251 |
def f(): |
| 252 |
try: |
| 253 |
os.initgroups('sUJJeumz', 4321) |
| 254 |
except OSError: |
| 255 |
return 1 |
| 256 |
return 0 |
| 257 |
res = self.interpret(f, []) |
| 258 |
assert res == 1 |
| 259 |
|
| 260 |
@py.test.mark.skipif("not hasattr(os, 'tcgetpgrp')") |
| 261 |
def test_tcgetpgrp(self): |
| 262 |
def f(fd): |
| 263 |
try: |
| 264 |
return os.tcgetpgrp(fd) |
| 265 |
except OSError: |
| 266 |
return 42 |
| 267 |
res = self.interpret(f, [9999]) |
| 268 |
assert res == 42 |
| 269 |
|
| 270 |
@py.test.mark.skipif("not hasattr(os, 'tcsetpgrp')") |
| 271 |
def test_tcsetpgrp(self): |
| 272 |
def f(fd, pgrp): |
| 273 |
try: |
| 274 |
os.tcsetpgrp(fd, pgrp) |
| 275 |
except OSError: |
| 276 |
return 1 |
| 277 |
return 0 |
| 278 |
res = self.interpret(f, [9999, 1]) |
| 279 |
assert res == 1 |
| 280 |
|
| 281 |
@py.test.mark.skipif("not hasattr(os, 'getresuid')") |
| 282 |
def test_getresuid(self): |
| 283 |
def f(): |
| 284 |
a, b, c = os.getresuid() |
| 285 |
return a + b * 37 + c * 1291 |
| 286 |
res = self.interpret(f, []) |
| 287 |
a, b, c = os.getresuid() |
| 288 |
assert res == a + b * 37 + c * 1291 |
| 289 |
|
| 290 |
@py.test.mark.skipif("not hasattr(os, 'getresgid')") |
| 291 |
def test_getresgid(self): |
| 292 |
def f(): |
| 293 |
a, b, c = os.getresgid() |
| 294 |
return a + b * 37 + c * 1291 |
| 295 |
res = self.interpret(f, []) |
| 296 |
a, b, c = os.getresgid() |
| 297 |
assert res == a + b * 37 + c * 1291 |
| 298 |
|
| 299 |
@py.test.mark.skipif("not hasattr(os, 'setresuid')") |
| 300 |
def test_setresuid(self): |
| 301 |
def f(): |
| 302 |
a, b, c = os.getresuid() |
| 303 |
a = (a + 1) - 1 |
| 304 |
os.setresuid(a, b, c) |
| 305 |
self.interpret(f, []) |
| 306 |
|
| 307 |
@py.test.mark.skipif("not hasattr(os, 'setresgid')") |
| 308 |
def test_setresgid(self): |
| 309 |
def f(): |
| 310 |
a, b, c = os.getresgid() |
| 311 |
a = (a + 1) - 1 |
| 312 |
os.setresgid(a, b, c) |
| 313 |
self.interpret(f, []) |