Added zero length check to gen_rand_key and unit tests for this
@@ -4,9 +4,26 @@ use rand::{OsRng, Rng};
/// Return a random byte vector of a given length. This uses the [rand](https://crates.io/crates/rand) crate,
/// which means that random data is read from the OS source /dev/urandom or CryptGenRandom().
pub fn gen_rand_key(len: usize) -> Vec<u8> {
+
+ assert!(len > 0);
+
let mut generator = OsRng::new().unwrap();
let mut rand_bytes_vec = vec![0u8; len];
generator.fill_bytes(&mut rand_bytes_vec);
rand_bytes_vec
}
+
+#[test]
+fn rand_key_len_ok() {
+
+ gen_rand_key(4);
+}
+
+#[test]
+#[should_panic]
+fn rand_key_len_zero() {
+
+ gen_rand_key(0);
+
+}
\ No newline at end of file
