1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
use std::sync::{Arc, Mutex};
use uuid::Uuid;
use super::{commands, state::State, MockHsm};
use adapters::{Adapter, AdapterError, AdapterErrorKind::ConnectionFailed};
use commands::CommandType;
use securechannel::CommandMessage;
pub struct MockAdapter(Arc<Mutex<State>>);
impl Adapter for MockAdapter {
type Config = MockHsm;
fn open(hsm: &MockHsm) -> Result<Self, AdapterError> {
Ok(MockAdapter(hsm.0.clone()))
}
fn healthcheck(&self) -> Result<(), AdapterError> {
Ok(())
}
fn send_message(&self, _uuid: Uuid, body: Vec<u8>) -> Result<Vec<u8>, AdapterError> {
let command = CommandMessage::parse(body)
.map_err(|e| err!(ConnectionFailed, "error parsing command: {}", e))?;
let mut state = self
.0
.lock()
.map_err(|e| err!(ConnectionFailed, "error obtaining state lock: {}", e))?;
match command.command_type {
CommandType::CreateSession => commands::create_session(&mut state, &command),
CommandType::AuthSession => commands::authenticate_session(&mut state, &command),
CommandType::SessionMessage => commands::session_message(&mut state, command),
unsupported => fail!(ConnectionFailed, "unsupported command: {:?}", unsupported),
}
}
}