Skip to content

Commit 4d92915

Browse files
Kailai-Wangjingleizhang
authored andcommitted
Add more testcases for IMP mock (#1057)
* add more testcases * fix compile * fix file checker in tee-worker-ci Co-authored-by: ericzhang <[email protected]>
1 parent acc3f81 commit 4d92915

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

.github/workflows/tee-worker-ci.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ jobs:
5858
build-parachain-docker:
5959
runs-on: ubuntu-latest
6060
needs: check-file-change
61+
if: needs.check-file-change.outputs.src == 'true'
6162
steps:
6263
- uses: actions/checkout@v3
6364

@@ -78,6 +79,7 @@ jobs:
7879
build-test:
7980
runs-on: ubuntu-20.04
8081
needs: check-file-change
82+
if: needs.check-file-change.outputs.src == 'true'
8183
strategy:
8284
fail-fast: false
8385
matrix:
@@ -140,6 +142,7 @@ jobs:
140142
clippy:
141143
runs-on: ubuntu-latest
142144
needs: check-file-change
145+
if: needs.check-file-change.outputs.src == 'true'
143146
container: "integritee/integritee-dev:0.1.9"
144147
steps:
145148
- uses: actions/checkout@v3
@@ -175,6 +178,7 @@ jobs:
175178
fmt:
176179
runs-on: ubuntu-latest
177180
needs: check-file-change
181+
if: needs.check-file-change.outputs.src == 'true'
178182
steps:
179183
- uses: actions/checkout@v3
180184
- name: init rust

pallets/identity-management-mock/src/mock.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
#![cfg(test)]
1818

19-
use crate::{
19+
pub use crate::{
2020
self as pallet_identity_management_mock,
2121
key::{aes_encrypt_default, tee_encrypt},
2222
ChallengeCode,
@@ -32,7 +32,7 @@ use frame_support::{
3232
traits::{ConstU128, ConstU16, ConstU32, ConstU64, Everything},
3333
};
3434
use frame_system as system;
35-
use mock_tee_primitives::{
35+
pub use mock_tee_primitives::{
3636
EthereumSignature, EvmNetwork, Identity, IdentityHandle, IdentityMultiSignature,
3737
IdentityWebType, SubstrateNetwork, TwitterValidationData, UserShieldingKeyType, ValidationData,
3838
Web2Network, Web2ValidationData, Web3CommonValidationData, Web3Network, Web3ValidationData,
@@ -142,11 +142,11 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
142142
ext
143143
}
144144

145-
pub fn create_mock_twitter_identity() -> Identity {
145+
pub fn create_mock_twitter_identity(twitter_handle: &[u8]) -> Identity {
146146
Identity {
147147
web_type: IdentityWebType::Web2(Web2Network::Twitter),
148148
handle: IdentityHandle::String(
149-
b"aliceTwitterHandle".to_vec().try_into().expect("convert to BoundedVec failed"),
149+
twitter_handle.to_vec().try_into().expect("convert to BoundedVec failed"),
150150
),
151151
}
152152
}

pallets/identity-management-mock/src/tests.rs

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@
1616

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

19+
use codec::Encode;
1920
use frame_support::assert_noop;
20-
use sp_core::{Pair, H256};
21+
use sp_core::{blake2_256, Pair, H256};
2122

2223
#[test]
2324
fn unpriveledged_origin_call_fails() {
@@ -50,7 +51,7 @@ fn set_user_shielding_key_works() {
5051
fn link_twitter_identity_works() {
5152
new_test_ext().execute_with(|| {
5253
System::set_block_number(5);
53-
setup_link_identity(2, create_mock_twitter_identity(), 5);
54+
setup_link_identity(2, create_mock_twitter_identity(b"alice"), 5);
5455
});
5556
}
5657

@@ -78,7 +79,7 @@ fn link_eth_identity_works() {
7879
fn verify_twitter_identity_works() {
7980
new_test_ext().execute_with(|| {
8081
System::set_block_number(3);
81-
setup_verify_twitter_identity(2, create_mock_twitter_identity(), 3);
82+
setup_verify_twitter_identity(2, create_mock_twitter_identity(b"alice"), 3);
8283
});
8384
}
8485

@@ -99,3 +100,53 @@ fn verify_eth_identity_works() {
99100
setup_verify_eth_identity(2, p, 4);
100101
});
101102
}
103+
104+
#[test]
105+
fn double_link_twitter_identity_works() {
106+
new_test_ext().execute_with(|| {
107+
// link and verify the first twitter handle
108+
System::set_block_number(3);
109+
setup_verify_twitter_identity(2, create_mock_twitter_identity(b"alice"), 3);
110+
// link second twitter handle works
111+
System::set_block_number(4);
112+
setup_link_identity(2, create_mock_twitter_identity(b"bob"), 4);
113+
});
114+
}
115+
116+
#[test]
117+
fn wrong_polkadot_verification_message_fails() {
118+
new_test_ext().execute_with(|| {
119+
System::set_block_number(3);
120+
let p = sp_core::sr25519::Pair::from_string("//Alice", None).unwrap();
121+
let identity = create_mock_polkadot_identity(p.public().0);
122+
let who = 2;
123+
setup_link_identity(who, identity.clone(), 3);
124+
125+
System::set_block_number(4);
126+
let encrypted_identity = tee_encrypt(identity.encode().as_slice());
127+
128+
// intentionally construct a wrong verification message
129+
let wrong_msg = blake2_256(&[0u8; 16]).to_vec();
130+
let sig = p.sign(&wrong_msg);
131+
let common_validation_data = Web3CommonValidationData {
132+
message: wrong_msg.try_into().unwrap(),
133+
signature: IdentityMultiSignature::Sr25519(sig),
134+
};
135+
136+
let validation_data = match &identity.web_type {
137+
IdentityWebType::Web3(Web3Network::Substrate(SubstrateNetwork::Polkadot)) =>
138+
ValidationData::Web3(Web3ValidationData::Substrate(common_validation_data)),
139+
_ => panic!("unxpected web_type"),
140+
};
141+
142+
assert_noop!(
143+
IdentityManagementMock::verify_identity(
144+
Origin::signed(who),
145+
H256::random(),
146+
encrypted_identity,
147+
tee_encrypt(validation_data.encode().as_slice()),
148+
),
149+
Error::<Test>::UnexpectedMessage
150+
);
151+
});
152+
}

0 commit comments

Comments
 (0)