Skip to content

Commit e7b4e7e

Browse files
committed
Move test client back in the Parsec repo
This commit moves the test client back in the main Parsec repo and moves it on top of the `BasicClient` - the main core client, thus making the most out of functionality re-use. Signed-off-by: Ionut Mihalcea <[email protected]>
1 parent 369328a commit e7b4e7e

20 files changed

+788
-113
lines changed

Cargo.lock

Lines changed: 20 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ name = "parsec"
1818
path = "src/bin/main.rs"
1919

2020
[dependencies]
21-
parsec-interface = "0.12.0"
21+
parsec-interface = "0.13.0"
2222
rand = "0.7.2"
2323
base64 = "0.10.1"
2424
uuid = "0.7.4"
@@ -40,12 +40,13 @@ derivative = "1.0.3"
4040
version = "3.0.0"
4141

4242
[dev-dependencies]
43-
parsec-client-test = { git = "https://github.com/parallaxsecond/parsec-client-test", tag = "0.3.0" }
4443
num_cpus = "1.10.1"
4544
picky-asn1-der = "0.2.2"
4645
picky-asn1 = "0.2.1"
4746
serde = { version = "1.0", features = ["derive"] }
4847
sha2 = "0.8.1"
48+
parsec-client = { git = "https://github.com/parallaxsecond/parsec-client-rust" }
49+
parsec-interface = { version = "0.13.0", features = ["testing"] }
4950

5051
[build-dependencies]
5152
bindgen = "0.50.0"

