capsules_core/test/
rng.rs1use core::cell::Cell;
15
16use kernel::ErrorCode;
17use kernel::debug;
18use kernel::hil::entropy;
19use kernel::hil::rng;
20
21const ELEMENTS: usize = 8;
22
23pub struct TestRng<'a> {
25 rng: &'a dyn rng::Rng<'a>,
26 pool: Cell<[u32; ELEMENTS]>,
27 count: Cell<usize>,
28}
29
30impl<'a> TestRng<'a> {
31 pub fn new(rng: &'a dyn rng::Rng<'a>) -> TestRng<'a> {
32 TestRng {
33 rng,
34 pool: Cell::new([0xeeeeeeee; ELEMENTS]),
35 count: Cell::new(0),
36 }
37 }
38
39 pub fn run(&self) {
40 match self.rng.get() {
41 Ok(()) => debug!("RNG test: first get Ok(())"),
42 _ => panic!("RNG test: unable to get random numbers"),
43 }
44 }
45}
46
47impl rng::Client for TestRng<'_> {
48 fn randomness_available(
49 &self,
50 randomness: &mut dyn Iterator<Item = u32>,
51 error: Result<(), ErrorCode>,
52 ) -> rng::Continue {
53 let mut val = randomness.next();
54 if error != Ok(()) {
55 panic!(
56 "RNG test: randomness_available called with error {:?}",
57 error
58 );
59 }
60 while val.is_some() {
61 let data = val.unwrap();
63
64 let mut pool = self.pool.get();
65 let mut count = self.count.get();
66 pool[count] = data;
67 count += 1;
68 self.pool.set(pool);
69 self.count.set(count);
70
71 if count >= ELEMENTS {
72 debug!("RNG test: obtained all {} values. They are:", count);
73 for (i, c) in pool.iter().enumerate() {
74 debug!("[{:02x}]: {:08x}", i, c);
75 }
76 return rng::Continue::Done;
77 } else {
78 val = randomness.next();
79 }
80 }
81 rng::Continue::More
83 }
84}
85
86pub struct TestEntropy32<'a> {
88 egen: &'a dyn entropy::Entropy32<'a>,
89 pool: Cell<[u32; ELEMENTS]>,
90 count: Cell<usize>,
91}
92
93impl<'a> TestEntropy32<'a> {
94 pub fn new(egen: &'a dyn entropy::Entropy32<'a>) -> TestEntropy32<'a> {
95 TestEntropy32 {
96 egen,
97 pool: Cell::new([0xeeeeeeee; ELEMENTS]),
98 count: Cell::new(0),
99 }
100 }
101
102 pub fn run(&self) {
103 match self.egen.get() {
104 Ok(()) => debug!("Entropy32 test: first get Ok(())"),
105 _ => panic!("Entropy32 test: unable to get entropy"),
106 }
107 }
108}
109
110impl entropy::Client32 for TestEntropy32<'_> {
111 fn entropy_available(
112 &self,
113 entropy: &mut dyn Iterator<Item = u32>,
114 error: Result<(), ErrorCode>,
115 ) -> entropy::Continue {
116 let mut val = entropy.next();
117 if error != Ok(()) {
118 panic!(
119 "RNG test: randomness_available called with error {:?}",
120 error
121 );
122 }
123 while val.is_some() {
124 let data = val.unwrap();
126
127 let mut pool = self.pool.get();
128 let mut count = self.count.get();
129 pool[count] = data;
130 count += 1;
131 self.pool.set(pool);
132 self.count.set(count);
133
134 if count >= ELEMENTS {
135 debug!("Entropy test: obtained all {} values. They are:", count);
136 for i in 0..pool.len() {
137 debug!("[{:02x}]: {:08x}", i, pool[i]);
138 }
139 return entropy::Continue::Done;
140 } else {
141 val = entropy.next();
142 }
143 }
144 entropy::Continue::More
146 }
147}
148
149pub struct TestEntropy8<'a> {
151 egen: &'a dyn entropy::Entropy8<'a>,
152 pool: Cell<[u8; ELEMENTS]>,
153 count: Cell<usize>,
154}
155
156impl<'a> TestEntropy8<'a> {
157 pub fn new(egen: &'a dyn entropy::Entropy8<'a>) -> TestEntropy8<'a> {
158 TestEntropy8 {
159 egen,
160 pool: Cell::new([0xee; ELEMENTS]),
161 count: Cell::new(0),
162 }
163 }
164
165 pub fn run(&self) {
166 match self.egen.get() {
167 Ok(()) => debug!("Entropy8 test: first get Ok(())"),
168 _ => panic!("RNG test: unable to get random numbers"),
169 }
170 }
171}
172
173impl entropy::Client8 for TestEntropy8<'_> {
174 fn entropy_available(
175 &self,
176 entropy: &mut dyn Iterator<Item = u8>,
177 error: Result<(), ErrorCode>,
178 ) -> entropy::Continue {
179 let mut val = entropy.next();
180 if error != Ok(()) {
181 panic!(
182 "Entropy8 test: entropy_available called with error {:?}",
183 error
184 );
185 }
186 while val.is_some() {
187 debug!("Entropy8 test: entropy_available iterator returned Some, adding.");
188 let data = val.unwrap();
189
190 let mut pool = self.pool.get();
191 let mut count = self.count.get();
192 pool[count] = data;
193 count += 1;
194 self.pool.set(pool);
195 self.count.set(count);
196
197 if count >= ELEMENTS {
198 debug!("RNG test: obtained {} values. They are:", count);
199 for i in 0..pool.len() {
200 debug!("[{:02x}]: {:02x}", i, pool[i]);
201 }
202 return entropy::Continue::Done;
203 } else {
204 val = entropy.next();
205 }
206 }
207 debug!("Entropy8 test: entropy_available iterator returned None, requesting more.");
208 entropy::Continue::More
210 }
211}