nasl 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. data/.gitignore +9 -0
  2. data/Gemfile +4 -0
  3. data/Rakefile +8 -0
  4. data/bin/nasl-parse +33 -0
  5. data/lib/nasl/cli.rb +94 -0
  6. data/lib/nasl/command.rb +96 -0
  7. data/lib/nasl/commands/benchmark.rb +55 -0
  8. data/lib/nasl/commands/parse.rb +55 -0
  9. data/lib/nasl/commands/test.rb +37 -0
  10. data/lib/nasl/commands/tokenize.rb +46 -0
  11. data/lib/nasl/commands/xml.rb +41 -0
  12. data/lib/nasl/context.rb +102 -0
  13. data/lib/nasl/grammar.racc +513 -0
  14. data/lib/nasl/grammar.tab.rb +1650 -0
  15. data/lib/nasl/parser/argument.rb +50 -0
  16. data/lib/nasl/parser/assigment.rb +45 -0
  17. data/lib/nasl/parser/block.rb +41 -0
  18. data/lib/nasl/parser/break.rb +32 -0
  19. data/lib/nasl/parser/call.rb +48 -0
  20. data/lib/nasl/parser/continue.rb +32 -0
  21. data/lib/nasl/parser/decrement.rb +48 -0
  22. data/lib/nasl/parser/empty.rb +32 -0
  23. data/lib/nasl/parser/export.rb +41 -0
  24. data/lib/nasl/parser/expression.rb +56 -0
  25. data/lib/nasl/parser/for.rb +47 -0
  26. data/lib/nasl/parser/foreach.rb +45 -0
  27. data/lib/nasl/parser/function.rb +45 -0
  28. data/lib/nasl/parser/global.rb +41 -0
  29. data/lib/nasl/parser/identifier.rb +43 -0
  30. data/lib/nasl/parser/if.rb +45 -0
  31. data/lib/nasl/parser/import.rb +41 -0
  32. data/lib/nasl/parser/include.rb +41 -0
  33. data/lib/nasl/parser/increment.rb +48 -0
  34. data/lib/nasl/parser/integer.rb +70 -0
  35. data/lib/nasl/parser/ip.rb +43 -0
  36. data/lib/nasl/parser/local.rb +41 -0
  37. data/lib/nasl/parser/lvalue.rb +43 -0
  38. data/lib/nasl/parser/node.rb +73 -0
  39. data/lib/nasl/parser/repeat.rb +43 -0
  40. data/lib/nasl/parser/repetition.rb +43 -0
  41. data/lib/nasl/parser/return.rb +41 -0
  42. data/lib/nasl/parser/string.rb +48 -0
  43. data/lib/nasl/parser/tree.rb +59 -0
  44. data/lib/nasl/parser/undefined.rb +35 -0
  45. data/lib/nasl/parser/while.rb +43 -0
  46. data/lib/nasl/parser.rb +45 -0
  47. data/lib/nasl/test.rb +98 -0
  48. data/lib/nasl/token.rb +69 -0
  49. data/lib/nasl/tokenizer.rb +327 -0
  50. data/lib/nasl/version.rb +3 -0
  51. data/lib/nasl.rb +52 -0
  52. data/nasl.gemspec +26 -0
  53. data/test/test_helper.rb +6 -0
  54. data/test/unit/parser/test_assignment.rb +111 -0
  55. data/test/unit/parser/test_blank.rb +44 -0
  56. data/test/unit/parser/test_block.rb +35 -0
  57. data/test/unit/parser/test_constant.rb +65 -0
  58. data/test/unit/parser/test_empty.rb +36 -0
  59. data/test/unit/parser/test_expressions.rb +57 -0
  60. data/test/unit/parser/test_function.rb +82 -0
  61. data/test/unit/parser/test_if.rb +85 -0
  62. data/test/unit/parser/test_include.rb +37 -0
  63. data/test/unit/parser/test_incr_decr.rb +51 -0
  64. data/test/unit/parser/test_ip.rb +33 -0
  65. data/test/unit/parser/test_return.rb +51 -0
  66. data/test/unit/parser/test_string.rb +46 -0
  67. data/test/unit/parser/test_whitespace.rb +56 -0
  68. data/test/unit/test_context.rb +240 -0
  69. data/test/unit/tokenizer/test_empty.rb +53 -0
  70. data/test/unit/tokenizer/test_integer.rb +68 -0
  71. data/test/unit/tokenizer/test_string.rb +94 -0
  72. metadata +161 -0
