Skip to content

Commit a4d0a2c

Browse files
authored
Merge pull request #197 from ionut-arm/config-test
Keep list_providers order; add cfg tests
2 parents 5c581d4 + 2fee72f commit a4d0a2c

File tree

13 files changed

+194
-69
lines changed

13 files changed

+194
-69
lines changed

ci.sh

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@ cleanup () {
1212
if [ -n "$PARSEC_PID" ]; then kill $PARSEC_PID || true ; fi
1313
# Stop tpm_server if running
1414
if [ -n "$TPM_SRV_PID" ]; then kill $TPM_SRV_PID || true; fi
15-
# Remove the slot_number line added by find_slot_number.sh
16-
sed -i '/^slot_number =.*/d' $CONFIG_PATH
15+
# Remove the slot_number line added earlier
16+
find e2e_tests -name "*toml" -not -name "Cargo.toml" -exec sed -i 's/^slot_number =.*/# slot_number/' {} \;
1717
# Remove fake mapping and temp files
18-
if [ -d "mappings" ]; then rm -rf -- "mappings"; fi
19-
if [ -f "NVChip" ]; then rm "NVChip" ; fi
18+
rm -rf "mappings"
19+
rm -f "NVChip"
20+
rm -f "e2e_tests/provider_cfg/tmp_config.toml"
2021

2122
if [ -z "$NO_CARGO_CLEAN" ]; then cargo clean; fi
2223
}
@@ -50,6 +51,7 @@ error_msg () {
5051
NO_CARGO_CLEAN=
5152
NO_STRESS_TEST=
5253
PROVIDER_NAME=
54+
CONFIG_PATH=$(pwd)/e2e_tests/provider_cfg/tmp_config.toml
5355
while [ "$#" -gt 0 ]; do
5456
case "$1" in
5557
--no-cargo-clean )
@@ -63,7 +65,7 @@ while [ "$#" -gt 0 ]; do
6365
error_msg "Only one provider name must be given"
6466
fi
6567
PROVIDER_NAME=$1
66-
CONFIG_PATH="e2e_tests/provider_cfg/$1/config.toml"
68+
cp $(pwd)/e2e_tests/provider_cfg/$1/config.toml $CONFIG_PATH
6769
if [ "$PROVIDER_NAME" = "all" ]; then
6870
FEATURES="--features=all-providers"
6971
else
@@ -94,8 +96,13 @@ if [ "$PROVIDER_NAME" = "tpm" ] || [ "$PROVIDER_NAME" = "all" ]; then
9496
fi
9597

9698
if [ "$PROVIDER_NAME" = "pkcs11" ] || [ "$PROVIDER_NAME" = "all" ]; then
97-
# Find and append the slot number at the end of the configuration file.
98-
e2e_tests/provider_cfg/pkcs11/find_slot_number.sh $CONFIG_PATH
99+
pushd e2e_tests
100+
# This command suppose that the slot created by the container will be the first one that appears
101+
# when printing all the available slots.
102+
SLOT_NUMBER=`softhsm2-util --show-slots | head -n2 | tail -n1 | cut -d " " -f 2`
103+
# Find all TOML files in the directory (except Cargo.toml) and replace the commented slot number with the valid one
104+
find . -name "*toml" -not -name "Cargo.toml" -exec sed -i "s/^# slot_number.*$/slot_number = $SLOT_NUMBER/" {} \;
105+
popd
99106
fi
100107

101108
echo "Build test"
@@ -128,6 +135,7 @@ pgrep -f target/debug/parsec >/dev/null
128135
if [ "$PROVIDER_NAME" = "all" ]; then
129136
echo "Execute all-providers tests"
130137
RUST_BACKTRACE=1 cargo test --manifest-path ./e2e_tests/Cargo.toml all_providers
138+
RUST_BACKTRACE=1 cargo test --manifest-path ./e2e_tests/Cargo.toml config
131139
else
132140
# Per provider tests
133141
echo "Execute normal tests"

e2e_tests/.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
target
2-
Cargo.lock
2+
Cargo.lock
3+
provider_cfg/tmp_config.toml

