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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::convert::TryFrom;
use teaclave_types::{FunctionArguments, FunctionRuntime};
#[derive(Default)]
pub struct Echo;
#[derive(serde::Deserialize)]
struct EchoArguments {
message: String,
}
impl TryFrom<FunctionArguments> for EchoArguments {
type Error = anyhow::Error;
fn try_from(arguments: FunctionArguments) -> Result<Self, Self::Error> {
use anyhow::Context;
serde_json::from_str(&arguments.into_string()).context("Cannot deserialize arguments")
}
}
impl Echo {
pub const NAME: &'static str = "builtin-echo";
pub fn new() -> Self {
Default::default()
}
pub fn run(
&self,
arguments: FunctionArguments,
_runtime: FunctionRuntime,
) -> anyhow::Result<String> {
let message = EchoArguments::try_from(arguments)?.message;
#[cfg(test_mode)]
log::info!("{}", message);
#[cfg(test_mode)]
log::debug!("{}", message);
Ok(message)
}
}
#[cfg(feature = "enclave_unit_test")]
pub mod tests {
use super::*;
use serde_json::json;
use teaclave_runtime::*;
use teaclave_test_utils::*;
use teaclave_types::*;
pub fn run_tests() -> bool {
run_tests!(test_echo)
}
fn test_echo() {
let args = FunctionArguments::from_json(json!({
"message": "Hello Teaclave!"
}))
.unwrap();
let input_files = StagedFiles::default();
let output_files = StagedFiles::default();
let runtime = Box::new(RawIoRuntime::new(input_files, output_files));
let function = Echo;
let summary = function.run(args, runtime).unwrap();
assert_eq!(summary, "Hello Teaclave!");
}
}