Test writing volunteers needed

Dinu C. Gherman gherman at darwin.in-berlin.de
Fri Jul 14 05:24:40 EDT 2000


"A.M. Kuchling" wrote:
> 
> Skip Montanaro <skip at mojam.com> wrote:
> >I'm look for a few people to write some regression tests for the posix (and
> >windows or mac equivalents), os and sys modules.  Some of that functionality
> 
> To start the ball rolling, here's a little test case for the set{re,e}{u,g}id
> functions that I added to the CVS version last night.

What about using the excellent Python unittest module 
(pyunit.sourceforge.net) to accomplish this, like in the 
code below? Note that I've not tested it on a Unix box and 
suppose the calls to some get* functions look in lines 
with trailing '# ??' do really look like shown...

Apart from these details, what unittest gives you is a 
very simple and useful test framework for *automated* test-
ing where you specify your test cases *with expected out-
put* such that you can build entire test suites to be run 
whenever you feel like you need a new short "confidence 
kick" before you continue with new functionality. Ideally, 
i.e. as an XP purist, you should write these tests *before* 
writing the *real code*!

Dinu

-- 
Dinu C. Gherman
................................................................
"The only possible values [for quality] are 'excellent' and 'in-
sanely excellent', depending on whether lives are at stake or 
not. Otherwise you don't enjoy your work, you don't work well, 
and the project goes down the drain." 
                    (Kent Beck, "Extreme Programming Explained")



import unittest


class SetXIDTestCase(unittest.TestCase):
    "Test if a single PDF Number is read and written correctly."
    
    def testSetreuidSetregid(self):
        "Simple test case for os.setreuid(), os.setregid()"
        
        import os, pwd

        uid = os.getuid()
        msg = "This test must be run as root; skipping"
        assert uid == 0, msg

        nobody_uid, nobody_gid = pwd.getpwnam('nobody')[2:4]

        # Set real uid and gid to root, effective uid and gid to nobody
        os.setreuid( 0, nobody_uid )
        os.setregid( 0, nobody_gid )

        # Change effective ID to nobody
        os.seteuid( nobody_uid )

        # Expect these to fail
        try:
            os.setuid( 1 )
        except os.error:
            pass
        else:
            msg = 'Should have been unable to change UID to 1'
            assert 1 == 0, msg

        try:
            os.setgid( 1 )
        except os.error:
            pass
        else:
            msg = 'Should have been unable to change GID to 1'
            assert 1 == 0, msg

        # These should succeed, since we're just swapping the IDs
        os.setreuid( nobody_uid, 0 )
        msg = 'Unable to swap IDs.'
        assert os.getreuid( nobody_uid ) == 0, msg # ??

        os.setregid( nobody_gid, 0 )
        msg = 'Unable to swap group IDs.'
        assert os.getregid( nobody_uid ) == 0, msg # ??

        # These should now succeed, since we're now back to root
privilege
        os.setgid( 1 )
        msg = 'Unable to reset group ID.'
        assert os.getgid() == 1, msg

        os.setuid( 1 )
        msg = 'Unable to reset user ID.'
        assert os.getuid() == 1, msg


def makeSuite():
    suite = unittest.TestSuite()
    suite.addTest(SetXIDTestCase('testSetreuidSetregid'))
    return suite


if __name__ == "__main__":
    unittest.TextTestRunner().run(makeSuite())



More information about the Python-list mailing list