VirtualBox

Ignore:
Timestamp:
May 10, 2019 3:29:51 PM (6 years ago)
Author:
vboxsync
Message:

tdAddGuestCtrl.py,utils.py: Attempt at generic byte/string array comparison function to make testboxsky7 happy.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/ValidationKit/common/utils.py

    r76553 r78463  
    570570    return shutil.copyfile(sFileSrc, sFileDst);
    571571
     572
     573def getDiskUsage(sPath):
     574    """
     575    Get free space of a partition that corresponds to specified sPath in MB.
     576
     577    Returns partition free space value in MB.
     578    """
     579    if platform.system() == 'Windows':
     580        oCTypeFreeSpace = ctypes.c_ulonglong(0);
     581        ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(sPath), None, None,
     582                                                   ctypes.pointer(oCTypeFreeSpace));
     583        cbFreeSpace = oCTypeFreeSpace.value;
     584    else:
     585        oStats = os.statvfs(sPath); # pylint: disable=E1101
     586        cbFreeSpace = long(oStats.f_frsize) * oStats.f_bfree;
     587
     588    # Convert to MB
     589    cMbFreeSpace = long(cbFreeSpace) / (1024 * 1024);
     590
     591    return cMbFreeSpace;
     592
     593
     594
    572595#
    573596# SubProcess.
     
    16931716
    16941717
     1718def chmodPlusX(sFile):
     1719    """
     1720    Makes the specified file or directory executable.
     1721    Returns success indicator, no exceptions.
     1722
     1723    Note! Symbolic links are followed and the target will be changed.
     1724    """
     1725    try:
     1726        oStat = os.stat(sFile);
     1727    except:
     1728        return False;
     1729    try:
     1730        os.chmod(sFile, oStat.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH);
     1731    except:
     1732        return False;
     1733    return True;
     1734
     1735
    16951736#
    16961737# TestSuite stuff.
     
    17661807    return 1;
    17671808
    1768 
    1769 #
    1770 # Misc.
    1771 #
    17721809
    17731810def versionCompare(sVer1, sVer2):
     
    18341871
    18351872
    1836 def chmodPlusX(sFile):
    1837     """
    1838     Makes the specified file or directory executable.
    1839     Returns success indicator, no exceptions.
    1840 
    1841     Note! Symbolic links are followed and the target will be changed.
    1842     """
    1843     try:
    1844         oStat = os.stat(sFile);
    1845     except:
    1846         return False;
    1847     try:
    1848         os.chmod(sFile, oStat.st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH);
    1849     except:
    1850         return False;
    1851     return True;
    1852 
     1873#
     1874# Unpacking.
     1875#
    18531876
    18541877def unpackZipFile(sArchive, sDstDir, fnLog, fnError = None, fnFilter = None):
     
    20402063
    20412064
    2042 def getDiskUsage(sPath):
    2043     """
    2044     Get free space of a partition that corresponds to specified sPath in MB.
    2045 
    2046     Returns partition free space value in MB.
    2047     """
    2048     if platform.system() == 'Windows':
    2049         oCTypeFreeSpace = ctypes.c_ulonglong(0);
    2050         ctypes.windll.kernel32.GetDiskFreeSpaceExW(ctypes.c_wchar_p(sPath), None, None,
    2051                                                    ctypes.pointer(oCTypeFreeSpace));
    2052         cbFreeSpace = oCTypeFreeSpace.value;
     2065#
     2066# Misc.
     2067#
     2068def areBytesEqual(oLeft, oRight):
     2069    """
     2070    Compares two byte arrays, strings or whatnot.
     2071
     2072    returns true / false accordingly.
     2073    """
     2074
     2075    # If both are None, consider them equal (bogus?):
     2076    if oLeft is None and oRight is None:
     2077        return True;
     2078
     2079    # If just one is None, they can't match:
     2080    if oLeft is None or oRight is None:
     2081        return False;
     2082
     2083    # If both have the same type, use the compare operator of the class:
     2084    if type(oLeft) is type(oRight):
     2085        return oLeft == oRight;
     2086
     2087    # On the offchance that they're both strings, but of different types.
     2088    if isString(oLeft) and isString(oRight):
     2089        return oLeft == oRight;
     2090
     2091    # Convert strings to byte arrays:
     2092    if sys.version_info[0] >= 3:
     2093        if isString(oLeft):
     2094            try:    oLeft = bytes(oLeft, 'utf-8');
     2095            except: pass;
     2096        if isString(oRight):
     2097            try:    oRight = bytes(oRight, 'utf-8');
     2098            except: pass;
    20532099    else:
    2054         oStats = os.statvfs(sPath); # pylint: disable=E1101
    2055         cbFreeSpace = long(oStats.f_frsize) * oStats.f_bfree;
    2056 
    2057     # Convert to MB
    2058     cMbFreeSpace = long(cbFreeSpace) / (1024 * 1024);
    2059 
    2060     return cMbFreeSpace;
     2100        if isString(oLeft):
     2101            try:    oLeft = bytearray(oLeft, 'utf-8');
     2102            except: pass;
     2103        if isString(oRight):
     2104            try:    oRight = bytearray(oRight, 'utf-8');
     2105            except: pass;
     2106
     2107    # Check if we now have the same type for both:
     2108    if type(oLeft) is type(oRight):
     2109        return oLeft == oRight;
     2110
     2111    # Do item by item comparison:
     2112    if len(oLeft) != len(oRight):
     2113        return False;
     2114    i = len(oLeft);
     2115    while i > 0:
     2116        i = i - 1;
     2117        if oLeft[i] != oRight[i]:
     2118            return False;
     2119    return True;
    20612120
    20622121
     
    20922151        self.assertEqual(hasNonAsciiCharacters(b'\x20\x81\x20'), True);
    20932152
     2153    def testAreBytesEqual(self):
     2154        self.assertEqual(areBytesEqual(None, None), True);
     2155        self.assertEqual(areBytesEqual(None, ''), False);
     2156        self.assertEqual(areBytesEqual('', ''), True);
     2157        self.assertEqual(areBytesEqual('1', '1'), True);
     2158        self.assertEqual(areBytesEqual('12345', '1234'), False);
     2159        self.assertEqual(areBytesEqual('1234', '1234'), True);
     2160        self.assertEqual(areBytesEqual('1234', b'1234'), True);
     2161        self.assertEqual(areBytesEqual(b'1234', b'1234'), True);
     2162        self.assertEqual(areBytesEqual(b'1234', '1234'), True);
     2163        self.assertEqual(areBytesEqual(b'1234', bytearray([0x31,0x32,0x33,0x34])), True);
     2164        self.assertEqual(areBytesEqual('1234', bytearray([0x31,0x32,0x33,0x34])), True);
     2165        self.assertEqual(areBytesEqual(bytearray([0x31,0x32,0x33,0x34]), bytearray([0x31,0x32,0x33,0x34])), True);
     2166        self.assertEqual(areBytesEqual(bytearray([0x31,0x32,0x33,0x34]), '1224'), False);
     2167        self.assertEqual(areBytesEqual(bytearray([0x31,0x32,0x33,0x34]), bytearray([0x31,0x32,0x32,0x34])), False);
     2168
    20942169if __name__ == '__main__':
    20952170    unittest.main();
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette