Coverage for test/d7a/system_files/test_firmware_version.py: 100%
37 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 unittest
21from bitstring import ConstBitStream
23from d7a.system_files.firmware_version import FirmwareVersionFile
26class FirmwareVersionFileTest(unittest.TestCase):
28 def test_default_constructor(self):
29 f = FirmwareVersionFile()
30 self.assertEqual(f.d7a_protocol_version_major, 0)
31 self.assertEqual(f.d7a_protocol_version_minor, 0)
32 self.assertEqual(f.application_name, "")
33 self.assertEqual(f.git_sha1, "")
35 def test_invalid_app_name(self):
36 def bad(): FirmwareVersionFile(application_name="toolongname") # can be max 6
37 self.assertRaises(ValueError, bad)
39 def test_parsing(self):
40 file_contents = [
41 1, 1, # D7AP v1.1
42 0, 0, # FS version 0
43 0x74, 0x68, 0x72, 0x6f, 0x75, 0x67, # app name: throug(hput_test)
44 0x39, 0x61, 0x61, 0x62, 0x66, 0x61, 0x61 # git sha1
45 ]
47 f = FirmwareVersionFile.parse(ConstBitStream(bytes=file_contents))
48 self.assertEqual(f.d7a_protocol_version_major, 1)
49 self.assertEqual(f.d7a_protocol_version_minor, 1)
50 self.assertEqual(f.filesystem_version_major, 0)
51 self.assertEqual(f.filesystem_version_minor, 0)
52 self.assertEqual(f.application_name, "throug")
53 self.assertEqual(f.git_sha1, "9aabfaa")
55 def test_parsing_short(self):
56 file_contents = [
57 1, 1, # D7AP v1.1
58 0, 0, # FS version 0
59 0x74
60 ]
62 f = FirmwareVersionFile.parse(ConstBitStream(bytes=file_contents), offset=0, length=5)
63 self.assertEqual(f.d7a_protocol_version_major, 1)
64 self.assertEqual(f.d7a_protocol_version_minor, 1)
65 self.assertEqual(f.filesystem_version_major, 0)
66 self.assertEqual(f.filesystem_version_minor, 0)
67 self.assertEqual(f.application_name, "")
68 self.assertEqual(f.git_sha1, "")
70 def test_byte_generation(self):
71 expected = [
72 1, 1, # D7AP v1.1
73 2, 0, # FS version 0
74 0x74, 0x68, 0x72, 0x6f, 0x75, 0x67, # app name: throug(hput_test)
75 0x39, 0x61, 0x61, 0x62, 0x66, 0x61, 0x61 # git sha1
76 ]
78 bytes = bytearray(FirmwareVersionFile(d7a_protocol_version_major=1, d7a_protocol_version_minor=1,
79 filesystem_version_major=2, filesystem_version_minor=0,
80 application_name="throug", git_sha1="9aabfaa"))
81 self.assertEqual(len(bytes), 17)
82 for i in range(len(bytes)):
83 self.assertEqual(bytes[i], expected[i])