Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add last_err for error handling #20

Merged
merged 3 commits into from
Mar 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Runtime: add hex()
  • Loading branch information
kpcyrd committed Mar 20, 2018
commit 4cd6da64dfe6d889f811c52e485e1e0f711be820
31 changes: 31 additions & 0 deletions src/ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl Script {
let state = State::new();

runtime::execve(&mut lua, state.clone());
runtime::hex(&mut lua, state.clone());
runtime::http_basic_auth(&mut lua, state.clone());
runtime::ldap_bind(&mut lua, state.clone());
runtime::ldap_escape(&mut lua, state.clone());
Expand Down Expand Up @@ -194,4 +195,34 @@ mod tests {
let result = script.run_once("invalid", "wrong").unwrap();
assert!(!result);
}

#[test]
fn verify_hex() {
let script = Script::load_from(r#"
descr = "hex test"

function verify(user, password)
x = hex({0x6F, 0x68, 0x61, 0x69, 0x0A, 0x00})
return x == "6f6861690a00"
end
"#.as_bytes()).unwrap();

let result = script.run_once("x", "x").unwrap();
assert!(result);
}

#[test]
fn verify_hex_empty() {
let script = Script::load_from(r#"
descr = "hex test"

function verify(user, password)
x = hex({})
return x == ""
end
"#.as_bytes()).unwrap();

let result = script.run_once("x", "x").unwrap();
assert!(result);
}
}
20 changes: 20 additions & 0 deletions src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,26 @@ pub fn execve(lua: &mut hlua::Lua, state: State) {
}))
}

pub fn hex(lua: &mut hlua::Lua, _state: State) {
lua.set("hex", hlua::function1(move |bytes: Vec<AnyLuaValue>| -> Result<String> {
let mut out = String::new();

for num in bytes {
match num {
AnyLuaValue::LuaNumber(num) => {
if num > 255.0 || num < 0.0 {
return Err(format!("number is out of range: {:?}", num).into());
}
out += &format!("{:02x}", num as u8);
},
_ => return Err(format!("unexpected type: {:?}", num).into()),
}
}

Ok(out)
}))
}

pub fn http_basic_auth(lua: &mut hlua::Lua, state: State) {
lua.set("http_basic_auth", hlua::function3(move |url: String, user: String, password: String| -> Result<bool> {
let client = reqwest::Client::new();
Expand Down