-
Notifications
You must be signed in to change notification settings - Fork 3
/
blocking_ops.rs
130 lines (118 loc) · 5.4 KB
/
blocking_ops.rs
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
mod common;
// Note: The blocking functions need minimal testing as they primarily just call their async counterparts
#[cfg(feature = "blocking")]
mod integration_tests {
use crate::common::{create_id_all_classes, gen_jwt, USER_PASSWORD};
use ironoxide::blocking::{BlockingIronOxideConfig, RuntimeConfig};
use ironoxide::{
blocking::BlockingIronOxide,
group::GroupCreateOpts,
user::{UserCreateOpts, UserId},
InitAndRotationCheck, IronOxideErr,
};
use std::convert::TryInto;
use std::sync::Arc;
use tokio::time::Duration;
// Tests a UserOp (user_create/generate_new_device), a GroupOp (group_create),
// and ironoxide::blocking functions (initialize/initialize_check_rotation)
#[test]
fn rotate_all() -> Result<(), IronOxideErr> {
let account_id: UserId = create_id_all_classes("").try_into()?;
let jwt = gen_jwt(Some(account_id.id())).0;
BlockingIronOxide::user_create(&jwt, USER_PASSWORD, &UserCreateOpts::new(true))?;
let device = BlockingIronOxide::generate_new_device(
&gen_jwt(Some(account_id.id())).0,
USER_PASSWORD,
&Default::default(),
)?
.into();
let creator_sdk = ironoxide::blocking::initialize(&device, Default::default())?;
// making non-default groups so I can specify needs_rotation of true
let group_create = creator_sdk.group_create(&GroupCreateOpts::new(
None,
None,
true,
true,
None,
vec![],
vec![],
true,
))?;
assert_eq!(group_create.needs_rotation(), Some(true));
let init_and_rotation_check =
ironoxide::blocking::initialize_check_rotation(&device, Default::default())?;
let (user_result, group_result) = match init_and_rotation_check {
InitAndRotationCheck::NoRotationNeeded(_) => {
panic!("both user and groups should need rotation!");
}
InitAndRotationCheck::RotationNeeded(io, rot) => io.rotate_all(&rot, USER_PASSWORD)?,
};
assert!(user_result.is_some());
assert!(group_result.is_some());
assert_eq!(group_result.unwrap().len(), 1);
let user_result = BlockingIronOxide::user_verify(&jwt)?;
assert!(!user_result.unwrap().needs_rotation());
let group_get_result = creator_sdk.group_get_metadata(group_create.id())?;
assert!(!group_get_result.needs_rotation().unwrap());
Ok(())
}
// Tests a DocumentOp (document_encrypt) and a DocumentAdvancedOp (document_encrypt_unmanaged)
#[test]
fn document_encrypt() -> Result<(), IronOxideErr> {
let account_id: UserId = create_id_all_classes("").try_into()?;
BlockingIronOxide::user_create(
&gen_jwt(Some(account_id.id())).0,
USER_PASSWORD,
&UserCreateOpts::new(false),
)?;
let device = BlockingIronOxide::generate_new_device(
&gen_jwt(Some(account_id.id())).0,
USER_PASSWORD,
&Default::default(),
)?
.into();
// let sdk = ironoxide::blocking::initialize(&device, Default::default())?;
let sdk = Arc::new(ironoxide::blocking::initialize(
&device,
BlockingIronOxideConfig {
rt_config: RuntimeConfig {
// TODO this doesn't seem to have the intended effect when executing inside of std::thread::spawn below
core_threads: Some(2),
max_threads: Some(3),
thread_stack_size_bytes: None,
},
},
)?);
let doc = [42u8; 64];
// Spin up an absurd number of threads in hopes that the Runtime internal to the sdk will limit the number
// that concurrently execute.
// TODO: It would be expected that all the threads spin up, but are then blocked when they try to use the SDK's
// TODO: internal Runtime, but that doesn't seem to be happening. The config above seems to have no affect
// TODO: on the number of CPU cores that are concurrently utilized.
// TODO See the test in document_ops where the shared Runtime is created in the test and the configs do seem to
// TODO: limit the number of concurrently executing threads
// TODO: Perhaps this has to do with the different between the blocking API's use of `block_on` vs the
// TODO: async test's usage of `tokio::spawn` to run tasks. More investigation is needed.
let mut threads = vec![];
for _i in 0..2000 {
let sdk_ref = sdk.clone();
threads.push(std::thread::spawn(move || {
dbg!(&_i);
let _result = sdk_ref.document_encrypt(&doc, &Default::default()).unwrap();
}));
}
let mut joined_count = 0;
for t in threads {
t.join().expect("couldn't join");
joined_count += 1;
}
assert_eq!(joined_count, 2000);
let doc_result = sdk.document_encrypt(&doc, &Default::default())?;
assert_eq!(doc_result.grants().len(), 1);
assert_eq!(doc_result.access_errs().len(), 0);
let doc_unmanaged_result = sdk.document_encrypt_unmanaged(&doc, &Default::default())?;
assert_eq!(doc_unmanaged_result.grants().len(), 1);
assert_eq!(doc_unmanaged_result.access_errs().len(), 0);
Ok(())
}
}