2019-02-22 19:26:10 +00:00
|
|
|
use std::fs::{create_dir_all, read_link, remove_file, rename, File};
|
2019-02-07 19:58:31 +00:00
|
|
|
use std::io::{Read, Write};
|
2019-02-21 21:05:01 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-02-07 19:58:31 +00:00
|
|
|
use std::str;
|
2018-08-16 18:35:19 +00:00
|
|
|
|
|
|
|
use serde_json;
|
2019-02-07 19:58:31 +00:00
|
|
|
use tempfile;
|
2018-10-24 17:45:11 +00:00
|
|
|
use url;
|
2019-02-22 14:45:34 +00:00
|
|
|
use pathdiff::diff_paths;
|
2018-08-16 18:35:19 +00:00
|
|
|
|
2019-02-22 15:14:57 +00:00
|
|
|
use sequoia_openpgp::armor::{Writer, Kind};
|
|
|
|
|
2019-02-07 19:58:31 +00:00
|
|
|
use database::{Database, Delete, Verify};
|
2019-01-04 13:07:14 +00:00
|
|
|
use types::{Email, Fingerprint, KeyID};
|
2019-02-07 19:58:31 +00:00
|
|
|
use Result;
|
2018-08-16 18:35:19 +00:00
|
|
|
|
2018-09-19 20:22:59 +00:00
|
|
|
pub struct Filesystem {
|
2018-08-16 18:35:19 +00:00
|
|
|
base: PathBuf,
|
2019-02-21 21:05:01 +00:00
|
|
|
base_by_keyid: PathBuf,
|
|
|
|
base_by_fingerprint: PathBuf,
|
|
|
|
base_by_email: PathBuf,
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-21 21:05:01 +00:00
|
|
|
/// Returns the given path, ensuring that the parent directory exists.
|
|
|
|
///
|
|
|
|
/// Use this on paths returned by .path_to_* before creating the
|
|
|
|
/// object.
|
|
|
|
fn ensure_parent(path: &Path) -> Result<&Path> {
|
|
|
|
let parent = path.parent().unwrap();
|
2019-02-22 10:56:31 +00:00
|
|
|
create_dir_all(parent)?;
|
2019-02-21 21:05:01 +00:00
|
|
|
Ok(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-16 18:35:19 +00:00
|
|
|
impl Filesystem {
|
|
|
|
pub fn new<P: Into<PathBuf>>(base: P) -> Result<Self> {
|
|
|
|
use std::fs;
|
|
|
|
|
|
|
|
let base: PathBuf = base.into();
|
|
|
|
|
|
|
|
if fs::create_dir(&base).is_err() {
|
|
|
|
let meta = fs::metadata(&base);
|
|
|
|
|
|
|
|
match meta {
|
|
|
|
Ok(meta) => {
|
|
|
|
if !meta.file_type().is_dir() {
|
2019-02-22 20:29:53 +00:00
|
|
|
return Err(failure::format_err!(
|
2019-02-07 19:58:31 +00:00
|
|
|
"'{}' exists already and is not a directory",
|
2019-02-22 20:29:53 +00:00
|
|
|
base.display()));
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if meta.permissions().readonly() {
|
2019-02-22 20:29:53 +00:00
|
|
|
return Err(failure::format_err!(
|
2019-02-07 19:58:31 +00:00
|
|
|
"Cannot write '{}'",
|
2019-02-22 20:29:53 +00:00
|
|
|
base.display()));
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(e) => {
|
2019-02-22 20:29:53 +00:00
|
|
|
return Err(failure::format_err!(
|
2019-02-07 19:58:31 +00:00
|
|
|
"Cannot read '{}': {}",
|
2019-02-22 20:29:53 +00:00
|
|
|
base.display(), e));
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// create directories
|
|
|
|
create_dir_all(base.join("verification_tokens"))?;
|
|
|
|
create_dir_all(base.join("deletion_tokens"))?;
|
|
|
|
create_dir_all(base.join("scratch_pad"))?;
|
2019-02-21 21:05:01 +00:00
|
|
|
|
|
|
|
let base_by_keyid = base.join("public").join("by-keyid");
|
2019-02-22 15:08:52 +00:00
|
|
|
let base_by_fingerprint = base.join("public").join("by-fpr");
|
2019-02-21 21:05:01 +00:00
|
|
|
let base_by_email = base.join("public").join("by-email");
|
|
|
|
create_dir_all(&base_by_keyid)?;
|
|
|
|
create_dir_all(&base_by_fingerprint)?;
|
|
|
|
create_dir_all(&base_by_email)?;
|
2018-08-16 18:35:19 +00:00
|
|
|
|
|
|
|
info!("Opened base dir '{}'", base.display());
|
2019-02-21 21:05:01 +00:00
|
|
|
Ok(Filesystem {
|
|
|
|
base: base,
|
|
|
|
base_by_keyid: base_by_keyid,
|
|
|
|
base_by_fingerprint: base_by_fingerprint,
|
|
|
|
base_by_email: base_by_email,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the path to the given KeyID.
|
|
|
|
fn path_to_keyid(&self, keyid: &KeyID) -> PathBuf {
|
2019-02-21 23:29:15 +00:00
|
|
|
let hex = keyid.to_string();
|
|
|
|
self.base_by_keyid.join(&hex[..2]).join(&hex[2..])
|
2019-02-21 21:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the path to the given Fingerprint.
|
|
|
|
fn path_to_fingerprint(&self, fingerprint: &Fingerprint) -> PathBuf {
|
2019-02-21 23:29:15 +00:00
|
|
|
let hex = fingerprint.to_string();
|
|
|
|
self.base_by_fingerprint.join(&hex[..2]).join(&hex[2..])
|
2019-02-21 21:05:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns the path to the given Email.
|
|
|
|
fn path_to_email(&self, email: &str) -> PathBuf {
|
2019-02-21 23:29:15 +00:00
|
|
|
if email.len() > 2 {
|
|
|
|
self.base_by_email.join(&email[..2]).join(&email[2..])
|
|
|
|
} else {
|
|
|
|
self.base_by_email.join(email)
|
|
|
|
}
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_token<'a>(&self, base: &'a str) -> Result<(File, String)> {
|
|
|
|
use rand::distributions::Alphanumeric;
|
2019-02-07 19:58:31 +00:00
|
|
|
use rand::{thread_rng, Rng};
|
2018-08-16 18:35:19 +00:00
|
|
|
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
// samples from [a-zA-Z0-9]
|
|
|
|
// 43 chars ~ 256 bit
|
|
|
|
let name: String = rng.sample_iter(&Alphanumeric).take(43).collect();
|
|
|
|
let dir = self.base.join(base);
|
|
|
|
let fd = File::create(dir.join(name.clone()))?;
|
|
|
|
|
|
|
|
Ok((fd, name))
|
|
|
|
}
|
|
|
|
|
2019-02-07 19:58:31 +00:00
|
|
|
fn pop_token<'a>(
|
|
|
|
&self, base: &'a str, token: &'a str,
|
|
|
|
) -> Result<Box<[u8]>> {
|
2018-08-16 18:35:19 +00:00
|
|
|
let path = self.base.join(base).join(token);
|
|
|
|
let buf = {
|
|
|
|
let mut fd = File::open(path.clone())?;
|
|
|
|
let mut buf = Vec::default();
|
|
|
|
|
|
|
|
fd.read_to_end(&mut buf)?;
|
|
|
|
buf.into_boxed_slice()
|
|
|
|
};
|
|
|
|
|
|
|
|
remove_file(path)?;
|
|
|
|
Ok(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 19:26:10 +00:00
|
|
|
// Like `symlink`, but instead of failing if `symlink_name` already
|
|
|
|
// exists, atomically update `symlink_name` to have `symlink_content`.
|
|
|
|
fn symlink(symlink_content: &Path, symlink_name: &Path) -> Result<()> {
|
|
|
|
use std::os::unix::fs::{symlink};
|
|
|
|
|
|
|
|
let symlink_dir = ensure_parent(symlink_name)?.parent().unwrap();
|
|
|
|
let tmp_dir = tempfile::Builder::new()
|
|
|
|
.prefix("link")
|
|
|
|
.rand_bytes(16)
|
|
|
|
.tempdir_in(symlink_dir)?;
|
|
|
|
let symlink_name_tmp = tmp_dir.path().join("link");
|
|
|
|
|
|
|
|
symlink(&symlink_content, &symlink_name_tmp)?;
|
|
|
|
rename(&symlink_name_tmp, &symlink_name)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2018-08-16 18:35:19 +00:00
|
|
|
impl Database for Filesystem {
|
2018-09-19 20:22:59 +00:00
|
|
|
fn new_verify_token(&self, payload: Verify) -> Result<String> {
|
2018-08-16 18:35:19 +00:00
|
|
|
let (mut fd, name) = self.new_token("verification_tokens")?;
|
|
|
|
fd.write_all(serde_json::to_string(&payload)?.as_bytes())?;
|
|
|
|
|
|
|
|
Ok(name)
|
|
|
|
}
|
|
|
|
|
2018-09-19 20:22:59 +00:00
|
|
|
fn new_delete_token(&self, payload: Delete) -> Result<String> {
|
2018-08-16 18:35:19 +00:00
|
|
|
let (mut fd, name) = self.new_token("deletion_tokens")?;
|
|
|
|
fd.write_all(serde_json::to_string(&payload)?.as_bytes())?;
|
|
|
|
|
|
|
|
Ok(name)
|
|
|
|
}
|
|
|
|
|
2019-02-22 11:59:25 +00:00
|
|
|
fn update(
|
|
|
|
&self, fpr: &Fingerprint, new: Option<&[u8]>,
|
|
|
|
) -> Result<()> {
|
2019-02-21 21:05:01 +00:00
|
|
|
let target = self.path_to_fingerprint(fpr);
|
2018-08-16 18:35:19 +00:00
|
|
|
let dir = self.base.join("scratch_pad");
|
|
|
|
|
|
|
|
match new {
|
|
|
|
Some(new) => {
|
|
|
|
let mut tmp = tempfile::Builder::new()
|
|
|
|
.prefix("key")
|
|
|
|
.rand_bytes(16)
|
|
|
|
.tempfile_in(dir)?;
|
2019-02-22 15:14:57 +00:00
|
|
|
|
|
|
|
{
|
|
|
|
let mut armor_writer = Writer::new(&mut tmp, Kind::PublicKey,
|
|
|
|
&[][..])?;
|
|
|
|
|
|
|
|
armor_writer.write_all(new)?;
|
|
|
|
}
|
2018-08-16 18:35:19 +00:00
|
|
|
|
2019-02-21 21:05:01 +00:00
|
|
|
let _ = tmp.persist(ensure_parent(&target)?)?;
|
2019-01-04 13:20:48 +00:00
|
|
|
|
|
|
|
// fix permissions to 640
|
|
|
|
if cfg!(unix) {
|
|
|
|
use std::fs::{set_permissions, Permissions};
|
2019-02-07 19:58:31 +00:00
|
|
|
use std::os::unix::fs::PermissionsExt;
|
2019-01-04 13:20:48 +00:00
|
|
|
|
|
|
|
let perm = Permissions::from_mode(0o640);
|
|
|
|
set_permissions(target, perm)?;
|
|
|
|
}
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
None => {
|
|
|
|
remove_file(target)?;
|
|
|
|
}
|
|
|
|
}
|
2019-02-22 11:59:25 +00:00
|
|
|
|
|
|
|
Ok(())
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:13:20 +00:00
|
|
|
fn link_email(&self, email: &Email, fpr: &Fingerprint) -> Result<()> {
|
2019-02-07 19:58:31 +00:00
|
|
|
let email =
|
|
|
|
url::form_urlencoded::byte_serialize(email.to_string().as_bytes())
|
|
|
|
.collect::<String>();
|
2019-02-21 21:05:01 +00:00
|
|
|
let link = self.path_to_email(&email);
|
2019-02-22 14:45:34 +00:00
|
|
|
let target = diff_paths(&self.path_to_fingerprint(fpr),
|
|
|
|
link.parent().unwrap()).unwrap();
|
2018-08-16 18:35:19 +00:00
|
|
|
|
2019-02-22 19:26:10 +00:00
|
|
|
if link == target {
|
|
|
|
return Ok(());
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:26:10 +00:00
|
|
|
symlink(&target, ensure_parent(&link)?)
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:13:20 +00:00
|
|
|
fn unlink_email(&self, email: &Email, fpr: &Fingerprint) -> Result<()> {
|
2019-02-07 19:58:31 +00:00
|
|
|
let email =
|
|
|
|
url::form_urlencoded::byte_serialize(email.to_string().as_bytes())
|
|
|
|
.collect::<String>();
|
2019-02-21 21:05:01 +00:00
|
|
|
let link = self.path_to_email(&email);
|
2018-08-16 18:35:19 +00:00
|
|
|
|
|
|
|
match read_link(link.clone()) {
|
|
|
|
Ok(target) => {
|
2019-02-22 14:45:34 +00:00
|
|
|
let expected = diff_paths(&self.path_to_fingerprint(fpr),
|
|
|
|
link.parent().unwrap()).unwrap();
|
2018-08-16 18:35:19 +00:00
|
|
|
|
|
|
|
if target == expected {
|
2019-02-22 00:13:20 +00:00
|
|
|
remove_file(link)?;
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {}
|
|
|
|
}
|
2019-02-22 19:26:10 +00:00
|
|
|
|
2019-02-22 00:13:20 +00:00
|
|
|
Ok(())
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:13:20 +00:00
|
|
|
fn link_kid(&self, kid: &KeyID, fpr: &Fingerprint) -> Result<()> {
|
2019-02-21 21:05:01 +00:00
|
|
|
let link = self.path_to_keyid(kid);
|
2019-02-22 14:45:34 +00:00
|
|
|
let target = diff_paths(&self.path_to_fingerprint(fpr),
|
|
|
|
link.parent().unwrap()).unwrap();
|
2019-01-04 13:07:14 +00:00
|
|
|
|
2019-02-22 19:26:10 +00:00
|
|
|
if link == target {
|
|
|
|
return Ok(());
|
2019-01-04 13:07:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 19:39:51 +00:00
|
|
|
if link.exists() {
|
|
|
|
match link.symlink_metadata() {
|
|
|
|
Ok(ref meta) if meta.file_type().is_file() => {
|
|
|
|
// If a key is a subkey and a primary key, prefer
|
|
|
|
// the primary.
|
|
|
|
return Ok(());
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-22 19:26:10 +00:00
|
|
|
symlink(&target, ensure_parent(&link)?)
|
2019-01-04 13:07:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:13:20 +00:00
|
|
|
fn unlink_kid(&self, kid: &KeyID, fpr: &Fingerprint) -> Result<()> {
|
2019-02-21 21:05:01 +00:00
|
|
|
let link = self.path_to_keyid(kid);
|
2019-01-04 13:07:14 +00:00
|
|
|
|
|
|
|
match read_link(link.clone()) {
|
|
|
|
Ok(target) => {
|
2019-02-21 21:05:01 +00:00
|
|
|
let expected = self.path_to_fingerprint(fpr);
|
2019-01-04 13:07:14 +00:00
|
|
|
|
|
|
|
if target == expected {
|
2019-02-22 00:13:20 +00:00
|
|
|
remove_file(link)?;
|
2019-01-04 13:07:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {}
|
|
|
|
}
|
2019-02-22 19:26:10 +00:00
|
|
|
|
2019-02-22 00:13:20 +00:00
|
|
|
Ok(())
|
2019-01-04 13:07:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:13:20 +00:00
|
|
|
fn link_fpr(&self, from: &Fingerprint, fpr: &Fingerprint) -> Result<()> {
|
2019-02-22 14:45:34 +00:00
|
|
|
if from == fpr {
|
2019-02-22 00:13:20 +00:00
|
|
|
return Ok(());
|
2019-02-07 19:58:31 +00:00
|
|
|
}
|
2019-02-22 14:45:34 +00:00
|
|
|
|
|
|
|
let link = self.path_to_fingerprint(from);
|
|
|
|
let target = diff_paths(&self.path_to_fingerprint(fpr),
|
|
|
|
link.parent().unwrap()).unwrap();
|
|
|
|
|
2019-02-22 19:26:10 +00:00
|
|
|
symlink(&target, ensure_parent(&link)?)
|
2019-01-04 13:07:14 +00:00
|
|
|
}
|
|
|
|
|
2019-02-22 00:13:20 +00:00
|
|
|
fn unlink_fpr(&self, from: &Fingerprint, fpr: &Fingerprint) -> Result<()> {
|
2019-02-21 21:05:01 +00:00
|
|
|
let link = self.path_to_fingerprint(from);
|
2019-01-04 13:07:14 +00:00
|
|
|
|
|
|
|
match read_link(link.clone()) {
|
|
|
|
Ok(target) => {
|
2019-02-21 21:05:01 +00:00
|
|
|
let expected = self.path_to_fingerprint(fpr);
|
2019-01-04 13:07:14 +00:00
|
|
|
|
|
|
|
if target == expected {
|
2019-02-22 00:13:20 +00:00
|
|
|
remove_file(link)?;
|
2019-01-04 13:07:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(_) => {}
|
|
|
|
}
|
2019-02-22 00:13:20 +00:00
|
|
|
Ok(())
|
2019-01-04 13:07:14 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 20:22:59 +00:00
|
|
|
fn pop_verify_token(&self, token: &str) -> Option<Verify> {
|
2019-02-07 19:58:31 +00:00
|
|
|
self.pop_token("verification_tokens", token)
|
|
|
|
.ok()
|
|
|
|
.and_then(|raw| str::from_utf8(&raw).ok().map(|s| s.to_string()))
|
|
|
|
.and_then(|s| {
|
|
|
|
let s = serde_json::from_str(&s);
|
|
|
|
s.ok()
|
|
|
|
})
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
2018-09-19 20:22:59 +00:00
|
|
|
fn pop_delete_token(&self, token: &str) -> Option<Delete> {
|
2019-02-07 19:58:31 +00:00
|
|
|
self.pop_token("deletion_tokens", token)
|
|
|
|
.ok()
|
|
|
|
.and_then(|raw| str::from_utf8(&raw).ok().map(|s| s.to_string()))
|
|
|
|
.and_then(|s| serde_json::from_str(&s).ok())
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: slow
|
|
|
|
fn by_fpr(&self, fpr: &Fingerprint) -> Option<Box<[u8]>> {
|
2019-02-21 21:05:01 +00:00
|
|
|
let target = self.path_to_fingerprint(fpr);
|
2018-08-16 18:35:19 +00:00
|
|
|
|
|
|
|
File::open(target).ok().and_then(|mut fd| {
|
|
|
|
let mut buf = Vec::default();
|
2019-02-22 13:42:29 +00:00
|
|
|
if fd.read_to_end(&mut buf).is_ok() {
|
2018-08-16 18:35:19 +00:00
|
|
|
Some(buf.into_boxed_slice())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: slow
|
2018-10-24 17:45:11 +00:00
|
|
|
fn by_email(&self, email: &Email) -> Option<Box<[u8]>> {
|
2018-09-19 20:23:39 +00:00
|
|
|
use std::fs;
|
2018-08-16 18:35:19 +00:00
|
|
|
|
2019-02-07 19:58:31 +00:00
|
|
|
let email =
|
|
|
|
url::form_urlencoded::byte_serialize(email.to_string().as_bytes())
|
|
|
|
.collect::<String>();
|
2019-02-21 21:05:01 +00:00
|
|
|
let path = self.path_to_email(&email);
|
2018-09-19 20:23:39 +00:00
|
|
|
|
2019-02-07 19:58:31 +00:00
|
|
|
fs::canonicalize(path)
|
|
|
|
.ok()
|
|
|
|
.and_then(
|
|
|
|
|p| {
|
|
|
|
if p.starts_with(&self.base) {
|
|
|
|
Some(p)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.and_then(|p| File::open(p).ok())
|
|
|
|
.and_then(|mut fd| {
|
2018-09-19 20:23:39 +00:00
|
|
|
let mut buf = Vec::default();
|
2019-02-22 13:42:29 +00:00
|
|
|
if fd.read_to_end(&mut buf).is_ok() {
|
2018-09-19 20:23:39 +00:00
|
|
|
Some(buf.into_boxed_slice())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
2019-01-04 13:07:14 +00:00
|
|
|
|
|
|
|
// XXX: slow
|
|
|
|
fn by_kid(&self, kid: &KeyID) -> Option<Box<[u8]>> {
|
|
|
|
use std::fs;
|
|
|
|
|
2019-02-21 21:05:01 +00:00
|
|
|
let path = self.path_to_keyid(kid);
|
2019-02-07 19:58:31 +00:00
|
|
|
|
|
|
|
fs::canonicalize(path)
|
|
|
|
.ok()
|
|
|
|
.and_then(
|
|
|
|
|p| {
|
|
|
|
if p.starts_with(&self.base) {
|
|
|
|
Some(p)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
},
|
|
|
|
)
|
|
|
|
.and_then(|p| File::open(p).ok())
|
|
|
|
.and_then(|mut fd| {
|
2019-01-04 13:07:14 +00:00
|
|
|
let mut buf = Vec::default();
|
2019-02-22 13:42:29 +00:00
|
|
|
if fd.read_to_end(&mut buf).is_ok() {
|
2019-01-04 13:07:14 +00:00
|
|
|
Some(buf.into_boxed_slice())
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
use database::test;
|
2019-02-07 19:58:31 +00:00
|
|
|
use sequoia_openpgp::tpk::TPKBuilder;
|
|
|
|
use tempfile::TempDir;
|
2018-08-16 18:35:19 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn init() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let _ = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
2018-10-25 15:42:02 +00:00
|
|
|
let db = Filesystem::new(tmpdir.path()).unwrap();
|
2018-11-22 15:40:59 +00:00
|
|
|
let k1 = TPKBuilder::default().add_userid("a").generate().unwrap().0;
|
|
|
|
let k2 = TPKBuilder::default().add_userid("b").generate().unwrap().0;
|
|
|
|
let k3 = TPKBuilder::default().add_userid("c").generate().unwrap().0;
|
2018-08-16 18:35:19 +00:00
|
|
|
|
|
|
|
assert!(db.merge_or_publish(k1).unwrap().len() > 0);
|
|
|
|
assert!(db.merge_or_publish(k2.clone()).unwrap().len() > 0);
|
|
|
|
assert!(!db.merge_or_publish(k2).unwrap().len() > 0);
|
|
|
|
assert!(db.merge_or_publish(k3.clone()).unwrap().len() > 0);
|
|
|
|
assert!(!db.merge_or_publish(k3.clone()).unwrap().len() > 0);
|
|
|
|
assert!(!db.merge_or_publish(k3).unwrap().len() > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uid_verification() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let mut db = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
|
|
|
|
test::test_uid_verification(&mut db);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uid_deletion() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let mut db = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
|
|
|
|
test::test_uid_deletion(&mut db);
|
|
|
|
}
|
2019-01-04 13:07:14 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn subkey_lookup() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let mut db = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
|
|
|
|
test::test_subkey_lookup(&mut db);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn kid_lookup() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let mut db = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
|
|
|
|
test::test_kid_lookup(&mut db);
|
|
|
|
}
|
2019-01-15 15:59:35 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uid_revocation() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let mut db = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
|
|
|
|
test::test_uid_revocation(&mut db);
|
|
|
|
}
|
2019-02-07 20:01:59 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn key_reupload() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let mut db = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
|
|
|
|
test::test_reupload(&mut db);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uid_replacement() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let mut db = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
|
|
|
|
test::test_uid_replacement(&mut db);
|
|
|
|
}
|
2019-02-08 14:46:05 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn uid_stealing() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let mut db = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
|
|
|
|
test::test_steal_uid(&mut db);
|
|
|
|
}
|
2019-02-22 19:23:24 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn same_email_1() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let mut db = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
|
|
|
|
test::test_same_email_1(&mut db);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn same_email_2() {
|
|
|
|
let tmpdir = TempDir::new().unwrap();
|
|
|
|
let mut db = Filesystem::new(tmpdir.path()).unwrap();
|
|
|
|
|
|
|
|
test::test_same_email_2(&mut db);
|
|
|
|
}
|
2018-08-16 18:35:19 +00:00
|
|
|
}
|