Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions deltachat-ffi/deltachat.h
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,21 @@ char* dc_get_connectivity_html (dc_context_t* context);
void dc_configure (dc_context_t* context);


/**
* Add fake transport that cannot be used to connect.
*
* Used for offline tests only.
*

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it intentional that there is no

*  @memberof dc_context_t
* ... 

doc for instructing doxygen to group it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added @memberof and @params

* To add a transport, use JSON-RPC calls `add_or_update_transport`
* and `add_transport_from_qr` instead.
*
* @memberof dc_context_t
* @param context The context object.
* @param addr The email address of the new transport.
*/
void dc_add_pseudo_transport (dc_context_t* context, const char *addr);


/**
* Check if the context is already configured.
*
Expand Down
16 changes: 16 additions & 0 deletions deltachat-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use deltachat::key::preconfigure_keypair;
use deltachat::message::MsgId;
use deltachat::qr_code_generator::{create_qr_svg, generate_backup_qr, get_securejoin_qr_svg};
use deltachat::stock_str::StockMessage;
use deltachat::transport::add_pseudo_transport;
use deltachat::webxdc::StatusUpdateSerial;
use deltachat::*;
use deltachat::{accounts::Accounts, log::LogExt};
Expand Down Expand Up @@ -414,6 +415,21 @@ pub unsafe extern "C" fn dc_configure(context: *mut dc_context_t) {
spawn_configure(ctx.clone());
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn dc_add_pseudo_transport(
context: *mut dc_context_t,
addr: *const libc::c_char,
) {
if context.is_null() {
eprintln!("ignoring careless call to dc_add_pseudo_transport()");
return;
}

let ctx = unsafe { &*context };
let addr = to_string_lossy(addr);
block_on(add_pseudo_transport(ctx, &addr)).log_err(ctx).ok();
}

#[unsafe(no_mangle)]
pub unsafe extern "C" fn dc_is_configured(context: *mut dc_context_t) -> libc::c_int {
if context.is_null() {
Expand Down
5 changes: 5 additions & 0 deletions python/src/deltachat/testplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
from queue import Queue
from typing import Callable, Dict, List, Optional

from .capi import lib
from .cutil import as_dc_charpointer

import pytest
from _pytest._code import Source

Expand Down Expand Up @@ -366,6 +369,7 @@ def get_pseudo_configured_account(self, passphrase: Optional[str] = None) -> Acc
ac.open(passphrase)
acname = ac._logid
addr = f"{acname}@offline.org"
lib.dc_add_pseudo_transport(ac._dc_context, as_dc_charpointer(addr))
ac.update_config(
{
"configured_addr": addr,
Expand All @@ -374,6 +378,7 @@ def get_pseudo_configured_account(self, passphrase: Optional[str] = None) -> Acc
)
self._preconfigure_key(ac)
self._acsetup.init_logging(ac)
assert ac.is_configured(), "Pseudo configured account should look like if it is configured"
return ac

def new_online_configuring_account(self, cloned_from=None, **kwargs) -> Account:
Expand Down
57 changes: 23 additions & 34 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::log::LogExt;
use crate::mimefactory::RECOMMENDED_FILE_SIZE;
use crate::sync::{self, Sync::*, SyncData};
use crate::tools::get_abs_path;
use crate::transport::{add_pseudo_transport, transport_addrs};
use crate::transport::transport_addrs;
use crate::{constants, stats};

/// The available configuration keys.
Expand Down Expand Up @@ -753,39 +753,28 @@ impl Context {
bail!("Cannot unset configured_addr");
};

if !self.is_configured().await? {
info!(
self,
"Creating a pseudo configured account which will not be able to send or receive messages. Only meant for tests!"
);
add_pseudo_transport(self, addr).await?;
self.sql
.set_raw_config(Config::ConfiguredAddr.as_ref(), Some(addr))
.await?;
} else {
self.sql
.transaction(|transaction| {
if transaction.query_row(
"SELECT COUNT(*) FROM transports WHERE addr=?",
(addr,),
|row| {
let res: i64 = row.get(0)?;
Ok(res)
},
)? == 0
{
bail!("Address does not belong to any transport.");
}
transaction.execute(
"UPDATE config SET value=? WHERE keyname='configured_addr'",
(addr,),
)?;

Ok(())
})
.await?;
self.sql.uncache_raw_config("configured_addr").await;
}
self.sql
.transaction(|transaction| {
if transaction.query_row(
"SELECT COUNT(*) FROM transports WHERE addr=?",
(addr,),
|row| {
let res: i64 = row.get(0)?;
Ok(res)
},
)? == 0
{
bail!("Address does not belong to any transport.");
}
transaction.execute(
"INSERT OR REPLACE INTO config (keyname, value) VALUES ('configured_addr', ?)",
(addr,),
)?;

Ok(())
})
.await?;
self.sql.uncache_raw_config("configured_addr").await;
}
_ => {
self.sql.set_raw_config(key.as_ref(), value).await?;
Expand Down
2 changes: 1 addition & 1 deletion src/configure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,7 @@ mod tests {
let mut tcm = TestContextManager::new();
let t = &tcm.unconfigured().await;

// Setting ConfiguredAddr on an unconfigured account creates a pseudo transport
add_pseudo_transport(t, "[email protected]").await?;
t.set_config(Config::ConfiguredAddr, Some("[email protected]"))
.await?;
assert_eq!(t.count_transports().await?, 1);
Expand Down
4 changes: 4 additions & 0 deletions src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,7 @@ mod tests {
use crate::config::Config;
use crate::test_utils::{TestContext, TestContextManager, alice_keypair};
use crate::tools::SystemTime;
use crate::transport::add_pseudo_transport;

static KEYPAIR: LazyLock<SignedSecretKey> = LazyLock::new(alice_keypair);

Expand Down Expand Up @@ -811,6 +812,7 @@ i8pcjGO+IZffvyZJVRWfVooBJmWWbPB1pueo3tx8w3+fcuzpxz+RLFKaPyqXO+dD
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_load_self_generate_public() {
let t = TestContext::new().await;
add_pseudo_transport(&t, "[email protected]").await.unwrap();
t.set_config(Config::ConfiguredAddr, Some("[email protected]"))
.await
.unwrap();
Expand All @@ -821,6 +823,7 @@ i8pcjGO+IZffvyZJVRWfVooBJmWWbPB1pueo3tx8w3+fcuzpxz+RLFKaPyqXO+dD
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_load_self_generate_secret() {
let t = TestContext::new().await;
add_pseudo_transport(&t, "[email protected]").await.unwrap();
t.set_config(Config::ConfiguredAddr, Some("[email protected]"))
.await
.unwrap();
Expand All @@ -833,6 +836,7 @@ i8pcjGO+IZffvyZJVRWfVooBJmWWbPB1pueo3tx8w3+fcuzpxz+RLFKaPyqXO+dD
use std::thread;

let t = TestContext::new().await;
add_pseudo_transport(&t, "[email protected]").await.unwrap();
t.set_config(Config::ConfiguredAddr, Some("[email protected]"))
.await
.unwrap();
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ pub mod stock_str;
pub mod storage_usage;
mod sync;
mod token;
mod transport;
pub mod transport;
mod update_helper;
pub mod webxdc;
#[macro_use]
Expand Down
4 changes: 4 additions & 0 deletions src/pgp/pgp_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::sync::LazyLock;
use tokio::sync::OnceCell;

use super::*;
use crate::transport::add_pseudo_transport;
use crate::{
config::Config,
decrypt,
Expand All @@ -19,6 +20,9 @@ async fn decrypt_bytes(
auth_tokens_for_decryption: &[String],
) -> Result<pgp::composed::Message<'static>> {
let t = &TestContext::new().await;
add_pseudo_transport(t, "[email protected]")
.await
.expect("Failed to add pseudo transport");
t.set_config(Config::ConfiguredAddr, Some("[email protected]"))
.await
.expect("Failed to configure address");
Expand Down
3 changes: 3 additions & 0 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,9 @@ impl TestContext {
/// The context will be configured but the key will not be pre-generated so if a key is
/// used the fingerprint will be different every time.
pub async fn configure_addr(&self, addr: &str) {
add_pseudo_transport(&self.ctx, addr)
.await
.expect("Failed to add pseudo transport");
self.ctx
.set_config(Config::ConfiguredAddr, Some(addr))
.await
Expand Down
2 changes: 1 addition & 1 deletion src/transport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,7 +765,7 @@ pub(crate) fn maybe_update_sending_transport(
}

/// Adds transport entry to the `transports` table with empty configuration.
pub(crate) async fn add_pseudo_transport(context: &Context, addr: &str) -> Result<()> {
pub async fn add_pseudo_transport(context: &Context, addr: &str) -> Result<()> {
context.sql
.execute(
"INSERT OR IGNORE INTO transports (addr, entered_param, configured_param) VALUES (?, ?, ?)",
Expand Down
Loading