Skip to content
This repository was archived by the owner on May 9, 2022. It is now read-only.

Commit 4312a49

Browse files
authored
Merge pull request #89 from registreerocks/refactor-ecall-tests
test: clean up ECALL and web API tests
2 parents 926640d + b44b7f4 commit 4312a49

17 files changed

+203
-174
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rtc_data_service/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ insta = "1.7.1"
3939
sodalite = "0.4.0"
4040
uuid = "0.8.2"
4141

42+
# Only for type name references. (This should match the version used by actix-web.)
43+
actix-http = "3.0.0-beta.6"
44+
4245
[features]
4346
test = []
4447

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//! Test ECALL: `local_attestation`
2+
3+
use sgx_types::sgx_status_t;
4+
5+
use crate::helpers;
6+
7+
#[test]
8+
fn test_local_attestation_success() {
9+
let auth_enclave = helpers::init_auth_enclave();
10+
let data_enclave = helpers::init_data_enclave();
11+
12+
let res = data_enclave.local_attestation(auth_enclave.geteid());
13+
assert_eq!(res, sgx_status_t::SGX_SUCCESS);
14+
15+
// TODO: Integration test for message sending
16+
// We should consider moving the integration tests for enclave interaction into rtc_uenclave
17+
// since these tests does not need anything from the data_service
18+
}

rtc_data_service/tests/ecalls/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
//! ECALL tests
2+
3+
mod local_attestation;

rtc_data_service/tests/exec_token.rs

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

