Skip to content

Commit

Permalink
tests correction done
Browse files Browse the repository at this point in the history
  • Loading branch information
1010adigupta committed Aug 3, 2023
1 parent c4e23ad commit 4f7f17f
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 29 deletions.
7 changes: 7 additions & 0 deletions teos-common/src/appointment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ impl Locators {
locator,
})
}

pub fn new(txid: Txid) -> Self {
Locators {
data: txid[..LOCATOR_LEN].try_into().unwrap(),
locator: Locator(txid[..LOCATOR_LEN].try_into().unwrap()),
}
}
}


Expand Down
5 changes: 3 additions & 2 deletions teos/src/watcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ mod tests {
receipt.subscription_expiry(),
START_HEIGHT as u32 + DURATION
);

#[cfg(feature = "accountable")]
assert!(cryptography::verify(
&receipt.to_vec(),
&receipt.signature().unwrap(),
Expand Down Expand Up @@ -1072,8 +1072,9 @@ mod tests {
user_id,
ConfirmationStatus::InMempoolSince(chain.get_block_count()),
);
#[cfg(feature = "accountable")]
let receipt = watcher.add_appointment(triggered_appointment.inner, signature);

#[cfg(feature = "accountable")]
assert!(matches!(
receipt,
Err(AddAppointmentFailure::AlreadyTriggered)
Expand Down
27 changes: 19 additions & 8 deletions watchtower-plugin/src/dbm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,7 @@ mod tests {
receipt.available_slots(),
receipt.subscription_start(),
receipt.subscription_expiry(),
HashMap::new(),
Vec::new(),
Vec::new(),
Vec::new()
);
Expand Down Expand Up @@ -1007,6 +1007,7 @@ mod tests {
dbm.store_tower_record(tower_id, net_addr, &receipt).unwrap();

// Add some appointment receipts and check they match
#[cfg(feature = "accountable")]
let mut receipts = HashMap::new();
for _ in 0..5 {
let appointment = generate_random_appointment(None);
Expand Down Expand Up @@ -1042,13 +1043,14 @@ mod tests {

// If there is no appointment receipt for the given (locator, tower_id) pair, Error::NotFound is returned
// Try first with both being unknown
#[cfg(feature = "accountable")]
assert!(dbm.load_appointment_receipt(tower_id, appointment.locator).is_none());

// Add the tower but not the appointment and try again
let net_addr = "talaia.watch";
let receipt = get_random_registration_receipt();
dbm.store_tower_record(tower_id, net_addr, &receipt).unwrap();

#[cfg(feature = "accountable")]
assert!(dbm.load_appointment_receipt(tower_id, appointment.locator).is_none());

// Add both
Expand All @@ -1058,18 +1060,20 @@ mod tests {
receipt.subscription_start(),
receipt.subscription_expiry()
);
#[cfg(feature = "accountable")]
let appointment_receipt = AppointmentReceipt::with_signature(
"user_signature".to_owned(),
42,
"tower_signature".to_owned()
);
#[cfg(feature = "accountable")]
dbm.store_appointment_receipt(
tower_id,
appointment.locator,
tower_summary.available_slots,
&appointment_receipt
).unwrap();

#[cfg(feature = "accountable")]
assert_eq!(
dbm.load_appointment_receipt(tower_id, appointment.locator).unwrap(),
appointment_receipt
Expand Down Expand Up @@ -1101,14 +1105,15 @@ mod tests {
let mut invalid_appointments = HashSet::new();
for _ in 0..5 {
let appointment = generate_random_appointment(None);
#[cfg(feature = "accountable")]
let appointment_receipt = AppointmentReceipt::with_signature(
user_signature.to_owned(),
42,
"tower_signature".to_owned()
);
let pending_appointment = generate_random_appointment(None);
let invalid_appointment = generate_random_appointment(None);

#[cfg(feature = "accountable")]
dbm.store_appointment_receipt(
tower_id,
appointment.locator,
Expand Down Expand Up @@ -1341,25 +1346,28 @@ mod tests {

// Store a misbehaving proof and load it back
let appointment = generate_random_appointment(None);
#[cfg(feature = "accountable")]
let appointment_receipt = AppointmentReceipt::with_signature(
"user_signature".to_owned(),
42,
"tower_signature".to_owned()
);

#[cfg(feature = "accountable")]
let proof = MisbehaviorProof::new(
appointment.locator,
appointment_receipt,
get_random_user_id()
);

#[cfg(feature = "accountable")]
dbm.store_misbehaving_proof(tower_id, &proof).unwrap();
#[cfg(feature = "accountable")]
assert_eq!(dbm.load_misbehaving_proof(tower_id).unwrap(), proof);
}

#[test]
fn test_store_load_non_existing_misbehaving_proof() {
let dbm = DBM::in_memory().unwrap();
#[cfg(feature = "accountable")]
assert!(dbm.load_misbehaving_proof(get_random_user_id()).is_none());
}

Expand All @@ -1383,25 +1391,28 @@ mod tests {

// // Store a misbehaving proof check
let appointment = generate_random_appointment(None);
#[cfg(feature = "accountable")]
let appointment_receipt = AppointmentReceipt::with_signature(
"user_signature".to_owned(),
42,
"tower_signature".to_owned()
);

#[cfg(feature = "accountable")]
let proof = MisbehaviorProof::new(
appointment.locator,
appointment_receipt,
get_random_user_id()
);

#[cfg(feature = "accountable")]
dbm.store_misbehaving_proof(tower_id, &proof).unwrap();
#[cfg(feature = "accountable")]
assert!(dbm.exists_misbehaving_proof(tower_id));
}

#[test]
fn test_exists_misbehaving_proof_false() {
let dbm = DBM::in_memory().unwrap();
#[cfg(feature = "accountable")]
assert!(!dbm.exists_misbehaving_proof(get_random_user_id()));
}

Expand Down
52 changes: 48 additions & 4 deletions watchtower-plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,21 @@ impl MisbehaviorProof {
#[cfg(test)]
mod tests {
use super::*;

#[cfg(feature = "accountable")]
const STATUSES: [TowerStatus; 5] = [
TowerStatus::Reachable,
TowerStatus::TemporaryUnreachable,
TowerStatus::Unreachable,
TowerStatus::SubscriptionError,
TowerStatus::Misbehaving,
];

#[cfg(not(feature = "accountable"))]
const STATUSES: [TowerStatus; 4] = [
TowerStatus::Reachable,
TowerStatus::TemporaryUnreachable,
TowerStatus::Unreachable,
TowerStatus::SubscriptionError,
];
const AVAILABLE_SLOTS: u32 = 21;
const SUBSCRIPTION_START: u32 = 100;
const SUBSCRIPTION_EXPIRY: u32 = SUBSCRIPTION_START + 42;
Expand Down Expand Up @@ -341,6 +347,7 @@ mod tests {
}

#[test]
#[cfg(feature = "accountable")]
fn test_is_misbehaving() {
for status in STATUSES {
if status == Misbehaving {
Expand Down Expand Up @@ -461,7 +468,7 @@ mod tests {
use super::*;

use teos_common::test_utils::{generate_random_appointment, get_random_user_id};

#[cfg(feature = "accountable")]
impl TowerInfo {
pub fn empty(
net_addr: String,
Expand All @@ -480,8 +487,28 @@ mod tests {
)
}
}
#[cfg(not(feature = "accountable"))]
impl TowerInfo {
pub fn empty(
net_addr: String,
available_slots: u32,
subscription_start: u32,
subscription_expiry: u32,
) -> Self {
TowerInfo::new(
net_addr,
available_slots,
subscription_start,
subscription_expiry,
Vec::new(),
Vec::new(),
Vec::new(),
)
}
}

#[test]
#[cfg(feature = "accountable")]
fn test_new() {
let tower_info = TowerInfo::new(
"addr".to_owned(),
Expand All @@ -494,6 +521,23 @@ mod tests {
);

assert!(tower_info.status.is_reachable());
#[cfg(feature = "accountable")]
assert!(tower_info.misbehaving_proof.is_none());
}
#[cfg(not(feature = "accountable"))]
fn test_new() {
let tower_info = TowerInfo::new(
"addr".to_owned(),
AVAILABLE_SLOTS,
SUBSCRIPTION_START,
SUBSCRIPTION_EXPIRY,
Vec::new(),
Vec::new(),
Vec::new(),
);

assert!(tower_info.status.is_reachable());
#[cfg(feature = "accountable")]
assert!(tower_info.misbehaving_proof.is_none());
}

Expand All @@ -510,7 +554,7 @@ mod tests {
tower_info.status = TowerStatus::Unreachable;
assert_eq!(unreachable_tower, tower_info);
}

#[cfg(feature = "accountable")]
#[test]
fn test_set_misbehaving_proof() {
let mut tower_info = TowerInfo::empty(
Expand Down
48 changes: 42 additions & 6 deletions watchtower-plugin/src/net/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,10 +435,14 @@ mod tests {
// in case there are no errors. All the error cases will be tested in `send_appointment`.
let (tower_sk, tower_pk) = cryptography::get_random_keypair();
let appointment = generate_random_appointment(None);

#[cfg(feature = "accountable")]
let appointment_receipt = get_random_appointment_receipt(tower_sk);
#[cfg(feature = "accountable")]
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator, &appointment_receipt);
#[cfg(not(feature = "accountable"))]
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator);

let mut server = mockito::Server::new_async().await;
let api_mock = server
Expand All @@ -448,7 +452,7 @@ mod tests {
.with_body(json!(add_appointment_response).to_string())
.create_async()
.await;

#[cfg(feature = "accountable")]
let (response, receipt) = add_appointment(
TowerId(tower_pk),
&NetAddr::new(server.url()),
Expand All @@ -458,20 +462,35 @@ mod tests {
)
.await
.unwrap();
#[cfg(not(feature = "accountable"))]
let (response) = add_appointment(
TowerId(tower_pk),
&NetAddr::new(server.url()),
&None,
&appointment,
"None"
)
.await
.unwrap();

api_mock.assert_async().await;
assert_eq!(response, add_appointment_response.available_slots);
#[cfg(feature = "accountable")]
assert_eq!(receipt, appointment_receipt);
}

#[tokio::test]
async fn test_send_appointment() {
let (tower_sk, tower_pk) = cryptography::get_random_keypair();
let appointment = generate_random_appointment(None);

#[cfg(feature = "accountable")]
let appointment_receipt = get_random_appointment_receipt(tower_sk);
#[cfg(feature = "accountable")]
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator, &appointment_receipt);
#[cfg(not(feature = "accountable"))]
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator);

let mut server = mockito::Server::new_async().await;
let api_mock = server
Expand All @@ -481,7 +500,7 @@ mod tests {
.with_body(json!(add_appointment_response).to_string())
.create_async()
.await;

#[cfg(feature = "accountable")]
let (response, receipt) = send_appointment(
TowerId(tower_pk),
&NetAddr::new(server.url()),
Expand All @@ -491,9 +510,20 @@ mod tests {
)
.await
.unwrap();
#[cfg(not(feature = "accountable"))]
let (response) = send_appointment(
TowerId(tower_pk),
&NetAddr::new(server.url()),
&None,
&appointment,
"None",
)
.await
.unwrap();

api_mock.assert_async().await;
assert_eq!(response, add_appointment_response);
#[cfg(feature = "accountable")]
assert_eq!(receipt, appointment_receipt);
}

Expand All @@ -502,9 +532,14 @@ mod tests {
let (sybil_tower_sk, sibyl_tower_pk) = cryptography::get_random_keypair();
let appointment = generate_random_appointment(None);

let appointment_receipt = get_random_appointment_receipt(sybil_tower_sk);
#[cfg(feature = "accountable")]
let appointment_receipt = get_random_appointment_receipt(tower_sk);
#[cfg(feature = "accountable")]
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator, &appointment_receipt);
#[cfg(not(feature = "accountable"))]
let add_appointment_response =
get_dummy_add_appointment_response(appointment.locator);

let mut server = mockito::Server::new_async().await;
let api_mock = server
Expand All @@ -521,12 +556,13 @@ mod tests {
&NetAddr::new(server.url()),
&None,
&appointment,
appointment_receipt.user_signature(),
"None",
)
.await
.unwrap_err();

api_mock.assert_async().await;
#[cfg(feature = "accountable")]
if let AddAppointmentError::SignatureError(proof) = error {
assert_eq!(
MisbehaviorProof::new(
Expand Down
Loading

0 comments on commit 4f7f17f

Please sign in to comment.