README.md

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,17 +80,47 @@ $ cd parsec
8080
$ RUST_LOG=info cargo run
8181
```
8282

83-
Parsec Client Libraries can now communicate with the service. For example using the Rust Test client,
83+
Parsec Client Libraries can now communicate with the service. For example using the Rust client,
8484
RSA signatures can be done as follows:
8585
```rust
86-
use parsec_client_test::TestClient;
87-
88-
let mut client = TestClient::new();
86+
use parsec_client::BasicClient;
87+
use parsec_client::auth::AuthenticationData;
88+
use parsec_client::core::ProviderID;
89+
use parsec_client::core::psa_algorithm::{Algorithm, AsymmetricSignature, Hash};
90+
use parsec_client::core::psa_key_attributes::{KeyAttributes, KeyType, KeyPolicy, UsageFlags};
91+
use sha2::Sha256;
92+
93+
let app_identity_ = AuthenticationData::AppIdentity(String::from("my-app"));
94+
let client = BasicClient::new(app_identity, ProviderID::Tpm);
95+
let sign_alg = AsymmetricSignature::RsaPkcs1v15Sign {
96+
hash_alg: Hash::Sha256,
97+
};
98+
let key_attrs = KeyAttributes {
99+
key_type: KeyType::RsaKeyPair,
100+
key_bits: 1024,
101+
key_policy: KeyPolicy {
102+
key_usage_flags: UsageFlags {
103+
sign_hash: true,
104+
verify_hash: true,
105+
sign_message: true,
106+
verify_message: true,
107+
export: true,
108+
encrypt: false,
109+
decrypt: false,
110+
cache: false,
111+
copy: false,
112+
derive: false,
113+
},
114+
key_algorithm: Algorithm::AsymmetricSignature(sign_alg),
115+
},
116+
};
89117
let key_name = String::from("🔑 What shall I sign? 🔑");
90-
client.generate_rsa_sign_key(key_name.clone()).unwrap();
91-
let signature = client.sign(key_name,
92-
String::from("Platform AbstRaction for SECurity").into_bytes())
93-
.unwrap();
118+
client.psa_generate_key(key_name.clone(), key_attrs).unwrap();
119+
120+
let mut hasher = Sha256::new();
121+
hasher.input(b"Bob wrote this message.");
122+
let hash = hasher.result().to_vec();
123+
let signature = client.psa_sign_hash(key_name, hash, sign_alg).unwrap();
94124
```
95125

96126
Check the [**user**](https://parallaxsecond.github.io/parsec-book/parsec_users.html), [**client developer**](https://parallaxsecond.github.io/parsec-book/parsec_client/index.html) and [**service developer**](https://parallaxsecond.github.io/parsec-book/parsec_service/index.html) guides for more information on building, installing, testing and using Parsec!

tests/all_providers/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
15-
use parsec_client_test::TestClient;
16-
use parsec_interface::requests::Result;
15+
use crate::test_clients::TestClient;
16+
use parsec_client::error::Result;
1717
use parsec_interface::requests::{Opcode, ProviderID};
1818
use std::collections::HashSet;
1919
use uuid::Uuid;

tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@
4444

4545
mod all_providers;
4646
mod per_provider;
47+
mod test_clients;

tests/per_provider/normal_tests/asym_sign_verify.rs

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
15-
use parsec_client_test::TestClient;
15+
use crate::test_clients::error::{Error, Result};
16+
use crate::test_clients::TestClient;
1617
use parsec_interface::operations::psa_algorithm::*;
1718
use parsec_interface::operations::psa_key_attributes::*;
18-
use parsec_interface::requests::{ResponseStatus, Result};
19+
use parsec_interface::requests::ResponseStatus;
1920
use sha2::{Digest, Sha256};
2021

2122
const HASH: [u8; 32] = [
@@ -30,7 +31,7 @@ fn asym_sign_no_key() {
3031
let status = client
3132
.sign_with_rsa_sha256(key_name, HASH.to_vec())
3233
.expect_err("Key should not exist.");
33-
assert_eq!(status, ResponseStatus::PsaErrorDoesNotExist);
34+
assert_eq!(status, Error::Service(ResponseStatus::PsaErrorDoesNotExist));
3435
}
3536

3637
#[test]
@@ -41,7 +42,7 @@ fn asym_verify_no_key() {
4142
let status = client
4243
.verify_with_rsa_sha256(key_name, HASH.to_vec(), signature)
4344
.expect_err("Verification should have failed");
44-
assert_eq!(status, ResponseStatus::PsaErrorDoesNotExist);
45+
assert_eq!(status, Error::Service(ResponseStatus::PsaErrorDoesNotExist));
4546
}
4647

4748
#[test]
@@ -67,8 +68,8 @@ fn asym_verify_fail() -> Result<()> {
6768
let status = client
6869
.verify_with_rsa_sha256(key_name, HASH.to_vec(), signature)
6970
.expect_err("Verification should fail.");
70-
if !(status == ResponseStatus::PsaErrorInvalidSignature
71-
|| status == ResponseStatus::PsaErrorCorruptionDetected)
71+
if !(status == Error::Service(ResponseStatus::PsaErrorInvalidSignature)
72+
|| status == Error::Service(ResponseStatus::PsaErrorCorruptionDetected))
7273
{
7374
panic!("An invalid signature or a tampering detection should be the only reasons of the verification failing.");
7475
} else {
@@ -171,7 +172,7 @@ fn sign_hash_not_permitted() -> Result<()> {
171172

172173
let status = client.sign_with_rsa_sha256(key_name, hash).unwrap_err();
173174

174-
assert_eq!(status, ResponseStatus::PsaErrorNotPermitted);
175+
assert_eq!(status, Error::Service(ResponseStatus::PsaErrorNotPermitted));
175176

176177
Ok(())
177178
}
@@ -190,8 +191,14 @@ fn sign_hash_bad_format() -> Result<()> {
190191
.unwrap_err();
191192
let status2 = client.sign_with_rsa_sha256(key_name, hash2).unwrap_err();
192193

193-
assert_eq!(status1, ResponseStatus::PsaErrorInvalidArgument);
194-
assert_eq!(status2, ResponseStatus::PsaErrorInvalidArgument);
194+
assert_eq!(
195+
status1,
196+
Error::Service(ResponseStatus::PsaErrorInvalidArgument)
197+
);
198+
assert_eq!(
199+
status2,
200+
Error::Service(ResponseStatus::PsaErrorInvalidArgument)
201+
);
195202
Ok(())
196203
}
197204

@@ -247,7 +254,7 @@ fn verify_hash_not_permitted() -> Result<()> {
247254
.verify_with_rsa_sha256(key_name, hash, signature)
248255
.unwrap_err();
249256

250-
assert_eq!(status, ResponseStatus::PsaErrorNotPermitted);
257+
assert_eq!(status, Error::Service(ResponseStatus::PsaErrorNotPermitted));
251258
Ok(())
252259
}
253260

@@ -271,8 +278,14 @@ fn verify_hash_bad_format() -> Result<()> {
271278
.verify_with_rsa_sha256(key_name, hash2, signature)
272279
.unwrap_err();
273280

274-
assert_eq!(status1, ResponseStatus::PsaErrorInvalidArgument);
275-
assert_eq!(status2, ResponseStatus::PsaErrorInvalidArgument);
281+
assert_eq!(
282+
status1,
283+
Error::Service(ResponseStatus::PsaErrorInvalidArgument)
284+
);
285+
assert_eq!(
286+
status2,
287+
Error::Service(ResponseStatus::PsaErrorInvalidArgument)
288+
);
276289
Ok(())
277290
}
278291

@@ -293,7 +306,10 @@ fn fail_verify_hash() -> Result<()> {
293306
let status = client
294307
.verify_with_rsa_sha256(key_name, hash, signature)
295308
.unwrap_err();
296-
assert_eq!(status, ResponseStatus::PsaErrorInvalidSignature);
309+
assert_eq!(
310+
status,
311+
Error::Service(ResponseStatus::PsaErrorInvalidSignature)
312+
);
297313
Ok(())
298314
}
299315

@@ -314,6 +330,9 @@ fn fail_verify_hash2() -> Result<()> {
314330
let status = client
315331
.verify_with_rsa_sha256(key_name, hash, signature)
316332
.unwrap_err();
317-
assert_eq!(status, ResponseStatus::PsaErrorInvalidSignature);
333+
assert_eq!(
334+
status,
335+
Error::Service(ResponseStatus::PsaErrorInvalidSignature)
336+
);
318337
Ok(())
319338
}

tests/per_provider/normal_tests/auth.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
// See the License for the specific language governing permissions and
1414
// limitations under the License.
15-
use parsec_client_test::TestClient;
16-
use parsec_interface::requests::{ResponseStatus, Result};
15+
use crate::test_clients::TestClient;
16+
use parsec_client::error::{Error, Result};
17+
use parsec_interface::requests::ResponseStatus;
1718

1819
#[test]
1920
fn two_auths_same_key_name() -> Result<()> {
2021
let key_name = String::from("two_auths_same_key_name");
2122
let mut client = TestClient::new();
22-
let auth1 = String::from("first_client").into_bytes();
23-
let auth2 = String::from("second_client").into_bytes();
23+
let auth1 = String::from("first_client");
24+
let auth2 = String::from("second_client");
2425

2526
client.set_auth(auth1);
2627
client.generate_rsa_sign_key(key_name.clone())?;
@@ -33,8 +34,8 @@ fn two_auths_same_key_name() -> Result<()> {
3334
fn delete_wrong_key() -> Result<()> {
3435
let key_name = String::from("delete_wrong_key");
3536
let mut client = TestClient::new();
36-
let auth1 = String::from("first_client").into_bytes();
37-
let auth2 = String::from("second_client").into_bytes();
37+
let auth1 = String::from("first_client");
38+
let auth2 = String::from("second_client");
3839

3940
client.set_auth(auth1);
4041
client.generate_rsa_sign_key(key_name.clone())?;
@@ -43,7 +44,7 @@ fn delete_wrong_key() -> Result<()> {
4344
let status = client
4445
.destroy_key(key_name)
4546
.expect_err("Destroying key should have failed");
46-
assert_eq!(status, ResponseStatus::PsaErrorDoesNotExist);
47+
assert_eq!(status, Error::Service(ResponseStatus::PsaErrorDoesNotExist));
4748

4849
Ok(())
4950
}

0 commit comments

Comments
 (0)