def AssertScreenCharsInRectEqual(rect, expected_lines): global gHaveAsserted gHaveAsserted = True if rect.height() != len(expected_lines): raise esctypes.InternalError( "Height of rect (%d) does not match number of expected lines (%d)" % (rect.height(), len(expected_lines))) # Check each point individually. The dumb checksum algorithm can't distinguish # "ab" from "ba", so equivalence of two multiple-character rects means nothing. # |actual| and |expected| will form human-readable arrays of lines actual = [] expected = [] # Additional information about mismatches. errorLocations = [] for point in rect.points(): y = point.y() - rect.top() x = point.x() - rect.left() expected_line = expected_lines[y] if rect.width() != len(expected_line): fmt = ( "Width of rect (%d) does not match number of characters in expected line " + "index %d, coordinate %d (its length is %d)") raise esctypes.InternalError( fmt % (rect.width(), y, point.y(), len(expected_lines[y]))) expected_checksum = ord(expected_line[x]) actual_checksum = GetChecksumOfRect( Rect(left=point.x(), top=point.y(), right=point.x(), bottom=point.y())) if len(actual) <= y: actual.append("") if actual_checksum == 0: actual[y] += '.' else: actual[y] += chr(actual_checksum) if len(expected) <= y: expected.append("") if expected_checksum == 0: expected[y] += '.' else: expected[y] += chr(expected_checksum) if expected_checksum != actual_checksum: errorLocations.append( "At %s expected '%c' (0x%02x) but got '%c' (0x%02x)" % (str(point), chr(expected_checksum), expected_checksum, chr(actual_checksum), actual_checksum)) if len(errorLocations) > 0: Raise(esctypes.ChecksumException(errorLocations, actual, expected))
def GetWindowTitle(): if escargs.args.expected_terminal == "iTerm2": raise esctypes.InternalError( "iTerm2 uses L instead of l as initial char for window title reports." ) esccmd.XTERM_WINOPS(esccmd.WINOP_REPORT_WINDOW_TITLE) return escio.ReadOSC("l")
def ReadCSI(expected_final, expected_prefix=None): """Read a CSI code ending with |expected_final| and returns an array of parameters. """ c = read(1) if c == ESC: ReadOrDie('[') elif ord(c) != 0x9b: raise esctypes.InternalError("Read %c (0x%02x), expected CSI" % (c, ord(c))) params = [] current_param = "" c = read(1) if not c.isdigit() and c != ';': if c == expected_prefix: c = read(1) else: raise esctypes.InternalError("Unexpected character 0x%02x" % ord(c)) while True: if c == ";": params.append(int(current_param)) current_param = "" elif c >= '0' and c <= '9': current_param += c else: # Read all the final characters, asserting they match. while True: AssertCharsEqual(c, expected_final[0]) expected_final = expected_final[1:] if len(expected_final) > 0: c = read(1) else: break if current_param == "": params.append(None) else: params.append(int(current_param)) break c = read(1) return params
def read(n): """Try to read n bytes. Times out if it takes more than 1 second to read any given byte.""" s = "" f = sys.stdin.fileno() for _ in xrange(n): r, w, e = select.select([f], [], [], escargs.args.timeout) if f not in r: raise esctypes.InternalError("Timeout waiting to read.") s += os.read(f, 1) return s
def ReadDCS(): """ Read a DCS code. Returns the characters between DCS and ST. """ c = read(1) if c == ESC: ReadOrDie("P") elif ord(c) != 0x90: raise esctypes.InternalError("Read %c (0x%02x), expected DCS" % (c, ord(c))) result = "" while not result.endswith(ST) and not result.endswith(chr(0x9c)): c = read(1) result += c if result.endswith(ST): return result[:-2] else: return result[:-1]
def WriteCSI(prefix="", params=[], intermediate="", final="", requestsReport=False): if len(final) == 0: raise esctypes.InternalError("final must not be empty") def StringifyCSIParam(p): if p is None: return "" else: return str(p) str_params = map(StringifyCSIParam, params) # Remove trailing empty args while len(str_params) > 0 and str_params[-1] == "": str_params = str_params[:-1] joined_params = ";".join(str_params) sequence = CSI() + prefix + joined_params + intermediate + final LogDebug("Send sequence: " + sequence.replace(ESC, "<ESC>")) Write(sequence, sideChannelOk=not requestsReport)
def func_wrapper(self, *args, **kwargs): if escargs.args.expected_terminal == terminal: if not shouldTry: raise esctypes.KnownBug(reason + " (not trying)") try: func(self, *args, **kwargs) except Exception, e: tb = traceback.format_exc() lines = tb.split("\n") lines = map(lambda x: "KNOWN BUG: " + x, lines) raise esctypes.KnownBug(reason + "\n" + "\n".join(lines)) # Shouldn't get here because the test should have failed. If 'force' is on then # tests always pass, though. if not escargs.args.force and not noop: raise esctypes.InternalError("Should have failed") elif noop: raise esctypes.KnownBug( reason + " (test ran and passed, but is documented as a 'no-op'; the nature of the bug makes it untestable)" )
def func_wrapper(self, *args, **kwargs): hasOption = (escargs.args.options is not None and option in escargs.args.options) if escargs.args.expected_terminal == terminal: try: func(self, *args, **kwargs) except Exception, e: if hasOption: # Failed despite option being set. Re-raise. raise tb = traceback.format_exc() lines = tb.split("\n") lines = map( lambda x: "EXPECTED FAILURE (MISSING OPTION): " + x, lines) raise esctypes.KnownBug(reason + "\n\n" + "\n".join(lines)) # Got here because test passed. If the option isn't set, that's # unexpected so we raise an error. if not escargs.args.force and not hasOption and not allowPassWithoutOption: raise esctypes.InternalError("Should have failed: " + reason)
def AssertCharsEqual(c, e): if c != e: raise esctypes.InternalError("Read %c (0x%02x), expected %c (0x%02x)" % (c, ord(c), e, ord(e)))