File: test_posix.py

package info (click to toggle)
pypy 5.6.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 97,040 kB
  • ctags: 185,069
  • sloc: python: 1,147,862; ansic: 49,642; cpp: 5,245; asm: 5,169; makefile: 529; sh: 481; xml: 232; lisp: 45
file content (304 lines) | stat: -rw-r--r-- 8,947 bytes parent folder | download
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
import py.test
from rpython.rtyper.test.tool import BaseRtypingTest
from rpython.rtyper.annlowlevel import hlstr
from rpython.tool.udir import udir
from rpython.rlib.rarithmetic import is_valid_int

import os
exec 'import %s as posix' % os.name

def setup_module(module):
    testf = udir.join('test.txt')
    module.path = testf.strpath

class TestPosix(BaseRtypingTest):

    def setup_method(self, meth):
        # prepare/restore the file before each test
        testfile = open(path, 'wb')
        testfile.write('This is a test')
        testfile.close()

    def test_open(self):
        def f():
            ff = posix.open(path, posix.O_RDONLY, 0777)
            return ff
        func = self.interpret(f, [])
        assert is_valid_int(func)

    def test_fstat(self):
        def fo(fi):
            g = posix.fstat(fi)
            return g
        fi = os.open(path,os.O_RDONLY,0777)
        func = self.interpret(fo,[fi])
        stat = os.fstat(fi)
        for i in range(len(stat)):
            assert long(getattr(func, 'item%d' % i)) == stat[i]


    def test_stat(self):
        def fo():
            g = posix.stat(path)
            return g
        func = self.interpret(fo,[])
        stat = os.stat(path)
        for i in range(len(stat)):
            assert long(getattr(func, 'item%d' % i)) == stat[i]

    def test_stat_exception(self):
        def fo():
            try:
                posix.stat('I/do/not/exist')
            except OSError:
                return True
            else:
                return False
        res = self.interpret(fo,[])
        assert res

    def test_times(self):
        py.test.skip("llinterp does not like tuple returns")
        from rpython.rtyper.test.test_llinterp import interpret
        times = interpret(lambda: posix.times(), ())
        assert isinstance(times, tuple)
        assert len(times) == 5
        for value in times:
            assert is_valid_int(value)


    def test_lseek(self):
        def f(fi, pos):
            posix.lseek(fi, pos, 0)
        fi = os.open(path, os.O_RDONLY, 0777)
        func = self.interpret(f, [fi, 5])
        res = os.read(fi, 2)
        assert res =='is'

    def test_isatty(self):
        def f(fi):
            posix.isatty(fi)
        fi = os.open(path, os.O_RDONLY, 0777)
        func = self.interpret(f, [fi])
        assert not func
        os.close(fi)
        func = self.interpret(f, [fi])
        assert not func

    def test_getcwd(self):
        def f():
            return posix.getcwd()
        res = self.interpret(f,[])
        cwd = os.getcwd()
        #print res.chars,cwd
        assert self.ll_to_string(res) == cwd

    def test_write(self):
        def f(fi):
            if fi > 0:
                text = 'This is a test'
            else:
                text = '333'
            return posix.write(fi,text)
        fi = os.open(path,os.O_WRONLY,0777)
        text = 'This is a test'
        func = self.interpret(f,[fi])
        os.close(fi)
        fi = os.open(path,os.O_RDONLY,0777)
        res = os.read(fi,20)
        assert res == text

    def test_read(self):
        def f(fi,len):
            return posix.read(fi,len)
        fi = os.open(path,os.O_WRONLY,0777)
        text = 'This is a test'
        os.write(fi,text)
        os.close(fi)
        fi = os.open(path,os.O_RDONLY,0777)
        res = self.interpret(f,[fi,20])
        assert self.ll_to_string(res) == text

    @py.test.mark.skipif("not hasattr(os, 'chown')")
    def test_chown(self):
        f = open(path, "w")
        f.write("xyz")
        f.close()
        def f():
            try:
                posix.chown(path, os.getuid(), os.getgid())
                return 1
            except OSError:
                return 2

        assert self.interpret(f, []) == 1
        os.unlink(path)
        assert self.interpret(f, []) == 2

    def test_close(self):
        def f(fi):
            return posix.close(fi)
        fi = os.open(path,os.O_WRONLY,0777)
        text = 'This is a test'
        os.write(fi,text)
        res = self.interpret(f,[fi])
        py.test.raises( OSError, os.fstat, fi)

    @py.test.mark.skipif("not hasattr(os, 'ftruncate')")
    def test_ftruncate(self):
        def f(fi,len):
            os.ftruncate(fi,len)
        fi = os.open(path,os.O_RDWR,0777)
        func = self.interpret(f,[fi,6])
        assert os.fstat(fi).st_size == 6

    @py.test.mark.skipif("not hasattr(os, 'getuid')")
    def test_getuid(self):
        def f():
            return os.getuid()
        assert self.interpret(f, []) == f()

    @py.test.mark.skipif("not hasattr(os, 'getgid')")
    def test_getgid(self):
        def f():
            return os.getgid()
        assert self.interpret(f, []) == f()

    @py.test.mark.skipif("not hasattr(os, 'setuid')")
    def test_os_setuid(self):
        def f():
            os.setuid(os.getuid())
            return os.getuid()
        assert self.interpret(f, []) == f()

    @py.test.mark.skipif("not hasattr(os, 'sysconf')")
    def test_os_sysconf(self):
        def f(i):
            return os.sysconf(i)
        assert self.interpret(f, [13]) == f(13)

    @py.test.mark.skipif("not hasattr(os, 'confstr')")
    def test_os_confstr(self):
        def f(i):
            try:
                return os.confstr(i)
            except OSError:
                return "oooops!!"
        some_value = os.confstr_names.values()[-1]
        res = self.interpret(f, [some_value])
        assert hlstr(res) == f(some_value)
        res = self.interpret(f, [94781413])
        assert hlstr(res) == "oooops!!"

    @py.test.mark.skipif("not hasattr(os, 'pathconf')")
    def test_os_pathconf(self):
        def f(i):
            return os.pathconf("/tmp", i)
        i = os.pathconf_names["PC_NAME_MAX"]
        some_value = self.interpret(f, [i])
        assert some_value >= 31

    @py.test.mark.skipif("not hasattr(os, 'chroot')")
    def test_os_chroot(self):
        def f():
            try:
                os.chroot('!@$#!#%$#^#@!#!$$#^')
            except OSError:
                return 1
            return 0

        assert self.interpret(f, []) == 1

    def test_os_wstar(self):
        from rpython.rlib import rposix
        for name in rposix.WAIT_MACROS:
            if not hasattr(os, name):
                continue
            def fun(s):
                return getattr(os, name)(s)

            for value in [0, 1, 127, 128, 255]:
                res = self.interpret(fun, [value])
                assert res == fun(value)

    @py.test.mark.skipif("not hasattr(os, 'getgroups')")
    def test_getgroups(self):
        def f():
            return os.getgroups()
        ll_a = self.interpret(f, [])
        assert self.ll_to_list(ll_a) == f()

    @py.test.mark.skipif("not hasattr(os, 'setgroups')")
    def test_setgroups(self):
        def f():
            try:
                os.setgroups(os.getgroups())
            except OSError:
                pass
        self.interpret(f, [])

    @py.test.mark.skipif("not hasattr(os, 'initgroups')")
    def test_initgroups(self):
        def f():
            try:
                os.initgroups('sUJJeumz', 4321)
            except OSError:
                return 1
            return 0
        res = self.interpret(f, [])
        assert res == 1

    @py.test.mark.skipif("not hasattr(os, 'tcgetpgrp')")
    def test_tcgetpgrp(self):
        def f(fd):
            try:
                return os.tcgetpgrp(fd)
            except OSError:
                return 42
        res = self.interpret(f, [9999])
        assert res == 42

    @py.test.mark.skipif("not hasattr(os, 'tcsetpgrp')")
    def test_tcsetpgrp(self):
        def f(fd, pgrp):
            try:
                os.tcsetpgrp(fd, pgrp)
            except OSError:
                return 1
            return 0
        res = self.interpret(f, [9999, 1])
        assert res == 1

    @py.test.mark.skipif("not hasattr(os, 'getresuid')")
    def test_getresuid(self):
        def f():
            a, b, c = os.getresuid()
            return a + b * 37 + c * 1291
        res = self.interpret(f, [])
        a, b, c = os.getresuid()
        assert res == a + b * 37 + c * 1291

    @py.test.mark.skipif("not hasattr(os, 'getresgid')")
    def test_getresgid(self):
        def f():
            a, b, c = os.getresgid()
            return a + b * 37 + c * 1291
        res = self.interpret(f, [])
        a, b, c = os.getresgid()
        assert res == a + b * 37 + c * 1291

    @py.test.mark.skipif("not hasattr(os, 'setresuid')")
    def test_setresuid(self):
        def f():
            a, b, c = os.getresuid()
            a = (a + 1) - 1
            os.setresuid(a, b, c)
        self.interpret(f, [])

    @py.test.mark.skipif("not hasattr(os, 'setresgid')")
    def test_setresgid(self):
        def f():
            a, b, c = os.getresgid()
            a = (a + 1) - 1
            os.setresgid(a, b, c)
        self.interpret(f, [])