def test_provider_info_none(): ctx = ffi.gc( lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL), lib.dc_context_unref, ) assert lib.dc_provider_new_from_email( ctx, cutil.as_dc_charpointer("*****@*****.**")) == ffi.NULL
def test_dc_close_events(tmpdir): ctx = ffi.gc( capi.lib.dc_context_new(capi.lib.py_dc_callback, ffi.NULL, ffi.NULL), lib.dc_context_unref, ) evlog = EventLogger(ctx) evlog.set_timeout(5) set_context_callback( ctx, lambda ctx, evt_name, data1, data2: evlog(evt_name, data1, data2)) p = tmpdir.join("hello.db") lib.dc_open(ctx, p.strpath.encode("ascii"), ffi.NULL) capi.lib.dc_close(ctx) def find(info_string): while 1: ev = evlog.get_matching("DC_EVENT_INFO", check_error=False) data2 = ev[2] if info_string in data2: return else: print("skipping event", *ev) find("disconnecting inbox-thread") find("disconnecting sentbox-thread") find("disconnecting mvbox-thread") find("disconnecting SMTP") find("Database closed")
def __init__( # noqa self, db_path: str, os_name: str, logger: logging.Logger) -> None: """initialize account object. :param db_path: a path to the account database. The database will be created if it doesn't exist. :param os_name: this is only for decorative use. :param logger: account logger. """ # initialize per-account plugin system self._pm = hookspec.PerAccount._make_plugin_manager() self._logging = True self.add_account_plugin(self) self.db_path = db_path if hasattr(db_path, "encode"): db_path = db_path.encode("utf8") # type: ignore self._dc_context = ffi.gc( lib.dc_context_new(as_dc_charpointer(os_name), db_path, ffi.NULL), lib.dc_context_unref, ) if self._dc_context == ffi.NULL: raise ValueError(f"Could not dc_context_new: {os_name} {db_path}") self._shutdown_event = Event() self._event_thread = BotEventThread(self, logger) self._configkeys = self.get_config("sys.config_keys").split() hook = hookspec.Global._get_plugin_manager().hook hook.dc_account_init(account=self)
def _inner_run(self) -> None: event_emitter = ffi.gc( lib.dc_get_event_emitter(self.account._dc_context), lib.dc_event_emitter_unref, ) while not self._marked_for_shutdown: event = lib.dc_get_next_event(event_emitter) if event == ffi.NULL: break if self._marked_for_shutdown: break evt = lib.dc_event_get_id(event) data1 = lib.dc_event_get_data1_int(event) # the following code relates to the deltachat/_build.py's helper # function which provides us signature info of an event call evt_name = deltachat.get_dc_event_name(evt) if lib.dc_event_has_string_data(evt): data2 = from_optional_dc_charpointer( lib.dc_event_get_data2_str(event)) else: data2 = lib.dc_event_get_data2_int(event) lib.dc_event_unref(event) ffi_event = FFIEvent(name=evt_name, data1=data1, data2=data2) try: self.account._pm.hook.ac_process_ffi_event(account=self, ffi_event=ffi_event) for name, kwargs in self._map_ffi_event(ffi_event): self.account.log( f"calling hook name={name} kwargs={kwargs}") hook = getattr(self.account._pm.hook, name) hook(**kwargs) except Exception as err: if self.account._dc_context is not None: self.logger.exception(err)
def test_empty_blobdir(tmpdir): # Apparently some client code expects this to be the same as passing NULL. ctx = ffi.gc( lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL), lib.dc_context_unref, ) db_fname = tmpdir.join("hello.db") assert lib.dc_open(ctx, db_fname.strpath.encode("ascii"), b"")
def test_is_open_actually_open(tmpdir): ctx = ffi.gc( lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL), lib.dc_context_unref, ) db_fname = tmpdir.join("test.db") lib.dc_open(ctx, db_fname.strpath.encode("ascii"), ffi.NULL) assert lib.dc_is_open(ctx) == 1
def test_get_info_closed(): ctx = ffi.gc( lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL), lib.dc_context_unref, ) info = cutil.from_dc_charpointer(lib.dc_get_info(ctx)) assert 'deltachat_core_version' in info assert 'database_dir' not in info
def test_empty_blobdir(tmpdir): db_fname = tmpdir.join("hello.db") # Apparently some client code expects this to be the same as passing NULL. ctx = ffi.gc( lib.dc_context_new(ffi.NULL, db_fname.strpath.encode("ascii"), b""), lib.dc_context_unref, ) assert ctx != ffi.NULL
def test_wrong_db(tmpdir): dc_context = ffi.gc( lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL), lib.dc_context_unref, ) p = tmpdir.join("hello.db") # write an invalid database file p.write("x123" * 10) assert not lib.dc_open(dc_context, p.strpath.encode("ascii"), ffi.NULL)
def test_get_info_open(tmpdir): db_fname = tmpdir.join("test.db") ctx = ffi.gc( lib.dc_context_new(ffi.NULL, db_fname.strpath.encode("ascii"), ffi.NULL), lib.dc_context_unref, ) info = cutil.from_dc_charpointer(lib.dc_get_info(ctx)) assert 'deltachat_core_version' in info assert 'database_dir' in info
def test_is_open_closed(): ctx = ffi.gc( lib.dc_context_new(lib.py_dc_callback, ffi.NULL, ffi.NULL), lib.dc_context_unref, ) assert lib.dc_is_open(ctx) == 0