Fix tests after state_key = String from Option<String>

This commit is contained in:
Devin Ragotzy 2020-10-25 06:53:53 -04:00
parent d19c0d4129
commit 11e8856a91
6 changed files with 76 additions and 68 deletions

View File

@ -1,9 +1,10 @@
// `cargo bench` works, but if you use `cargo bench -- --save-baseline <name>`
// Because of criterion `cargo bench` works,
// but if you use `cargo bench -- --save-baseline <name>`
// or pass any other args to it, it fails with the error
// `cargo bench unknown option --save-baseline`.
// To pass args to criterion, use this form
// `cargo bench --bench <name of the bench> -- --save-baseline <name>`.
use std::{collections::BTreeMap, convert::TryFrom, time::UNIX_EPOCH, sync::Arc};
use std::{collections::BTreeMap, convert::TryFrom, sync::Arc, time::UNIX_EPOCH};
use criterion::{criterion_group, criterion_main, Criterion};
use maplit::btreemap;
@ -150,8 +151,7 @@ impl TestStore {
&[],
));
let cre = create_event.event_id();
self.0
.insert(cre.clone(), Arc::clone(&create_event));
self.0.insert(cre.clone(), Arc::clone(&create_event));
let alice_mem = to_pdu_event(
"IMA",
@ -162,8 +162,7 @@ impl TestStore {
&[cre.clone()],
&[cre.clone()],
);
self.0
.insert(alice_mem.event_id(), Arc::clone(&alice_mem));
self.0.insert(alice_mem.event_id(), Arc::clone(&alice_mem));
let join_rules = to_pdu_event(
"IJR",
@ -188,8 +187,7 @@ impl TestStore {
&[cre.clone(), join_rules.event_id()],
&[join_rules.event_id()],
);
self.0
.insert(bob_mem.event_id(), Arc::clone(&bob_mem));
self.0.insert(bob_mem.event_id(), Arc::clone(&bob_mem));
let charlie_mem = to_pdu_event(
"IMC",
@ -420,8 +418,8 @@ fn INITIAL_EVENTS() -> BTreeMap<EventId, Arc<StateEvent>> {
to_pdu_event::<EventId>(
"START",
charlie(),
EventType::RoomMessage,
None,
EventType::RoomTopic,
Some(""),
json!({}),
&[],
&[],
@ -429,8 +427,8 @@ fn INITIAL_EVENTS() -> BTreeMap<EventId, Arc<StateEvent>> {
to_pdu_event::<EventId>(
"END",
charlie(),
EventType::RoomMessage,
None,
EventType::RoomTopic,
Some(""),
json!({}),
&[],
&[],

View File

@ -87,7 +87,7 @@ pub fn auth_check(
// TODO do_size_check is false when called by `iterative_auth_check`
// do_size_check is also mostly accomplished by ruma with the exception of checking event_type,
// state_key, and json are below a certain size (255 and 65536 respectively)
// state_key, and json are below a certain size (255 and 65_536 respectively)
// Implementation of https://matrix.org/docs/spec/rooms/v1#authorization-rules
//
@ -180,9 +180,7 @@ pub fn auth_check(
// }
// If sender's domain doesn't matches state_key, reject
if incoming_event.state_key()
!= incoming_event.sender().server_name().as_str()
{
if incoming_event.state_key() != incoming_event.sender().server_name().as_str() {
tracing::warn!("state_key does not match sender");
return Ok(false);
}
@ -769,7 +767,10 @@ pub fn get_send_level(
}
/// Check user can send invite.
pub fn can_send_invite(event: &Requester<'_>, auth_events: &StateMap<Arc<StateEvent>>) -> Result<bool> {
pub fn can_send_invite(
event: &Requester<'_>,
auth_events: &StateMap<Arc<StateEvent>>,
) -> Result<bool> {
let user_level = get_user_power_level(event.sender, auth_events);
let key = (EventType::RoomPowerLevels, "".into());
let invite_level = auth_events

View File

@ -679,11 +679,11 @@ impl StateResolution {
return Ok(*depth);
}
dbg!(&sort_ev);
// dbg!(&sort_ev);
let auth_events = sort_ev.auth_events();
event = None;
for aid in auth_events {
dbg!(&aid);
// dbg!(&aid);
let aev = StateResolution::get_or_load_event(room_id, &aid, event_map, store)
.ok_or(Error::NotFound("Auth event not found".to_owned()))?;
if aev.is_type_and_key(EventType::RoomPowerLevels, "") {

View File

@ -218,8 +218,8 @@ fn INITIAL_EVENTS() -> BTreeMap<EventId, Arc<StateEvent>> {
to_pdu_event::<EventId>(
"END",
charlie(),
EventType::RoomMessage,
None,
EventType::RoomTopic,
Some(""),
json!({}),
&[],
&[],
@ -285,7 +285,7 @@ fn test_event_sort() {
shuffle(&mut events_to_sort);
let power_level = resolved_power.get(&(EventType::RoomPowerLevels, Some("".into())));
let power_level = resolved_power.get(&(EventType::RoomPowerLevels, "".into()));
let sorted_event_ids = state_res::StateResolution::mainline_sort(
&room_id(),

View File

@ -1,6 +1,6 @@
#![allow(clippy::or_fun_call, clippy::expect_fun_call)]
use std::{collections::BTreeMap, convert::TryFrom, sync::Once, time::UNIX_EPOCH, sync::Arc};
use std::{collections::BTreeMap, convert::TryFrom, sync::Arc, sync::Once, time::UNIX_EPOCH};
use ruma::{
events::{
@ -21,7 +21,11 @@ static LOGGER: Once = Once::new();
static mut SERVER_TIMESTAMP: i32 = 0;
fn do_check(events: &[Arc<StateEvent>], edges: Vec<Vec<EventId>>, expected_state_ids: Vec<EventId>) {
fn do_check(
events: &[Arc<StateEvent>],
edges: Vec<Vec<EventId>>,
expected_state_ids: Vec<EventId>,
) {
// to activate logging use `RUST_LOG=debug cargo t`
let _ = LOGGER.call_once(|| {
tracer::fmt()
@ -115,17 +119,17 @@ fn do_check(events: &[Arc<StateEvent>], edges: Vec<Vec<EventId>>, expected_state
let mut state_after = state_before.clone();
if fake_event.state_key().is_some() {
let ty = fake_event.kind().clone();
// we know there is a state_key unwrap OK
let key = fake_event.state_key().clone();
state_after.insert((ty, key), event_id.clone());
}
// if fake_event.state_key().is_some() {
let ty = fake_event.kind().clone();
// we know there is a state_key unwrap OK
let key = fake_event.state_key().clone();
state_after.insert((ty, key), event_id.clone());
// }
let auth_types = state_res::auth_types_for_event(
fake_event.kind(),
fake_event.sender(),
fake_event.state_key(),
Some(fake_event.state_key()),
fake_event.content().clone(),
);
@ -144,7 +148,7 @@ fn do_check(events: &[Arc<StateEvent>], edges: Vec<Vec<EventId>>, expected_state
&e.event_id().to_string(),
e.sender().clone(),
e.kind(),
e.state_key().as_deref(),
Some(e.state_key()).as_deref(),
e.content().clone(),
&auth_events,
prev_events,
@ -400,8 +404,8 @@ fn INITIAL_EVENTS() -> BTreeMap<EventId, Arc<StateEvent>> {
to_pdu_event::<EventId>(
"START",
charlie(),
EventType::RoomMessage,
None,
EventType::RoomTopic,
Some(""),
json!({}),
&[],
&[],
@ -409,8 +413,8 @@ fn INITIAL_EVENTS() -> BTreeMap<EventId, Arc<StateEvent>> {
to_pdu_event::<EventId>(
"END",
charlie(),
EventType::RoomMessage,
None,
EventType::RoomTopic,
Some(""),
json!({}),
&[],
&[],
@ -484,7 +488,7 @@ fn ban_with_auth_chains() {
.map(|list| list.into_iter().map(event_id).collect::<Vec<_>>())
.collect::<Vec<_>>();
let expected_state_ids = vec!["PA", "MB"]
let expected_state_ids = vec!["PA", "MB", "END"]
.into_iter()
.map(event_id)
.collect::<Vec<_>>();
@ -523,7 +527,7 @@ fn base_with_auth_chains() {
"END",
];
for id in expected.iter().map(|i| event_id(i)) {
// make sure our resolved events are equall to the expected list
// make sure our resolved events are equal to the expected list
assert!(resolved.iter().any(|eid| eid == &id), "{}", id)
}
assert_eq!(expected.len(), resolved.len())
@ -641,7 +645,10 @@ fn join_rule_with_auth_chain() {
.map(|list| list.into_iter().map(event_id).collect::<Vec<_>>())
.collect::<Vec<_>>();
let expected_state_ids = vec!["JR"].into_iter().map(event_id).collect::<Vec<_>>();
let expected_state_ids = vec!["JR", "END"]
.into_iter()
.map(event_id)
.collect::<Vec<_>>();
do_check(
&join_rule.values().cloned().collect::<Vec<_>>(),

View File

@ -1,4 +1,4 @@
use std::{collections::BTreeMap, convert::TryFrom, time::UNIX_EPOCH, sync::Arc};
use std::{collections::BTreeMap, convert::TryFrom, sync::Arc, time::UNIX_EPOCH};
use maplit::btreemap;
use ruma::{
@ -262,8 +262,8 @@ fn INITIAL_EVENTS() -> BTreeMap<EventId, Arc<StateEvent>> {
Some(zera().to_string().as_str()),
member_content_join(),
),
to_init_pdu_event("START", zera(), EventType::RoomMessage, None, json!({})),
to_init_pdu_event("END", zera(), EventType::RoomMessage, None, json!({})),
to_init_pdu_event("START", zera(), EventType::RoomTopic, Some(""), json!({})),
to_init_pdu_event("END", zera(), EventType::RoomTopic, Some(""), json!({})),
]
.into_iter()
.map(|ev| (ev.event_id(), ev))
@ -280,7 +280,11 @@ fn INITIAL_EDGES() -> Vec<EventId> {
.collect::<Vec<_>>()
}
fn do_check(events: &[Arc<StateEvent>], edges: Vec<Vec<EventId>>, expected_state_ids: Vec<EventId>) {
fn do_check(
events: &[Arc<StateEvent>],
edges: Vec<Vec<EventId>>,
expected_state_ids: Vec<EventId>,
) {
// to activate logging use `RUST_LOG=debug cargo t one_test_only`
let _ = LOGGER.call_once(|| {
tracer::fmt()
@ -380,17 +384,17 @@ fn do_check(events: &[Arc<StateEvent>], edges: Vec<Vec<EventId>>, expected_state
let mut state_after = state_before.clone();
if fake_event.state_key().is_some() {
let ty = fake_event.kind().clone();
// we know there is a state_key unwrap OK
let key = fake_event.state_key().clone();
state_after.insert((ty, key), event_id.clone());
}
// if fake_event.state_key().is_some() {
let ty = fake_event.kind().clone();
// we know there is a state_key unwrap OK
let key = fake_event.state_key().clone();
state_after.insert((ty, key), event_id.clone());
// }
let auth_types = state_res::auth_types_for_event(
fake_event.kind(),
fake_event.sender(),
fake_event.state_key(),
Some(fake_event.state_key()),
fake_event.content().clone(),
);
@ -409,7 +413,7 @@ fn do_check(events: &[Arc<StateEvent>], edges: Vec<Vec<EventId>>, expected_state
&e.event_id().to_string(),
e.sender().clone(),
e.kind(),
e.state_key().as_deref(),
Some(e.state_key()).as_deref(),
e.content().clone(),
&auth_events,
prev_events,
@ -498,7 +502,7 @@ fn ban_vs_power_level() {
.map(|list| list.into_iter().map(event_id).collect::<Vec<_>>())
.collect::<Vec<_>>();
let expected_state_ids = vec!["PA", "MA", "MB"]
let expected_state_ids = vec!["PA", "MA", "MB", "END"]
.into_iter()
.map(event_id)
.collect::<Vec<_>>();
@ -543,7 +547,7 @@ fn topic_basic() {
.map(|list| list.into_iter().map(event_id).collect::<Vec<_>>())
.collect::<Vec<_>>();
let expected_state_ids = vec!["PA2", "T2"]
let expected_state_ids = vec!["PA2", "T2", "END"]
.into_iter()
.map(event_id)
.collect::<Vec<_>>();
@ -580,7 +584,7 @@ fn topic_reset() {
.map(|list| list.into_iter().map(event_id).collect::<Vec<_>>())
.collect::<Vec<_>>();
let expected_state_ids = vec!["T1", "MB", "PA"]
let expected_state_ids = vec!["T1", "MB", "PA", "END"]
.into_iter()
.map(event_id)
.collect::<Vec<_>>();
@ -612,7 +616,7 @@ fn join_rule_evasion() {
.map(|list| list.into_iter().map(event_id).collect::<Vec<_>>())
.collect::<Vec<_>>();
let expected_state_ids = vec![event_id("JR")];
let expected_state_ids = vec![event_id("JR"), event_id("END")];
do_check(events, edges, expected_state_ids)
}
@ -648,7 +652,10 @@ fn offtopic_power_level() {
.map(|list| list.into_iter().map(event_id).collect::<Vec<_>>())
.collect::<Vec<_>>();
let expected_state_ids = vec!["PC"].into_iter().map(event_id).collect::<Vec<_>>();
let expected_state_ids = vec!["PC", "END"]
.into_iter()
.map(event_id)
.collect::<Vec<_>>();
do_check(events, edges, expected_state_ids)
}
@ -680,7 +687,7 @@ fn topic_setting() {
json!({"users": {alice(): 100, bob(): 50}}),
),
to_init_pdu_event("T3", bob(), EventType::RoomTopic, Some(""), json!({})),
to_init_pdu_event("MZ1", zera(), EventType::RoomMessage, None, json!({})),
to_init_pdu_event("MZ1", zera(), EventType::RoomTopic, Some(""), json!({})),
to_init_pdu_event("T4", alice(), EventType::RoomTopic, Some(""), json!({})),
];
@ -692,7 +699,7 @@ fn topic_setting() {
.map(|list| list.into_iter().map(event_id).collect::<Vec<_>>())
.collect::<Vec<_>>();
let expected_state_ids = vec!["T4", "PA2"]
let expected_state_ids = vec!["T4", "PA2", "END"]
.into_iter()
.map(event_id)
.collect::<Vec<_>>();
@ -778,8 +785,7 @@ impl TestStore {
&[],
);
let cre = create_event.event_id();
self.0
.insert(cre.clone(), Arc::clone(&create_event));
self.0.insert(cre.clone(), Arc::clone(&create_event));
let alice_mem = to_pdu_event(
"IMA",
@ -790,8 +796,7 @@ impl TestStore {
&[cre.clone()],
&[cre.clone()],
);
self.0
.insert(alice_mem.event_id(), Arc::clone(&alice_mem));
self.0.insert(alice_mem.event_id(), Arc::clone(&alice_mem));
let join_rules = to_pdu_event(
"IJR",
@ -802,8 +807,7 @@ impl TestStore {
&[cre.clone(), alice_mem.event_id()],
&[alice_mem.event_id()],
);
self.0
.insert(join_rules.event_id(), join_rules.clone());
self.0.insert(join_rules.event_id(), join_rules.clone());
// Bob and Charlie join at the same time, so there is a fork
// this will be represented in the state_sets when we resolve
@ -816,8 +820,7 @@ impl TestStore {
&[cre.clone(), join_rules.event_id()],
&[join_rules.event_id()],
);
self.0
.insert(bob_mem.event_id(), bob_mem.clone());
self.0.insert(bob_mem.event_id(), bob_mem.clone());
let charlie_mem = to_pdu_event(
"IMC",
@ -828,8 +831,7 @@ impl TestStore {
&[cre, join_rules.event_id()],
&[join_rules.event_id()],
);
self.0
.insert(charlie_mem.event_id(), charlie_mem.clone());
self.0.insert(charlie_mem.event_id(), charlie_mem.clone());
let state_at_bob = [&create_event, &alice_mem, &join_rules, &bob_mem]
.iter()