e2e_tests/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ picky-asn1-der = "0.2.2"
1818
picky-asn1 = "0.2.1"
1919
serde = { version = "1.0", features = ["derive"] }
2020
sha2 = "0.8.1"
21-
parsec-client = { git = "https://github.com/parallaxsecond/parsec-client-rust", features = ["testing"] }
21+
parsec-client = { version = "0.6.0", features = ["testing"] }
2222
log = "0.4.8"
2323
rand = "0.7.3"
2424

e2e_tests/provider_cfg/all/config.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,5 @@ provider_type = "Pkcs11"
2626
key_info_manager = "on-disk-manager"
2727
library_path = "/usr/local/lib/softhsm/libsofthsm2.so"
2828
user_pin = "123456"
29-
# The slot_number mandatory field is going to be added by the find_slot_number.sh script
30-
# to the last line of this file in the form:
31-
# slot_number = 123456
29+
# The slot_number mandatory field is going to replace the following line with a valid number
30+
# slot_number

e2e_tests/provider_cfg/pkcs11/config.toml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,5 @@ provider_type = "Pkcs11"
1818
key_info_manager = "on-disk-manager"
1919
library_path = "/usr/local/lib/softhsm/libsofthsm2.so"
2020
user_pin = "123456"
21-
# The slot_number mandatory field is going to be added by the find_slot_number.sh script
22-
# to the last line of this file in the form:
23-
# slot_number = 123456
21+
# The slot_number mandatory field is going to replace the following line with a valid number
22+
# slot_number

e2e_tests/provider_cfg/pkcs11/find_slot_number.sh

Lines changed: 0 additions & 19 deletions
This file was deleted.

e2e_tests/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Copyright 2020 Contributors to the Parsec project.
22
// SPDX-License-Identifier: Apache-2.0
3+
#![allow(clippy::multiple_crate_versions)]
34
pub mod raw_request;
45
pub mod stress;
56

