Ticket #13579: 13579_test_permissions.patch

File 13579_test_permissions.patch, 3.7 KB (added by jdemeyer, 9 years ago)
  • sage/misc/temporary_file.py

    # 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  
    6666        '/home/username/.sage/temp/hostname/7961/dir_testing_XgRu4p.extension/'
    6767        sage: os.chdir(d)
    6868        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
    6974    """
    7075    from sage.misc.misc import SAGE_TMP
    7176    tmp = tempfile.mkdtemp(prefix=name, suffix=ext, dir=str(SAGE_TMP))
     
    108113        sage: fn  # random
    109114        '/home/username/.sage/temp/hostname/8044/just_for_testing_tVVHsn.extension'
    110115        sage: _ = open(fn, 'w')
     116
     117    Temporary files are unaccessible by other users::
     118
     119        sage: os.stat(fn).st_mode & 0o077
     120        0
    111121    """
    112122    from sage.misc.misc import SAGE_TMP
    113123    handle, tmp = tempfile.mkstemp(prefix=name, suffix=ext, dir=str(SAGE_TMP))
  • sage/tests/cmdline.py

    diff --git a/sage/tests/cmdline.py b/sage/tests/cmdline.py
    a b  
    5353import os, select
    5454
    5555
    56 def test_executable(args, input="", timeout=50.0):
     56def test_executable(args, input="", timeout=50.0, cwd=None):
    5757    """
    5858    Run the program defined by ``args`` using the string ``input`` on
    5959    the standard input.
     
    6969    - ``timeout`` -- if the program produces no output for ``timeout``
    7070      seconds, a RuntimeError is raised.
    7171
     72    - ``cwd`` -- (default: ``None``) if not None, run the program from
     73      the given directory.
     74
    7275    OUTPUT: a tuple ``(out, err, ret)`` with the standard output,
    7376    standard error and exitcode of the program run.
    7477
     
    233236        ''
    234237        sage: ret
    235238        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)
    238240        sage: print out
    239241        34
    240242        sage: err
     
    257259        ''
    258260        sage: ret
    259261        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)
    262263        sage: print out
    263264        1
    264265        sage: err
     
    311312        True
    312313        sage: os.environ['SAGE_TESTDIR'] = OLD_TESTDIR  # just in case
    313314
     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
    314333    Test external programs being called by Sage::
    315334   
    316335        sage: (out, err, ret) = test_executable(["sage", "--sh"], "echo Hello World\nexit 42\n")
     
    503522        True
    504523   
    505524    """
    506     p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
     525    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE, cwd=cwd)
    507526    if input: p.stdin.write(input)
    508527    p.stdin.close()
    509528    fdout = p.stdout.fileno()