Skip to content

Commit

Permalink
Expose internal functions to create storages, and make tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
bltavares committed May 17, 2020
1 parent 1d8c6da commit e846f3b
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 19 deletions.
2 changes: 1 addition & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use test::Bencher;

use hypercore::{Feed, Storage};

async fn create_feed(page_size: usize) -> Result<Feed<RandomAccessMemory>, Error> {
async fn create_feed(page_size: usize) -> Result<Feed, Error> {
let storage =
Storage::new(|_| Box::pin(async move { Ok(RandomAccessMemory::new(page_size)) })).await?;
Feed::with_storage(storage).await
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ pub use crate::feed::Feed;
pub use crate::feed_builder::FeedBuilder;
pub use crate::proof::Proof;
pub use crate::replicate::Peer;
pub use crate::storage::{Node, NodeTrait, Storage, Store};
pub use crate::storage::{
storage_disk, storage_memory, BoxStorage, Node, NodeTrait, Storage, Store,
};
pub use ed25519_dalek::{PublicKey, SecretKey};

use std::path::Path;
Expand Down
1 change: 1 addition & 0 deletions src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub struct PartialKeypair {
pub secret: Option<SecretKey>,
}

/// Dynamic-dispatch Storage wrapper
pub type BoxStorage = Box<dyn DynStorage>;

/// Create a new instance backed by a `RandomAccessMemory` instance.
Expand Down
2 changes: 1 addition & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use futures::future::FutureExt;
use hypercore::{Feed, Storage, Store};
use random_access_memory as ram;

pub async fn create_feed(page_size: usize) -> Result<Feed<ram::RandomAccessMemory>, Error> {
pub async fn create_feed(page_size: usize) -> Result<Feed, Error> {
let create = |_store: Store| async move { Ok(ram::RandomAccessMemory::new(page_size)) }.boxed();
let storage = Storage::new(create).await?;
Feed::with_storage(storage).await
Expand Down
4 changes: 2 additions & 2 deletions tests/compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use std::path::{Path, PathBuf};
use data_encoding::HEXLOWER;
use ed25519_dalek::{Keypair, Signature};
use hypercore::Feed;
use hypercore::{Storage, Store};
use hypercore::{Storage, Store, BoxStorage};
use random_access_disk::RandomAccessDisk;
use remove_dir_all::remove_dir_all;

Expand Down Expand Up @@ -144,7 +144,7 @@ fn storage_path<P: AsRef<Path>>(dir: P, s: Store) -> PathBuf {
dir.as_ref().join(filename)
}

async fn mk_storage() -> (PathBuf, Storage<RandomAccessDisk>) {
async fn mk_storage() -> (PathBuf, BoxStorage) {
let temp_dir = tempfile::tempdir().unwrap();
let dir = temp_dir.into_path();
let storage = Storage::new(|s| {
Expand Down
17 changes: 8 additions & 9 deletions tests/feed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,15 @@ mod common;

use common::create_feed;
use hypercore::{generate_keypair, Feed, NodeTrait, PublicKey, SecretKey, Storage};
use random_access_storage::RandomAccess;
use hypercore::{storage_disk, storage_memory};
use std::env::temp_dir;
use std::fmt::Debug;
use std::fs;
use std::io::Write;

#[async_std::test]
async fn create_with_key() {
let keypair = generate_keypair();
let storage = Storage::new_memory().await.unwrap();
let storage = storage_memory().await.unwrap();
let _feed = Feed::builder(keypair.public, storage)
.secret_key(keypair.secret)
.build()
Expand Down Expand Up @@ -155,7 +154,7 @@ async fn put_with_data() {

// Create a second feed with the first feed's key.
let (public, secret) = copy_keys(&a);
let storage = Storage::new_memory().await.unwrap();
let storage = storage_memory().await.unwrap();
let mut b = Feed::builder(public, storage)
.secret_key(secret)
.build()
Expand Down Expand Up @@ -187,7 +186,7 @@ async fn put_with_data() {

#[async_std::test]
async fn create_with_storage() {
let storage = Storage::new_memory().await.unwrap();
let storage = storage_memory().await.unwrap();
assert!(
Feed::with_storage(storage).await.is_ok(),
"Could not create a feed with a storage."
Expand All @@ -196,7 +195,7 @@ async fn create_with_storage() {

#[async_std::test]
async fn create_with_stored_public_key() {
let mut storage = Storage::new_memory().await.unwrap();
let mut storage = storage_memory().await.unwrap();
let keypair = generate_keypair();
storage.write_public_key(&keypair.public).await.unwrap();
assert!(
Expand All @@ -207,7 +206,7 @@ async fn create_with_stored_public_key() {

#[async_std::test]
async fn create_with_stored_keys() {
let mut storage = Storage::new_memory().await.unwrap();
let mut storage = storage_memory().await.unwrap();
let keypair = generate_keypair();
storage.write_public_key(&keypair.public).await.unwrap();
storage.write_secret_key(&keypair.secret).await.unwrap();
Expand All @@ -218,7 +217,7 @@ async fn create_with_stored_keys() {
}

fn copy_keys(
feed: &Feed<impl RandomAccess<Error = Box<dyn std::error::Error + Send + Sync>> + Debug + Send>,
feed: &Feed,
) -> (PublicKey, SecretKey) {
match &feed.secret_key() {
Some(secret) => {
Expand Down Expand Up @@ -254,7 +253,7 @@ async fn audit() {
async fn audit_bad_data() {
let mut dir = temp_dir();
dir.push("audit_bad_data");
let storage = Storage::new_disk(&dir).await.unwrap();
let storage = storage_disk(&dir).await.unwrap();
let mut feed = Feed::with_storage(storage).await.unwrap();
feed.append(b"hello").await.unwrap();
feed.append(b"world").await.unwrap();
Expand Down
10 changes: 5 additions & 5 deletions tests/storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use ed25519_dalek::PublicKey;
use hypercore::{generate_keypair, sign, verify, Signature, Storage};
use hypercore::{generate_keypair, sign, storage_memory, verify, Signature};

#[async_std::test]
async fn should_write_and_read_keypair() {
Expand All @@ -8,7 +8,7 @@ async fn should_write_and_read_keypair() {
// prepare a signature
let sig: Signature = sign(&keypair.public, &keypair.secret, msg);

let mut storage = Storage::new_memory().await.unwrap();
let mut storage = storage_memory().await.unwrap();
assert!(
storage.write_secret_key(&keypair.secret).await.is_ok(),
"Can not store secret key."
Expand All @@ -27,7 +27,7 @@ async fn should_write_and_read_keypair() {
#[async_std::test]
async fn should_read_partial_keypair() {
let keypair = generate_keypair();
let mut storage = Storage::new_memory().await.unwrap();
let mut storage = storage_memory().await.unwrap();
assert!(
storage.write_public_key(&keypair.public).await.is_ok(),
"Can not store public key."
Expand All @@ -39,13 +39,13 @@ async fn should_read_partial_keypair() {

#[async_std::test]
async fn should_read_no_keypair() {
let mut storage = Storage::new_memory().await.unwrap();
let mut storage = storage_memory().await.unwrap();
let partial = storage.read_partial_keypair().await;
assert!(partial.is_none(), "A key is present");
}

#[async_std::test]
async fn should_read_empty_public_key() {
let mut storage = Storage::new_memory().await.unwrap();
let mut storage = storage_memory().await.unwrap();
assert!(storage.read_public_key().await.is_err());
}

0 comments on commit e846f3b

Please sign in to comment.