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
@@ -0,0 +1,51 @@
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 TestIncrDecr < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_parentheses
31
+ fail_parse('q()++;');
32
+
33
+ pass('q++;');
34
+ pass('q[1]++;');
35
+ pass('q[1][2]++;');
36
+ pass('q[1][3]++;');
37
+
38
+ pass('++q;');
39
+ pass('++q[1];');
40
+ pass('++q[1][2];');
41
+ pass('++q[1][3];');
42
+
43
+ pass('q["a"]++;');
44
+ pass('q["a"]["b"]++;');
45
+ pass('q["a"]["b"]["c"]++;');
46
+
47
+ pass('++q["a"];');
48
+ pass('++q["a"]["b"];');
49
+ pass('++q["a"]["b"]["c"];');
50
+ end
51
+ end
@@ -0,0 +1,33 @@
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 TestIp < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_ip
31
+ pass('q = 1.1.1.1;');
32
+ end
33
+ end
@@ -0,0 +1,51 @@
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 TestReturn < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_blank
31
+ fail_parse(%q|return()|)
32
+ fail_parse(%q|return();|)
33
+ fail_parse(%q|return{}|)
34
+ fail_parse(%q|return{};|)
35
+ fail_parse(%q|return ()|)
36
+ fail_parse(%q|return ();|)
37
+ fail_parse(%q|return {}|)
38
+ fail_parse(%q|return {};|)
39
+
40
+ pass(%q|return;|)
41
+ end
42
+
43
+ def test_expression
44
+ pass(%q|return(a);|)
45
+ pass(%q|return(a + b)==c;|)
46
+ end
47
+
48
+ def test_literal
49
+ pass(%q|return a;|)
50
+ end
51
+ end
@@ -0,0 +1,46 @@
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 TestString < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_empty
31
+ pass(%q|z = '';|);
32
+ pass(%q|z = "";|);
33
+ end
34
+
35
+ def test_escapes
36
+ pass(%q|z = '\\'';|);
37
+ pass(%q|z = '\\\\';|);
38
+ pass(%q|z = "\\";|);
39
+ pass(%q|z = "\\\\";|);
40
+ end
41
+
42
+ def test_newlines
43
+ pass(%Q|z = 'foo\nbar';|);
44
+ pass(%Q|z = "foo\nbar";|);
45
+ end
46
+ end
@@ -0,0 +1,56 @@
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 TestWhitespace < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_comment
31
+ pass("#")
32
+ pass(" #")
33
+ pass("##")
34
+ pass("# foo\n# bar")
35
+ end
36
+
37
+ def test_if
38
+ # Single statement IF with whitespace.
39
+ fail_parse("if ();")
40
+ fail_parse("if (1)")
41
+
42
+ pass("if(1);")
43
+ pass("if (1) ;")
44
+
45
+ # Block IF with whitespace.
46
+ fail_parse("if(){}")
47
+
48
+ pass("if(1){}")
49
+ pass("if(1){}")
50
+ pass("if (1) {}")
51
+ pass("if (1) {;}")
52
+ pass("if (1) {;;}")
53
+ pass("if (1)\n{\n}\n")
54
+ end
55
+ end
56
+
@@ -0,0 +1,240 @@
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 TestContext < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ # Five is the minimum needed to check all cases (first, after first, middle,
31
+ # before end, end), so do double that for peace of mind.
32
+ @@cols = 5 * 2
33
+ @@rows = 5 * 2
34
+
35
+ def grid(pos)
36
+ line = "." * @@cols + "\n"
37
+ grid = line * @@rows
38
+ grid[pos] = "*"
39
+
40
+ return grid
41
+ end
42
+
43
+ def test_column_beginning_of_line
44
+ 0.upto(@@rows - 1) do |row|
45
+ # Calculate the position for the beginning of the line.
46
+ pos = row * (@@cols + 1)
47
+
48
+ # Create the grid for this iteration.
49
+ code = grid(pos)
50
+
51
+ # Create a context object with our test grid.
52
+ ctx = context(code)
53
+
54
+ # Find the column for the current position.
55
+ res = ctx.col(pos)
56
+
57
+ # Check that the column is the first one on the line.
58
+ assert_equal(0, res, code)
59
+ end
60
+ end
61
+
62
+ def test_column_end_of_line
63
+ 0.upto(@@rows - 1) do |row|
64
+ # Calculate the position for the end of the line.
65
+ pos = row * (@@cols + 1) + (@@cols - 1)
66
+
67
+ # Create the grid for this iteration.
68
+ code = grid(pos)
69
+
70
+ # Create a context object with our test grid.
71
+ ctx = context(code)
72
+
73
+ # Find the column for the current position.
74
+ res = ctx.col(pos)
75
+
76
+ # Check that the column is the last one on the line.
77
+ assert_equal(@@cols - 1, res, code)
78
+ end
79
+ end
80
+
81
+ def test_column_middle_of_line
82
+ 0.upto(@@rows - 1) do |row|
83
+ 1.upto(@@cols - 2) do |col|
84
+ # Calculate the position of the character on the line.
85
+ pos = row * (@@cols + 1) + col
86
+
87
+ # Create the grid for this iteration.
88
+ code = grid(pos)
89
+
90
+ # Create a context object with our test grid.
91
+ ctx = context(code)
92
+
93
+ # Find the column for the current position.
94
+ res = ctx.col(pos)
95
+
96
+ # Check that the column is the expected one.
97
+ assert_equal(col, res, code)
98
+ end
99
+ end
100
+ end
101
+
102
+ def test_row_beginning_of_code
103
+ 0.upto(@@cols - 1) do |col|
104
+ # Calculate the position on the first line.
105
+ pos = col
106
+
107
+ # Create the grid for this iteration.
108
+ code = grid(pos)
109
+
110
+ # Create a context object with our test grid.
111
+ ctx = context(code)
112
+
113
+ # Find the row for the current position.
114
+ res = ctx.row(pos)
115
+
116
+ # Check that the column is the first one on the line.
117
+ assert_equal(1, res, code)
118
+ end
119
+ end
120
+
121
+ def test_row_end_of_code
122
+ 0.upto(@@cols - 1) do |col|
123
+ # Calculate the position on the last line.
124
+ pos = (@@rows - 1) * (@@cols + 1) + col
125
+
126
+ # Create the grid for this iteration.
127
+ code = grid(pos)
128
+
129
+ # Create a context object with our test grid.
130
+ ctx = context(code)
131
+
132
+ # Find the row for the current position.
133
+ res = ctx.row(pos)
134
+
135
+ # Check that the column is the last one on the line.
136
+ assert_equal(@@rows, res, code)
137
+ end
138
+ end
139
+
140
+ def test_row_middle_of_code
141
+ 1.upto(@@rows - 2) do |row|
142
+ 0.upto(@@cols - 1) do |col|
143
+ # Calculate the position of the character on the line.
144
+ pos = row * (@@cols + 1) + col
145
+
146
+ # Create the grid for this iteration.
147
+ code = grid(pos)
148
+
149
+ # Create a context object with our test grid.
150
+ ctx = context(code)
151
+
152
+ # Find the column for the current position.
153
+ res = ctx.row(pos)
154
+
155
+ # Check that the column is the expected one.
156
+ assert_equal(row + 1, res, code)
157
+ end
158
+ end
159
+ end
160
+
161
+ def test_single_ctx_line_beginning_of_code
162
+ 0.upto(@@cols - 1) do |col|
163
+ # Calculate the position on the first line.
164
+ pos = col
165
+
166
+ # Create the grid for this iteration.
167
+ code = grid(pos)
168
+
169
+ # Create a context object with our test grid.
170
+ ctx = context(code)
171
+
172
+ # Find the default context for the current position.
173
+ res = ctx.context(pos..pos + 1, nil, false, false)
174
+
175
+ # Check that the context is the entire first line.
176
+ assert_equal(code.split.first << "\n", res, code)
177
+ end
178
+ end
179
+
180
+ def test_single_ctx_line_end_of_code
181
+ 0.upto(@@cols - 1) do |col|
182
+ # Calculate the position on the last line.
183
+ pos = (@@rows - 1) * (@@cols + 1) + col
184
+
185
+ # Create the grid for this iteration.
186
+ code = grid(pos)
187
+
188
+ # Create a context object with our test grid.
189
+ ctx = context(code)
190
+
191
+ # Find the default context for the current position.
192
+ res = ctx.context(pos..pos + 1, nil, false, false)
193
+
194
+ # Check that the context is the entire last line.
195
+ assert_equal(code.split.last << "\n", res, code)
196
+ end
197
+ end
198
+
199
+ def test_single_ctx_middle_of_code
200
+ 1.upto(@@rows - 2) do |row|
201
+ 0.upto(@@cols - 1) do |col|
202
+ # Calculate the position of the character on the line.
203
+ pos = row * (@@cols + 1) + col
204
+
205
+ # Create the grid for this iteration.
206
+ code = grid(pos)
207
+
208
+ # Create a context object with our test grid.
209
+ ctx = context(code)
210
+
211
+ # Find the default context for the current position.
212
+ res = ctx.context(pos..pos + 1, nil, false, false)
213
+
214
+ # Check that the context is the entire last line.
215
+ assert_equal(code.split[row] << "\n", res, code)
216
+ end
217
+ end
218
+ end
219
+
220
+ def test_all_ctx_all_of_code
221
+ 0.upto(@@rows - 1) do |row|
222
+ 0.upto(@@cols - 1) do |col|
223
+ # Calculate the position of the character on the line.
224
+ pos = row * (@@cols + 1) + col
225
+
226
+ # Create the grid for this iteration.
227
+ code = grid(pos)
228
+
229
+ # Create a context object with our test grid.
230
+ ctx = context(code)
231
+
232
+ # Find the full context for the current position.
233
+ res = ctx.context(pos..pos + 1, 0..-1, false, false)
234
+
235
+ # Check that the context is the entire grid.
236
+ assert_equal(code, res, code)
237
+ end
238
+ end
239
+ end
240
+ end
@@ -0,0 +1,53 @@
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 TestTokenizerEmpty < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def test_nothing
31
+ tkz = tokenize("")
32
+ type, tok = tkz.get_token
33
+ assert_equal(false, type)
34
+ assert_equal(:EOF, tok.type)
35
+ assert_equal("$", tok.body)
36
+ end
37
+
38
+ def test_whitespace
39
+ tkz = tokenize("\n")
40
+ type, tok = tkz.get_token
41
+ assert_equal(false, type)
42
+ assert_equal(:EOF, tok.type)
43
+ assert_equal("$", tok.body)
44
+ end
45
+
46
+ def test_comments
47
+ tkz = tokenize("#")
48
+ type, tok = tkz.get_token
49
+ assert_equal(false, type)
50
+ assert_equal(:EOF, tok.type)
51
+ assert_equal("$", tok.body)
52
+ end
53
+ end
@@ -0,0 +1,68 @@
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 TestTokenizerInteger < Test::Unit::TestCase
28
+ include Nasl::Test
29
+
30
+ def exhaustive(base, type, prefix="")
31
+ # Test all 16-bit integers.
32
+ 0.upto(2 ** 16 - 1) do |integer|
33
+ tkz = tokenize("#{prefix}#{integer.to_s(base)}")
34
+ assert_nothing_raised(Nasl::TokenException) { tkz.get_token }
35
+ assert_equal(type, tkz.reset.get_token.first)
36
+ end
37
+ end
38
+
39
+ def test_bad_hex
40
+ "g".upto("z") do |digit|
41
+ [:downcase, :upcase].each do |type|
42
+ tkz = tokenize("0x#{digit}".send(type))
43
+ assert_raise(Nasl::TokenException) { tkz.get_token }
44
+ end
45
+ end
46
+ end
47
+
48
+ def test_bad_decimal
49
+ 1.upto(9) do |digit|
50
+ "a".upto("z") do |letter|
51
+ tkz = tokenize("#{digit}#{letter}")
52
+ assert_raise(Nasl::TokenException) { tkz.get_token }
53
+ end
54
+ end
55
+ end
56
+
57
+ def test_exhaustive_hex
58
+ exhaustive(16, :INT_HEX, "0x")
59
+ end
60
+
61
+ def test_exhaustive_octal
62
+ exhaustive(8, :INT_OCT, "0")
63
+ end
64
+
65
+ def test_exhaustive_decimal
66
+ exhaustive(10, :INT_DEC)
67
+ end
68
+ end