82 lines
4.3 KiB
Rust
82 lines
4.3 KiB
Rust
//! Per-kind content checks of staged add-on files.
|
|
|
|
mod addons_support;
|
|
|
|
use addons_support::{THEME, TINY, WASM, entry, index, manifest};
|
|
use notescraft_engine::addons::{Entry, Kind, check_content};
|
|
|
|
type Staged = Vec<(String, Vec<u8>)>;
|
|
|
|
fn parsed(kind: &str, id: &str, files: &[(&str, &[u8])], permissions: &[&str]) -> Entry {
|
|
index(&[entry(kind, id, "1.0.0", files, permissions)], &[]).entries.remove(0)
|
|
}
|
|
|
|
fn run(kind: Kind, id: &str, files: &[(&str, &[u8])], permissions: &[&str]) -> Result<(), String> {
|
|
let staged: Staged = files.iter().map(|(p, b)| ((*p).to_owned(), b.to_vec())).collect();
|
|
check_content(kind, &parsed(kind.as_str(), id, files, permissions), &staged).map_err(|e| e.to_string())
|
|
}
|
|
|
|
#[test]
|
|
fn grammar() {
|
|
assert_eq!(run(Kind::Grammar, "tiny", &[("tiny.sublime-syntax", TINY.as_bytes())], &[]), Ok(()));
|
|
assert!(run(Kind::Grammar, "tiny", &[("tiny.sublime-syntax", b"<plist></plist>")], &[]).is_err());
|
|
assert!(run(Kind::Grammar, "tiny", &[("tiny.sublime-syntax", &[0xff, 0xfe])], &[]).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn theme() {
|
|
assert_eq!(run(Kind::Theme, "mine", &[("mine.toml", THEME.as_bytes())], &[]), Ok(()));
|
|
let wrong_id = run(Kind::Theme, "other", &[("other.toml", THEME.as_bytes())], &[]);
|
|
assert!(wrong_id.unwrap_err().contains("entry"));
|
|
let builtin = notescraft_themes::builtin_ids()[0];
|
|
let reuse = format!("id = \"{builtin}\"\nname = \"X\"\nvariant = \"dark\"\n");
|
|
assert!(run(Kind::Theme, builtin, &[("t.toml", reuse.as_bytes())], &[]).unwrap_err().contains("built-in"));
|
|
let bad_value = "id = \"mine\"\nname = \"Mine\"\nvariant = \"dark\"\n[colors]\nbackground = \"nope\"\n";
|
|
assert!(run(Kind::Theme, "mine", &[("mine.toml", bad_value.as_bytes())], &[]).unwrap_err().contains("rejected"));
|
|
}
|
|
|
|
#[test]
|
|
fn plugin() {
|
|
let good = manifest("dice", "1.0.0", &["vault.read"]);
|
|
let files: &[(&str, &[u8])] = &[("manifest.json", good.as_bytes()), ("plugin.wasm", WASM)];
|
|
assert_eq!(run(Kind::Plugin, "dice", files, &["vault.read"]), Ok(()));
|
|
let greedy = manifest("dice", "1.0.0", &["vault.read", "vault.write"]);
|
|
let files: &[(&str, &[u8])] = &[("manifest.json", greedy.as_bytes()), ("plugin.wasm", WASM)];
|
|
assert!(run(Kind::Plugin, "dice", files, &["vault.read"]).unwrap_err().contains("permissions"));
|
|
let other = manifest("other", "1.0.0", &[]);
|
|
assert!(run(Kind::Plugin, "dice", &[("manifest.json", other.as_bytes()), ("plugin.wasm", WASM)], &[]).is_err());
|
|
let old = manifest("dice", "0.9.0", &[]);
|
|
assert!(run(Kind::Plugin, "dice", &[("manifest.json", old.as_bytes()), ("plugin.wasm", WASM)], &[]).is_err());
|
|
let plain = manifest("dice", "1.0.0", &[]);
|
|
assert!(run(Kind::Plugin, "dice", &[("manifest.json", plain.as_bytes()), ("plugin.wasm", b"MZ not wasm")], &[]).is_err());
|
|
assert!(run(Kind::Plugin, "dice", &[("plugin.wasm", WASM)], &[]).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn font_and_snippet() {
|
|
assert_eq!(run(Kind::Font, "f", &[("f.ttf", &[0, 1, 0, 0, 9])], &[]), Ok(()));
|
|
assert_eq!(run(Kind::Font, "f", &[("f.otf", b"OTTO..")], &[]), Ok(()));
|
|
assert!(run(Kind::Font, "f", &[("f.ttf", b"GIF89a")], &[]).is_err());
|
|
assert_eq!(run(Kind::Snippet, "s", &[("s.md", b"# hi")], &[]), Ok(()));
|
|
assert!(run(Kind::Snippet, "s", &[("s.md", b"a\0b")], &[]).is_err());
|
|
assert!(run(Kind::Snippet, "s", &[("s.md", &[0xff])], &[]).is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn pack_checks_each_file_by_folder() {
|
|
let ok: &[(&str, &[u8])] = &[("grammars/tiny.sublime-syntax", TINY.as_bytes()), ("themes/mine.toml", THEME.as_bytes()), ("icons/a.json", b"{}")];
|
|
assert_eq!(run(Kind::Pack, "p", ok, &[]), Ok(()));
|
|
let bad: &[(&str, &[u8])] = &[("grammars/tiny.sublime-syntax", TINY.as_bytes()), ("icons/a.json", b"{nope")];
|
|
assert!(run(Kind::Pack, "p", bad, &[]).is_err(), "one bad file fails the pack");
|
|
}
|
|
|
|
#[test]
|
|
fn the_staged_files_must_be_the_listed_files() {
|
|
let listed: &[(&str, &[u8])] = &[("tiny.sublime-syntax", TINY.as_bytes())];
|
|
let entry = parsed("grammar", "tiny", listed, &[]);
|
|
let swapped: Staged = vec![("evil.sublime-syntax".into(), TINY.as_bytes().to_vec())];
|
|
assert!(check_content(Kind::Grammar, &entry, &swapped).is_err());
|
|
let doubled: Staged = vec![("tiny.sublime-syntax".into(), TINY.as_bytes().to_vec()), ("tiny.sublime-syntax".into(), TINY.as_bytes().to_vec())];
|
|
assert!(check_content(Kind::Grammar, &entry, &doubled).is_err());
|
|
}
|