1
+ import random
1
2
import zigpy_znp .types as t
2
3
import zigpy_znp .commands as c
3
4
4
- from zigpy_znp .tools .flash_backup import main as flash_backup
5
+ from zigpy_znp .tools .flash_read import main as flash_read
6
+ from zigpy_znp .tools .flash_write import main as flash_write
5
7
6
8
from test_api import pytest_mark_asyncio_timeout # noqa: F401
7
9
from test_application import znp_server # noqa: F401
8
10
from test_tools_nvram import openable_serial_znp_server # noqa: F401
9
11
10
12
11
- # Just random bytes
12
- FAKE_FLASH = bytes .fromhex (
13
- """
14
- a66ea64b2299ef91102c692c8739433776ac1f7967b2d7be3b532db5255dee88f49cad134ef4155375d2
15
- 67acecbe64637bd1df47ce1cb8b776caad7a7cd2b39892b69fbf2420176e598f689df05a3554400efb99
16
- 60dcedfb3416fe72b1570b6eb4aa877213afb92c7a6fc8b755e7457072a8c4d4ac9ec727b7748b267fda
17
- 241334ab9195b4eb52cb50b396859c355dfad136e1c56b18f6599e08a7464524587a44ea0caaeb2b0a79
18
- 44ff74576db0c16b133f862de8ee8b6b37181a897416b40c589a645c62bbc6b2b4e993a6ee39ca1141bb
19
- 7baeb7bb85476c7b905fa8f3f2148fe1162a218fb575eb3ed9849bc63212f7332a27f83c75e6590a25ad
20
- 8ad3d13b212da0142bc257851afcc7c87c80c23d9f741f7159ccc89fed58ff2369523af224369df39224
21
- a4154dc2932958d3289d387356af931aa6e02d8216bffc3972674cf060de50c10e0705b2f80d7b54c763
22
- 0999d2f28f8e3b1917d89e960a1893ebdaa1695c5b2f1fc36efb144b326d4cb8119803ea327f2848b45a
23
- a6e3e1ca93459eb848a8333826b12d87949be6cf652b1265a7c74e2b750303ee25f6296ed687393cb1a1
24
- 64648ae92eb2c426ea3f35770f6d64fefcd87fc9835ab39134be9a5d325cc2839a47515f15ce5b2072fe
25
- 808a5e897a273f883751d029bec9fe89797fd2940603537770c745c17e817e495e4d8741e744b652254b
26
- 2b776c1d313ca30a
27
- """
28
- )
13
+ random .seed (12345 )
14
+ FAKE_IMAGE_SIZE = 2 ** 10
15
+ FAKE_FLASH = random .getrandbits (FAKE_IMAGE_SIZE * 8 ).to_bytes (FAKE_IMAGE_SIZE , "little" )
16
+ random .seed ()
29
17
30
18
31
19
@pytest_mark_asyncio_timeout (seconds = 5 )
32
- async def test_flash_backup (openable_serial_znp_server , tmp_path ): # noqa: F811
20
+ async def test_flash_backup_write (
21
+ openable_serial_znp_server , tmp_path , mocker # noqa: F811
22
+ ):
23
+ # It takes too long otherwise
24
+ mocker .patch ("zigpy_znp.commands.ubl.IMAGE_SIZE" , FAKE_IMAGE_SIZE )
25
+
26
+ WRITABLE_FLASH = bytearray (len (FAKE_FLASH ))
27
+
33
28
openable_serial_znp_server .reply_to (
34
29
request = c .UBL .HandshakeReq .Req (partial = True ),
35
30
responses = [
@@ -46,7 +41,7 @@ async def test_flash_backup(openable_serial_znp_server, tmp_path): # noqa: F811
46
41
47
42
def read_flash (req ):
48
43
offset = req .FlashWordAddr * 4
49
- data = FAKE_FLASH [offset : offset + 64 ]
44
+ data = WRITABLE_FLASH [offset : offset + 64 ]
50
45
51
46
# We should not read partial blocks
52
47
assert len (data ) in (0 , 64 )
@@ -60,11 +55,37 @@ def read_flash(req):
60
55
Data = t .TrailingBytes (data ),
61
56
)
62
57
58
+ def write_flash (req ):
59
+ offset = req .FlashWordAddr * 4
60
+
61
+ assert len (req .Data ) == 64
62
+
63
+ WRITABLE_FLASH [offset : offset + 64 ] = req .Data
64
+ assert len (WRITABLE_FLASH ) == FAKE_IMAGE_SIZE
65
+
66
+ return c .UBL .WriteRsp .Callback (Status = c .ubl .BootloaderStatus .SUCCESS )
67
+
63
68
openable_serial_znp_server .reply_to (
64
69
request = c .UBL .ReadReq .Req (partial = True ), responses = [read_flash ]
65
70
)
66
71
72
+ openable_serial_znp_server .reply_to (
73
+ request = c .UBL .WriteReq .Req (partial = True ), responses = [write_flash ]
74
+ )
75
+
76
+ openable_serial_znp_server .reply_to (
77
+ request = c .UBL .EnableReq .Req (partial = True ),
78
+ responses = [c .UBL .EnableRsp .Callback (Status = c .ubl .BootloaderStatus .SUCCESS )],
79
+ )
80
+
81
+ # First we write the flash
82
+ firmware_file = tmp_path / "firmware.bin"
83
+ firmware_file .write_bytes (FAKE_FLASH )
84
+ await flash_write ([openable_serial_znp_server ._port_path , "-i" , str (firmware_file )])
85
+
86
+ # And then make a backup
67
87
backup_file = tmp_path / "backup.bin"
68
- await flash_backup ([openable_serial_znp_server ._port_path , "-o" , str (backup_file )])
88
+ await flash_read ([openable_serial_znp_server ._port_path , "-o" , str (backup_file )])
69
89
90
+ # They should be identical
70
91
assert backup_file .read_bytes () == FAKE_FLASH
0 commit comments