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
|
# Copyright 2007 Google Inc.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
"""Unit tests for nss_cache/util/file_formats.py."""
__author__ = (
"jaq@google.com (Jamie Wilkinson)",
"vasilios@google.com (Vasilios Hoffman)",
)
import unittest
from nss_cache.util import file_formats
class TestFilesUtils(unittest.TestCase):
def testReadPasswdEntry(self):
"""We correctly parse a typical entry in /etc/passwd format."""
parser = file_formats.FilesPasswdMapParser()
file_entry = "root:x:0:0:Rootsy:/root:/bin/bash"
map_entry = parser._ReadEntry(file_entry)
self.assertEqual(map_entry.name, "root")
self.assertEqual(map_entry.passwd, "x")
self.assertEqual(map_entry.uid, 0)
self.assertEqual(map_entry.gid, 0)
self.assertEqual(map_entry.gecos, "Rootsy")
self.assertEqual(map_entry.dir, "/root")
self.assertEqual(map_entry.shell, "/bin/bash")
def testReadGroupEntry(self):
"""We correctly parse a typical entry in /etc/group format."""
parser = file_formats.FilesGroupMapParser()
file_entry = "root:x:0:zero_cool,acid_burn"
map_entry = parser._ReadEntry(file_entry)
self.assertEqual(map_entry.name, "root")
self.assertEqual(map_entry.passwd, "x")
self.assertEqual(map_entry.gid, 0)
self.assertEqual(map_entry.members, ["zero_cool", "acid_burn"])
def testReadShadowEntry(self):
"""We correctly parse a typical entry in /etc/shadow format."""
parser = file_formats.FilesShadowMapParser()
file_entry = "root:$1$zomgmd5support:::::::"
map_entry = parser._ReadEntry(file_entry)
self.assertEqual(map_entry.name, "root")
self.assertEqual(map_entry.passwd, "$1$zomgmd5support")
self.assertEqual(map_entry.lstchg, None)
self.assertEqual(map_entry.min, None)
self.assertEqual(map_entry.max, None)
self.assertEqual(map_entry.warn, None)
self.assertEqual(map_entry.inact, None)
self.assertEqual(map_entry.expire, None)
self.assertEqual(map_entry.flag, None)
def testReadNetgroupEntry(self):
"""We correctly parse a typical entry in /etc/netgroup format."""
parser = file_formats.FilesNetgroupMapParser()
file_entry = "administrators unix_admins noc_monkeys (-,zero_cool,)"
map_entry = parser._ReadEntry(file_entry)
self.assertEqual(map_entry.name, "administrators")
self.assertEqual(map_entry.entries, "unix_admins noc_monkeys (-,zero_cool,)")
def testReadEmptyNetgroupEntry(self):
"""We correctly parse a memberless netgroup entry."""
parser = file_formats.FilesNetgroupMapParser()
file_entry = "administrators"
map_entry = parser._ReadEntry(file_entry)
self.assertEqual(map_entry.name, "administrators")
self.assertEqual(map_entry.entries, "")
def testReadAutomountEntry(self):
"""We correctly parse a typical entry in /etc/auto.* format."""
parser = file_formats.FilesAutomountMapParser()
file_entry = "scratch -tcp,rw,intr,bg fileserver:/scratch"
map_entry = parser._ReadEntry(file_entry)
self.assertEqual(map_entry.key, "scratch")
self.assertEqual(map_entry.options, "-tcp,rw,intr,bg")
self.assertEqual(map_entry.location, "fileserver:/scratch")
def testReadAutmountEntryWithExtraWhitespace(self):
"""Extra whitespace doesn't break the parsing."""
parser = file_formats.FilesAutomountMapParser()
file_entry = "scratch fileserver:/scratch"
map_entry = parser._ReadEntry(file_entry)
self.assertEqual(map_entry.key, "scratch")
self.assertEqual(map_entry.options, None)
self.assertEqual(map_entry.location, "fileserver:/scratch")
def testReadBadAutomountEntry(self):
"""Cope with empty data."""
parser = file_formats.FilesAutomountMapParser()
file_entry = ""
map_entry = parser._ReadEntry(file_entry)
self.assertEqual(None, map_entry)
if __name__ == "__main__":
unittest.main()
|