Coverage for test/d7a/system_files/test_uid.py: 100%

24 statements  

« 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 

21from bitstring import ConstBitStream 

22from d7a.system_files.uid import UidFile 

23 

24 

25class TestUiFile(unittest.TestCase): 

26 

27 def test_default_constructor(self): 

28 f = UidFile() 

29 self.assertEqual(f.uid, 0) 

30 

31 def test_constructor(self): 

32 uid = 0xDEADBEEF 

33 f = UidFile(uid=uid) 

34 self.assertEqual(f.uid, uid) 

35 

36 def test_invalid_id(self): 

37 def bad(): UidFile(uid=-1) 

38 self.assertRaises(ValueError, bad) 

39 

40 def test_parsing(self): 

41 file_contents = [ 

42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09 # UID 

43 ] 

44 

45 config = UidFile.parse(ConstBitStream(bytes=file_contents)) 

46 self.assertEqual(config.uid, 9) 

47 

48 def test_byte_generation(self): 

49 uid = 12345 

50 bytes = bytearray(UidFile(uid)) 

51 self.assertEqual(len(bytes), 8) 

52 self.assertEqual(struct.unpack(">Q", bytes)[0], uid)