diem_config/config/
test_config.rs1use crate::keys::ConfigKey;
9use diem_crypto::{ed25519::Ed25519PrivateKey, PrivateKey, Uniform};
10use diem_temppath::TempPath;
11use diem_types::transaction::authenticator::AuthenticationKey;
12use rand::rngs::StdRng;
13use serde::{Deserialize, Serialize};
14use std::path::Path;
15
16#[derive(Debug, Default, Deserialize, Serialize)]
17#[serde(deny_unknown_fields)]
18pub struct TestConfig {
19 pub auth_key: Option<AuthenticationKey>,
20 pub operator_key: Option<ConfigKey<Ed25519PrivateKey>>,
21 pub owner_key: Option<ConfigKey<Ed25519PrivateKey>>,
22 pub execution_key: Option<ConfigKey<Ed25519PrivateKey>>,
23 #[serde(skip)]
27 temp_dir: Option<TempPath>,
28}
29
30impl Clone for TestConfig {
31 fn clone(&self) -> Self {
32 Self {
33 auth_key: self.auth_key,
34 operator_key: self.operator_key.clone(),
35 owner_key: self.owner_key.clone(),
36 execution_key: self.execution_key.clone(),
37 temp_dir: None,
38 }
39 }
40}
41
42impl PartialEq for TestConfig {
43 fn eq(&self, other: &Self) -> bool {
44 self.operator_key == other.operator_key
45 && self.owner_key == other.owner_key
46 && self.auth_key == other.auth_key
47 && self.execution_key == other.execution_key
48 }
49}
50
51impl TestConfig {
52 pub fn open_module() -> Self {
53 Self {
54 auth_key: None,
55 operator_key: None,
56 owner_key: None,
57 execution_key: None,
58 temp_dir: None,
59 }
60 }
61
62 pub fn new_with_temp_dir(temp_dir: Option<TempPath>) -> Self {
63 let temp_dir = temp_dir.unwrap_or_else(|| {
64 let temp_dir = TempPath::new();
65 temp_dir.create_as_dir().expect("error creating tempdir");
66 temp_dir
67 });
68 Self {
69 auth_key: None,
70 operator_key: None,
71 owner_key: None,
72 execution_key: None,
73 temp_dir: Some(temp_dir),
74 }
75 }
76
77 pub fn execution_key(&mut self, key: Ed25519PrivateKey) {
78 self.execution_key = Some(ConfigKey::new(key))
79 }
80
81 pub fn operator_key(&mut self, key: Ed25519PrivateKey) {
82 self.operator_key = Some(ConfigKey::new(key))
83 }
84
85 pub fn owner_key(&mut self, key: Ed25519PrivateKey) {
86 self.owner_key = Some(ConfigKey::new(key))
87 }
88
89 pub fn random_account_key(&mut self, rng: &mut StdRng) {
90 let privkey = Ed25519PrivateKey::generate(rng);
91 self.auth_key = Some(AuthenticationKey::ed25519(&privkey.public_key()));
92 self.operator_key = Some(ConfigKey::new(privkey));
93
94 let privkey = Ed25519PrivateKey::generate(rng);
95 self.owner_key = Some(ConfigKey::new(privkey));
96 }
97
98 pub fn random_execution_key(&mut self, rng: &mut StdRng) {
99 let privkey = Ed25519PrivateKey::generate(rng);
100 self.execution_key = Some(ConfigKey::new(privkey));
101 }
102
103 pub fn temp_dir(&self) -> Option<&Path> {
104 self.temp_dir.as_ref().map(|temp_dir| temp_dir.path())
105 }
106}
107
108#[cfg(test)]
109mod test {
110 use super::*;
111 use rand::{rngs::StdRng, SeedableRng};
112
113 #[test]
114 fn verify_test_config_equality_using_keys() {
115 let mut test_config = TestConfig::new_with_temp_dir(None);
117 assert_eq!(test_config.operator_key, None);
118 assert_eq!(test_config.owner_key, None);
119 assert_eq!(test_config.execution_key, None);
120
121 let mut clone_test_config = test_config.clone();
123 assert_eq!(clone_test_config, test_config);
124
125 let mut rng = StdRng::from_seed([0u8; 32]);
127 test_config.random_account_key(&mut rng);
128 test_config.random_execution_key(&mut rng);
129
130 assert_ne!(clone_test_config, test_config);
132
133 clone_test_config.auth_key = test_config.auth_key;
135 clone_test_config.execution_key = test_config.execution_key.clone();
136 clone_test_config.operator_key = test_config.operator_key.clone();
137 clone_test_config.owner_key = test_config.owner_key.clone();
138
139 assert_eq!(clone_test_config, test_config);
141 }
142}