Skip to content

Commit

Permalink
Merge pull request #3 from flxo/master
Browse files Browse the repository at this point in the history
fix metadata path
  • Loading branch information
BarbossHack authored Aug 31, 2023
2 parents 5488e76 + 3f949bc commit 4ee1eed
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
on: [push, pull_request]

name: CI

jobs:
check:
name: Check
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: check

test:
name: Test Suite
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: -- --test-threads 1

fmt:
name: Rustfmt
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: stable
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
13 changes: 9 additions & 4 deletions src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,14 @@ impl Shmap {

// If an encryption key was provided, decrypt the value
let bytes = if let Some(cipher) = &self.cipher {
let nonce = Nonce::from_slice(&mmap[..12]);
cipher.decrypt(nonce, &mmap[12..])?
// Check length of data - must be at least 12 bytes for nonce
// otherwise it's not a valid nonce.
if mmap.len() < 12 {
return Ok(None);
} else {
let nonce = Nonce::from_slice(&mmap[..12]);
cipher.decrypt(nonce, &mmap[12..])?
}
} else {
mmap.to_vec()
};
Expand Down Expand Up @@ -262,8 +268,7 @@ impl Shmap {
if let Ok(dir_entry) = dir_entry_res {
let filename = dir_entry.file_name().to_string_lossy().to_string();
if filename.starts_with(SHMAP_PREFIX) && !filename.ends_with(METADATA_SUFFIX) {
let metadata_filename =
format!("{}.{}", dir_entry.path().to_string_lossy(), METADATA_SUFFIX);
let metadata_filename = format!("{}.{}", filename, METADATA_SUFFIX);
match self.get_deserialize::<Metadata>(&metadata_filename) {
Ok(Some(metadata)) => match metadata.expiration {
Some(expiration) => {
Expand Down
1 change: 1 addition & 0 deletions src/shm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::os::unix::io::RawFd;
pub const SHM_DIR: &str = "/dev/shm";

/// File descriptor struct, allowing to close fd on Drop
#[derive(Debug)]
pub struct Fd(RawFd);

impl From<RawFd> for Fd {
Expand Down
22 changes: 21 additions & 1 deletion src/tests/map.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{map::sanitize_key, shm::shm_open_read, Shmap};
use memmap2::Mmap;
use rand::{distributions::Alphanumeric, prelude::SliceRandom, thread_rng, Rng};
use std::time::Duration;
use std::{collections::HashSet, time::Duration};

pub fn rand_string(len: usize) -> String {
rand::thread_rng()
Expand Down Expand Up @@ -320,3 +320,23 @@ fn test_metadatas_concurrency() {
t1.join().unwrap();
t2.join().unwrap();
}

// test key listing
#[test]
fn test_list_keys() {
const NUM: usize = 5;
let shmap = Shmap::new();

let keys = (0..NUM).map(|i| rand_string(i)).collect::<HashSet<_>>();
keys.iter().for_each(|key| {
let value = rand_string(50);
shmap.insert(&key, value).unwrap();
});

// Other tests may run in parallel. Ensure that at least NUM keys are present.
assert!(shmap.keys().unwrap().len() >= NUM);

// At least all inserted keys must be present.
let current_keys = shmap.keys().unwrap().into_iter().collect();
assert!(keys.is_subset(&current_keys));
}

0 comments on commit 4ee1eed

Please sign in to comment.