1#!/usr/bin/env python 2# 3# Copyright (C) 2016 The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of the License at 8# 9# http://www.apache.org/licenses/LICENSE-2.0 10# 11# Unless required by applicable law or agreed to in writing, software 12# distributed under the License is distributed on an "AS IS" BASIS, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16# 17 18import os 19import unittest 20 21from vts.utils.python.archive import archive_parser 22 23 24class ArchiveParserTest(unittest.TestCase): 25 """Unit tests for archive_parser of vts.utils.python.archive. 26 """ 27 28 def testReadHeaderPass(self): 29 """Tests that archive is read when header is correct. 30 31 Parses archive content containing only the signature. 32 """ 33 try: 34 archive = archive_parser.Archive(archive_parser.Archive.GLOBAL_SIG) 35 archive.Parse() 36 except ValueError: 37 self.fail('Archive reader read improperly.') 38 39 def testReadHeaderFail(self): 40 """Tests that parser throws error when header is invalid. 41 42 Parses archive content lacking the correct signature. 43 """ 44 archive = archive_parser.Archive('Fail.') 45 self.assertRaises(ValueError, archive.Parse) 46 47 def testReadFile(self): 48 """Tests that file is read correctly. 49 50 Tests that correctly formatted file in archive is read correctly. 51 """ 52 content = archive_parser.Archive.GLOBAL_SIG 53 file_name = 'test_file' 54 content += file_name + ' ' * (archive_parser.Archive.FILE_ID_LENGTH - 55 len(file_name)) 56 content += ' ' * archive_parser.Archive.FILE_TIMESTAMP_LENGTH 57 content += ' ' * archive_parser.Archive.OWNER_ID_LENGTH 58 content += ' ' * archive_parser.Archive.GROUP_ID_LENGTH 59 content += ' ' * archive_parser.Archive.FILE_MODE_LENGTH 60 61 message = 'test file contents' 62 message_size = str(len(message)) 63 content += message_size + ' ' * (archive_parser.Archive.CONTENT_SIZE_LENGTH - 64 len(message_size)) 65 content += archive_parser.Archive.END_TAG 66 content += message 67 archive = archive_parser.Archive(content) 68 archive.Parse() 69 self.assertIn(file_name, archive.files) 70 self.assertEquals(archive.files[file_name], message) 71 72if __name__ == "__main__": 73 unittest.main() 74