Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more testcases for IMP mock #1057

Merged
merged 4 commits into from
Dec 5, 2022
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
4 changes: 4 additions & 0 deletions .github/workflows/tee-worker-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ jobs:
build-parachain-docker:
runs-on: ubuntu-latest
needs: check-file-change
if: needs.check-file-change.outputs.src == 'true'
steps:
- uses: actions/checkout@v3

Expand All @@ -78,6 +79,7 @@ jobs:
build-test:
runs-on: ubuntu-20.04
needs: check-file-change
if: needs.check-file-change.outputs.src == 'true'
strategy:
fail-fast: false
matrix:
Expand Down Expand Up @@ -140,6 +142,7 @@ jobs:
clippy:
runs-on: ubuntu-latest
needs: check-file-change
if: needs.check-file-change.outputs.src == 'true'
container: "integritee/integritee-dev:0.1.9"
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -175,6 +178,7 @@ jobs:
fmt:
runs-on: ubuntu-latest
needs: check-file-change
if: needs.check-file-change.outputs.src == 'true'
steps:
- uses: actions/checkout@v3
- name: init rust
Expand Down
8 changes: 4 additions & 4 deletions pallets/identity-management-mock/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

#![cfg(test)]

use crate::{
pub use crate::{
self as pallet_identity_management_mock,
key::{aes_encrypt_default, tee_encrypt},
ChallengeCode,
Expand All @@ -32,7 +32,7 @@ use frame_support::{
traits::{ConstU128, ConstU16, ConstU32, ConstU64, Everything},
};
use frame_system as system;
use mock_tee_primitives::{
pub use mock_tee_primitives::{
EthereumSignature, EvmNetwork, Identity, IdentityHandle, IdentityMultiSignature,
IdentityWebType, SubstrateNetwork, TwitterValidationData, UserShieldingKeyType, ValidationData,
Web2Network, Web2ValidationData, Web3CommonValidationData, Web3Network, Web3ValidationData,
Expand Down Expand Up @@ -142,11 +142,11 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
ext
}

pub fn create_mock_twitter_identity() -> Identity {
pub fn create_mock_twitter_identity(twitter_handle: &[u8]) -> Identity {
Identity {
web_type: IdentityWebType::Web2(Web2Network::Twitter),
handle: IdentityHandle::String(
b"aliceTwitterHandle".to_vec().try_into().expect("convert to BoundedVec failed"),
twitter_handle.to_vec().try_into().expect("convert to BoundedVec failed"),
),
}
}
Expand Down
57 changes: 54 additions & 3 deletions pallets/identity-management-mock/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@

use crate::{mock::*, Error};

use codec::Encode;
use frame_support::assert_noop;
use sp_core::{Pair, H256};
use sp_core::{blake2_256, Pair, H256};

#[test]
fn unpriveledged_origin_call_fails() {
Expand Down Expand Up @@ -50,7 +51,7 @@ fn set_user_shielding_key_works() {
fn link_twitter_identity_works() {
new_test_ext().execute_with(|| {
System::set_block_number(5);
setup_link_identity(2, create_mock_twitter_identity(), 5);
setup_link_identity(2, create_mock_twitter_identity(b"alice"), 5);
});
}

Expand Down Expand Up @@ -78,7 +79,7 @@ fn link_eth_identity_works() {
fn verify_twitter_identity_works() {
new_test_ext().execute_with(|| {
System::set_block_number(3);
setup_verify_twitter_identity(2, create_mock_twitter_identity(), 3);
setup_verify_twitter_identity(2, create_mock_twitter_identity(b"alice"), 3);
});
}

Expand All @@ -99,3 +100,53 @@ fn verify_eth_identity_works() {
setup_verify_eth_identity(2, p, 4);
});
}

#[test]
fn double_link_twitter_identity_works() {
new_test_ext().execute_with(|| {
// link and verify the first twitter handle
System::set_block_number(3);
setup_verify_twitter_identity(2, create_mock_twitter_identity(b"alice"), 3);
// link second twitter handle works
System::set_block_number(4);
setup_link_identity(2, create_mock_twitter_identity(b"bob"), 4);
});
}

#[test]
fn wrong_polkadot_verification_message_fails() {
new_test_ext().execute_with(|| {
System::set_block_number(3);
let p = sp_core::sr25519::Pair::from_string("//Alice", None).unwrap();
let identity = create_mock_polkadot_identity(p.public().0);
let who = 2;
setup_link_identity(who, identity.clone(), 3);

System::set_block_number(4);
let encrypted_identity = tee_encrypt(identity.encode().as_slice());

// intentionally construct a wrong verification message
let wrong_msg = blake2_256(&[0u8; 16]).to_vec();
let sig = p.sign(&wrong_msg);
let common_validation_data = Web3CommonValidationData {
message: wrong_msg.try_into().unwrap(),
signature: IdentityMultiSignature::Sr25519(sig),
};

let validation_data = match &identity.web_type {
IdentityWebType::Web3(Web3Network::Substrate(SubstrateNetwork::Polkadot)) =>
ValidationData::Web3(Web3ValidationData::Substrate(common_validation_data)),
_ => panic!("unxpected web_type"),
};

assert_noop!(
IdentityManagementMock::verify_identity(
Origin::signed(who),
H256::random(),
encrypted_identity,
tee_encrypt(validation_data.encode().as_slice()),
),
Error::<Test>::UnexpectedMessage
);
});
}