e2e_tests/tests/config/mod.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright 2020 Contributors to the Parsec project.
2+
// SPDX-License-Identifier: Apache-2.0
3+
use e2e_tests::TestClient;
4+
use log::{error, info};
5+
use std::env;
6+
use std::fs;
7+
use std::path::PathBuf;
8+
use std::process::Command;
9+
use std::thread;
10+
use std::time::Duration;
11+
use uuid::Uuid;
12+
13+
const CONFIG_TOMLS_FOLDER: &str = "tests/config/tomls";
14+
const SERVICE_CONFIG_PATH: &str = "provider_cfg/tmp_config.toml";
15+
16+
fn set_config(filename: &str) {
17+
info!("Changing service configuration file to {}", filename);
18+
let config_path = PathBuf::from(SERVICE_CONFIG_PATH);
19+
let mut new_config = env::current_dir() // this is the root of the crate for tests
20+
.unwrap();
21+
new_config.push(CONFIG_TOMLS_FOLDER);
22+
new_config.push(filename);
23+
if !new_config.exists() {
24+
error!("Configuration file {} does not exist", filename);
25+
panic!();
26+
}
27+
28+
let _ = fs::copy(new_config, config_path).unwrap();
29+
}
30+
31+
fn reload_service() {
32+
info!("Reloading Parsec service");
33+
34+
let _ = Command::new("pkill")
35+
.arg("-SIGHUP")
36+
.arg("parsec")
37+
.output()
38+
.expect("Reloading service failed");
39+
40+
// wait for the service to restart
41+
thread::sleep(Duration::from_secs(2));
42+
}
43+
44+
#[test]
45+
fn list_providers() {
46+
set_config("list_providers_1.toml");
47+
reload_service();
48+
49+
let mut client = TestClient::new();
50+
let providers = client.list_providers().unwrap();
51+
let uuids: Vec<Uuid> = providers.iter().map(|p| p.uuid).collect();
52+
assert_eq!(
53+
uuids,
54+
vec![
55+
Uuid::parse_str("1c1139dc-ad7c-47dc-ad6b-db6fdb466552").unwrap(), // Mbed crypto provider
56+
Uuid::parse_str("1e4954a4-ff21-46d3-ab0c-661eeb667e1d").unwrap(), // Tpm provider
57+
Uuid::parse_str("30e39502-eba6-4d60-a4af-c518b7f5e38f").unwrap(), // Pkcs11 provider
58+
Uuid::parse_str("47049873-2a43-4845-9d72-831eab668784").unwrap(), // Core provider
59+
]
60+
);
61+
62+
set_config("list_providers_2.toml");
63+
reload_service();
64+
65+
let providers = client.list_providers().unwrap();
66+
let uuids: Vec<Uuid> = providers.iter().map(|p| p.uuid).collect();
67+
assert_eq!(
68+
uuids,
69+
vec![
70+
Uuid::parse_str("30e39502-eba6-4d60-a4af-c518b7f5e38f").unwrap(), // Pkcs11 provider
71+
Uuid::parse_str("1c1139dc-ad7c-47dc-ad6b-db6fdb466552").unwrap(), // Mbed crypto provider
72+
Uuid::parse_str("1e4954a4-ff21-46d3-ab0c-661eeb667e1d").unwrap(), // Tpm provider
73+
Uuid::parse_str("47049873-2a43-4845-9d72-831eab668784").unwrap(), // Core provider
74+
]
75+
);
76+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[core_settings]
2+
# The CI already timestamps the logs
3+
log_timestamp = false
4+
log_error_details = true
5+
6+
[listener]
7+
listener_type = "DomainSocket"
8+
timeout = 200 # in milliseconds
9+
10+
[[key_manager]]
11+
name = "on-disk-manager"
12+
manager_type = "OnDisk"
13+
14+
[[provider]]
15+
provider_type = "MbedCrypto"
16+
key_info_manager = "on-disk-manager"
17+
18+
[[provider]]
19+
provider_type = "Tpm"
20+
key_info_manager = "on-disk-manager"
21+
tcti = "mssim"
22+
owner_hierarchy_auth = "tpm_pass"
23+
24+
[[provider]]
25+
provider_type = "Pkcs11"
26+
key_info_manager = "on-disk-manager"
27+
library_path = "/usr/local/lib/softhsm/libsofthsm2.so"
28+
user_pin = "123456"
29+
# The slot_number mandatory field is going to replace the following line with a valid number
30+
# slot_number
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
[core_settings]
2+
# The CI already timestamps the logs
3+
log_timestamp = false
4+
log_error_details = true
5+
6+
[listener]
7+
listener_type = "DomainSocket"
8+
timeout = 200 # in milliseconds
9+
10+
[[key_manager]]
11+
name = "on-disk-manager"
12+
manager_type = "OnDisk"
13+
14+
[[provider]]
15+
provider_type = "Pkcs11"
16+
key_info_manager = "on-disk-manager"
17+
library_path = "/usr/local/lib/softhsm/libsofthsm2.so"
18+
user_pin = "123456"
19+
# The slot_number mandatory field is going to replace the following line with a valid number
20+
# slot_number
21+
22+
[[provider]]
23+
provider_type = "MbedCrypto"
24+
key_info_manager = "on-disk-manager"
25+
26+
[[provider]]
27+
provider_type = "Tpm"
28+
key_info_manager = "on-disk-manager"
29+
tcti = "mssim"
30+
owner_hierarchy_auth = "tpm_pass"

e2e_tests/tests/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@
3131
#![allow(clippy::multiple_crate_versions)]
3232

3333
mod all_providers;
34+
mod config;
3435
mod per_provider;

src/providers/core_provider/mod.rs

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -71,41 +71,19 @@ pub struct CoreProviderBuilder {
7171
}
7272

7373
impl CoreProviderBuilder {
74-
pub fn new() -> std::io::Result<Self> {
75-
let crate_version: Version = Version::from_str(version!()).or_else(|e| {
76-
format_error!("Error parsing the crate version", e);
77-
Err(Error::new(
78-
ErrorKind::InvalidData,
79-
"crate version number has invalid format",
80-
))
81-
})?;
82-
83-
let provider_info = vec![ProviderInfo {
84-
// Assigned UUID for this provider: 47049873-2a43-4845-9d72-831eab668784
85-
uuid: Uuid::parse_str("47049873-2a43-4845-9d72-831eab668784").or_else(|_| Err(Error::new(
86-
ErrorKind::InvalidData,
87-
"provider UUID is invalid",
88-
)))?,
89-
description: String::from("Software provider that implements only administrative (i.e. no cryptographic) operations"),
90-
vendor: String::new(),
91-
version_maj: crate_version.major,
92-
version_min: crate_version.minor,
93-
version_rev: crate_version.patch,
94-
id: ProviderID::Core,
95-
}];
96-
74+
pub fn new() -> Self {
9775
let mut provider_opcodes = HashMap::new();
9876
let _ = provider_opcodes.insert(
9977
ProviderID::Core,
10078
SUPPORTED_OPCODES.iter().copied().collect(),
10179
);
10280

103-
Ok(CoreProviderBuilder {
81+
CoreProviderBuilder {
10482
version_maj: None,
10583
version_min: None,
106-
provider_info,
84+
provider_info: Vec::new(),
10785
provider_opcodes,
108-
})
86+
}
10987
}
11088

11189
pub fn with_wire_protocol_version(mut self, version_min: u8, version_maj: u8) -> Self {
@@ -126,7 +104,28 @@ impl CoreProviderBuilder {
126104
self
127105
}
128106

129-
pub fn build(self) -> std::io::Result<CoreProvider> {
107+
pub fn build(mut self) -> std::io::Result<CoreProvider> {
108+
let crate_version: Version = Version::from_str(version!()).or_else(|e| {
109+
format_error!("Error parsing the crate version", e);
110+
Err(Error::new(
111+
ErrorKind::InvalidData,
112+
"crate version number has invalid format",
113+
))
114+
})?;
115+
self.provider_info.push(ProviderInfo {
116+
// Assigned UUID for this provider: 47049873-2a43-4845-9d72-831eab668784
117+
uuid: Uuid::parse_str("47049873-2a43-4845-9d72-831eab668784").or_else(|_| Err(Error::new(
118+
ErrorKind::InvalidData,
119+
"provider UUID is invalid",
120+
)))?,
121+
description: String::from("Software provider that implements only administrative (i.e. no cryptographic) operations"),
122+
vendor: String::new(),
123+
version_maj: crate_version.major,
124+
version_min: crate_version.minor,
125+
version_rev: crate_version.patch,
126+
id: ProviderID::Core,
127+
});
128+
130129
let core_provider = CoreProvider {
131130
wire_protocol_version_maj: self
132131
.version_maj

src/utils/service_builder.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,14 +150,14 @@ impl ServiceBuilder {
150150
}
151151

152152
fn build_backend_handlers(
153-
mut providers: HashMap<ProviderID, Provider>,
153+
mut providers: Vec<(ProviderID, Provider)>,
154154
) -> Result<HashMap<ProviderID, BackEndHandler>> {
155155
let mut map = HashMap::new();
156156

157-
let mut core_provider_builder = CoreProviderBuilder::new()?
157+
let mut core_provider_builder = CoreProviderBuilder::new()
158158
.with_wire_protocol_version(WIRE_PROTOCOL_VERSION_MINOR, WIRE_PROTOCOL_VERSION_MAJOR);
159159

160-
for (provider_id, provider) in providers.drain() {
160+
for (provider_id, provider) in providers.drain(..) {
161161
let (info, opcodes) = provider.describe().or_else(|_| {
162162
Err(Error::new(
163163
ErrorKind::InvalidData,
@@ -192,11 +192,11 @@ fn build_backend_handlers(
192192
fn build_providers(
193193
configs: &[ProviderConfig],
194194
key_info_managers: HashMap<String, KeyInfoManager>,
195-
) -> HashMap<ProviderID, Provider> {
196-
let mut map = HashMap::new();
195+
) -> Vec<(ProviderID, Provider)> {
196+
let mut list = Vec::new();
197197
for config in configs {
198198
let provider_id = config.provider_id();
199-
if map.contains_key(&provider_id) {
199+
if list.iter().any(|(id, _)| *id == provider_id) {
200200
warn!("Parsec currently only supports one instance of each provider type. Ignoring {} and continuing...", provider_id);
201201
continue;
202202
}
@@ -222,10 +222,10 @@ fn build_providers(
222222
continue;
223223
}
224224
};
225-
let _ = map.insert(provider_id, provider);
225+
let _ = list.push((provider_id, provider));
226226
}
227227

228-
map
228+
list
229229
}
230230

231231
// This cfg_attr is used to allow the fact that key_info_manager is not used when there is no

0 commit comments

Comments
 (0)