Changeset 79255 in vbox for trunk/src/VBox/ValidationKit/testdriver/testfileset.py
- Timestamp:
- Jun 20, 2019 3:14:07 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/ValidationKit/testdriver/testfileset.py
r79208 r79255 131 131 132 132 return True; 133 134 @staticmethod 135 def hexFormatBytes(abBuf): 136 """ Formats a buffer/string/whatever as a string of hex bytes """ 137 if sys.version_info[0] >= 3: 138 if utils.isString(abBuf): 139 try: abBuf = bytes(abBuf, 'utf-8'); 140 except: pass; 141 else: 142 if utils.isString(abBuf): 143 try: abBuf = bytearray(abBuf, 'utf-8'); # pylint: disable=redefined-variable-type 144 except: pass; 145 sRet = ''; 146 off = 0; 147 for off, bByte in enumerate(abBuf): 148 if off > 0: 149 sRet += ' ' if off & 7 else '-'; 150 if isinstance(bByte, int): 151 sRet += '%02x' % (bByte,); 152 else: 153 sRet += '%02x' % (ord(bByte),); 154 return sRet; 155 156 def equalMemory(self, abBuf, offFile = 0): 157 """ 158 Compares the content of the given buffer with the file content at that 159 file offset. 160 161 Returns True if it matches, False + error logging if it does not match. 162 """ 163 if not abBuf: 164 return True; 165 if offFile >= self.cbContent: 166 return reporter.error('buffer @ %s LB %s is beyond the end of the file (%s bytes)!' 167 % (offFile, len(abBuf), self.cbContent,)); 168 if offFile + len(abBuf) > self.cbContent: 169 return reporter.error('buffer @ %s LB %s is partially beyond the end of the file (%s bytes)!' 170 % (offFile, len(abBuf), self.cbContent,)); 171 if utils.areBytesEqual(abBuf, self.abContent[offFile:(offFile + len(abBuf))]): 172 return True; 173 174 reporter.error('mismatch with buffer @ %s LB %s (cbContent=%s)!' % (offFile, len(abBuf), self.cbContent,)); 175 reporter.error(' type(abBuf): %s' % (type(abBuf),)); 176 #if isinstance(abBuf, memoryview): 177 # reporter.error(' nbytes=%s len=%s itemsize=%s type(obj)=%s' 178 # % (abBuf.nbytes, len(abBuf), abBuf.itemsize, type(abBuf.obj),)); 179 reporter.error('type(abContent): %s' % (type(self.abContent),)); 180 181 offBuf = 0; 182 cbLeft = len(abBuf); 183 while cbLeft > 0: 184 cbLine = min(16, cbLeft); 185 abBuf1 = abBuf[offBuf:(offBuf + cbLine)]; 186 abBuf2 = self.abContent[offFile:(offFile + cbLine)]; 187 if not utils.areBytesEqual(abBuf1, abBuf2): 188 try: sStr1 = self.hexFormatBytes(abBuf1); 189 except: sStr1 = 'oops'; 190 try: sStr2 = self.hexFormatBytes(abBuf2); 191 except: sStr2 = 'oops'; 192 reporter.log('%#10x: %s' % (offBuf, sStr1,)); 193 reporter.log('%#10x: %s' % (offFile, sStr2,)); 194 195 # Advance. 196 offBuf += 16; 197 offFile += 16; 198 cbLeft -= 16; 199 200 return False; 201 202 133 203 134 204 … … 516 586 self.assertTrue(isinstance(oSet.chooseRandomFile(), TestFile)); 517 587 588 def testHexFormatBytes(self): 589 self.assertEqual(TestFile.hexFormatBytes(bytearray([0,1,2,3,4,5,6,7,8,9])), 590 '00 01 02 03 04 05 06 07-08 09'); 591 self.assertEqual(TestFile.hexFormatBytes(memoryview(bytearray([0,1,2,3,4,5,6,7,8,9,10, 16]))), 592 '00 01 02 03 04 05 06 07-08 09 0a 10'); 593 518 594 519 595 if __name__ == '__main__':
Note:
See TracChangeset
for help on using the changeset viewer.