def test_logging(self): opts = AdHocStruct() opts.remote_logging = "yes" log_messages = [] def FakeSource(): s = StubSourceMixin() s.counter = 0 return s def do_log(level, line): log_messages.append((level, line)) def _LoggingServer(): ls = logging_server.LoggingServer() ls.do_log = do_log return ls self._test_mixin_class(_LoggingServer, opts, {}, FakeSource) self.handle_packet(("logging", 10, "hello", time.time())) message = log_messages[0] assert message[0] == 10 assert message[1].endswith("hello") #multi-part: self.handle_packet(("logging", 20, ["multi", "messages"], time.time())) #invalid: with silence_error(logging_server): self.handle_packet(("logging", 20, nostr(), time.time()))
def test_mmap(self): class badfile: def close(self): raise Exception("test close failure handling") import tempfile tmp_dir = tempfile.gettempdir() for mmap, ctx in { "off": DummyContextManager(), "on": silence_info(log), tmp_dir + "/xpra-mmap-test-file-%i" % os.getpid(): silence_info(log), tmp_dir + "/xpra-fail-mmap-test-file-%i" % os.getpid(): silence_error(log), }.items(): opts = AdHocStruct() opts.mmap = mmap opts.mmap_group = False with ctx: m = self._test_mixin_class(MmapClient, opts, { "mmap.enabled": True, }) fail = bool(m.mmap_filename) and m.mmap_filename.find("fail") >= 0 assert m.mmap_enabled == (mmap != "off" and not fail) assert len(self.exit_codes) == int(fail) m.cleanup() #no-op: m.cleanup() m.mmap_tempfile = badfile() m.cleanup()
def test_encoding(self): opts = AdHocStruct() opts.encoding = "" opts.encodings = ["rgb", "png", "jpeg"] opts.quality = 1 opts.min_quality = 20 opts.speed = 0 opts.min_speed = 20 opts.video_scaling = "no" opts.video_decoders = [] opts.csc_modules = [] opts.video_encoders = [] m = self._test_mixin_class(Encodings, opts, { "encodings" : ["rgb"], "encodings.core" : ["rgb32", "rgb24", "png"], "encodings.problematic" : [], "encoding" : "" }) m.set_encoding("auto") def f(fn, err): try: fn() except Exception: pass else: raise Exception(err) def set_invalid_encoding(): m.set_encoding("invalid") f(set_invalid_encoding, "should not be able to set encoding 'invalid'") #this will trigger a warning: with silence_error(log): m.set_encoding("jpeg") #quality: for q in (-1, 0, 1, 99, 100): m.quality = q m.send_quality() for q in (-2, 101): m.quality = q f(m.send_quality, "should not be able to send invalid quality %i" % q) #min-quality: for q in (-1, 0, 1, 99, 100): m.min_quality = q m.send_min_quality() for q in (-2, 101): m.min_quality = q f(m.send_min_quality, "should not be able to send invalid min-quality %i" % q) #speed: for s in (-1, 0, 1, 99, 100): m.speed = s m.send_speed() for s in (-2, 101): m.speed = s f(m.send_speed, "should not be able to send invalid speed %i" % s) #min-speed: for s in (-1, 0, 1, 99, 100): m.min_speed = s m.send_min_speed() for s in (-2, 101): m.min_speed = s f(m.send_min_speed, "should not be able to send invalid min-speed %i" % s)
def test_parse_image_data(self): p = common.parse_image_data with silence_error(common): assert p(None) is None assert p((1, 2, 3)) is None assert p((1, 2, 3, 4, 5, 6, 7)) is None assert p((10, 10, 40, True, 32, 4, b"0" * 40 * 10)) is not None assert p((10, 10, 40, False, 24, 4, b"0" * 40 * 10)) is not None assert p((10, 10, 30, False, 24, 3, b"0" * 30 * 10)) is not None
def test_idle_raises_exception(self): qs = QueueScheduler() def raise_exception(): raise Exception("test scheduler error handling") with silence_error(log): qs.idle_add(raise_exception) qs.timeout_add(500, qs.stop) qs.run()
def test_verify_packet(self): for x in (True, 1, "hello", {}, None): assert verify_packet(x) is False assert verify_packet(["foo", 1]) is True assert verify_packet(["foo", [1,2,3], {1:2}]) is True with silence_error(log): assert verify_packet(["no-floats test", 1.1]) is False, "floats are not allowed" assert verify_packet(["foo", [None], {1:2}]) is False, "no None values" assert verify_packet(["foo", [1,2,3], {object() : 2}]) is False assert verify_packet(["foo", [1,2,3], {1 : 2.2}]) is False
def test_netifaces(self): ifaces = get_interfaces() if not ifaces: return ip_ifaces = defaultdict(list) for iface in ifaces: if if_nametoindex: try: i = if_nametoindex(iface) except Exception: pass else: if if_indextoname: assert if_indextoname( i ) == iface, "expected interface %s for index %i but got %s" % ( iface, i, if_indextoname(i)) ipmasks = do_get_bind_ifacemask(iface) for ip, _ in ipmasks: ip_ifaces[ip].append(iface) for ip, ifaces in ip_ifaces.items(): assert get_iface( ip ) in ifaces, "expected interface for ip %s to be one of %s but got %s" % ( ip, ifaces, get_iface(ip)) ia = get_interfaces_addresses() assert ia #for iface, address in ia.items(): # iface2 = get_interface(address) # assert iface2==iface, "expected %s but got %s" % (iface, iface2) get_gateways() get_bind_IPs() get_ssl_info() get_info() if if_indextoname: assert if_indextoname(-1) is None def invalid_iface(s): v = get_iface(s) if v: raise Exception( "invalid IP '%s' should not return interface '%s'" % (s, v)) invalid_iface(None) invalid_iface("") invalid_iface("%") invalid_iface(":") with silence_error(net_util): invalid_iface("INVALIDHOSTNAME") invalid_iface("10.0.0") get_iface("localhost") assert get_interface("invalid") is None
def test_idle_raises_exception(self): qs = QueueScheduler() def raise_exception(): raise Exception("test scheduler error handling") from unit.test_util import silence_error with silence_error(queue_scheduler): qs.idle_add(raise_exception) qs.timeout_add(500, qs.stop) qs.run()
def test_parse_image_path(self): from xpra.platform.paths import get_icon_filename filename = get_icon_filename("xpra") assert common.parse_image_path(filename) is not None f = tempfile.NamedTemporaryFile(prefix="test-invalid-file", delete=False) try: f.file.write(b"0000000000000001111111111111111111111") f.file.flush() f.close() for x in ("", None, "/invalid-path", f.name): with silence_error(common): assert common.parse_image_path(x) is None finally: os.unlink(f.name)
def test_shell(self): from xpra.server.source import shell_mixin protocol = AdHocStruct() protocol._conn = AdHocStruct() protocol._conn.options = {"shell" : "yes"} m = self._test_mixin_class(shell_mixin.ShellMixin, protocol=protocol) def noop(*_args): pass m.send = noop out,err = m.shell_exec("print('hello')") assert out.rstrip("\n")=="hello", "expected 'hello' but got '%s'" % out.rstrip("\n") assert not err with silence_error(shell_mixin): out,err = m.shell_exec("--not-a-statement--") assert not out assert err
def test_run(self): assert get_worker(False) is None w = get_worker() assert repr(w) def error_item(): raise Exception("work item test error") with silence_error(background_worker): add_work_item(error_item) time.sleep(0.1) #add the same item twice, with "no-duplicates" #(should only get added once) ndc = [] def nodupe(): ndc.append(True) w.add(nodupe, False) w.add(nodupe, False) time.sleep(1) with LoggerSilencer(background_worker, ("warn", "info")): #trigger the warning with more than 10 items: def slow_item(): time.sleep(1) for _ in range(12): w.add(slow_item) stop_worker() stop_worker(True) #no-op: stop_worker(True) #let the worker print its messages: time.sleep(1) assert len( ndc) == 1, "nodupe item should have been run once only, got %i" % ( len(ndc), )