From cdf151644c2bc7108418ffc8a4270a1e145c6184 Mon Sep 17 00:00:00 2001 From: Victor Roest Date: Sat, 22 Jun 2019 15:34:46 +0200 Subject: [PATCH] added python tests to travis MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit sudo fix-travis install pipenv from pip what are repos anyways why have the latest versions of packages in repos, what would be the use of that?! ~~or why not run ci on docker containers~~ We heard you liked dependencies so we added dependencies to your dependency manager Signing and verifying now done in rust instead of python Co-authored-by: Jonathan Dönszelmann updated py-ipv8 formatted updated Pipfile dependencies install libssl to download pipenv dependencies add openssl install with pip3 python3.7 + pip ensure pip #42 fuck travis commit message courtesy of: Co-authored-by: Jonathan Dönszelmann Travis docs were wrong, what a suprise guess updating ubuntu may help idk, kinda want to run gitlab-ci inside of travis now next up: what are version numbers anyway python without pip, c'mon travis are you even trying? why did you even make the 'python' command in your yml.... travis sucks ubuntu + travis sucks more how long will this go on for yes no getting salty y --- .travis.yml | 15 +++++++ Pipfile.lock | 2 +- py-ipv8 | 2 +- rust_ipv8/src/crypto/signature.rs | 13 ++++++ rust_ipv8/src/lib.rs | 1 + rust_ipv8/src/util.rs | 52 ++++++++++++++++++++++ rust_ipv8_in_python/src/borrowedbundler.rs | 15 +++++-- 7 files changed, 95 insertions(+), 5 deletions(-) create mode 100644 rust_ipv8/src/util.rs diff --git a/.travis.yml b/.travis.yml index f9b3f988..2627fd9c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: rust cache: cargo +dist: xenial rust_template: &rust_template before_script: cd rust_ipv8 @@ -36,6 +37,20 @@ jobs: grcov ccov.zip -s src/ -t lcov --llvm --branch --ignore-not-existing --ignore-dir "/*" -o lcov.info && bash <(curl -s https://codecov.io/bash) -f lcov.info; + - install: + - sudo apt update -y + - sudo apt install -y software-properties-common + - sudo add-apt-repository -y ppa:deadsnakes/ppa + - sudo apt update -y + - sudo apt install -y python3.7-dev libsodium-dev + - curl https://bootstrap.pypa.io/get-pip.py | sudo python3.7 + - sudo python3.7 -m pip install pipenv + - sudo python3.7 -m pip install pyo3-pack + script: + - bash -c "PYTHON_VERSION=python3.7 ./run_python_tests.sh" + name: "Python library" + rust: nightly + - script: cargo test name: "Stable" <<: *rust_template diff --git a/Pipfile.lock b/Pipfile.lock index a2c88b02..364b797b 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -193,7 +193,7 @@ }, "rust-ipv8-in-python": { "hashes": [ - "sha256:23f43225d8b655f55c4ad6a29498f824f01263cd4ab07ae02c101f2ba36d3eba" + "sha256:be4cacbfccde1d8f5774f4d0746d8e2e103c2682a751c3270249f65c175521c3" ], "version": "==0.1.0" }, diff --git a/py-ipv8 b/py-ipv8 index 539a32e0..f6e9db24 160000 --- a/py-ipv8 +++ b/py-ipv8 @@ -1 +1 @@ -Subproject commit 539a32e0e0b2b9fdd9f76235821363e4680e3c2a +Subproject commit f6e9db246945235b64accd585a3d74b84804d611 diff --git a/rust_ipv8/src/crypto/signature.rs b/rust_ipv8/src/crypto/signature.rs index 18998646..69bb84ec 100644 --- a/rust_ipv8/src/crypto/signature.rs +++ b/rust_ipv8/src/crypto/signature.rs @@ -69,6 +69,19 @@ impl KeyPair { Ok(KeyPair(ring_key)) } + /// Creates a keypair with the provided and seed and checks if the generated key matches the given public key + pub fn from_seed_checked( + seed: &[u8; 32], + publickey: &Ed25519PublicKey, + ) -> Result> { + let trusted_seed = untrusted::Input::from(seed); + let public = untrusted::Input::from(&*publickey); + let ring_key = + ring::signature::Ed25519KeyPair::from_seed_and_public_key(trusted_seed, public) + .or_else(|_| Err(Box::new(KeyRejectedError)))?; + Ok(KeyPair(ring_key)) + } + #[doc(hidden)] pub fn from_seed_unchecked(seed: &[u8; 32]) -> Result> { warn!("DANGER ZONE! Creating seed without checking it against a public key"); diff --git a/rust_ipv8/src/lib.rs b/rust_ipv8/src/lib.rs index 39afa458..6f5b7f34 100644 --- a/rust_ipv8/src/lib.rs +++ b/rust_ipv8/src/lib.rs @@ -5,6 +5,7 @@ extern crate log; pub mod error; pub mod serialization; +pub mod util; pub mod community; pub mod configuration; diff --git a/rust_ipv8/src/util.rs b/rust_ipv8/src/util.rs new file mode 100644 index 00000000..12799fcb --- /dev/null +++ b/rust_ipv8/src/util.rs @@ -0,0 +1,52 @@ +//! Various utility functions to be used in conjunction with ipv8 + +use std::error::Error; + +create_error!(ConversionError, "Converting to a fixed size array failed."); + +/// Helper function types which have the [FromBytes](zerocopy::FromBytes) trait to be converted to some fixed size variant. +/// Doesn't copy. +pub fn as_fixed_size(data: &[u8]) -> Result<&T, Box> +where + T: zerocopy::FromBytes, +{ + Ok( + (zerocopy::LayoutVerified::<_, T>::new(data).ok_or_else(|| Box::new(ConversionError))?) + .into_ref(), + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_as_fixed_size_valid() { + let data = &[0u8, 1u8, 2u8]; + let fixed: [u8; 3] = *as_fixed_size(data).unwrap(); + assert_eq!(fixed, [0u8, 1u8, 2u8]); + } + + #[test] + fn test_as_fixed_size_invalid_too_large() { + let data = &[0u8, 1u8, 2u8]; + let fixed: Result<&[u8; 4], Box> = as_fixed_size(data); + + match fixed { + Ok(_) => assert!(false), + Err(_) => assert!(true), + }; + } + + #[test] + fn test_as_fixed_size_invalid_too_small() { + let data = &[0u8, 1u8, 2u8]; + let fixed: Result<&[u8; 2], Box> = as_fixed_size(data); + + match fixed { + Ok(_) => assert!(false), + Err(_) => assert!(true), + }; + } + +} diff --git a/rust_ipv8_in_python/src/borrowedbundler.rs b/rust_ipv8_in_python/src/borrowedbundler.rs index 9a2270ca..f4590c9c 100644 --- a/rust_ipv8_in_python/src/borrowedbundler.rs +++ b/rust_ipv8_in_python/src/borrowedbundler.rs @@ -2,6 +2,8 @@ use pyo3::prelude::*; use rust_ipv8::serialization::Packet; use pyo3::exceptions::{ValueError, KeyError}; use pyo3::types::PyBytes; +use rust_ipv8::crypto::signature::KeyPair; +use rust_ipv8::util::as_fixed_size; #[pymodule] pub fn borrowed_bundler(_py: Python, m: &PyModule) -> PyResult<()> { @@ -30,15 +32,22 @@ pub fn borrowed_bundler(_py: Python, m: &PyModule) -> PyResult<()> { #[pyfn(m, "borrowed_create_signature")] fn borrowed_create_signature<'py>( py: Python<'py>, + seed: &PyBytes, key: &PyBytes, data: &PyBytes, ) -> PyResult<&'py PyBytes> { let p = Packet(data.as_bytes().to_vec()); - let ourkey = PrivateKey::from_vec(key.as_bytes().to_vec()) + + // Zerocopy that shiz + let key_fixed: &[u8; 32] = + as_fixed_size(key.as_bytes()).or(Err(KeyError::py_err("Key length was wrong")))?; + let seed_fixed: &[u8; 32] = + as_fixed_size(seed.as_bytes()).or(Err(KeyError::py_err("Seed length was wrong")))?; + + let ourkey = KeyPair::from_seed_checked(seed_fixed, key_fixed) .or(Err(KeyError::py_err("Invalid key given")))?; - dbg!(key); - let signed = p.sign(ourkey).or(Err(KeyError::py_err("Sign error")))?; + let signed = p.sign(&ourkey).or(Err(KeyError::py_err("Sign error")))?; Ok(PyBytes::new(py, &*signed.0)) }