2 # Copyright (c) 2014 The Native Client Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Tests for archive info."""
13 sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
14 import pynacl.working_directory
19 class TestArchiveInfo(unittest.TestCase):
21 def CreateTemporaryArchive(self):
22 archive_name = 'test_name' + str(random.random())
23 archive_hash = 'test_hash' + str(random.random())
24 archive_url = 'test_url' + str(random.random())
25 tar_src_dir = 'test_src' + str(random.random())
26 extract_dir = 'test_extr' + str(random.random())
28 archive = archive_info.ArchiveInfo(archive_name, archive_hash, archive_url,
29 tar_src_dir, extract_dir)
33 def test_HashEmptyForMissingFiles(self):
34 # Many scripts rely on the archive hash returning None for missing files.
35 with pynacl.working_directory.TemporaryWorkingDirectory() as work_dir:
36 self.assertEqual(None, archive_info.GetArchiveHash('missingfile.tgz'))
38 def test_ArchiveHashStable(self):
39 # Check if archive hash produces a stable hash
40 with pynacl.working_directory.TemporaryWorkingDirectory() as work_dir:
41 temp1 = os.path.join(work_dir, 'temp1.txt')
42 temp2 = os.path.join(work_dir, 'temp2.txt')
44 temp_contents = 'this is a test'
45 with open(temp1, 'wt') as f:
46 f.write(temp_contents)
47 with open(temp2, 'wt') as f:
48 f.write(temp_contents)
50 self.assertEqual(archive_info.GetArchiveHash(temp1),
51 archive_info.GetArchiveHash(temp2))
53 def test_ArchiveConstructor(self):
54 archive_name = 'test_archive'
55 archive_hash = 'test_archive_hash'
56 archive_url = 'test_archive_url'
57 tar_src_dir = 'test_archive_dir'
58 extract_dir = 'test_extraction_dir'
60 archive = archive_info.ArchiveInfo(archive_name, archive_hash, archive_url,
61 tar_src_dir, extract_dir)
63 archive_data = archive.GetArchiveData()
64 self.assertEqual(archive_data.name, archive_name)
65 self.assertEqual(archive_data.hash, archive_hash)
66 self.assertEqual(archive_data.url, archive_url)
67 self.assertEqual(archive_data.tar_src_dir, tar_src_dir)
68 self.assertEqual(archive_data.extract_dir, extract_dir)
70 def test_ArchiveFileSaveLoad(self):
71 with pynacl.working_directory.TemporaryWorkingDirectory() as work_dir:
72 archive = self.CreateTemporaryArchive()
73 temp_json = os.path.join(work_dir, 'archive.json')
74 archive.SaveArchiveInfoFile(temp_json)
75 loaded_archive = archive_info.ArchiveInfo(archive_info_file=temp_json)
76 self.assertEqual(archive, loaded_archive)
78 def test_DumpArchiveJson(self):
79 archive = self.CreateTemporaryArchive()
80 archive_json = archive.DumpArchiveJson()
81 loaded_archive = archive_info.ArchiveInfo(archive_info_file=archive_json)
82 self.assertEqual(archive, loaded_archive)
85 if __name__ == '__main__':