| OLD | NEW |
| (Empty) |
| 1 # Copyright (c) 2008 Twisted Matrix Laboratories. | |
| 2 # See LICENSE for details. | |
| 3 | |
| 4 """ | |
| 5 Tests for L{twisted.scripts.mktap}. | |
| 6 """ | |
| 7 | |
| 8 import sys | |
| 9 try: | |
| 10 import pwd, grp | |
| 11 except ImportError: | |
| 12 pwd = None | |
| 13 | |
| 14 from twisted.trial.unittest import TestCase | |
| 15 | |
| 16 from twisted.scripts.mktap import run, getid | |
| 17 from twisted.application.service import IProcess, loadApplication | |
| 18 from twisted.test.test_twistd import patchUserDatabase | |
| 19 | |
| 20 | |
| 21 class RunTests(TestCase): | |
| 22 """ | |
| 23 Tests for L{twisted.scripts.mktap.run}. | |
| 24 """ | |
| 25 def setUp(self): | |
| 26 """ | |
| 27 Save the original value of L{sys.argv} so that tests can change it | |
| 28 as necessary. | |
| 29 """ | |
| 30 self.argv = sys.argv[:] | |
| 31 | |
| 32 | |
| 33 def tearDown(self): | |
| 34 """ | |
| 35 Restore the original value of L{sys.argv}. | |
| 36 """ | |
| 37 sys.argv[:] = self.argv | |
| 38 | |
| 39 | |
| 40 def _saveConfiguredIDTest(self, argv, uid, gid): | |
| 41 """ | |
| 42 Test that when L{run} is invoked and L{sys.argv} has the given | |
| 43 value, the resulting application has the specified UID and GID. | |
| 44 | |
| 45 @type argv: C{list} of C{str} | |
| 46 @param argv: The value to which to set L{sys.argv} before calling L{run}
. | |
| 47 | |
| 48 @type uid: C{int} | |
| 49 @param uid: The expected value for the resulting application's | |
| 50 L{IProcess.uid}. | |
| 51 | |
| 52 @type gid: C{int} | |
| 53 @param gid: The expected value for the resulting application's | |
| 54 L{IProcess.gid}. | |
| 55 """ | |
| 56 sys.argv = argv | |
| 57 run() | |
| 58 app = loadApplication("ftp.tap", "pickle", None) | |
| 59 process = IProcess(app) | |
| 60 self.assertEqual(process.uid, uid) | |
| 61 self.assertEqual(process.gid, gid) | |
| 62 | |
| 63 | |
| 64 def test_getNumericID(self): | |
| 65 """ | |
| 66 L{run} extracts numeric UID and GID information from the command | |
| 67 line and persists it with the application object. | |
| 68 """ | |
| 69 uid = 1234 | |
| 70 gid = 4321 | |
| 71 self._saveConfiguredIDTest( | |
| 72 ["mktap", "--uid", str(uid), "--gid", str(gid), "ftp"], | |
| 73 uid, gid) | |
| 74 | |
| 75 | |
| 76 def test_getNameID(self): | |
| 77 """ | |
| 78 L{run} extracts name UID and GID information from the command | |
| 79 line and persists it with the application object. | |
| 80 """ | |
| 81 user = "foo" | |
| 82 uid = 1234 | |
| 83 group = "bar" | |
| 84 gid = 4321 | |
| 85 patchUserDatabase(self.patch, user, uid, group, gid) | |
| 86 self._saveConfiguredIDTest( | |
| 87 ["mktap", "--uid", user, "--gid", group, "ftp"], | |
| 88 uid, gid) | |
| 89 if pwd is None: | |
| 90 test_getNameID.skip = ( | |
| 91 "Username/UID Group name/GID translation requires pwd and grp " | |
| 92 "modules.") | |
| 93 | |
| 94 | |
| 95 | |
| 96 class HelperTests(TestCase): | |
| 97 """ | |
| 98 Tests for miscellaneous utility functions related to mktap. | |
| 99 """ | |
| 100 def test_getid(self): | |
| 101 """ | |
| 102 L{getid} returns a two-tuple of integers giving the numeric values of | |
| 103 the strings it is passed. | |
| 104 """ | |
| 105 uid = 1234 | |
| 106 gid = 4321 | |
| 107 self.assertEqual(getid(str(uid), str(gid)), (uid, gid)) | |
| OLD | NEW |