data/lib/nasl.rb ADDED
@@ -0,0 +1,52 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011, Mak Kolybabi
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ require 'nasl/version'
28
+ require 'pathname'
29
+
30
+ module Nasl
31
+ def self.root
32
+ @root ||= Pathname.new('').expand_path
33
+ end
34
+
35
+ def self.lib
36
+ root + 'lib'
37
+ end
38
+
39
+ def self.test
40
+ root + 'test'
41
+ end
42
+
43
+ autoload :Cli, 'nasl/cli'
44
+ autoload :Command, 'nasl/command'
45
+ autoload :Context, 'nasl/context'
46
+ autoload :Parser, 'nasl/parser'
47
+ autoload :Token, 'nasl/token'
48
+ autoload :Tokenizer, 'nasl/tokenizer'
49
+ autoload :Test, 'nasl/test'
50
+ end
51
+
52
+ $LOAD_PATH.unshift(Nasl.lib.to_s)
data/nasl.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path('../lib', __FILE__)
3
+ require 'nasl/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'nasl'
7
+ s.version = Nasl::VERSION
8
+ s.authors = ['Mak Kolybabi']
9
+ s.email = ['mak@kolybabi.com']
10
+ s.homepage = 'http://github.com/mogigoma/nasl'
11
+ s.summary = %q{A parser for the Nessus Attack Scripting Language.}
12
+
13
+ s.rubyforge_project = 'nasl'
14
+
15
+ s.files = `git ls-files`.split("\n") + ['lib/nasl/grammar.tab.rb']
16
+
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ['lib']
20
+
21
+ s.add_development_dependency 'rake'
22
+
23
+ s.add_runtime_dependency 'builder'
24
+ s.add_runtime_dependency 'racc'
25
+ s.add_runtime_dependency 'rainbow'
26
+ end
@@ -0,0 +1,6 @@
1
+ require 'test/unit'
2
+ require 'nasl'
3
+
4
+ class Test::Unit::TestCase
5
+ include Nasl::Test
6
+ end
@@ -0,0 +1,111 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011, Mak Kolybabi
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestAssignment < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_number
31
+ same(
32
+ "q = 0;",
33
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><integer value="0"/></assignment></tree>'
34
+ )
35
+ end
36
+
37
+ def test_string
38
+ same(
39
+ "q = '';",
40
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><data></data></assignment></tree>'
41
+ )
42
+ same(
43
+ "q = 'foo';",
44
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><data>foo</data></assignment></tree>'
45
+ )
46
+ same(
47
+ 'q = "";',
48
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><string></string></assignment></tree>'
49
+ )
50
+ same(
51
+ 'q = "foo";',
52
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><string>foo</string></assignment></tree>'
53
+ )
54
+ end
55
+
56
+ def test_function
57
+ same(
58
+ "q = foo();",
59
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><call><identifier name="foo"/></call></assignment></tree>'
60
+ )
61
+ end
62
+
63
+ def test_chain
64
+ same(
65
+ 'b = a = 0;',
66
+ '<tree><assignment><op>=</op><lvalue><identifier name="b"/></lvalue><assignment><op>=</op><lvalue><identifier name="a"/></lvalue><integer value="0"/></assignment></assignment></tree>'
67
+ )
68
+ same(
69
+ 'b = 1 + a = 0;',
70
+ '<tree><assignment><op>=</op><lvalue><identifier name="b"/></lvalue><expression><op>+</op><integer value="1"/><assignment><op>=</op><lvalue><identifier name="a"/></lvalue><integer value="0"/></assignment></expression></assignment></tree>'
71
+ )
72
+ same(
73
+ 'c = 1 + b = 1 + a = 0;',
74
+ '<tree><assignment><op>=</op><lvalue><identifier name="c"/></lvalue><expression><op>+</op><integer value="1"/><assignment><op>=</op><lvalue><identifier name="b"/></lvalue><expression><op>+</op><integer value="1"/><assignment><op>=</op><lvalue><identifier name="a"/></lvalue><integer value="0"/></assignment></expression></assignment></expression></assignment></tree>'
75
+ )
76
+ end
77
+
78
+ def test_expression
79
+ same(
80
+ "q = 0 + 0;",
81
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><expression><op>+</op><integer value="0"/><integer value="0"/></expression></assignment></tree>'
82
+ )
83
+ same(
84
+ "q = 0 - 0;",
85
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><expression><op>-</op><integer value="0"/><integer value="0"/></expression></assignment></tree>'
86
+ )
87
+ same(
88
+ "q = 0 * 0;",
89
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><expression><op>*</op><integer value="0"/><integer value="0"/></expression></assignment></tree>'
90
+ )
91
+ same(
92
+ "q = 0 / 0;",
93
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><expression><op>/</op><integer value="0"/><integer value="0"/></expression></assignment></tree>'
94
+ )
95
+ same(
96
+ "q = 0 % 0;",
97
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><expression><op>%</op><integer value="0"/><integer value="0"/></expression></assignment></tree>'
98
+ )
99
+ end
100
+
101
+ def test_conditional
102
+ same(
103
+ "if (q = foo());",
104
+ '<tree><if><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><call><identifier name="foo"/></call></assignment><empty/></if></tree>'
105
+ )
106
+ same(
107
+ "while (q = foo());",
108
+ '<tree><while><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><call><identifier name="foo"/></call></assignment><empty/></while></tree>'
109
+ )
110
+ end
111
+ end
@@ -0,0 +1,44 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011, Mak Kolybabi
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestBlank < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_blank
31
+ same(
32
+ ";",
33
+ "<tree><empty/></tree>"
34
+ )
35
+ same(
36
+ ";;",
37
+ "<tree><empty/><empty/></tree>"
38
+ )
39
+ same(
40
+ ";;;",
41
+ "<tree><empty/><empty/><empty/></tree>"
42
+ )
43
+ end
44
+ end
@@ -0,0 +1,35 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011, Mak Kolybabi
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestBlock < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_empty
31
+ same("{}", "<tree><block></block></tree>")
32
+ same("{;}", "<tree><block><empty/></block></tree>")
33
+ same("{\n#\n}", "<tree><block></block></tree>")
34
+ end
35
+ end
@@ -0,0 +1,65 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011, Mak Kolybabi
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestConstant < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_false
31
+ diff(
32
+ "z = FALSE;",
33
+ '<tree><assignment><op>=</op><lvalue><identifier name="z"/></lvalue><identifier name="FALSE"/></assignment></tree>'
34
+ )
35
+
36
+ same(
37
+ "z = FALSE;",
38
+ '<tree><assignment><op>=</op><lvalue><identifier name="z"/></lvalue><false/></assignment></tree>'
39
+ )
40
+ end
41
+
42
+ def test_null
43
+ diff(
44
+ "z = NULL;",
45
+ '<tree><assignment><op>=</op><lvalue><identifier name="z"/></lvalue><identifier name="NULL"/></assignment></tree>'
46
+ )
47
+
48
+ same(
49
+ "z = NULL;",
50
+ '<tree><assignment><op>=</op><lvalue><identifier name="z"/></lvalue><null/></assignment></tree>'
51
+ )
52
+ end
53
+
54
+ def test_true
55
+ diff(
56
+ "z = TRUE;",
57
+ '<tree><assignment><op>=</op><lvalue><identifier name="z"/></lvalue><identifier name="TRUE"/></assignment></tree>'
58
+ )
59
+
60
+ same(
61
+ "z = TRUE;",
62
+ '<tree><assignment><op>=</op><lvalue><identifier name="z"/></lvalue><true/></assignment></tree>'
63
+ )
64
+ end
65
+ end
@@ -0,0 +1,36 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011, Mak Kolybabi
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestEmpty < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_empty
31
+ same(
32
+ "",
33
+ "<tree/>"
34
+ )
35
+ end
36
+ end
@@ -0,0 +1,57 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011, Mak Kolybabi
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestExpressions < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_parentheses
31
+ pass("q = (-a);")
32
+ pass("q = (~a);")
33
+ pass("q = (a);")
34
+ pass("q = (a + b);")
35
+ pass("q = (a + (b + c));")
36
+ pass("q = ((a + b) + (b + d));")
37
+ pass("q = (a + b) == c;")
38
+ pass("q = (a + b) == (c + d);")
39
+ pass("q = ((a + b) == (c + d));")
40
+ pass("q = (a + b) >> c;")
41
+ pass("q = (a + b) >> (c + d);")
42
+ pass("q = ((a + b) >> (c + d));")
43
+ pass("q = (((1)));")
44
+ pass("q = (((a = b)));")
45
+ end
46
+
47
+ def test_bitwise
48
+ pass("q = 0 | 1;")
49
+ end
50
+
51
+ def test_precedence
52
+ same(
53
+ 'q = a + b / c + d;',
54
+ '<tree><assignment><op>=</op><lvalue><identifier name="q"/></lvalue><expression><op>+</op><expression><op>+</op><lvalue><identifier name="a"/></lvalue><expression><op>/</op><lvalue><identifier name="b"/></lvalue><lvalue><identifier name="c"/></lvalue></expression></expression><lvalue><identifier name="d"/></lvalue></expression></assignment></tree>'
55
+ )
56
+ end
57
+ end
@@ -0,0 +1,82 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011, Mak Kolybabi
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestFunction < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_keyword_prefix
31
+ # While we never want to have a function with the same name as a keyword,
32
+ # having a keyword at the start of a function name is perfectly valid.
33
+ fail_parse("if();")
34
+ fail_parse("break();")
35
+ fail_parse("continue();")
36
+ fail_parse("else();")
37
+ fail_parse("export();")
38
+ fail_parse("for();")
39
+ fail_parse("foreach();")
40
+ fail_parse("function();")
41
+ fail_parse("global_var();")
42
+ fail_parse("if();")
43
+ fail_parse("import();")
44
+ fail_parse("include();")
45
+ fail_parse("local_var();")
46
+ fail_parse("repeat();")
47
+ fail_parse("return();")
48
+ fail_parse("until();")
49
+ fail_parse("while();")
50
+
51
+ # While we never want to have a function with the same name as a constant,
52
+ # having a constant at the start of a function name is perfectly valid.
53
+ fail_parse("FALSE();")
54
+ fail_parse("NULL();")
55
+ fail_parse("TRUE();")
56
+
57
+ # X is an exception. It is a valid function name, despite being a keyword.
58
+ pass("x();")
59
+
60
+ pass("if_();")
61
+ pass("break_();")
62
+ pass("continue_();")
63
+ pass("else_();")
64
+ pass("export_();")
65
+ pass("for_();")
66
+ pass("foreach_();")
67
+ pass("function_();")
68
+ pass("global_var_();")
69
+ pass("if_();")
70
+ pass("import_();")
71
+ pass("include_();")
72
+ pass("local_var_();")
73
+ pass("repeat_();")
74
+ pass("return_();")
75
+ pass("until_();")
76
+ pass("while_();")
77
+
78
+ pass("FALSE_();")
79
+ pass("NULL_();")
80
+ pass("TRUE_();")
81
+ end
82
+ end
@@ -0,0 +1,85 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011, Mak Kolybabi
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestIf < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_number
31
+ pass(%q|if (-1);|)
32
+ pass(%q|if (0);|)
33
+ pass(%q|if (1);|)
34
+ end
35
+
36
+ def test_assigment
37
+ pass(%q|if (q = 1);|)
38
+ end
39
+
40
+ def test_call
41
+ pass(%q|if (foo());|)
42
+ end
43
+
44
+ def test_constant
45
+ pass(%q|if (FALSE);|)
46
+ pass(%q|if (NULL);|)
47
+ pass(%q|if (TRUE);|)
48
+ end
49
+
50
+ def test_string
51
+ pass(%q|if ('');|)
52
+ pass(%q|if ('foo');|)
53
+ pass(%q|if ("");|)
54
+ pass(%q|if ("foo");|)
55
+ end
56
+
57
+ def test_simple
58
+ pass(%q|if (1);|)
59
+ pass(%q|if (1) foo();|)
60
+ end
61
+
62
+ def test_compound
63
+ pass(%q|if (1) {}|)
64
+ pass(%q|if (1) {foo();}|)
65
+ end
66
+
67
+ def test_nested
68
+ pass(%q|if (1) if (1) foo();|)
69
+ pass(%q|if (1) if (1) {foo();}|)
70
+ end
71
+
72
+ def test_dangling
73
+ # This is the correct way to parse the ambiguity presented by the dangling
74
+ # else problem. The else should be attached to the nearest unterminated If.
75
+ same(
76
+ %q|if (1) if (1) foo(); else bar();|,
77
+ '<tree><if><integer value="1"/><if><integer value="1"/><call><identifier name="foo"/></call><call><identifier name="bar"/></call></if></if></tree>'
78
+ )
79
+
80
+ diff(
81
+ %q|if (1) if (1) foo(); else bar();|,
82
+ '<tree><if><integer value="1"/><if><integer value="1"/><call><identifier name="foo"/></call></if><call><identifier name="bar"/></call></if></tree>'
83
+ )
84
+ end
85
+ end
@@ -0,0 +1,37 @@
1
+ ################################################################################
2
+ # Copyright (c) 2011, Mak Kolybabi
3
+ # All rights reserved.
4
+ #
5
+ # Redistribution and use in source and binary forms, with or without
6
+ # modification, are permitted provided that the following conditions are met:
7
+ #
8
+ # 1. Redistributions of source code must retain the above copyright notice, this
9
+ # list of conditions and the following disclaimer.
10
+ #
11
+ # 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ # this list of conditions and the following disclaimer in the documentation
13
+ # and/or other materials provided with the distribution.
14
+ #
15
+ # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16
+ # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17
+ # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18
+ # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19
+ # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20
+ # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21
+ # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22
+ # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23
+ # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24
+ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
+ ################################################################################
26
+
27
+ class TestInclude < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_include
31
+ fail_parse(%q|include ();|)
32
+ pass(%q|include ('');|)
33
+ pass(%q|include ('q.inc');|)
34
+ pass(%q|include ("");|)
35
+ pass(%q|include ("q.inc");|)
36
+ end
37
+ end