/usr/lib/python2.7/dist-packages/linaro_image_tools/hwpack/tests/test_better_tarfile.py is in python-linaro-image-tools 2016.05-1.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | # Copyright (C) 2010, 2011 Linaro
#
# Author: James Westby <james.westby@linaro.org>
#
# This file is part of Linaro Image Tools.
#
# Linaro Image Tools 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.
#
# Linaro Image Tools 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 Linaro Image Tools; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
# USA.
from contextlib import contextmanager
from StringIO import StringIO
import tarfile
from testtools import TestCase
from linaro_image_tools.hwpack.better_tarfile import writeable_tarfile
@contextmanager
def standard_tarfile(backing_file, mode="r", seek=True):
"""A context manager to open a stdlib tarfile.
:param backing_file: the file object to take the tarfile
contents from.
:param mode: the mode to open the tarfile with.
:param seek: whether to seek the backing file to 0 before
opening.
"""
if seek:
backing_file.seek(0)
tf = tarfile.TarFile.open(mode=mode, fileobj=backing_file)
try:
yield tf
finally:
tf.close()
class TarFileTests(TestCase):
def test_creates_empty_tarfile(self):
backing_file = StringIO()
with writeable_tarfile(backing_file):
pass
with standard_tarfile(backing_file, seek=False) as tf:
self.assertEqual([], tf.getnames())
def create_simple_tarball(self, contents, **kwargs):
backing_file = StringIO()
with writeable_tarfile(backing_file, **kwargs) as tf:
for path, content in contents:
if path[-1] == '/':
tf.create_dir(path)
else:
tf.create_file_from_string(path, content)
return backing_file
def test_create_file_from_string_adds_path(self):
backing_file = self.create_simple_tarball([("foo", "bar")])
with standard_tarfile(backing_file) as tf:
self.assertEqual(["foo"], tf.getnames())
def test_create_file_from_string_uses_content(self):
backing_file = self.create_simple_tarball([("foo", "bar")])
with standard_tarfile(backing_file) as tf:
self.assertEqual("bar", tf.extractfile("foo").read())
def test_create_file_from_string_sets_size(self):
backing_file = self.create_simple_tarball([("foo", "bar")])
with standard_tarfile(backing_file) as tf:
self.assertEqual(3, tf.getmember("foo").size)
def test_create_file_from_string_sets_mode(self):
backing_file = self.create_simple_tarball([("foo", "bar")])
with standard_tarfile(backing_file) as tf:
self.assertEqual(0644, tf.getmember("foo").mode)
def test_create_file_from_string_sets_type(self):
backing_file = self.create_simple_tarball([("foo", "bar")])
with standard_tarfile(backing_file) as tf:
self.assertEqual(tarfile.REGTYPE, tf.getmember("foo").type)
def test_create_file_from_string_sets_linkname(self):
backing_file = self.create_simple_tarball([("foo", "bar")])
with standard_tarfile(backing_file) as tf:
self.assertEqual('', tf.getmember("foo").linkname)
def test_create_file_uses_default_mtime(self):
now = 126793
backing_file = self.create_simple_tarball(
[("foo", "bar")], default_mtime=now)
with standard_tarfile(backing_file) as tf:
self.assertEqual(now, tf.getmember("foo").mtime)
def test_create_file_uses_default_uid(self):
uid = 1259
backing_file = self.create_simple_tarball(
[("foo", "bar")], default_uid=uid)
with standard_tarfile(backing_file) as tf:
self.assertEqual(uid, tf.getmember("foo").uid)
def test_create_file_uses_default_gid(self):
gid = 2259
backing_file = self.create_simple_tarball(
[("foo", "bar")], default_gid=gid)
with standard_tarfile(backing_file) as tf:
self.assertEqual(gid, tf.getmember("foo").gid)
def test_create_file_uses_default_uname(self):
uname = "someperson"
backing_file = self.create_simple_tarball(
[("foo", "bar")], default_uname=uname)
with standard_tarfile(backing_file) as tf:
self.assertEqual(uname, tf.getmember("foo").uname)
def test_create_file_uses_default_gname(self):
gname = "somegroup"
backing_file = self.create_simple_tarball(
[("foo", "bar")], default_gname=gname)
with standard_tarfile(backing_file) as tf:
self.assertEqual(gname, tf.getmember("foo").gname)
def test_create_dir_adds_path(self):
backing_file = self.create_simple_tarball([("foo/", "")])
with standard_tarfile(backing_file) as tf:
self.assertEqual(["foo"], tf.getnames())
def test_create_dir_sets_name(self):
backing_file = self.create_simple_tarball([("foo/", "")])
with standard_tarfile(backing_file) as tf:
self.assertEqual("foo", tf.getmember("foo").name)
def test_create_dir_sets_type(self):
backing_file = self.create_simple_tarball([("foo/", "")])
with standard_tarfile(backing_file) as tf:
self.assertEqual(tarfile.DIRTYPE, tf.getmember("foo").type)
def test_create_dir_sets_size(self):
backing_file = self.create_simple_tarball([("foo/", "")])
with standard_tarfile(backing_file) as tf:
self.assertEqual(0, tf.getmember("foo").size)
def test_create_dir_sets_mode(self):
backing_file = self.create_simple_tarball([("foo/", "")])
with standard_tarfile(backing_file) as tf:
self.assertEqual(0755, tf.getmember("foo").mode)
def test_create_dir_sets_linkname(self):
backing_file = self.create_simple_tarball([("foo/", "")])
with standard_tarfile(backing_file) as tf:
self.assertEqual('', tf.getmember("foo").linkname)
def test_create_dir_uses_default_mtime(self):
now = 126793
backing_file = self.create_simple_tarball(
[("foo/", "")], default_mtime=now)
with standard_tarfile(backing_file) as tf:
self.assertEqual(now, tf.getmember("foo").mtime)
def test_create_dir_uses_default_uid(self):
uid = 1259
backing_file = self.create_simple_tarball(
[("foo/", "")], default_uid=uid)
with standard_tarfile(backing_file) as tf:
self.assertEqual(uid, tf.getmember("foo").uid)
def test_create_dir_uses_default_gid(self):
gid = 2259
backing_file = self.create_simple_tarball(
[("foo/", "")], default_gid=gid)
with standard_tarfile(backing_file) as tf:
self.assertEqual(gid, tf.getmember("foo").gid)
def test_create_dir_uses_default_uname(self):
uname = "someperson"
backing_file = self.create_simple_tarball(
[("foo/", "")], default_uname=uname)
with standard_tarfile(backing_file) as tf:
self.assertEqual(uname, tf.getmember("foo").uname)
def test_create_dir_uses_default_gname(self):
gname = "somegroup"
backing_file = self.create_simple_tarball(
[("foo/", "")], default_gname=gname)
with standard_tarfile(backing_file) as tf:
self.assertEqual(gname, tf.getmember("foo").gname)
|