diff --git a/rust/src/nasl/interpreter/loop_extension.rs b/rust/src/nasl/interpreter/loop_extension.rs index 93245f962..000072c11 100644 --- a/rust/src/nasl/interpreter/loop_extension.rs +++ b/rust/src/nasl/interpreter/loop_extension.rs @@ -150,150 +150,139 @@ impl<'a> Interpreter<'a> { #[cfg(test)] mod tests { - // use crate::*; - // - // #[test] - // fn for_loop_test() { - // let code = r###" - // a = 0; - // for ( i = 1; i < 5; i++) { - // a += i; - // } - // a; - // "###; - // let binding = ContextFactory::default(); - // let context = binding.build(Default::default()); - // let register = Register::default(); - // let mut interpreter = Interpreter::new(register, &context); - // let mut interpreter = - // parse(code).map(|x| interpreter.resolve(&x.expect("unexpected parse error"))); - // assert_eq!(interpreter.next(), Some(Ok(0.into()))); - // assert_eq!(interpreter.next(), Some(Ok(NaslValue::Null))); - // assert_eq!(interpreter.next(), Some(Ok(10.into()))); - // } + use crate::nasl::test_prelude::*; - // #[test] - // fn for_loop_without_update() { - // let code = r###" - // a = 0; - // for (; a < 5; ) { - // a += 1; - // } - // a; - // "###; - // let register = Register::default(); - // let binding = ContextFactory::default(); - // let context = binding.build(Default::default()); - // let mut interpreter = Interpreter::new(register, &context); - // let mut interpreter = - // parse(code).map(|x| interpreter.resolve(&x.expect("unexpected parse error"))); - // assert_eq!(interpreter.next(), Some(Ok(0.into()))); - // assert_eq!(interpreter.next(), Some(Ok(NaslValue::Null))); - // assert_eq!(interpreter.next(), Some(Ok(5.into()))); - // } + #[test] + fn for_loop_test() { + let code = r###" + a = 0; + for ( i = 1; i < 5; i++) { + a += i; + } + a; + "###; + let mut t = TestBuilder::default(); + t.run_all(code); + let mut results = t.results(); + assert_eq!(results.remove(0).unwrap(), 0.into()); + assert_eq!(results.remove(0).unwrap(), NaslValue::Null); + assert_eq!(results.remove(0).unwrap(), 10.into()); + } + + #[test] + fn for_loop_without_update() { + let code = r###" + a = 0; + for (; a < 5; ) { + a += 1; + } + a; + "###; + let mut t = TestBuilder::default(); + t.run_all(code); + let mut results = t.results(); + assert_eq!(results.remove(0).unwrap(), 0.into()); + assert_eq!(results.remove(0).unwrap(), NaslValue::Null); + assert_eq!(results.remove(0).unwrap(), 5.into()); + } + + #[test] + fn for_each_loop_test() { + let code = r###" + arr[0] = 3; + arr[1] = 5; + a = 0; + foreach i (arr) { + a += i; + } + a; + "###; + let mut t = TestBuilder::default(); + t.run_all(code); + let mut results = t.results(); + + assert_eq!(results.remove(0).unwrap(), 3.into()); + assert_eq!(results.remove(0).unwrap(), 5.into()); + assert_eq!(results.remove(0).unwrap(), 0.into()); + assert_eq!(results.remove(0).unwrap(), NaslValue::Null); + assert_eq!(results.remove(0).unwrap(), 8.into()); + } + + #[test] + fn while_loop_test() { + let code = r###" + i = 4; + a = 0; + i > 0; + while(i > 0) { + a += i; + i--; + } + a; + i; + "###; - // #[test] - // fn for_each_loop_test() { - // let code = r###" - // arr[0] = 3; - // arr[1] = 5; - // a = 0; - // foreach i (arr) { - // a += i; - // } - // a; - // "###; - // let register = Register::default(); - // let binding = ContextFactory::default(); - // let context = binding.build(Default::default()); - // let mut interpreter = Interpreter::new(register, &context); - // let mut interpreter = - // parse(code).map(|x| interpreter.resolve(&x.expect("unexpected parse error"))); - // assert_eq!(interpreter.next(), Some(Ok(3.into()))); - // assert_eq!(interpreter.next(), Some(Ok(5.into()))); - // assert_eq!(interpreter.next(), Some(Ok(0.into()))); - // assert_eq!(interpreter.next(), Some(Ok(NaslValue::Null))); - // assert_eq!(interpreter.next(), Some(Ok(8.into()))); - // } + let mut t = TestBuilder::default(); + t.run_all(code); + let mut results = t.results(); - // #[test] - // fn while_loop_test() { - // let code = r###" - // i = 4; - // a = 0; - // i > 0; - // while(i > 0) { - // a += i; - // i--; - // } - // a; - // i; - // "###; - // let register = Register::default(); - // let binding = ContextFactory::default(); - // let context = binding.build(Default::default()); - // let mut interpreter = Interpreter::new(register, &context); - // let mut interpreter = - // parse(code).map(|x| interpreter.resolve(&x.expect("unexpected parse error"))); - // assert_eq!(interpreter.next(), Some(Ok(4.into()))); - // assert_eq!(interpreter.next(), Some(Ok(0.into()))); - // assert_eq!(interpreter.next(), Some(Ok(NaslValue::Boolean(true)))); - // } + assert_eq!(results.remove(0).unwrap(), 4.into()); + assert_eq!(results.remove(0).unwrap(), 0.into()); + assert_eq!(results.remove(0).unwrap(), NaslValue::Boolean(true)); + } + + #[test] + fn repeat_loop_test() { + let code = r###" + i = 10; + a = 0; + repeat { + a += i; + i--; + } until (i > 0); + a; + i; + "###; + + let mut t = TestBuilder::default(); + t.run_all(code); + let mut results = t.results(); - // #[test] - // fn repeat_loop_test() { - // let code = r###" - // i = 10; - // a = 0; - // repeat { - // a += i; - // i--; - // } until (i > 0); - // a; - // i; - // "###; - // let register = Register::default(); - // let binding = ContextFactory::default(); - // let context = binding.build(Default::default()); - // let mut interpreter = Interpreter::new(register, &context); - // let mut interpreter = - // parse(code).map(|x| interpreter.resolve(&x.expect("unexpected parse error"))); - // assert_eq!(interpreter.next(), Some(Ok(10.into()))); - // assert_eq!(interpreter.next(), Some(Ok(0.into()))); - // assert_eq!(interpreter.next(), Some(Ok(NaslValue::Null))); - // assert_eq!(interpreter.next(), Some(Ok(10.into()))); - // assert_eq!(interpreter.next(), Some(Ok(9.into()))); - // } + assert_eq!(results.remove(0).unwrap(), 10.into()); + assert_eq!(results.remove(0).unwrap(), 0.into()); + assert_eq!(results.remove(0).unwrap(), NaslValue::Null); + assert_eq!(results.remove(0).unwrap(), 10.into()); + assert_eq!(results.remove(0).unwrap(), 9.into()); + } - // #[test] - // fn control_flow() { - // let code = r###" - // a = 0; - // i = 5; - // while(i > 0) { - // if(i == 4) { - // i--; - // continue; - // } - // if (i == 1) { - // break; - // } - // a += i; - // i--; - // } - // a; - // i; - // "###; - // let register = Register::default(); - // let binding = ContextFactory::default(); - // let context = binding.build(Default::default()); - // let mut interpreter = Interpreter::new(register, &context); - // let mut interpreter = - // parse(code).map(|x| interpreter.resolve(&x.expect("unexpected parse error"))); - // assert_eq!(interpreter.next(), Some(Ok(0.into()))); - // assert_eq!(interpreter.next(), Some(Ok(5.into()))); - // assert_eq!(interpreter.next(), Some(Ok(NaslValue::Null))); - // assert_eq!(interpreter.next(), Some(Ok(10.into()))); - // assert_eq!(interpreter.next(), Some(Ok(1.into()))); - // } + #[test] + fn control_flow() { + let code = r###" + a = 0; + i = 5; + while(i > 0) { + if(i == 4) { + i--; + continue; + } + if (i == 1) { + break; + } + a += i; + i--; + } + a; + i; + "###; + + let mut t = TestBuilder::default(); + t.run_all(code); + let mut results = t.results(); + + assert_eq!(results.remove(0).unwrap(), 0.into()); + assert_eq!(results.remove(0).unwrap(), 5.into()); + assert_eq!(results.remove(0).unwrap(), NaslValue::Null); + assert_eq!(results.remove(0).unwrap(), 10.into()); + assert_eq!(results.remove(0).unwrap(), 1.into()); + } } diff --git a/rust/src/nasl/interpreter/operator.rs b/rust/src/nasl/interpreter/operator.rs index e175a2632..a6619da37 100644 --- a/rust/src/nasl/interpreter/operator.rs +++ b/rust/src/nasl/interpreter/operator.rs @@ -286,69 +286,68 @@ impl<'a> Interpreter<'a> { #[cfg(test)] mod tests { + use crate::nasl::test_prelude::*; + macro_rules! create_test { + ($($name:tt: $code:expr => $result:expr),*) => { - // use crate::*; + $( + #[test] + fn $name() { + let mut t = TestBuilder::default(); + t.ok($code, $result); + } + )* + }; + } - // macro_rules! create_test { - // ($($name:tt: $code:expr => $result:expr),*) => { + create_test! { + numeric_plus: "1+2;" => 3, + cast_to_string_middle_plus: "1+\"\"+2;" => "12", + cast_to_string_end_plus: "1+2+\"\";" => "3", + cast_to_string_end_plus_4: "1+2+\"\" + 4;" => "34", + cast_to_string_minus: "11-\"1\";" => "1", + string_plus: "\"hello \" + \"world!\";" => "hello world!", + string_minus : "\"hello \" - 'o ';" => "hell", + data_plus: "'hello ' + 'world!';" => "hello world!".as_bytes(), + data_minus: "'hello ' - 'o ';" => "hell".as_bytes(), - // $( - // #[test] - // fn $name() { - // let register = Register::default(); - // let binding = ContextFactory ::default(); - // let context = binding.build(Default::default()); - // let mut interpreter = Interpreter::new(register, &context); - // let parser = parse($code).map(|x| - // interpreter.resolve(&x.expect("unexpected parse error")) - // ); - // assert_eq!(parser.last(), Some(Ok($result))); - // } - // )* - // }; - // } - // create_test! { - // numeric_plus: "1+2;" => 3.into(), - // cast_to_string_middle_plus: "1+\"\"+2;" => "12".into(), - // cast_to_string_end_plus: "1+2+\"\";" => "3".into(), - // cast_to_string_end_plus_4: "1+2+\"\" + 4;" => "34".into(), - // cast_to_string_minus: "11-\"1\";" => "1".into(), - // string_plus: "\"hello \" + \"world!\";" => "hello world!".into(), - // string_minus : "\"hello \" - 'o ';" => "hell".into(), - // data_plus: "'hello ' + 'world!';" => "hello world!".as_bytes().into(), - // data_minus: "'hello ' - 'o ';" => "hell".as_bytes().into(), + cast_to_data_middle_plus: "1+''+2;" => "12".as_bytes(), + cast_to_data_end_plus: "1+2+'';" => "3".as_bytes(), + cast_to_data_end_plus_4: "1+2+'' + 4;" => "34".as_bytes(), + cast_to_data_minus: "11-'1';" => "1".as_bytes(), + numeric_minus : "1 - 2;" => -1, + multiplication: "1*2;" => 2, + division: "512/2;" => 256, + modulo: "512%2;" => 0, + left_shift: "512 << 2;" => 2048, + right_shift: "512 >> 2;" => 128, + unsigned_right_shift: "-2 >>> 2;" => 1073741823, + and: "-2 & 2;" => 2, + or: "-2 | 2;" => -2, + xor: "-2 ^ 2;" => -4, + pow: "2 ** 2;" => 4, + not: "~2;" => -3, + r_match: "'hello' =~ 'hell';" => true, + r_not_match: "'hello' !~ 'hell';" => false, + contains: "'hello' >< 'hell';" => true, + not_contains: "'hello' >!< 'hell';" => false, + bool_not: "!23;" => false, + bool_not_reverse: "!0;" => true, + bool_and: "1 && 1;" => true, + bool_or: "1 || 0;" => true, + equals_string: "'1' == '1';" => true, + equals_number: "1 == 1;" => true, + unequal: "1 != 1;" => false, + greater: "1 > 0;" => true, + less: "1 < 2;" => true, + greater_equal: "1 >= 1;" => true, + less_equal: "1 <= 1;" => true + } - // cast_to_data_middle_plus: "1+''+2;" => "12".as_bytes().into(), - // cast_to_data_end_plus: "1+2+'';" => "3".as_bytes().into(), - // cast_to_data_end_plus_4: "1+2+'' + 4;" => "34".as_bytes().into(), - // cast_to_data_minus: "11-'1';" => "1".as_bytes().into(), - // numeric_minus : "1 - 2;" => NaslValue::Number(-1), - // multiplication: "1*2;" => 2.into(), - // division: "512/2;" => 256.into(), - // modulo: "512%2;" => 0.into(), - // left_shift: "512 << 2;" => 2048.into(), - // right_shift: "512 >> 2;" => 128.into(), - // unsigned_right_shift: "-2 >>> 2;" => 1073741823.into(), - // and: "-2 & 2;" => 2.into(), - // or: "-2 | 2;" => NaslValue::Number(-2), - // xor: "-2 ^ 2;" => NaslValue::Number(-4), - // pow: "2 ** 2;" => 4.into(), - // not: "~2;" => NaslValue::Number(-3), - // r_match: "'hello' =~ 'hell';" => NaslValue::Boolean(true), - // r_not_match: "'hello' !~ 'hell';" => NaslValue::Boolean(false), - // contains: "'hello' >< 'hell';" => NaslValue::Boolean(true), - // not_contains: "'hello' >!< 'hell';" => NaslValue::Boolean(false), - // bool_not: "!23;" => NaslValue::Boolean(false), - // bool_not_reverse: "!0;" => NaslValue::Boolean(true), - // bool_and: "1 && 1;" => NaslValue::Boolean(true), - // bool_or: "1 || 0;" => NaslValue::Boolean(true), - // equals_string: "'1' == '1';" => NaslValue::Boolean(true), - // equals_number: "1 == 1;" => NaslValue::Boolean(true), - // unequal: "1 != 1;" => NaslValue::Boolean(false), - // greater: "1 > 0;" => NaslValue::Boolean(true), - // less: "1 < 2;" => NaslValue::Boolean(true), - // greater_equal: "1 >= 1;" => NaslValue::Boolean(true), - // less_equal: "1 <= 1;" => NaslValue::Boolean(true), - // x_gonna_give_it_ya: "function test() { }; test('hi') x 200;" => NaslValue::Null - // } + #[test] + fn x_gonna_give_it_ya() { + let mut t = TestBuilder::default(); + t.run_all("function test() { }; test('hi') x 200;"); + assert_eq!(t.results().pop().unwrap().unwrap(), NaslValue::Null); + } }