rtc_data_service/tests/helpers/mod.rs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//! Shared test helpers
2+
3+
mod types;
4+
5+
use std::sync::Arc;
6+
7+
use actix::Actor;
8+
use actix_web::App;
9+
10+
use rtc_uenclave::{EnclaveConfig, RtcAuthEnclave, RtcDataEnclave};
11+
12+
use rtc_data_service::auth_enclave_actor::AuthEnclaveActor;
13+
use rtc_data_service::data_enclave_actor::DataEnclaveActor;
14+
use rtc_data_service::data_upload::upload_file;
15+
use rtc_data_service::exec_token::req_exec_token;
16+
use rtc_data_service::handlers;
17+
18+
/// Initialise a data enclave for testing.
19+
pub(crate) fn init_auth_enclave() -> RtcAuthEnclave<EnclaveConfig> {
20+
RtcAuthEnclave::init(EnclaveConfig {
21+
lib_path: "/root/rtc-data/rtc_auth_enclave/build/bin/enclave.signed.so".to_string(),
22+
..Default::default()
23+
})
24+
.unwrap()
25+
}
26+
27+
/// Initialise a data enclave for testing.
28+
pub(crate) fn init_data_enclave() -> RtcDataEnclave<EnclaveConfig> {
29+
RtcDataEnclave::init(EnclaveConfig {
30+
lib_path: "/root/rtc-data/rtc_data_enclave/build/bin/enclave.signed.so".to_string(),
31+
..Default::default()
32+
})
33+
.unwrap()
34+
}
35+
36+
/// Initialise an instance of our web API for testing.
37+
///
38+
/// This should (roughly) mirror our `HttpServer` definition in `http_server::main`.
39+
pub(crate) async fn init_rtc_service() -> impl types::WebService {
40+
let app = App::new()
41+
.data(init_auth_enclave_actor().start())
42+
.data(init_data_enclave_actor().start())
43+
.service(handlers::auth_enclave_attestation)
44+
.service(handlers::data_enclave_attestation)
45+
.service(upload_file)
46+
.service(req_exec_token);
47+
actix_web::test::init_service(app).await
48+
}
49+
50+
fn init_auth_enclave_actor() -> AuthEnclaveActor {
51+
AuthEnclaveActor::new(Arc::new(EnclaveConfig {
52+
lib_path: "/root/rtc-data/rtc_auth_enclave/build/bin/enclave.signed.so".to_string(),
53+
..Default::default()
54+
}))
55+
}
56+
57+
fn init_data_enclave_actor() -> DataEnclaveActor {
58+
DataEnclaveActor::new(Arc::new(EnclaveConfig {
59+
lib_path: "/root/rtc-data/rtc_data_enclave/build/bin/enclave.signed.so".to_string(),
60+
..Default::default()
61+
}))
62+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//! Helper type definitions
2+
3+
use actix_http::body::Body;
4+
use actix_http::error::Error;
5+
use actix_http::Request;
6+
use actix_web::dev::{Service, ServiceResponse};
7+
8+
/// Shorthand for the complicated [`Service`] type returned by [`actix_web::test::init_service`].
9+
///
10+
/// This uses the "trait aliasing" technique described here:
11+
/// <https://www.worthe-it.co.za/blog/2017-01-15-aliasing-traits-in-rust.html>
12+
pub(crate) trait WebService:
13+
Service<Request, Response = ServiceResponse<Body>, Error = Error>
14+
{
15+
}
16+
17+
impl<S> WebService for S where S: Service<Request, Response = ServiceResponse<Body>, Error = Error> {}

rtc_data_service/tests/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
//! Top-level test module
2+
3+
mod helpers;
4+
5+
mod ecalls;
6+
mod web_api;

rtc_data_service/tests/server.rs

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
use actix_web::test;
2+
3+
use crate::helpers;
4+
5+
#[actix_rt::test]
6+
async fn auth_service_attestation_ok() {
7+
attestation_ok("/auth/attest").await;
8+
}
9+
10+
#[actix_rt::test]
11+
async fn data_service_attestation_ok() {
12+
attestation_ok("/data/attest").await;
13+
}
14+
15+
async fn attestation_ok(uri_path: &str) {
16+
let app = helpers::init_rtc_service().await;
17+
18+
let req = test::TestRequest::get().uri(uri_path).to_request();
19+
let resp = test::call_service(&app, req).await;
20+
21+
insta::assert_debug_snapshot!(resp);
22+
23+
let body = test::read_body(resp).await;
24+
insta::assert_debug_snapshot!(body);
25+
}

rtc_data_service/tests/data_upload.rs renamed to rtc_data_service/tests/web_api/data_upload.rs

Lines changed: 12 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
//! Tests for [`rtc_data_service::data_upload`]
22
3-
use actix::Actor;
4-
use actix_web::{
5-
test::{self, read_body},
6-
App,
7-
};
8-
use rtc_data_service::data_enclave_actor::*;
9-
use rtc_data_service::data_upload::*;
10-
use rtc_uenclave::EnclaveConfig;
3+
use std::convert::TryInto;
4+
use std::path::Path;
5+
116
use sgx_types::sgx_target_info_t;
12-
use sodalite;
7+
8+
use actix_web::test;
139
use uuid::Uuid;
1410

15-
use std::{convert::TryInto, path::Path, sync::Arc};
11+
use rtc_data_service::data_upload::models;
12+
13+
use crate::helpers;
1614

1715
// See rtc_tenclave/src/crypto.rs
1816
const CRYPTO_BOX_ZEROBYTES: usize = 32;
@@ -21,28 +19,11 @@ const CRYPTO_BOX_BOXZEROBYTES: usize = 16;
2119
/// Upload some data, decrypt and check the result.
2220
#[actix_rt::test]
2321
async fn data_service_data_upload_ok() {
24-
// TODO: Split this test into re-usable components
25-
let mut app = test::init_service(
26-
App::new()
27-
.data(
28-
DataEnclaveActor::new(Arc::new(EnclaveConfig {
29-
lib_path: "/root/rtc-data/rtc_data_enclave/build/bin/enclave.signed.so"
30-
.to_string(),
31-
..Default::default()
32-
}))
33-
.start(),
34-
)
35-
.service(upload_file),
36-
)
37-
.await;
22+
let app = helpers::init_rtc_service().await;
3823

3924
// TODO: Add a test that can run inside of the enclave and use the JWT token to get
4025
// the enclave key
41-
let enclave = rtc_uenclave::RtcDataEnclave::init(EnclaveConfig {
42-
lib_path: "/root/rtc-data/rtc_data_enclave/build/bin/enclave.signed.so".to_string(),
43-
..Default::default()
44-
})
45-
.unwrap();
26+
let enclave = helpers::init_data_enclave();
4627

4728
let enclave_pubkey = enclave
4829
.create_report(&sgx_target_info_t::default())
@@ -80,11 +61,11 @@ async fn data_service_data_upload_ok() {
8061
.set_json(&req_body)
8162
.to_request();
8263

83-
let resp = test::call_service(&mut app, req).await;
64+
let resp = test::call_service(&app, req).await;
8465

8566
assert!(resp.status().is_success());
8667

87-
let body: models::ResponseBody = serde_json::from_slice(&read_body(resp).await).unwrap();
68+
let body: models::ResponseBody = serde_json::from_slice(&test::read_body(resp).await).unwrap();
8869

8970
// NOTE: re-add padding since sodalite supports the C-style nacl api
9071
let mut m = vec![0_u8; body.ciphertext.len() + CRYPTO_BOX_BOXZEROBYTES];

0 commit comments

Comments
 (0)