Skip to content

Commit

Permalink
Add some tests for show
Browse files Browse the repository at this point in the history
- Add test pg store directory
- Add a passing test for a file with just a gpg extension ie (test.gpg)
- Add a failing test for a file with a name like google.com.gpg.  Need
  to fix show function so this test passes
  • Loading branch information
allie-wake-up committed Jan 5, 2023
1 parent 28da397 commit bd9c7d5
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 13 deletions.
33 changes: 25 additions & 8 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@ env:
CARGO_TERM_COLOR: always

jobs:
build:
name: Test on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
test:
name: Test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
Expand All @@ -24,11 +21,31 @@ jobs:
with:
toolchain: stable
components: rustfmt, clippy
- name: Setup gpg
run: |
echo "${{secrets.GPG_PUBLIC_KEY}}" | gpg --import
echo "${{secrets.GPG_PRIVATE_KEY}}" | gpg --import
echo "${{secrets.GPG_OWNERTRUST}}" | gpg --import-ownertrust
./setup-tests.sh `gpg --list-keys --with-colons | awk -F: '/^fpr/ { print $10 }'`
- name: Clippy
run: cargo clippy
- name: RustFmt
run: cargo fmt -- --check
- name: Build
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose

build:
name: Build on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
- uses: actions/checkout@v2
- name: Setup rust toolchain
uses: actions-rs/toolchain@v1
with:
toolchain: stable
- name: Build
run: cargo build --verbose
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Session.vim
test-store-enc
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ unix password store compatible password manager written in rust

## Why?
Mainly to learn rust. My goal is to re-implement parts of pass in rust and try for very similar commands

## Running Tests

You need a gpg key pair to test with. Then run `./setup-tests.sh the-key-id`. This will make a `test-store-enc` folder with a `.gpg-id` file in it. It will then encrypt all of the files in the `test-store` folder.
15 changes: 15 additions & 0 deletions setup-tests.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh

if [ "$1" == "" ]; then
echo "Missing key id"
exit
fi

mkdir test-store-enc
echo "$1" > test-store-enc/.gpg-id

cd test-store
for file in *
do
gpg -o "../test-store-enc/$file" --recipient "$1" -e "$file"
done
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub use error::PointGuardError;
pub use opts::{Opts, Show, SubCommand};
pub use settings::Settings;

pub fn run(opts: Opts, settings: Settings) -> error::Result<()> {
pub fn run(buffer: &mut dyn std::io::Write, opts: Opts, settings: Settings) -> error::Result<()> {
let input = opts.input;
match opts
.subcmd
Expand All @@ -23,6 +23,6 @@ pub fn run(opts: Opts, settings: Settings) -> error::Result<()> {
Ok(())
}
}
SubCommand::Show(show_opts) => show::show(show_opts, settings),
SubCommand::Show(show_opts) => show::show(buffer, show_opts, settings),
}
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use clap::Clap;
fn main() {
let opts: pg::Opts = pg::Opts::parse();
let settings = pg::Settings::new().unwrap();
match pg::run(opts, settings) {
let mut stdout = std::io::stdout();
match pg::run(&mut stdout, opts, settings) {
Ok(_) => (),
Err(e) => match e {
pg::PointGuardError::GpgError(status, message) => {
Expand Down
44 changes: 42 additions & 2 deletions src/show.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ fn print_tree(path: &Path, input: Option<String>) -> Result<()> {
Ok(())
}

pub fn show(opts: Show, settings: Settings) -> Result<()> {
pub fn show(buffer: &mut dyn io::Write, opts: Show, settings: Settings) -> Result<()> {
let path = match &opts.input {
Some(name) => settings.dir.join(name),
None => settings.dir,
};
let file = path.with_extension("gpg");
println!("path: {:?}, file: {:?}", path, file);
if !path.exists() && !file.exists() {
return Err(io::Error::new(
io::ErrorKind::NotFound,
Expand All @@ -81,7 +82,46 @@ pub fn show(opts: Show, settings: Settings) -> Result<()> {
print_tree(&path, opts.input)
} else {
let pw = gpg::decrypt(&file)?;
println!("{}", pw);
write!(buffer, "{}", pw)?;
Ok(())
}
}

#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;

fn get_test_settings() -> Settings {
Settings {
dir: PathBuf::from("test-store-enc"),
clip_time: 45,
generated_length: 25,
editor: String::from("vim"),
}
}

#[test]
fn print_password() {
let mut result: Vec<u8> = vec![];
show(
&mut result,
Show::new(Some(String::from("test"))),
get_test_settings(),
)
.unwrap();
assert_eq!(String::from_utf8(result).unwrap().trim(), "password");
}

#[test]
fn print_website_password() {
let mut result: Vec<u8> = vec![];
show(
&mut result,
Show::new(Some(String::from("pointguard.dev"))),
get_test_settings(),
)
.unwrap();
assert_eq!(String::from_utf8(result).unwrap().trim(), "password");
}
}
1 change: 1 addition & 0 deletions test-store/pointguard.dev.gpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
password
1 change: 1 addition & 0 deletions test-store/test.gpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
password

0 comments on commit bd9c7d5

Please sign in to comment.