Skip to content

Commit

Permalink
added python tests to travis
Browse files Browse the repository at this point in the history
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 <[email protected]>

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 <[email protected]>

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
  • Loading branch information
Victor Roest committed Jun 22, 2019
1 parent 8730e5e commit 43e39f7
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 5 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
language: rust
cache: cargo
dist: xenial

rust_template: &rust_template
before_script: cd rust_ipv8
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion py-ipv8
13 changes: 13 additions & 0 deletions rust_ipv8/src/crypto/signature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self, Box<dyn Error>> {
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<Self, Box<dyn Error>> {
warn!("DANGER ZONE! Creating seed without checking it against a public key");
Expand Down
1 change: 1 addition & 0 deletions rust_ipv8/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ extern crate log;

pub mod error;
pub mod serialization;
pub mod util;

pub mod community;
pub mod configuration;
Expand Down
52 changes: 52 additions & 0 deletions rust_ipv8/src/util.rs
Original file line number Diff line number Diff line change
@@ -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<T>(data: &[u8]) -> Result<&T, Box<dyn Error>>
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<dyn Error>> = 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<dyn Error>> = as_fixed_size(data);

match fixed {
Ok(_) => assert!(false),
Err(_) => assert!(true),
};
}

}
15 changes: 12 additions & 3 deletions rust_ipv8_in_python/src/borrowedbundler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<()> {
Expand Down Expand Up @@ -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))
}
Expand Down

0 comments on commit 43e39f7

Please sign in to comment.