Skip to content

Commit

Permalink
Fix cargo fmt CI job not formatting rust tests and interface (#678)
Browse files Browse the repository at this point in the history
The CI job needs to be adapted to the new file structure introduced with the rust canister tests.
  • Loading branch information
Frederik Rothenberger authored Jun 13, 2022
1 parent 1f86760 commit 59c06f6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cargo-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ jobs:
# We don't want to commit formatting changes to main
if: ${{ github.ref != 'refs/heads/main' }}
with:
add: src/internet_identity
add: src
author_name: Formatting Committer
author_email: "<nobody@example.com>"
message: "Updating rust formatting"
Expand Down
33 changes: 27 additions & 6 deletions src/canister_tests/src/api.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,50 @@
/** The functions here are derived (manually) from Internet Identity's Candid file */

use crate::framework;
use ic_state_machine_tests::{CanisterId, StateMachine};
use internet_identity_interface as types;

/// A fake "health check" method that just checks the canister is alive a well.
pub fn health_check(env: &StateMachine, canister_id: CanisterId) {
let user_number: types::UserNumber = 0;
let _: (Vec<types::DeviceData>,) = framework::call_candid(env, canister_id, "lookup", (user_number,)).unwrap();
let _: (Vec<types::DeviceData>,) =
framework::call_candid(env, canister_id, "lookup", (user_number,)).unwrap();
}

pub fn create_challenge(env: &StateMachine, canister_id: CanisterId) -> types::Challenge {
let (c,) = framework::call_candid(env, canister_id, "create_challenge", ()).unwrap();
c
}

pub fn register(env: &StateMachine, canister_id: CanisterId, device_data: types::DeviceData, challenge_attempt: types::ChallengeAttempt) -> types::RegisterResponse {
match framework::call_candid_as(env, canister_id, framework::some_principal(), "register", (device_data, challenge_attempt)) {
pub fn register(
env: &StateMachine,
canister_id: CanisterId,
device_data: types::DeviceData,
challenge_attempt: types::ChallengeAttempt,
) -> types::RegisterResponse {
match framework::call_candid_as(
env,
canister_id,
framework::some_principal(),
"register",
(device_data, challenge_attempt),
) {
Ok((r,)) => r,
Err(e) => panic!("Failed to register: {:?}", e),
}
}

pub fn lookup(env: &StateMachine, canister_id: CanisterId, user_number: types::UserNumber) -> Vec<types::DeviceData> {
match framework::call_candid_as(env, canister_id, framework::some_principal(), "lookup", (user_number,)) {
pub fn lookup(
env: &StateMachine,
canister_id: CanisterId,
user_number: types::UserNumber,
) -> Vec<types::DeviceData> {
match framework::call_candid_as(
env,
canister_id,
framework::some_principal(),
"lookup",
(user_number,),
) {
Ok((r,)) => r,
Err(e) => panic!("Failed to lookup: {:?}", e),
}
Expand Down
58 changes: 32 additions & 26 deletions src/canister_tests/src/framework.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use candid::{parser::value::IDLValue, IDLArgs};
use candid::utils::{decode_args, encode_args, ArgumentDecoder, ArgumentEncoder};
use ic_types::{Principal};
use ic_state_machine_tests::{PrincipalId, CanisterId, StateMachine, UserError, WasmResult};
use serde_bytes::ByteBuf;
use lazy_static::lazy_static;
use candid::{parser::value::IDLValue, IDLArgs};
use ic_state_machine_tests::{CanisterId, PrincipalId, StateMachine, UserError, WasmResult};
use ic_types::Principal;
use internet_identity_interface as types;
use lazy_static::lazy_static;
use serde_bytes::ByteBuf;
use std::env;
use std::path;

Expand Down Expand Up @@ -41,27 +41,34 @@ lazy_static! {
};
}


/** Helper that returns the content of `default_path` if found, or None if the file does not exist.
* The `env_var` environment variable is also read for custom location; if the variable is set
* _but_ the Wasm module is not present, we simply panic (i.e. we don't return None)
*/
fn get_wasm_path(env_var: String, default_path: &path::PathBuf) -> Option<Vec<u8>> {
match env::var_os(env_var.clone()) {
None => {
if ! default_path.exists() {
return None
if !default_path.exists() {
return None;
}
Some(std::fs::read(default_path).expect(&format!("could not read Wasm module: {:?}", default_path)))
},
Some(
std::fs::read(default_path)
.expect(&format!("could not read Wasm module: {:?}", default_path)),
)
}
Some(path) => {
let pathname: String = path.into_string().expect(&format!("Invalid string path for {}", env_var.clone()));
let pathname: String = path
.into_string()
.expect(&format!("Invalid string path for {}", env_var.clone()));
let path = path::PathBuf::from(pathname.clone());
if ! path.exists() {
if !path.exists() {
panic!("Could not find {}", pathname);
}
Some(std::fs::read(path.clone()).expect(&format!("could not read Wasm module: {:?}", path)))
},
Some(
std::fs::read(path.clone())
.expect(&format!("could not read Wasm module: {:?}", path)),
)
}
}
}

Expand Down Expand Up @@ -97,7 +104,7 @@ pub fn some_device_data() -> types::DeviceData {
}
}

/* Here are a few functions that are not directly related to II and could be upstreamed
/* Here are a few functions that are not directly related to II and could be upstreamed
* (were actually stolen from somewhere else)
*/

Expand All @@ -108,10 +115,10 @@ pub fn call_candid_as<Input, Output>(
sender: PrincipalId,
method: &str,
input: Input,
) -> Result<Output, CallError>
) -> Result<Output, CallError>
where
Input: ArgumentEncoder,
Output: for<'a> ArgumentDecoder<'a>,
Input: ArgumentEncoder,
Output: for<'a> ArgumentDecoder<'a>,
{
with_candid(input, |bytes| {
env.execute_ingress_as(sender, canister_id, method, bytes)
Expand All @@ -124,10 +131,10 @@ pub fn call_candid<Input, Output>(
canister_id: CanisterId,
method: &str,
input: Input,
) -> Result<Output, CallError>
) -> Result<Output, CallError>
where
Input: ArgumentEncoder,
Output: for<'a> ArgumentDecoder<'a>,
Input: ArgumentEncoder,
Output: for<'a> ArgumentDecoder<'a>,
{
with_candid(input, |bytes| {
env.execute_ingress(canister_id, method, bytes)
Expand All @@ -145,10 +152,10 @@ pub enum CallError {
pub fn with_candid<Input, Output>(
input: Input,
f: impl FnOnce(Vec<u8>) -> Result<WasmResult, UserError>,
) -> Result<Output, CallError>
) -> Result<Output, CallError>
where
Input: ArgumentEncoder,
Output: for<'a> ArgumentDecoder<'a>,
Input: ArgumentEncoder,
Output: for<'a> ArgumentDecoder<'a>,
{
let in_bytes = encode_args(input).expect("failed to encode args");
match f(in_bytes) {
Expand All @@ -157,10 +164,9 @@ Output: for<'a> ArgumentDecoder<'a>,
"Failed to decode bytes {:?} as candid type: {}",
std::any::type_name::<Output>(),
e
)
)
})),
Ok(WasmResult::Reject(message)) => Err(CallError::Reject(message)),
Err(user_error) => Err(CallError::UserError(user_error)),
}
}

14 changes: 11 additions & 3 deletions src/canister_tests/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::api;
use crate::framework;
use ic_state_machine_tests::StateMachine;
use internet_identity_interface as types;
use crate::framework;
use crate::api;

#[test]
fn ii_canister_can_be_installed() {
Expand All @@ -24,7 +24,15 @@ fn ii_upgrade_retains_anchors() {
let env = StateMachine::new();
let canister_id = framework::install_ii_canister(&env, framework::II_WASM_PREVIOUS.clone());
let challenge = api::create_challenge(&env, canister_id);
let user_number = match api::register(&env, canister_id, framework::some_device_data(), types::ChallengeAttempt { chars: "a".to_string(), key: challenge.challenge_key }) {
let user_number = match api::register(
&env,
canister_id,
framework::some_device_data(),
types::ChallengeAttempt {
chars: "a".to_string(),
key: challenge.challenge_key,
},
) {
types::RegisterResponse::Registered { user_number } => user_number,
response => panic!("could not register: {:?}", response),
};
Expand Down
4 changes: 1 addition & 3 deletions src/internet_identity_interface/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use serde_bytes::ByteBuf;
use candid::{CandidType, Deserialize, Principal};
use serde_bytes::ByteBuf;

pub type UserNumber = u64;
pub type CredentialId = ByteBuf;
Expand Down Expand Up @@ -62,8 +62,6 @@ pub struct Challenge {

pub type ChallengeKey = String;



// The user's attempt
#[derive(Clone, Debug, CandidType, Deserialize)]
pub struct ChallengeAttempt {
Expand Down

0 comments on commit 59c06f6

Please sign in to comment.