]> git.apps.os.sepia.ceph.com Git - xfstests-dev.git/commitdiff
btrfs/154: migrate to python3 v2022.12.25
authorQu Wenruo <wqu@suse.com>
Fri, 23 Dec 2022 02:56:42 +0000 (10:56 +0800)
committerZorro Lang <zlang@kernel.org>
Sun, 25 Dec 2022 13:30:21 +0000 (21:30 +0800)
Test case btrfs/154 is still using python2 script, which is already EOL.
Some rolling distros like Archlinux is no longer providing python2
package anymore.

This means btrfs/154 will be harder and harder to run.

To fix the problem, migreate the python script to python3, this involves
the following changes:

- Change common/config to use python3
- Strong type conversion between string and bytes
  This means, anything involved in the forged bytes has to be bytes.

  And there is no safe way to convert forged bytes into string, unlike
  python2.
  I guess that's why the author went python2 in the first place.

  Thankfully os.rename() still accepts forged bytes.

- Use bytes specific checks for invalid chars.

The updated script can still cause the needed conflicts, can be verified
through "btrfs ins dump-tree" command.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Reviewed-by: Zorro Lang <zlang@redhat.com>
Signed-off-by: Zorro Lang <zlang@kernel.org>
common/config
src/btrfs_crc32c_forged_name.py
tests/btrfs/154

index b2802e5e0af10ee2ea8094f130e776a0fba1b142..e2aba5a923eda0a3047a825833e58688a9739852 100644 (file)
@@ -212,7 +212,7 @@ export NFS4_SETFACL_PROG="$(type -P nfs4_setfacl)"
 export NFS4_GETFACL_PROG="$(type -P nfs4_getfacl)"
 export UBIUPDATEVOL_PROG="$(type -P ubiupdatevol)"
 export THIN_CHECK_PROG="$(type -P thin_check)"
-export PYTHON2_PROG="$(type -P python2)"
+export PYTHON3_PROG="$(type -P python3)"
 export SQLITE3_PROG="$(type -P sqlite3)"
 export TIMEOUT_PROG="$(type -P timeout)"
 export SETCAP_PROG="$(type -P setcap)"
index 6c08fcb77637eb178d4450f9b7ab08f34d6ad96b..d29bbb709dcf3840e3cd90feafc4ecf1e3cffe9c 100755 (executable)
@@ -59,9 +59,10 @@ class CRC32(object):
     # deduce the 4 bytes we need to insert
     for c in struct.pack('<L',fwd_crc)[::-1]:
       bkd_crc = ((bkd_crc << 8) & 0xffffffff) ^ self.reverse[bkd_crc >> 24]
-      bkd_crc ^= ord(c)
+      bkd_crc ^= c
 
-    res = s[:pos] + struct.pack('<L', bkd_crc) + s[pos:]
+    res = bytes(s[:pos], 'ascii') + struct.pack('<L', bkd_crc) + \
+          bytes(s[pos:], 'ascii')
     return res
 
   def parse_args(self):
@@ -72,6 +73,12 @@ class CRC32(object):
                         help="number of forged names to create")
     return parser.parse_args()
 
+def has_invalid_chars(result: bytes):
+    for i in result:
+        if i == 0 or i == int.from_bytes(b'/', byteorder="little"):
+            return True
+    return False
+
 if __name__=='__main__':
 
   crc = CRC32()
@@ -80,12 +87,15 @@ if __name__=='__main__':
   args = crc.parse_args()
   dirpath=args.dir
   while count < args.count :
-    origname = os.urandom (89).encode ("hex")[:-1].strip ("\x00")
+    origname = os.urandom (89).hex()[:-1].strip ("\x00")
     forgename = crc.forge(wanted_crc, origname, 4)
-    if ("/" not in forgename) and ("\x00" not in forgename):
+    if not has_invalid_chars(forgename):
       srcpath=dirpath + '/' + str(count)
-      dstpath=dirpath + '/' + forgename
-      file (srcpath, 'a').close()
+      # We have to convert all strings to bytes to concatenate the forged
+      # name (bytes).
+      # Thankfully os.rename() can accept bytes directly.
+      dstpath=bytes(dirpath, "ascii") + bytes('/', "ascii") + forgename
+      open(srcpath, mode="a").close()
       os.rename(srcpath, dstpath)
       os.system('btrfs fi sync %s' % (dirpath))
       count+=1;
index 240c504c8ee88e119ba2da145699c910d341e410..6be2d5f6420924cb071cdf80895408ae4dc119ca 100755 (executable)
@@ -21,7 +21,7 @@ _begin_fstest auto quick
 
 _supported_fs btrfs
 _require_scratch
-_require_command $PYTHON2_PROG python2
+_require_command $PYTHON3_PROG python3
 
 # Currently in btrfs the node/leaf size can not be smaller than the page
 # size (but it can be greater than the page size). So use the largest
@@ -42,7 +42,7 @@ _scratch_mount
 #    ...
 #
 
-$PYTHON2_PROG $here/src/btrfs_crc32c_forged_name.py -d $SCRATCH_MNT -c 310
+$PYTHON3_PROG $here/src/btrfs_crc32c_forged_name.py -d $SCRATCH_MNT -c 310
 echo "Silence is golden"
 
 # success, all done