Skip to content

Commit ece3891

Browse files
CA-420307: Construct synthetic page data as byte arrays
Build the synthetic page data as byte arrays, rather than as strings. Convert the string portions in the synthetic page data as needed. str.encode() does not deal with embedded bytes in strings, as when converting using UTF-8 (the default), this will convert '\x80' into '\xC2\x80', which leads to incorrect data getting reported. Signed-off-by: Owen Smith <[email protected]> Co-authored-by: Mark Syms <[email protected]> Signed-off-by: Mark Syms <[email protected]>
1 parent 49a5d18 commit ece3891

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

libs/sm/core/scsiutil.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -340,21 +340,21 @@ def gen_synthetic_page_data(uuid):
340340
# we set the vendor ID to XENSRC
341341
# Note that the Page 80 serial number must be limited
342342
# to 16 characters
343-
page80 = ""
344-
page80 += "\x00\x80"
345-
page80 += "\x00\x12"
346-
page80 += uuid[0:16]
347-
page80 += " "
348-
349-
page83 = ""
350-
page83 += "\x00\x83"
351-
page83 += "\x00\x31"
352-
page83 += "\x02\x01\x00\x2d"
353-
page83 += "XENSRC "
354-
page83 += uuid
355-
page83 += " "
356-
return ["", base64.b64encode(str.encode(page80)).decode(),
357-
base64.b64encode(str.encode(page83)).decode()]
343+
page80 = b''
344+
page80 += b'\x00\x80'
345+
page80 += b'\x00\x12'
346+
page80 += str.encode(uuid[0:16])
347+
page80 += str.encode(" ")
348+
349+
page83 = b''
350+
page83 += b'\x00\x83'
351+
page83 += b'\x00\x31'
352+
page83 += b'\x02\x01\x00\x2d'
353+
page83 += str.encode("XENSRC ")
354+
page83 += str.encode(uuid)
355+
page83 += str.encode(" ")
356+
return ["", base64.b64encode(page80).decode(),
357+
base64.b64encode(page83).decode()]
358358

359359

360360
def gen_raw_page_data(path):

tests/test_blktap2.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import base64
12
import errno
23
import json
34
from io import StringIO
@@ -599,6 +600,17 @@ def find_by_path(path):
599600
# Assert
600601
mock_shutdown.assert_called_once()
601602

603+
def test_scsi_page_data(self):
604+
"""
605+
Check that the scsi page data is correctly encoded
606+
"""
607+
page_80 = base64.b64decode(self.vdi.xenstore_data["scsi/0x12/0x80"])
608+
page_83 = base64.b64decode(self.vdi.xenstore_data["scsi/0x12/0x83"])
609+
610+
self.assertEqual(b'\x00\x80\x00\x12', page_80[:4])
611+
self.assertEqual(b'\x00\x83\x00\x31\x02\x01\x00\x2d', page_83[:8])
612+
self.assertEqual('XENSRC ', page_83[8:16].decode())
613+
602614

603615
@mock.patch('sm.core.xs_errors.XML_DEFS', 'libs/sm/core/XE_SR_ERRORCODES.xml')
604616
class TestTapCtl(unittest.TestCase):

0 commit comments

Comments
 (0)