# HG changeset patch
# User Jeroen Demeyer <jdemeyer@cage.ugent.be>
# Date 1350457858 -7200
# Node ID 38ba4a31109d4f7836273e0670d963cc2cf01e5c
# Parent 5ed869e7f54efbbbae5d4bd797f2796ca323658d
Check that doctesting from a world-writable directory is refused
diff --git a/sage/misc/temporary_file.py b/sage/misc/temporary_file.py
a
|
b
|
|
66 | 66 | '/home/username/.sage/temp/hostname/7961/dir_testing_XgRu4p.extension/' |
67 | 67 | sage: os.chdir(d) |
68 | 68 | sage: _ = open('file_inside_d', 'w') |
| 69 | |
| 70 | Temporary directories are unaccessible by other users:: |
| 71 | |
| 72 | sage: os.stat(d).st_mode & 0o077 |
| 73 | 0 |
69 | 74 | """ |
70 | 75 | from sage.misc.misc import SAGE_TMP |
71 | 76 | tmp = tempfile.mkdtemp(prefix=name, suffix=ext, dir=str(SAGE_TMP)) |
… |
… |
|
108 | 113 | sage: fn # random |
109 | 114 | '/home/username/.sage/temp/hostname/8044/just_for_testing_tVVHsn.extension' |
110 | 115 | sage: _ = open(fn, 'w') |
| 116 | |
| 117 | Temporary files are unaccessible by other users:: |
| 118 | |
| 119 | sage: os.stat(fn).st_mode & 0o077 |
| 120 | 0 |
111 | 121 | """ |
112 | 122 | from sage.misc.misc import SAGE_TMP |
113 | 123 | handle, tmp = tempfile.mkstemp(prefix=name, suffix=ext, dir=str(SAGE_TMP)) |
diff --git a/sage/tests/cmdline.py b/sage/tests/cmdline.py
a
|
b
|
|
53 | 53 | import os, select |
54 | 54 | |
55 | 55 | |
56 | | def test_executable(args, input="", timeout=50.0): |
| 56 | def test_executable(args, input="", timeout=50.0, cwd=None): |
57 | 57 | """ |
58 | 58 | Run the program defined by ``args`` using the string ``input`` on |
59 | 59 | the standard input. |
… |
… |
|
69 | 69 | - ``timeout`` -- if the program produces no output for ``timeout`` |
70 | 70 | seconds, a RuntimeError is raised. |
71 | 71 | |
| 72 | - ``cwd`` -- (default: ``None``) if not None, run the program from |
| 73 | the given directory. |
| 74 | |
72 | 75 | OUTPUT: a tuple ``(out, err, ret)`` with the standard output, |
73 | 76 | standard error and exitcode of the program run. |
74 | 77 | |
… |
… |
|
233 | 236 | '' |
234 | 237 | sage: ret |
235 | 238 | 0 |
236 | | sage: os.chdir(dir) |
237 | | sage: (out, err, ret) = test_executable(["sage", name]) |
| 239 | sage: (out, err, ret) = test_executable(["sage", name], cwd=dir) |
238 | 240 | sage: print out |
239 | 241 | 34 |
240 | 242 | sage: err |
… |
… |
|
257 | 259 | '' |
258 | 260 | sage: ret |
259 | 261 | 0 |
260 | | sage: os.chdir(dir) |
261 | | sage: (out, err, ret) = test_executable(["sage", name]) |
| 262 | sage: (out, err, ret) = test_executable(["sage", name], cwd=dir) |
262 | 263 | sage: print out |
263 | 264 | 1 |
264 | 265 | sage: err |
… |
… |
|
311 | 312 | True |
312 | 313 | sage: os.environ['SAGE_TESTDIR'] = OLD_TESTDIR # just in case |
313 | 314 | |
| 315 | Check that Sage refuses to run doctests from a directory whose |
| 316 | permissions are too loose. We create a world-writable directory |
| 317 | inside a safe temporary directory to test this:: |
| 318 | |
| 319 | sage: d = os.path.join(tmp_dir(), "test") |
| 320 | sage: os.mkdir(d) |
| 321 | sage: os.chmod(d, 0o777) |
| 322 | sage: (out, err, ret) = test_executable(["sage", "-t", "nonexisting.py"], cwd=d) |
| 323 | sage: print err |
| 324 | Traceback (most recent call last): |
| 325 | ... |
| 326 | RuntimeError: refusing to run doctests... |
| 327 | sage: (out, err, ret) = test_executable(["sage", "-tp", "1", "nonexisting.py"], cwd=d) |
| 328 | sage: print err |
| 329 | Traceback (most recent call last): |
| 330 | ... |
| 331 | RuntimeError: refusing to run doctests... |
| 332 | |
314 | 333 | Test external programs being called by Sage:: |
315 | 334 | |
316 | 335 | sage: (out, err, ret) = test_executable(["sage", "--sh"], "echo Hello World\nexit 42\n") |
… |
… |
|
503 | 522 | True |
504 | 523 | |
505 | 524 | """ |
506 | | p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE) |
| 525 | p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=cwd) |
507 | 526 | if input: p.stdin.write(input) |
508 | 527 | p.stdin.close() |
509 | 528 | fdout = p.stdout.fileno() |