Coverage for test/d7a/fs/test_file_header.py: 100%
36 statements
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-24 08:03 +0200
« prev ^ index » next coverage.py v7.5.0, created at 2024-05-24 08:03 +0200
1#
2# Copyright (c) 2015-2021 University of Antwerp, Aloxy NV.
3#
4# This file is part of pyd7a.
5# See https://github.com/Sub-IoT/pyd7a for further info.
6#
7# Licensed under the Apache License, Version 2.0 (the "License");
8# you may not use this file except in compliance with the License.
9# You may obtain a copy of the License at
10#
11# http://www.apache.org/licenses/LICENSE-2.0
12#
13# Unless required by applicable law or agreed to in writing, software
14# distributed under the License is distributed on an "AS IS" BASIS,
15# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16# See the License for the specific language governing permissions and
17# limitations under the License.
18#
19import struct
20import unittest
22from bitstring import ConstBitStream
24from d7a.fs.file_header import FileHeader
25from d7a.fs.file_permissions import FilePermissions
26from d7a.fs.file_properties import ActionCondition, StorageClass, FileProperties
29class TestFileHeader(unittest.TestCase):
31 def test_parsing(self):
32 file_header_bytes = [
33 0xFC, # permissions
34 0xB3, # properties
35 0x41, # ALP cmd file id
36 0x42, # interface file id
37 0x00, 0x00, 0x01, 0x00, # file size
38 0x00, 0x00, 0x02, 0x00 # allocated size
39 ]
41 file_header = FileHeader.parse(ConstBitStream(bytes=file_header_bytes))
42 permission = file_header.permissions
43 self.assertEqual(permission.encrypted, True)
44 self.assertEqual(permission.executable, True)
45 self.assertEqual(permission.user_readable, True)
46 self.assertEqual(permission.user_writable, True)
47 self.assertEqual(permission.user_executable, True)
48 self.assertEqual(permission.guest_readable, True)
49 self.assertEqual(permission.guest_writable, False)
50 self.assertEqual(permission.guest_executable, False)
51 prop = file_header.properties
52 self.assertEqual(prop.act_enabled, True)
53 self.assertEqual(prop.act_condition, ActionCondition.WRITE_FLUSH)
54 self.assertEqual(prop.storage_class, StorageClass.PERMANENT)
55 self.assertEqual(file_header.alp_command_file_id, 0x41)
56 self.assertEqual(file_header.interface_file_id, 0x42)
57 self.assertEqual(file_header.file_size, 256)
58 self.assertEqual(file_header.allocated_size, 512)
61 def test_byte_generation(self):
62 file_header = FileHeader(
63 permissions=FilePermissions(encrypted=True, executable=True, user_readable=True, user_writable=True, user_executable=True,
64 guest_readable=True, guest_writable=False, guest_executable=False),
65 properties=FileProperties(act_enabled=True, act_condition=ActionCondition.WRITE_FLUSH, storage_class=StorageClass.PERMANENT),
66 alp_command_file_id=0x41,
67 interface_file_id=0x42,
68 file_size=20,
69 allocated_size=40
70 )
72 bytes = bytearray(file_header)
73 self.assertEqual(bytes[0], 0xFC)
74 self.assertEqual(bytes[1], 0xB3)
75 self.assertEqual(bytes[2], 0x41)
76 self.assertEqual(bytes[3], 0x42)
77 self.assertEqual(struct.unpack(">I", bytes[4:8])[0], 20)
78 self.assertEqual(struct.unpack(">I", bytes[8:12])[0], 40)