Просмотр исходного кода

blockchain/monero: Add tests for FixedByteArray

parazyd 1 год назад
Родитель
Сommit
8df6845b7c
1 измененных файлов с 25 добавлено и 0 удалено
  1. 25 0
      src/blockchain/monero/fixed_array.rs

+ 25 - 0
src/blockchain/monero/fixed_array.rs

@@ -156,3 +156,28 @@ impl AsyncDecodable for FixedByteArray {
         Ok(Self { elems, len: len as u8 })
         Ok(Self { elems, len: len as u8 })
     }
     }
 }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+
+    #[test]
+    fn assert_size() {
+        assert_eq!(std::mem::size_of::<FixedByteArray>(), MAX_ARR_SIZE + 1);
+    }
+
+    #[test]
+    fn capacity_overflow_does_not_panic() {
+        let data = &[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f];
+        let _result = FixedByteArray::decode(&mut data.as_slice()).unwrap_err();
+    }
+
+    #[test]
+    fn length_check() {
+        let mut buf = [u8::try_from(MAX_ARR_SIZE).unwrap(); MAX_ARR_SIZE + 1];
+        let fixed_byte_array = FixedByteArray::decode(&mut buf.as_slice()).unwrap();
+        assert_eq!(fixed_byte_array.len(), MAX_ARR_SIZE);
+        buf[0] += 1;
+        FixedByteArray::decode(&mut buf.as_slice()).unwrap_err();
+    }
+}