├── Gemfile ├── .github └── CODEOWNERS ├── test ├── test_helper.rb └── unit │ ├── parser │ ├── test_ip.rb │ ├── test_empty.rb │ ├── test_include.rb │ ├── test_while.rb │ ├── test_blank.rb │ ├── test_do_while.rb │ ├── test_list.rb │ ├── test_string.rb │ ├── test_repetition.rb │ ├── test_local.rb │ ├── test_global.rb │ ├── test_incr_decr.rb │ ├── test_whitespace.rb │ ├── test_return.rb │ ├── test_foreach.rb │ ├── test_namespace.rb │ ├── test_block.rb │ ├── test_constant.rb │ ├── test_expressions.rb │ ├── test_if.rb │ ├── test_object.rb │ ├── test_switch.rb │ ├── test_comment.rb │ ├── test_call.rb │ └── test_array.rb │ └── tokenizer │ ├── test_empty.rb │ ├── test_references.rb │ ├── test_integer.rb │ ├── test_operator_lookahead.rb │ └── test_string.rb ├── .gitignore ├── .travis.yml ├── lib ├── nasl │ ├── version.rb │ ├── parser │ │ ├── break.rb │ │ ├── empty.rb │ │ ├── continue.rb │ │ ├── undefined.rb │ │ ├── local.rb │ │ ├── global.rb │ │ ├── obj_var.rb │ │ ├── reference.rb │ │ ├── export.rb │ │ ├── import.rb │ │ ├── include.rb │ │ ├── list.rb │ │ ├── return.rb │ │ ├── repeat.rb │ │ ├── while.rb │ │ ├── do_while.rb │ │ ├── repetition.rb │ │ ├── key_value_pair.rb │ │ ├── parameter.rb │ │ ├── ip.rb │ │ ├── lvalue.rb │ │ ├── comment.rb │ │ ├── assigment.rb │ │ ├── foreach.rb │ │ ├── identifier.rb │ │ ├── if.rb │ │ ├── for.rb │ │ ├── decrement.rb │ │ ├── increment.rb │ │ ├── string.rb │ │ ├── object.rb │ │ ├── namespace.rb │ │ ├── call.rb │ │ ├── function_ref.rb │ │ ├── block.rb │ │ ├── argument.rb │ │ ├── switch.rb │ │ ├── array.rb │ │ ├── tree.rb │ │ ├── expression.rb │ │ ├── case.rb │ │ ├── integer.rb │ │ ├── function.rb │ │ └── node.rb │ ├── commands │ │ ├── test.rb │ │ ├── xml.rb │ │ ├── tokenize.rb │ │ ├── parse.rb │ │ └── benchmark.rb │ ├── parser.rb │ ├── token.rb │ ├── test.rb │ ├── cli.rb │ ├── command.rb │ └── context.rb └── nasl.rb ├── bin └── nasl-parse ├── Rakefile ├── nasl.gemspec └── README.md /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | gemspec 4 | -------------------------------------------------------------------------------- /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | * @tenable/nessus-plugins @tenable/reverse-engineering @tenable/oldowners 2 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require 'test/unit' 2 | require 'nasl' 3 | 4 | class Test::Unit::TestCase 5 | include Nasl::Test 6 | end 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Ruby gems files. 2 | *.gem 3 | .bundle 4 | Gemfile.lock 5 | pkg/* 6 | vendor/ 7 | 8 | # Generated files. 9 | *.rex.rb 10 | *.tab.rb 11 | 12 | # Backup files. 13 | \#*\# 14 | *~ 15 | *.bak 16 | 17 | # rbenv 18 | .ruby-version 19 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | sudo: false 3 | 4 | rvm: 5 | - 1.9.3 6 | - 2.0.0 7 | - 2.1.0 8 | - 2.2.0 9 | - 2.3.0 10 | 11 | gemfile: 12 | - Gemfile 13 | 14 | notifications: 15 | email: 16 | - jhammack@tenable.com 17 | - aweber@tenable.com 18 | - mcoumbes@tenable.com 19 | - aorr@tenable.com 20 | 21 | before_install: 22 | - gem --version 23 | 24 | -------------------------------------------------------------------------------- /lib/nasl/version.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | module Nasl 28 | VERSION = '0.5.0' 29 | end 30 | -------------------------------------------------------------------------------- /lib/nasl/parser/break.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Break < Node 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/nasl/parser/empty.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Empty < Node 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /lib/nasl/parser/continue.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Continue < Node 31 | end 32 | end 33 | -------------------------------------------------------------------------------- /bin/nasl-parse: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | ################################################################################ 4 | # Copyright (c) 2011-2014, Tenable Network Security 5 | # All rights reserved. 6 | # 7 | # Redistribution and use in source and binary forms, with or without 8 | # modification, are permitted provided that the following conditions are met: 9 | # 10 | # 1. Redistributions of source code must retain the above copyright notice, this 11 | # list of conditions and the following disclaimer. 12 | # 13 | # 2. Redistributions in binary form must reproduce the above copyright notice, 14 | # this list of conditions and the following disclaimer in the documentation 15 | # and/or other materials provided with the distribution. 16 | # 17 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 20 | # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 21 | # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 | # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 23 | # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 24 | # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 | # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 | ################################################################################ 28 | 29 | $LOAD_PATH.unshift(File.expand_path('../../lib', __FILE__)) 30 | 31 | require 'nasl' 32 | 33 | Nasl::Cli.run 34 | -------------------------------------------------------------------------------- /test/unit/parser/test_ip.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 34 | -------------------------------------------------------------------------------- /lib/nasl/parser/undefined.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Undefined < Node 31 | def to_xml(xml) 32 | xml.null 33 | end 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /test/unit/parser/test_empty.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | "" 34 | ) 35 | end 36 | end 37 | -------------------------------------------------------------------------------- /lib/nasl/commands/test.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | module Nasl 28 | class CommandTest < Command 29 | def self.binding 30 | 'test' 31 | end 32 | 33 | def self.run(cfg, args) 34 | Test.initialize!(args) 35 | end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /lib/nasl/commands/xml.rb: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright (c) 2011-2014, 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 | module Nasl 28 | class CommandXml < Command 29 | def self.binding 30 | 'xml' 31 | end 32 | 33 | def self.analyze(cfg, path, args) 34 | contents = File.open(path, "rb").read 35 | puts Parser.new.parse(contents).to_s 36 | end 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/nasl/parser/local.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Local < Node 31 | attr_reader :idents 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @idents = @tokens[1] 37 | 38 | @children << :idents 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/nasl/parser/global.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Global < Node 31 | attr_reader :idents 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @idents = @tokens[1] 37 | 38 | @children << :idents 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/nasl/parser/obj_var.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class ObjVar < Node 31 | attr_reader :idents 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @idents = @tokens[1] 37 | 38 | @children << :idents 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/nasl/parser/reference.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Reference < Node 31 | attr_reader :name 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @name = @tokens[0] 37 | 38 | @children << :name 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/nasl/parser/export.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Export < Node 31 | attr_reader :function 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @function = @tokens[1] 37 | 38 | @children << :function 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/nasl/parser/import.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Import < Node 31 | attr_reader :filename 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @filename = @tokens[2] 37 | 38 | @children << :filename 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/nasl/parser/include.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Include < Node 31 | attr_reader :filename 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @filename = @tokens[2] 37 | 38 | @children << :filename 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /test/unit/parser/test_include.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 38 | -------------------------------------------------------------------------------- /lib/nasl/parser/list.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class List < Node 31 | attr_reader :elems 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @elems = if @tokens[1].is_a? ::Array then @tokens[1] else [] end 37 | 38 | @children << :elems 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/nasl/parser/return.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Return < Node 31 | attr_reader :expr 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @expr = if @tokens.length == 3 then @tokens[1] else nil end 37 | 38 | @children << :expr 39 | end 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/nasl/parser/repeat.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Repeat < Node 31 | attr_reader :body, :cond 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @body = @tokens[1] 37 | @cond = @tokens[3] 38 | 39 | @children << :body 40 | @children << :cond 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/nasl/parser/while.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class While < Node 31 | attr_reader :body, :cond 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @cond = @tokens[2] 37 | @body = @tokens[4] 38 | 39 | @children << :cond 40 | @children << :body 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /test/unit/parser/test_while.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 TestWhile < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | # Check that the 'while' itself is in the tokens list 31 | def test_while_in_tokens 32 | tree = parse(%q|while (foo) {}|) 33 | assert_equal( 34 | tree.all(:While).first.tokens.first.to_s, 35 | "while" 36 | ) 37 | end 38 | end 39 | -------------------------------------------------------------------------------- /lib/nasl/parser/do_while.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class DoWhile < Node 31 | attr_reader :body, :cond 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @cond = @tokens[4] 37 | @body = @tokens[1] 38 | 39 | @children << :cond 40 | @children << :body 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/nasl/parser/repetition.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Repetition < Node 31 | attr_reader :call, :expr 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @call = tokens[0] 37 | @expr = tokens[2] 38 | 39 | @children << :call 40 | @children << :expr 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/nasl/parser/key_value_pair.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class KeyValuePair < Node 31 | attr_reader :key, :value 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @key = tokens[0] 37 | @value = tokens[2] 38 | 39 | @children << :key 40 | @children << :value 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/nasl/parser/parameter.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Parameter < Node 31 | attr_reader :name, :pass_by 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @name = @tokens[0] 37 | @pass_by = @tokens[1] 38 | 39 | @children << :name 40 | @children << :pass_by 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /lib/nasl/parser/ip.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Ip < Node 31 | attr_reader :octets 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @octets = [@tokens[0], @tokens[2], @tokens[4], @tokens[6]] 37 | end 38 | 39 | def to_xml(xml) 40 | xml.ip(:octets=>@octets.join('.')) 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /test/unit/parser/test_blank.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | "" 34 | ) 35 | same( 36 | ";;", 37 | "" 38 | ) 39 | same( 40 | ";;;", 41 | "" 42 | ) 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /test/unit/parser/test_do_while.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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 TestDoWhile < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_each 31 | tree = parse( 32 | <<-EOF 33 | x = 0; 34 | do 35 | { 36 | x ++; 37 | display(x); 38 | } 39 | while (x < 10) 40 | EOF 41 | ) 42 | do_whiles = tree.all(:DoWhile) 43 | assert_not_nil(tree) 44 | assert_equal(1, do_whiles.length) 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/nasl/parser/lvalue.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Lvalue < Node 31 | attr_reader :ident, :indexes 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @ident = @tokens.first 37 | @indexes = if @tokens.length == 2 then tokens.last else [] end 38 | 39 | @children << :ident 40 | @children << :indexes 41 | end 42 | end 43 | end 44 | -------------------------------------------------------------------------------- /test/unit/parser/test_list.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 TestList < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_empty 31 | tree = parse("foo = [];") 32 | assert_not_nil(tree) 33 | 34 | lists = tree.all(:List) 35 | assert_not_nil(lists) 36 | assert_equal(1, lists.length) 37 | 38 | list = lists.first 39 | assert_not_nil(list) 40 | assert_equal(0, list.elems.length) 41 | end 42 | end 43 | -------------------------------------------------------------------------------- /lib/nasl/parser.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 'racc/parser' 28 | require 'nasl/grammar.tab' 29 | require 'nasl/parser/tree' 30 | 31 | module Nasl 32 | class ParseException < Exception 33 | end 34 | 35 | class Parser 36 | def initialize 37 | @grammar = Grammar.new 38 | @env = Tree.new 39 | end 40 | 41 | def parse(code, path="(unknown)") 42 | @grammar.parse(@env, code, path) 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/nasl/parser/comment.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Comment < Node 31 | attr_reader :next, :text 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @text = @tokens.first 37 | @next = @tokens.last 38 | 39 | @next = nil if @tokens.length == 1 40 | end 41 | 42 | def to_xml(xml) 43 | xml.comment(@text) 44 | end 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/nasl/parser/assigment.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Assignment < Node 31 | attr_reader :expr, :lval, :op 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @lval = @tokens[0] 37 | @op = @tokens[1] 38 | @expr = @tokens[2] 39 | 40 | @children << :op 41 | @children << :lval 42 | @children << :expr 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/nasl/parser/foreach.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Foreach < Node 31 | attr_reader :body, :expr, :iter 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @iter = @tokens[1] 37 | @expr = @tokens[2] 38 | @body = @tokens[3] 39 | 40 | @children << :iter 41 | @children << :expr 42 | @children << :body 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /lib/nasl/parser/identifier.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Identifier < Node 31 | include Comparable 32 | attr_reader :name 33 | 34 | def initialize(tree, *tokens) 35 | super 36 | 37 | @name = @tokens.first.body 38 | end 39 | 40 | def <=>(other) 41 | self.name <=> other.name 42 | end 43 | 44 | def to_xml(xml) 45 | xml.identifier(:name=>@name) 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/nasl/parser/if.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class If < Node 31 | attr_reader :cond, :false, :true 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @cond = @tokens[2] 37 | @true = @tokens[4] 38 | @false = if @tokens.length == 7 then @tokens.last else nil end 39 | 40 | @children << :cond 41 | @children << :true 42 | @children << :false 43 | end 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /test/unit/parser/test_string.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 47 | -------------------------------------------------------------------------------- /lib/nasl/parser/for.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class For < Node 31 | attr_reader :body, :cond, :each, :init 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @init = @tokens[2] 37 | @cond = @tokens[4] 38 | @each = @tokens[6] 39 | @body = @tokens[8] 40 | 41 | @children << :init 42 | @children << :cond 43 | @children << :each 44 | @children << :body 45 | end 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/nasl/parser/decrement.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Decrement < Node 31 | attr_reader :ident, :type 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | if @tokens.first.is_a? Lvalue 37 | @type = :post 38 | @ident = @tokens[0] 39 | else 40 | @type = :pre 41 | @ident = @tokens[1] 42 | end 43 | 44 | @children << :ident 45 | @children << :type 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/nasl/parser/increment.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Increment < Node 31 | attr_reader :ident, :type 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | if @tokens.first.is_a? Lvalue 37 | @type = :post 38 | @ident = @tokens[0] 39 | else 40 | @type = :pre 41 | @ident = @tokens[1] 42 | end 43 | 44 | @children << :ident 45 | @children << :type 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /test/unit/parser/test_repetition.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 TestRepetition < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_does 31 | pass("exit() x 10;"); 32 | end 33 | 34 | def test_two_args 35 | tree = parse("exit(2, 3) x 10;") 36 | assert_not_nil(tree) 37 | 38 | calls = tree.all(:Call) 39 | assert_not_nil(calls) 40 | assert_equal(1, calls.length) 41 | 42 | call = calls.first 43 | assert_not_nil(call) 44 | assert_equal(2, call.args.length) 45 | end 46 | end 47 | -------------------------------------------------------------------------------- /lib/nasl/parser/string.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class String < Node 31 | include Comparable 32 | attr_reader :text, :type 33 | 34 | def initialize(tree, *tokens) 35 | super 36 | 37 | @text = @tokens.first.body 38 | @type = @tokens.first.type 39 | end 40 | 41 | def <=>(other) 42 | self.text <=> other.text 43 | end 44 | 45 | def to_xml(xml) 46 | xml.method_missing(@type.downcase, @text) 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/nasl/parser/object.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Object < Node 31 | attr_reader :name, :body 32 | 33 | include Enumerable 34 | 35 | def initialize(tree, *tokens) 36 | super 37 | 38 | @body = if @tokens.length == 5 then @tokens[3] else [] end 39 | @name = @tokens[1] 40 | 41 | @children << :name 42 | @children << :body 43 | end 44 | 45 | def each 46 | @body.each{ |stmt| yield stmt } 47 | end 48 | 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /lib/nasl/parser/namespace.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Namespace < Node 31 | attr_reader :name, :body 32 | 33 | include Enumerable 34 | 35 | def initialize(tree, *tokens) 36 | super 37 | 38 | @body = if @tokens.length == 5 then @tokens[3] else [] end 39 | @name = @tokens[1] 40 | 41 | @children << :name 42 | @children << :body 43 | end 44 | 45 | def each 46 | @body.each{ |stmt| yield stmt } 47 | end 48 | 49 | end 50 | end 51 | -------------------------------------------------------------------------------- /test/unit/parser/test_local.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 TestLocal < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_empty 31 | fail_parse('local_var;') 32 | end 33 | 34 | def test_ident 35 | pass('local_var a, b, c;') 36 | end 37 | 38 | def test_assign_expression 39 | pass('local_var a = 1, b = 2, c = 3;') 40 | end 41 | 42 | def test_assign_reference 43 | pass('local_var a = @a, b = @b, c = @c;') 44 | end 45 | 46 | def test_mixed 47 | pass('local_var a, b = 2, c = @c;') 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /test/unit/parser/test_global.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 TestGlobal < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_empty 31 | fail_parse('global_var;') 32 | end 33 | 34 | def test_ident 35 | pass('global_var a, b, c;') 36 | end 37 | 38 | def test_assign_expression 39 | pass('global_var a = 1, b = 2, c = 3;') 40 | end 41 | 42 | def test_assign_reference 43 | pass('global_var a = @a, b = @b, c = @c;') 44 | end 45 | 46 | def test_mixed 47 | pass('global_var a, b = 2, c = @c;') 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /test/unit/tokenizer/test_empty.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | end 46 | -------------------------------------------------------------------------------- /test/unit/tokenizer/test_references.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 TestTokenizerReferences < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_function_reference 31 | tkz = tokenize("@"); 32 | assert_nothing_raised(Nasl::TokenException) { tkz.get_token } 33 | assert_equal(:AT_SIGN, tkz.reset.get_token.first) 34 | end 35 | 36 | def test_variable_reference 37 | tkz = tokenize("&"); 38 | assert_nothing_raised(Nasl::TokenException) { tkz.get_token } 39 | assert_equal(:AMPERSAND, tkz.reset.get_token.first) 40 | end 41 | end 42 | -------------------------------------------------------------------------------- /lib/nasl/parser/call.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Call < Node 31 | attr_reader :arg, :args, :name 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @name = @tokens[0] 37 | 38 | @args = if @tokens[2].is_a? ::Array then @tokens[2] else [] end 39 | 40 | @arg = {} 41 | @args.select { |a| a.type == :named }.each do |a| 42 | @arg[a.name.name] = a.expr 43 | end 44 | 45 | @children << :name 46 | @children << :args 47 | end 48 | end 49 | end 50 | -------------------------------------------------------------------------------- /lib/nasl/parser/function_ref.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class FunctionReference < Node 31 | attr_reader :body, :params 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @body = @tokens.last 37 | 38 | if @tokens.length == 5 39 | @params = @tokens[0] 40 | else 41 | @params = [] 42 | end 43 | 44 | @children << :params 45 | @children << :body 46 | end 47 | 48 | def each 49 | @body.each{ |stmt| yield stmt } 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /lib/nasl/parser/block.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Block < Node 31 | attr_reader :body 32 | 33 | include Enumerable 34 | 35 | def initialize(tree, *tokens) 36 | super 37 | 38 | if (@tokens.length == 4) 39 | @body = [@tokens[1]] + @tokens[2] 40 | elsif (@tokens.length == 3) 41 | @body = @tokens[1] 42 | else 43 | @body = [] 44 | end 45 | 46 | @children << :body 47 | end 48 | 49 | def each 50 | @body.each{ |stmt| yield stmt } 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /lib/nasl/parser/argument.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Argument < Node 31 | attr_reader :expr, :name, :type 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | if @tokens.length == 3 37 | @name = @tokens.first 38 | @expr = @tokens.last 39 | @type = :named 40 | else 41 | @name = nil 42 | @expr = @tokens.first 43 | @type = :anonymous 44 | end 45 | 46 | @attributes << :type 47 | @children << :name 48 | @children << :expr 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /test/unit/parser/test_incr_decr.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 52 | -------------------------------------------------------------------------------- /lib/nasl/parser/switch.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Switch < Node 31 | attr_reader :switch_expr, :switch_op, :body 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | if @tokens.length == 5 37 | @switch_op = nil 38 | @switch_expr = @tokens[2] 39 | @body = @tokens[4] 40 | else 41 | @switch_op = @tokens[2] 42 | @switch_expr = @tokens[5] 43 | @body = @tokens[7] 44 | end 45 | 46 | @children << :switch_op 47 | @children << :switch_expr 48 | @children << :body 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/nasl/parser/array.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Array < Node 31 | attr_reader :keys, :pairs 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @pairs = if @tokens[1].is_a? ::Array then @tokens[1] else [] end 37 | 38 | @keys = Hash[@pairs.map do |p| 39 | if p.key.is_a? String 40 | k = p.key.text 41 | elsif p.key.is_a? Identifier 42 | k = p.key.name 43 | else 44 | k = p.key.value 45 | end 46 | 47 | [k, p.value] 48 | end] 49 | 50 | @children << :pairs 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /lib/nasl.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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(File.expand_path('../../', __FILE__)) 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) 53 | -------------------------------------------------------------------------------- /lib/nasl/commands/tokenize.rb: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright (c) 2011-2014, 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 | module Nasl 28 | class CommandTokenize < Command 29 | def self.binding 30 | 'tokenize' 31 | end 32 | 33 | def self.analyze(cfg, path, args) 34 | contents = File.open(path, "rb").read 35 | 36 | begin 37 | Tokenizer.new(contents, path).get_tokens.each do |t| 38 | puts "[#{(t.first.to_s + ',').ljust(10)}#{t.last.region.to_s.rjust(20)}]" 39 | end 40 | rescue TokenException => e 41 | puts "The tokenizer raised the following exceptions when processing #{path}:" 42 | puts e.message 43 | puts e.backtrace 44 | puts 45 | end 46 | end 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /test/unit/parser/test_whitespace.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | 57 | -------------------------------------------------------------------------------- /lib/nasl/commands/parse.rb: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright (c) 2011-2014, 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 | module Nasl 28 | class CommandParse < Command 29 | def self.binding 30 | 'parse' 31 | end 32 | 33 | def self.analyze(cfg, path, args) 34 | begin 35 | contents = File.open(path, "rb").read 36 | rescue 37 | puts "Failed to read in the contents of the file." 38 | return 39 | end 40 | 41 | begin 42 | Parser.new.parse(contents, path) 43 | rescue Exception => e 44 | puts "Failed to parse the contents of the file." 45 | puts e.message 46 | puts e.backtrace 47 | return 48 | end 49 | 50 | puts "Successfully parsed the contents of the file." 51 | end 52 | end 53 | end 54 | -------------------------------------------------------------------------------- /lib/nasl/commands/benchmark.rb: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Copyright (c) 2011-2014, 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 'benchmark' 28 | 29 | module Nasl 30 | class CommandBenchmark < Command 31 | def self.binding 32 | 'benchmark' 33 | end 34 | 35 | def self.analyze(cfg, path, args) 36 | Benchmark.bmbm do |b| 37 | # Read in the file outside of the benchmark, to avoid contaminating it 38 | # with filesystem operations. 39 | contents = File.open(path, "rb").read 40 | 41 | b.report("Tokenize") do 42 | cfg[:iterations].times { Tokenizer.new(contents, path).get_tokens } 43 | end 44 | 45 | b.report("Parse") do 46 | cfg[:iterations].times { Parser.new.parse(contents) } 47 | end 48 | end 49 | end 50 | end 51 | end 52 | -------------------------------------------------------------------------------- /lib/nasl/parser/tree.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 'builder' 28 | 29 | module Nasl 30 | class Tree < ::Array 31 | def all(cls) 32 | (@all[Nasl.const_get(cls).to_s] ||= []) 33 | end 34 | 35 | def register(node) 36 | (@all[node.class.name] ||= []) << node 37 | @parent.register(node) unless @parent.nil? 38 | end 39 | 40 | def initialize(parent=nil) 41 | @parent = parent 42 | @all = {} 43 | end 44 | 45 | def to_s 46 | text = '' 47 | 48 | xml = Builder::XmlMarkup.new(:target=>text, :indent=>2) 49 | 50 | if empty? 51 | xml.tree 52 | else 53 | xml.tree { self.map { |node| node.to_xml(xml) } } 54 | end 55 | 56 | text 57 | end 58 | end 59 | end 60 | -------------------------------------------------------------------------------- /lib/nasl/parser/expression.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Expression < Node 31 | attr_reader :lhs, :op, :rhs 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @children << :op 37 | 38 | if @tokens.first.is_a?(Token) && @tokens.first.type == :LPAREN 39 | @op = '()' 40 | @lhs = nil 41 | @rhs = @tokens[1] 42 | elsif @tokens.length == 2 43 | @op = @tokens.first 44 | @lhs = nil 45 | @rhs = @tokens.last 46 | else 47 | @children << :lhs 48 | @lhs = @tokens[0] 49 | @op = @tokens[1] 50 | @rhs = @tokens[2] 51 | end 52 | 53 | @children << :rhs 54 | end 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /test/unit/parser/test_return.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | pass(%q|return[];|) 42 | pass(%q|return [];|) 43 | pass(%q|return{};|) 44 | pass(%q|return {};|) 45 | end 46 | 47 | def test_expression 48 | pass(%q|return(a);|) 49 | pass(%q|return(a + b)==c;|) 50 | end 51 | 52 | def test_literal 53 | pass(%q|return a;|) 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /lib/nasl/parser/case.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Case < Node 31 | attr_reader :case_val, :case_op, :case_type 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | if @tokens.length == 3 37 | @case_op = nil 38 | @case_val = @tokens[1] 39 | @case_type = 'normal' 40 | elsif @tokens.length == 6 41 | @case_op = @tokens[2] 42 | @case_val = @tokens[4] 43 | @case_type = 'normal_with_op' 44 | else 45 | @case_op = nil 46 | @case_val = nil 47 | @case_type = 'default' 48 | end 49 | 50 | @children << :case_val 51 | @children << :case_op 52 | @children << :case_type 53 | end 54 | end 55 | end 56 | -------------------------------------------------------------------------------- /test/unit/parser/test_foreach.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 TestForeach < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_old 31 | same( 32 | %q|foreach foo (bar);|, 33 | '' 34 | ) 35 | end 36 | 37 | def test_new 38 | same( 39 | %q|foreach (foo in bar);|, 40 | '' 41 | ) 42 | end 43 | 44 | # Check that the 'foreach' itself is in the tokens list 45 | def test_foreach_in_tokens 46 | tree = parse(%q|foreach foo (bar);|) 47 | assert_equal( 48 | tree.all(:Foreach).first.tokens.first.to_s, 49 | "foreach" 50 | ) 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /test/unit/parser/test_namespace.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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 TestNamespace < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_empty 31 | same("namespace foo {}", "") 32 | end 33 | 34 | def test_inner_fn 35 | same("namespace foo { function fn(){} }", "normal") 36 | end 37 | 38 | def test_nested_namespace 39 | same("namespace foo { namespace bar {} }", "") 40 | end 41 | 42 | def test_namespace_indent 43 | same("foo::bob();", ""); 44 | end 45 | end 46 | -------------------------------------------------------------------------------- /test/unit/parser/test_block.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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_each 31 | tree = parse( 32 | <<-EOF 33 | { 34 | a = 1; 35 | break; 36 | fn(); 37 | ; 38 | } 39 | EOF 40 | ) 41 | assert_not_nil(tree) 42 | 43 | blks = tree.all(:Block) 44 | assert_not_nil(blks) 45 | assert_equal(1, blks.length) 46 | 47 | clss = [:Assignment, :Break, :Call, :Empty] 48 | 49 | i = 0 50 | blks.first.each do |stmt| 51 | assert_not_nil(stmt) 52 | assert_equal(Nasl.const_get(clss[i]), stmt.class) 53 | 54 | i += 1 55 | end 56 | end 57 | 58 | def test_empty 59 | same("{}", "") 60 | same("{;}", "") 61 | same("{\n#\n}", "") 62 | end 63 | end 64 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | $LOAD_PATH.unshift File.expand_path("../lib", __FILE__) 28 | 29 | require 'bundler/gem_tasks' 30 | require 'rake' 31 | require 'rake/clean' 32 | require 'rake/testtask' 33 | 34 | Rake::TestTask.new do |t| 35 | t.libs << 'test' 36 | t.test_files = FileList['test/**/test_*.rb'] 37 | end 38 | 39 | rule(%r{\.tab\.rb$} => lambda { |f| f.sub(/\.tab.rb/, '.racc') }) do |t| 40 | sh "racc #{t.source}" 41 | end 42 | 43 | desc "Generate grammar modules from Racc source." 44 | task :grammars => FileList['**/*.racc'].ext('.tab.rb') 45 | 46 | desc "Produce a fully-functional application." 47 | task :compile => [:grammars, :test] 48 | 49 | task :build => :compile do 50 | system "gem build nasl.gemspec" 51 | end 52 | 53 | task :tag_and_bag do 54 | system "git tag -a v#{Nasl::VERSION} -m 'version #{Nasl::VERSION}'" 55 | system "git push --tags" 56 | system "git checkout master" 57 | system "git merge #{Nasl::VERSION}" 58 | system "git push" 59 | end 60 | 61 | task :release => [:tag_and_bag, :build] do 62 | system "gem push nasl-#{Nasl::VERSION}.gem" 63 | end 64 | 65 | task :default => :compile 66 | -------------------------------------------------------------------------------- /lib/nasl/parser/integer.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Integer < Node 31 | include Comparable 32 | attr_reader :base, :value 33 | 34 | def initialize(tree, *tokens) 35 | super 36 | 37 | @base = case @tokens.first.type 38 | when :INT_DEC 39 | 10 40 | when :INT_HEX 41 | 16 42 | when :INT_OCT 43 | 8 44 | when :FALSE 45 | 10 46 | when :TRUE 47 | 10 48 | end 49 | 50 | @value = case @tokens.first.type 51 | when :FALSE 52 | 0 53 | when :TRUE 54 | 1 55 | else 56 | @tokens.first.body.to_i(@base) 57 | end 58 | end 59 | 60 | def <=>(other) 61 | self.value <=> other.value 62 | end 63 | 64 | def to_xml(xml) 65 | case @tokens.first.type 66 | when :FALSE 67 | xml.false 68 | when :TRUE 69 | xml.true 70 | else 71 | xml.integer(:value=>@value) 72 | end 73 | end 74 | end 75 | end 76 | -------------------------------------------------------------------------------- /lib/nasl/token.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | module Nasl 28 | class Token 29 | attr_reader :body, :ctx, :name, :region, :type 30 | 31 | def initialize(type, body, region, ctx) 32 | @type = type 33 | @body = body 34 | @region = region 35 | @ctx = ctx 36 | end 37 | 38 | def context(*args) 39 | @ctx.context(@region, *args) 40 | end 41 | 42 | def name 43 | case @type 44 | when *[:BREAK, :CONTINUE, :ELSE, :EXPORT, :FOR, :FOREACH, :FUNCTION, 45 | :GLOBAL, :IF, :IMPORT, :INCLUDE, :LOCAL, :REPEAT, :RETURN, :UNTIL, 46 | :REP, :WHILE, :NAMESPACE, :OBJECT, :VAR, :PUBLIC, :PRIVATE, :CASE, 47 | :SWITCH, :DEFAULT, :DO] 48 | "a keyword" 49 | when :UNDEF 50 | "an undefined constant" 51 | when *[:FALSE, :TRUE] 52 | "a boolean constant" 53 | when :IDENT 54 | "an identifier" 55 | when *[:DATA, :STRING] 56 | "a string" 57 | when *[:INT_DEC, :INT_HEX, :INT_OCT] 58 | "an integer" 59 | when :EOF 60 | "the end of the file" 61 | else 62 | "an operator" 63 | end 64 | end 65 | 66 | def to_s 67 | @body 68 | end 69 | end 70 | end 71 | -------------------------------------------------------------------------------- /test/unit/parser/test_constant.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | '=' 34 | ) 35 | 36 | same( 37 | "z = FALSE;", 38 | '=' 39 | ) 40 | end 41 | 42 | def test_null 43 | diff( 44 | "z = NULL;", 45 | '=' 46 | ) 47 | 48 | same( 49 | "z = NULL;", 50 | '=' 51 | ) 52 | end 53 | 54 | def test_true 55 | diff( 56 | "z = TRUE;", 57 | '=' 58 | ) 59 | 60 | same( 61 | "z = TRUE;", 62 | '=' 63 | ) 64 | end 65 | end 66 | -------------------------------------------------------------------------------- /test/unit/tokenizer/test_integer.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 69 | -------------------------------------------------------------------------------- /nasl.gemspec: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | # -*- encoding: utf-8 -*- 28 | 29 | $:.push File.expand_path('../lib', __FILE__) 30 | 31 | require 'nasl/version' 32 | 33 | Gem::Specification.new do |s| 34 | s.name = 'nasl' 35 | s.version = Nasl::VERSION 36 | s.license = 'BSD' 37 | s.homepage = 'http://github.com/tenable/nasl' 38 | s.summary = 'A language parser for the Nessus Attack Scripting Language.' 39 | s.description = 'A language parser for the Nessus Attack Scripting Language. Supporting NASL v5.2.' 40 | 41 | s.authors = ['Mak Kolybabi', 'Alex Weber', 'Jacob Hammack'] 42 | s.email = ['mak@kolybabi.com', 'aweber@tenble.com', 'jhammack@tenable.com'] 43 | 44 | s.rubyforge_project = 'nasl' 45 | 46 | s.files = `git ls-files`.split("\n") + Dir.glob('**/*.tab.rb') 47 | s.test_files = `git ls-files -- test/*`.split("\n") 48 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 49 | s.require_paths = ['lib'] 50 | 51 | s.add_development_dependency 'racc', '~> 1.4' 52 | s.add_development_dependency 'rake', '~> 10.3' 53 | 54 | s.add_runtime_dependency 'builder', '~> 3.2' 55 | s.add_runtime_dependency 'rainbow', '~> 2.0' 56 | s.add_runtime_dependency 'test-unit-minitest' 57 | end 58 | -------------------------------------------------------------------------------- /test/unit/parser/test_expressions.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | pass("q = 0 & 1;") 50 | end 51 | 52 | def test_period 53 | pass("q = a.b;") 54 | pass("q = a._;") 55 | fail_parse("q = a.1"); 56 | end 57 | 58 | def test_precedence 59 | same( 60 | 'q = a + b / c + d;', 61 | '=++/' 62 | ) 63 | end 64 | end 65 | -------------------------------------------------------------------------------- /test/unit/tokenizer/test_operator_lookahead.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | # Test that operators which could be interpreted as two separate operators... are not. 28 | class TestOperatorLookahead < Test::Unit::TestCase 29 | include Nasl::Test 30 | 31 | def assert_lookahead str 32 | assert_equal(tokenize(str).get_token.last.to_s, str) 33 | end 34 | 35 | def test_arithmetic 36 | assert_lookahead("/=") 37 | assert_lookahead("+=") 38 | assert_lookahead("-=") 39 | assert_lookahead("*=") 40 | assert_lookahead("%=") 41 | end 42 | 43 | def test_equality 44 | assert_lookahead(">=") 45 | assert_lookahead("<=") 46 | assert_lookahead("==") 47 | assert_lookahead("!=") 48 | end 49 | 50 | def test_substr_and_regex 51 | assert_lookahead(">!<") 52 | assert_lookahead("><") 53 | assert_lookahead("!~") 54 | assert_lookahead("=~") 55 | end 56 | 57 | def test_shifts 58 | assert_lookahead(">>=") 59 | assert_lookahead(">>>=") 60 | assert_lookahead("<<=") 61 | assert_lookahead(">>>") 62 | assert_lookahead("<<") 63 | assert_lookahead(">>") 64 | end 65 | 66 | def test_prefix_postfix 67 | assert_lookahead("--") 68 | assert_lookahead("++") 69 | end 70 | 71 | def logical 72 | assert_lookahead("||") 73 | assert_lookahead("&&") 74 | end 75 | end 76 | -------------------------------------------------------------------------------- /lib/nasl/parser/function.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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/parser/node' 28 | 29 | module Nasl 30 | class Function < Node 31 | attr_reader :body, :name, :params, :attribute, :fn_type 32 | 33 | def initialize(tree, *tokens) 34 | super 35 | 36 | @body = @tokens.last 37 | @fn_type = @tokens[1] 38 | 39 | if @fn_type == "obj" 40 | if @tokens.length == 8 41 | @name = @tokens[3] 42 | @attribute = @tokens[0] 43 | @params = @tokens[5] 44 | elsif @tokens.length == 7 45 | if @tokens[0].is_a?(Token) && @tokens[0].type == :FUNCTION 46 | @attribute = nil 47 | @params = @tokens[4] 48 | @name = @tokens[2] 49 | else 50 | @name = @tokens[3] 51 | @attribute = @tokens[0] 52 | @params = [] 53 | end 54 | elsif @tokens.length == 6 55 | @attribute = nil 56 | @params = [] 57 | @name = @tokens[2] 58 | end 59 | else 60 | @name = @tokens[2] 61 | @attribute = [] 62 | if @tokens.length == 7 63 | @params = @tokens[4] 64 | else 65 | @params = [] 66 | end 67 | end 68 | 69 | @children << :name 70 | @children << :attribute 71 | @children << :params 72 | @children << :body 73 | @children << :fn_type 74 | end 75 | 76 | def each 77 | @body.each{ |stmt| yield stmt } 78 | end 79 | 80 | end 81 | end 82 | -------------------------------------------------------------------------------- /lib/nasl/test.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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/parser' 28 | require 'test/unit' 29 | 30 | module Nasl 31 | module Test 32 | def self.initialize!(args) 33 | # Run all tests by default. 34 | args = ['unit/*', 'unit/*/*'] if args.empty? 35 | 36 | # Run each test or category of tests specified on the command line. 37 | args.each do |test| 38 | Dir.glob(Nasl.test + (test + '.rb')).each { |f| load(f) } 39 | end 40 | end 41 | 42 | def flatten(tree) 43 | tree.chomp.split(/\n/).map(&:strip).join 44 | end 45 | 46 | def parse(code) 47 | Nasl::Parser.new.parse(code, "(test)") 48 | end 49 | 50 | def tokenize(code) 51 | Nasl::Tokenizer.new(code, "(test)") 52 | end 53 | 54 | def context(code) 55 | Nasl::Context.new(code, "(test)") 56 | end 57 | 58 | def fail(code) 59 | tree = parse(code) 60 | 61 | assert_nil(tree) 62 | end 63 | 64 | def fail_parse(code) 65 | assert_raise(ParseException) { Nasl::Parser.new.parse(code) } 66 | end 67 | 68 | def fail_token(code) 69 | assert_raise(TokenException) { Nasl::Parser.new.parse(code) } 70 | end 71 | 72 | def pass(code) 73 | tree = parse(code) 74 | 75 | assert_not_nil(tree) 76 | end 77 | 78 | def diff(code, kg_tree) 79 | tree = parse(code) 80 | assert_not_nil(tree) 81 | assert_not_equal(flatten(kg_tree), flatten(tree.to_s)) if !tree.nil? 82 | end 83 | 84 | def same(code, kg_tree) 85 | tree = parse(code) 86 | assert_not_nil(tree) 87 | assert_equal(flatten(kg_tree), flatten(tree.to_s)) if !tree.nil? 88 | end 89 | end 90 | end 91 | -------------------------------------------------------------------------------- /test/unit/parser/test_if.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | '' 78 | ) 79 | 80 | diff( 81 | %q|if (1) if (1) foo(); else bar();|, 82 | '' 83 | ) 84 | end 85 | 86 | # Check that the 'if' itself is in the tokens list 87 | def test_if_in_tokens 88 | tree = parse(%q|if (foo) {};|) 89 | assert_equal( 90 | tree.all(:If).first.tokens.first.to_s, 91 | "if" 92 | ) 93 | end 94 | end 95 | -------------------------------------------------------------------------------- /test/unit/parser/test_object.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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 TestObject < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_each 31 | tree = parse( 32 | <<-EOF 33 | namespace test { 34 | namespace test_inner { 35 | object foo { 36 | var bob = 1; 37 | var a1,b1; 38 | # foo 39 | public function bar_pub() {} 40 | public function bar_pub1(foo) {} 41 | 42 | # foo 43 | function bar_priv_default() { a = 1; return a; } 44 | private function bar_priv() {} 45 | function test_bar1(foo) {} 46 | 47 | # foo 48 | function test_bar() 49 | { 50 | var test = 'a'; 51 | var x,y,z,t; 52 | } 53 | } 54 | } 55 | # foo! 56 | function foo(){} 57 | } 58 | # foo! 59 | function foo1(){} 60 | EOF 61 | ) 62 | assert_not_nil(tree) 63 | 64 | objects = tree.all(:Object) 65 | assert_equal(1, objects.length) 66 | assert_equal(objects[0].name.name, "foo") 67 | 68 | functions = tree.all(:Function) 69 | assert_equal(8, functions.length) 70 | 71 | assert_equal(functions[0].tokens[0].type.to_s, "PUBLIC") 72 | assert_equal(functions[3].tokens[0].type.to_s, "PRIVATE") 73 | 74 | # private / public object functions, no args 75 | assert_equal(functions[0].name.name, "bar_pub") 76 | assert_equal(functions[3].name.name, "bar_priv") 77 | 78 | # private object function without leading attribute, no args 79 | assert_equal(functions[2].name.name, "bar_priv_default") 80 | # private object function without leading attribute, args 81 | assert_equal(functions[4].name.name, "test_bar1") 82 | 83 | # with attribute and args 84 | assert_equal(functions[1].name.name, "bar_pub1") 85 | 86 | assert_equal(functions[0].fn_type, "obj") 87 | assert_equal(functions[7].fn_type, "normal") 88 | 89 | obj_vars = tree.all(:ObjVar) 90 | assert_equal(2, obj_vars.length) 91 | end 92 | 93 | end 94 | -------------------------------------------------------------------------------- /lib/nasl/cli.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 'optparse' 28 | 29 | module Nasl 30 | class Cli 31 | @@Iterations = 1000 32 | 33 | def self.run 34 | cfg = { 35 | :iterations => @@Iterations, 36 | :verbose => 0 37 | } 38 | 39 | Command.initialize! 40 | 41 | optparse = OptionParser.new do |opts| 42 | opts.banner = "Usage: nasl [options] [command [args]]" 43 | 44 | opts.on('-i', '--iterations=ITERS', 'Benchmarking iterations') do |iters| 45 | cfg[:iterations] = iters.to_i || @@Iterations 46 | end 47 | 48 | opts.on('-v', '--verbose', 'Output more information') do 49 | cfg[:verbose] += 1 50 | end 51 | end 52 | 53 | optparse.parse! 54 | 55 | # Sanity check the command line arguments. 56 | if ARGV.empty? 57 | puts "No command was specified." 58 | puts 59 | usage 60 | exit 1 61 | end 62 | 63 | cmd = ARGV.shift 64 | cls = Command.find(cmd) 65 | if cls.nil? then 66 | puts "Command '#{cmd}' not supported." 67 | puts 68 | usage 69 | exit 1 70 | end 71 | 72 | # Run the command. 73 | cls.run(cfg, ARGV) 74 | end 75 | 76 | def self.usage 77 | puts "nasl-parse [flags] [command] [path ...]" 78 | puts 79 | puts "Flags:" 80 | puts " -i iters Benchmark the parser running 'iters' iterations, default #@@Iterations." 81 | puts " Only valid with the 'benchmark' command." 82 | puts " -v Display more verbose (warning) messages." 83 | puts " -vv Display more verbose (informational) messages." 84 | puts 85 | puts "Commands:" 86 | puts " benchmark Benchmarks the parsing of the input path(s)." 87 | puts " parse Parses the input path(s)." 88 | puts " test Runs the specified unit tests, all are selected by default." 89 | puts " tokenize Tokenizes the input path(s)." 90 | puts " xml Parses the input path(s) and displays them as XML." 91 | end 92 | end 93 | end 94 | -------------------------------------------------------------------------------- /test/unit/parser/test_switch.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2018, Tenable Network Security 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 TestSwitch < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_each 31 | tree = parse( 32 | <<-EOF 33 | switch (value) 34 | { 35 | case 0: 36 | b = 1; 37 | break; 38 | case 3: 39 | break; 40 | default: 41 | a = 0; 42 | } 43 | switch [=~] (value1) 44 | { 45 | case "^abc.*": 46 | rtrn = 1; 47 | break; 48 | case "^cde.*": 49 | rtrn = 2; 50 | break; 51 | default: 52 | rtrn = 1; 53 | } 54 | switch (value2) 55 | { 56 | case [=~] "^abc.*": 57 | rtrn = 1; 58 | break; 59 | case [==] "cde": 60 | rtrn = 2; 61 | break; 62 | default: 63 | rtrn = 1; 64 | } 65 | switch (value3) 66 | { 67 | case ' ', '\n', '\t', '\r': 68 | return TRUE; 69 | default: 70 | return FALSE; 71 | } 72 | EOF 73 | ) 74 | assert_not_nil(tree) 75 | 76 | cases = tree.all(:Case) 77 | assert_not_nil(cases) 78 | assert_equal(11, cases.length) 79 | 80 | assert_equal(cases[0].case_op, nil) 81 | assert_equal(cases[6].case_op.to_s, "=~") 82 | 83 | assert_equal(cases[0].case_val[0].value, 0) 84 | assert_equal(cases[4].case_val[0].text, "^cde.*") 85 | assert_equal(cases[9].case_val[0].text, " ") 86 | assert_equal(cases[2].case_val, nil) 87 | assert_equal(cases[9].case_val[2].text, "\t") 88 | 89 | assert_equal(cases[6].case_type, "normal_with_op") 90 | assert_equal(cases[0].case_type, "normal") 91 | assert_equal(cases[2].case_type, "default") 92 | assert_equal(cases[6].case_type, "normal_with_op") 93 | 94 | switches = tree.all(:Switch) 95 | assert_not_nil(switches) 96 | assert_equal(4, switches.length) 97 | 98 | assert_equal(switches[1].switch_op.to_s, "=~") 99 | assert_equal(switches[0].switch_op, nil) 100 | 101 | assert_equal(switches[0].switch_expr.tokens[0].name, "value") 102 | assert_equal(switches[1].switch_expr.tokens[0].name, "value1") 103 | assert_equal(switches[2].switch_expr.tokens[0].name, "value2") 104 | end 105 | end 106 | -------------------------------------------------------------------------------- /lib/nasl/parser/node.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | module Nasl 28 | class Node 29 | attr_reader :ctx, :tokens 30 | 31 | def initialize(tree, *tokens) 32 | # Register new node in the tree. 33 | tree.register(self) 34 | 35 | # Create the arrays which are used for converting the parse tree to XML. 36 | @attributes = [] 37 | @children = [] 38 | 39 | # Store all of the tokens that made up this node. 40 | @tokens = tokens 41 | 42 | # handle empty token set 43 | 44 | # Extract the context object from the first token. 45 | if(!@tokens.nil? and @tokens.length>0 and !@tokens.first.nil?) 46 | @ctx = @tokens.first.ctx 47 | end 48 | end 49 | 50 | def context(*args) 51 | if(!@ctx.nil?) 52 | @ctx.context(region, *args) 53 | end 54 | end 55 | 56 | def region 57 | if(@tokens.flatten.first.nil?) 58 | return [] 59 | end 60 | @tokens.flatten.first.region.begin..@tokens.flatten.last.region.end 61 | end 62 | 63 | def to_xml(xml) 64 | # Mangle the class name into something more appropriate for XML. 65 | name = self.class.name.split('::').last 66 | name = name.gsub(/(.)([A-Z])/, '\1_\2').downcase 67 | 68 | # Create a hash from the attribute array. 69 | attr = Hash[@attributes.map{ |el| [el, self.send(el)] }] 70 | 71 | # If there are no attributes, make a modified opening tag. 72 | return xml.tag!(name, attr) if @children.empty? 73 | 74 | # Create the tag representing this node. 75 | xml.tag!(name, attr) do 76 | @children.each do |name| 77 | # Retrieve the object that the symbol indicates. 78 | obj = self.send(name) 79 | 80 | # Skip over empty children. 81 | next if obj.nil? 82 | 83 | # Handle objects that are arrays holding nodes, or basic types that 84 | # aren't nodes. 85 | if obj.is_a? ::Array 86 | obj.each { |el| el.to_xml(xml) } 87 | elsif obj.is_a? Node 88 | obj.to_xml(xml) 89 | else 90 | xml.tag!(name, obj.to_s) 91 | end 92 | end 93 | end 94 | end 95 | end 96 | end 97 | -------------------------------------------------------------------------------- /lib/nasl/command.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | module Nasl 28 | class Command 29 | def self.initialize! 30 | Dir.glob(Nasl.lib + 'nasl/commands/*.rb').each { |f| load(f) } 31 | end 32 | 33 | def self.all 34 | (@_all ||= []) 35 | end 36 | 37 | def self.inherited(cls) 38 | all << cls 39 | end 40 | 41 | def self.find(cmd) 42 | all.each do |cls| 43 | return cls if cls.binding == cmd 44 | end 45 | 46 | nil 47 | end 48 | 49 | def self.banner(title, width=80) 50 | # Create center of banner. 51 | middle = "[ #{title} ]" 52 | 53 | # Make sure width is a float. 54 | width = width.to_f 55 | 56 | # Create bars on either side. 57 | leftover = (width - middle.length) / 2 58 | left = '-' * leftover.floor 59 | right = '-' * leftover.ceil 60 | 61 | left + middle + right 62 | end 63 | 64 | def self.run(cfg, args) 65 | # Separate plugins and libraries from the rest of the arguments. 66 | paths = args.select { |arg| arg =~ /(\/|\.(inc|nasl))$/ } 67 | args -= paths 68 | 69 | # If we have no paths to process, there's a problem. Special purpose 70 | # commands that don't require files to be declared should override this 71 | # method. 72 | if paths.empty? 73 | puts "No directories (/), libraries (.inc), or plugins (.nasl) were specified." 74 | exit 1 75 | end 76 | 77 | # Collect all the paths together, recursively. 78 | dirents = [] 79 | paths.each do |path| 80 | Pathname.new(path).find do |dirent| 81 | if dirent.file? && dirent.extname =~ /inc|nasl/ 82 | dirents << dirent 83 | end 84 | end 85 | end 86 | 87 | # If the command is capable of handling all the paths at once, send them 88 | # in a group, otherwise send them individually. 89 | if self.respond_to? :analyze_all then 90 | analyze_all(cfg, dirents, args) 91 | else 92 | dirents.each do |d| 93 | puts banner(d.basename) 94 | analyze(cfg, d, args) 95 | puts banner(d.basename) 96 | end 97 | end 98 | end 99 | end 100 | end 101 | -------------------------------------------------------------------------------- /test/unit/tokenizer/test_string.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 | 28 | class TestTokenizerString < Test::Unit::TestCase 29 | include Nasl::Test 30 | 31 | def test_empty 32 | # Tokenize empty single-quoted string. 33 | tkz = tokenize(%q|''|) 34 | type, tok = tkz.get_token 35 | assert_equal(:DATA, type) 36 | assert_equal("", tok.body) 37 | 38 | # Tokenize empty double-quoted string. 39 | tkz = tokenize(%q|""|) 40 | type, tok = tkz.get_token 41 | assert_equal(:STRING, type) 42 | assert_equal("", tok.body) 43 | end 44 | 45 | def test_trailing_escapes 46 | # Tokenize single-quoted string with trailing escape character. This should 47 | # raise an exception single-quoted strings allow escape sequences, including 48 | # escaping single quotes. 49 | tkz = tokenize(%q|'\\'|) 50 | assert_raise(Nasl::TokenException) { tkz.get_token } 51 | 52 | # Tokenize double-quoted string with trailing escape character. This should 53 | # not raise an exception since double-quoted strings do not allow escape 54 | # sequences. 55 | tkz = tokenize(%q|"\\"|) 56 | assert_nothing_raised(Nasl::TokenException) { tkz.get_token } 57 | end 58 | 59 | def test_multiple_escapes 60 | 1.upto(10) do |i| 61 | tkz = tokenize("'" + ("\\" * i) + "'") 62 | 63 | # If there are an even number of escape sequences, then each one is 64 | # 'stuffed' so it is a valid single-quoted string. 65 | fn = if i % 2 == 0 then :assert_nothing_raised else :assert_raise end 66 | self.send(fn, Nasl::TokenException) { tkz.get_token } 67 | end 68 | 69 | 1.upto(10) do |i| 70 | # Any number of repeated escape sequences is fine, since they're ignored. 71 | tkz = tokenize('"' + ("\\" * i) + '"') 72 | assert_nothing_raised(Nasl::TokenException) { tkz.get_token } 73 | end 74 | end 75 | 76 | def test_missing_quote 77 | # Tokenize unterminated single-quoted string. 78 | tkz = tokenize(%q|'|) 79 | assert_raise(Nasl::TokenException) { tkz.get_token } 80 | 81 | # Tokenize unterminated double-quoted string. 82 | tkz = tokenize(%q|"|) 83 | assert_raise(Nasl::TokenException) { tkz.get_token } 84 | end 85 | 86 | def test_multiline 87 | # Tokenize multiline single-quoted string. 88 | tkz = tokenize(%q|'\n.\n.\n'|) 89 | assert_nothing_raised(Nasl::TokenException) { tkz.get_token } 90 | 91 | # Tokenize multiline double-quoted string. 92 | tkz = tokenize(%q|"\n.\n.\n"|) 93 | assert_nothing_raised(Nasl::TokenException) { tkz.get_token } 94 | end 95 | end 96 | -------------------------------------------------------------------------------- /lib/nasl/context.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 'rainbow' 28 | 29 | module Nasl 30 | class Context 31 | def initialize(code, path) 32 | @code = code 33 | @path = path 34 | 35 | # Find all of the newlines in the source code. 36 | i = 0 37 | @newlines = @code.split(/\n/).map do |nl| 38 | i += 1 + nl.length 39 | end 40 | 41 | # Treat the start and end of the file as a newlines to simplify the bol 42 | # and eol functions. 43 | @newlines.unshift(0) 44 | @newlines.push(@code.length) 45 | 46 | # Storing the list of newlines in descending order makes the row and 47 | # column code nicer. 48 | @newlines.reverse! 49 | end 50 | 51 | def bol(point) 52 | @newlines.find { |nl| nl <= point } 53 | end 54 | 55 | def eol(point) 56 | @code.index(/\n/, point) || @code.length 57 | end 58 | 59 | def col(point) 60 | # Columns use base zero indexing. 61 | point - bol(point) 62 | end 63 | 64 | def row(point) 65 | # Rows use base one indexing. 66 | @newlines.length - @newlines.index { |nl| nl <= point } 67 | end 68 | 69 | def context(inner, outer=nil, header=true, color=true) 70 | # If no outer region was provided, we will assume that the desired outer 71 | # is from the beginning of the line that the inner region starts on to the 72 | # end of the line that the inner region finishes on. 73 | outer = bol(inner.begin)..eol(inner.end) if outer.nil? 74 | 75 | # If the outer region argument was provided, but wasn't a Range, access 76 | # its region member. 77 | outer = outer.region unless outer.is_a? Range 78 | 79 | ctx = "" 80 | point = inner.begin 81 | 82 | # Create the location header. 83 | ctx << "Context for row #{row(point)}, column #{col(point)} in file #@path:\n" if header 84 | 85 | # Create the text to the left of the region. The only case where there is 86 | # no text to the left is at the start of the program. 87 | if outer.begin != inner.begin 88 | line = @code[outer.begin...inner.begin] 89 | line = Rainbow(line).color(:green) if color 90 | ctx << line 91 | end 92 | 93 | # Create the text in the region. 94 | line = @code[inner.begin...inner.end] 95 | line = Rainbow(line).color(:red) if color 96 | ctx << line 97 | 98 | # Create the text to the right of the region. 99 | line = @code[inner.end...outer.end].chomp 100 | line = Rainbow(line).color(:green) if color 101 | ctx << line 102 | 103 | ctx << "\n" 104 | end 105 | end 106 | end 107 | -------------------------------------------------------------------------------- /test/unit/parser/test_comment.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 TestComment < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_standalone 31 | tree = parse( 32 | <<-EOF 33 | # Standalone 34 | EOF 35 | ) 36 | assert_not_nil(tree) 37 | 38 | comms = tree.all(:Comment) 39 | assert_not_nil(comms) 40 | assert_equal(1, comms.length) 41 | 42 | comm = comms.first 43 | assert_equal("# Standalone", comm.text.body) 44 | assert_nil(comm.next) 45 | end 46 | 47 | def test_unattached 48 | tree = parse( 49 | <<-EOF 50 | # Unattached 51 | ; 52 | EOF 53 | ) 54 | assert_not_nil(tree) 55 | 56 | comms = tree.all(:Comment) 57 | assert_not_nil(comms) 58 | assert_equal(1, comms.length) 59 | 60 | comm = comms.first 61 | assert_equal("# Unattached", comm.text.body) 62 | assert_nil(comm.next) 63 | 64 | tree = parse( 65 | <<-EOF 66 | # Unattached 67 | global_var foo; 68 | EOF 69 | ) 70 | assert_not_nil(tree) 71 | 72 | comms = tree.all(:Comment) 73 | assert_not_nil(comms) 74 | assert_equal(1, comms.length) 75 | 76 | comm = comms.first 77 | assert_equal("# Unattached", comm.text.body) 78 | assert_nil(comm.next) 79 | end 80 | 81 | def test_export 82 | tree = parse( 83 | <<-EOF 84 | foo = TRUE; 85 | 86 | # Export 87 | export function foo() {} 88 | EOF 89 | ) 90 | assert_not_nil(tree) 91 | 92 | comms = tree.all(:Comment) 93 | assert_not_nil(comms) 94 | assert_equal(1, comms.length) 95 | 96 | comm = comms.first 97 | assert_equal("# Export", comm.text.body) 98 | assert_equal(Nasl::Export, comm.next.class) 99 | end 100 | 101 | def test_function 102 | tree = parse( 103 | <<-EOF 104 | foo = TRUE; 105 | 106 | # Function 107 | function foo() {} 108 | EOF 109 | ) 110 | assert_not_nil(tree) 111 | 112 | comms = tree.all(:Comment) 113 | assert_not_nil(comms) 114 | assert_equal(1, comms.length) 115 | 116 | comm = comms.first 117 | assert_equal("# Function", comm.text.body) 118 | assert_equal(Nasl::Function, comm.next.class) 119 | end 120 | 121 | def test_global 122 | tree = parse( 123 | <<-EOF 124 | foo = TRUE; 125 | 126 | # Global 127 | global_var bar; 128 | EOF 129 | ) 130 | assert_not_nil(tree) 131 | 132 | comms = tree.all(:Comment) 133 | assert_not_nil(comms) 134 | assert_equal(1, comms.length) 135 | 136 | comm = comms.first 137 | assert_equal("# Global", comm.text.body) 138 | assert_equal(Nasl::Global, comm.next.class) 139 | end 140 | end 141 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Installation 2 | 3 | [![Gem Version](http://img.shields.io/gem/v/nasl.svg?style=flat-square)](https://rubygems.org/gems/nasl) 4 | [![Build Status](http://img.shields.io/travis/tenable/nasl.svg)](https://travis-ci.org/tenable/nasl) 5 | 6 | ## Git 7 | 8 | Installing the source from Git is done as follows: 9 | 10 | git clone git://github.com/tenable/nasl.git 11 | 12 | ## Gem 13 | 14 | 15 | Installing the package from RubyGems is done as follows: 16 | 17 | gem install nasl 18 | 19 | # Usage 20 | 21 | ## Standalone 22 | 23 | To avoid conflicting with the NASL interpreter, the NASL gem's binary is 24 | installed as `nasl-parse`. As an application, it has very few actions that it 25 | can perform. 26 | 27 | * Benchmark: This benchmarks the time taken to parse the input path(s) over a 28 | number of iterations. The number of iterations can be adjusted by the `-i` 29 | switch. 30 | 31 | % nasl-parse -i 10 benchmark /opt/nessus/lib/nessus/plugins/ssl_certificate_chain.nasl 32 | -------------------------[ ssl_certificate_chain.nasl ]------------------------- 33 | Rehearsal -------------------------------------------- 34 | Tokenize 1.687500 0.000000 1.687500 ( 1.688397) 35 | Parse 1.835938 0.015625 1.851562 ( 1.857094) 36 | ----------------------------------- total: 3.539062sec 37 | 38 | user system total real 39 | Tokenize 1.304688 0.015625 1.320312 ( 1.319951) 40 | Parse 1.046875 0.000000 1.046875 ( 1.046957) 41 | -------------------------[ ssl_certificate_chain.nasl ]------------------------- 42 | 43 | * Parse: This parses the input path(s) and indicates whether the parse was 44 | successful. 45 | 46 | % nasl-parse parse /opt/nessus/lib/nessus/plugins/ssl_certificate_chain.nasl 47 | -------------------------[ ssl_certificate_chain.nasl ]------------------------- 48 | Successfully parsed the contents of the file. 49 | -------------------------[ ssl_certificate_chain.nasl ]------------------------- 50 | 51 | * Test: This runs unit tests distributed with the application. Specific unit 52 | tests can be specified on the command line, otherwise all tests will be run. 53 | 54 | % nasl-parse test 55 | Run options: 56 | 57 | # Running tests: 58 | 59 | ............................................................................. 60 | 61 | Finished tests in 11.773983s, 6.5398 tests/s, 33493.8487 assertions/s. 62 | 63 | 77 tests, 394356 assertions, 0 failures, 0 errors, 0 skips 64 | 65 | * Tokenize: This tokenizes the input path(s) and prints the token/byte-range 66 | pairs to stdout. 67 | 68 | % nasl-parse tokenize /opt/nessus/lib/nessus/plugins/ssl_certificate_chain.nasl 69 | -------------------------[ ssl_certificate_chain.nasl ]------------------------- 70 | [COMMENT, 0...1076] 71 | [IF, 1076...1079] 72 | [LPAREN, 1079...1080] 73 | [IDENT, 1080...1091] 74 | [CMP_LT, 1091...1093] 75 | ... 76 | -------------------------[ ssl_certificate_chain.nasl ]------------------------- 77 | 78 | * XML: This parses the input path(s), and then prints an XML representation of 79 | the parse tree to stdout. 80 | 81 | % nasl-parse xml /opt/nessus/lib/nessus/plugins/ssl_certificate_chain.nasl 82 | -------------------------[ ssl_certificate_chain.nasl ]------------------------- 83 | 84 | 85 | 86 | < 87 | 88 | ... 89 | -------------------------[ ssl_certificate_chain.nasl ]------------------------- 90 | 91 | # Library 92 | 93 | The primary users of this gem are [Pedant][pedant] and [Nasldoc][nasldoc]. Other 94 | uses are encouraged, of course! The `Parser` class is the most useful part, 95 | obviously, and can be used as follows: 96 | 97 | require 'nasl' 98 | 99 | tree = Nasl::Parser.new.parse(file_contents, path_to_file) 100 | 101 | That's all there is to it. If there are any errors, it'll throw an instance of 102 | `ParseException` that will include as much context about the error as possible. 103 | 104 | Development 105 | ----------- 106 | 107 | This project uses [Bundler](http://bundler.io/). 108 | 109 | If you have a brand-new Debian machine, do this as root: 110 | 111 | apt-get install ruby-dev rubygems git 112 | gem install bundler 113 | 114 | As your regular user: 115 | 116 | git clone https://github.com/tenable/nasl 117 | cd nasl 118 | bundle install --path vendor/bundle 119 | bundle exec rake grammars 120 | bundle exec rake test 121 | 122 | All the tests should pass! 123 | 124 | To run the nasl-parse command line, do `bundle exec ./bin/nasl-parse`, which should give 125 | a help message. 126 | 127 | [nasldoc]: https://github.com/tenable/nasldoc 128 | [pedant]: https://github.com/tenable/pedant 129 | -------------------------------------------------------------------------------- /test/unit/parser/test_call.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 TestCall < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_keyword_prefix 31 | # We never want to have a function with the same name as a keyword. 32 | fail_parse("break();") 33 | fail_parse("else();") 34 | fail_parse("export();") 35 | fail_parse("for();") 36 | fail_parse("foreach();") 37 | fail_parse("function();") 38 | fail_parse("global_var();") 39 | fail_parse("if();") 40 | fail_parse("import();") 41 | fail_parse("include();") 42 | fail_parse("local_var();") 43 | fail_parse("repeat();") 44 | fail_parse("return();") 45 | fail_parse("until();") 46 | fail_parse("while();") 47 | 48 | # We never want to have a function with the same name as a constant. 49 | fail_parse("FALSE();") 50 | fail_parse("NULL();") 51 | fail_parse("TRUE();") 52 | 53 | # 'in' and 'x' are exceptions. They are valid function names, despite being a keywords. 54 | pass("in();") 55 | pass("x();") 56 | 57 | # Having a keyword at the start of a function name is perfectly valid. 58 | pass("break_();") 59 | pass("continue_();") 60 | pass("else_();") 61 | pass("export_();") 62 | pass("for_();") 63 | pass("foreach_();") 64 | pass("function_();") 65 | pass("global_var_();") 66 | pass("if_();") 67 | pass("import_();") 68 | pass("include_();") 69 | pass("local_var_();") 70 | pass("repeat_();") 71 | pass("return_();") 72 | pass("until_();") 73 | pass("while_();") 74 | 75 | # Having a constant at the start of a function name is perfectly valid. 76 | pass("FALSE_();") 77 | pass("NULL_();") 78 | pass("TRUE_();") 79 | end 80 | 81 | def test_no_args 82 | tree = parse("foo[a][1]['b'][c+d].e.f.g();") 83 | assert_not_nil(tree) 84 | 85 | calls = tree.all(:Call) 86 | assert_not_nil(calls) 87 | assert_equal(1, calls.length) 88 | 89 | call = calls.first 90 | assert_not_nil(call) 91 | assert_equal(0, call.args.length) 92 | end 93 | 94 | def test_anonymous_args 95 | tree = parse("foo[a][1]['b'][c+d].e.f.g(1, '2', three);") 96 | assert_not_nil(tree) 97 | 98 | calls = tree.all(:Call) 99 | assert_not_nil(calls) 100 | assert_equal(1, calls.length) 101 | 102 | call = calls.first 103 | assert_not_nil(call) 104 | assert_equal(3, call.args.length) 105 | 106 | arg = call.args[0] 107 | assert_equal(:anonymous, arg.type) 108 | assert(arg.expr.is_a? Nasl::Integer) 109 | 110 | arg = call.args[1] 111 | assert_equal(:anonymous, arg.type) 112 | assert(arg.expr.is_a? Nasl::String) 113 | 114 | arg = call.args[2] 115 | assert_equal(:anonymous, arg.type) 116 | assert(arg.expr.is_a? Nasl::Lvalue) 117 | end 118 | 119 | def test_named_args 120 | tree = parse("foo[a][1]['b'][c+d].e.f.g(a:1, b:'2', c:three);") 121 | assert_not_nil(tree) 122 | 123 | calls = tree.all(:Call) 124 | assert_not_nil(calls) 125 | assert_equal(1, calls.length) 126 | 127 | call = calls.first 128 | assert_not_nil(call) 129 | assert_equal(3, call.args.length) 130 | 131 | arg = call.args[0] 132 | assert_equal(:named, arg.type) 133 | assert(arg.name.is_a? Nasl::Identifier) 134 | assert(arg.expr.is_a? Nasl::Integer) 135 | assert_equal(arg.expr, call.arg['a']) 136 | 137 | arg = call.args[1] 138 | assert_equal(:named, arg.type) 139 | assert(arg.name.is_a? Nasl::Identifier) 140 | assert(arg.expr.is_a? Nasl::String) 141 | assert_equal(arg.expr, call.arg['b']) 142 | 143 | arg = call.args[2] 144 | assert_equal(:named, arg.type) 145 | assert(arg.name.is_a? Nasl::Identifier) 146 | assert(arg.expr.is_a? Nasl::Lvalue) 147 | assert_equal(arg.expr, call.arg['c']) 148 | end 149 | 150 | def test_mixed_args 151 | tree = parse("foo[a][1]['b'][c+d].e.f.g(a:1, '2', c:three, bar());") 152 | assert_not_nil(tree) 153 | 154 | calls = tree.all(:Call) 155 | assert_not_nil(calls) 156 | assert_equal(2, calls.length) 157 | 158 | call = calls[1] 159 | assert_not_nil(call) 160 | assert_equal(4, call.args.length) 161 | 162 | arg = call.args[0] 163 | assert_equal(:named, arg.type) 164 | assert(arg.name.is_a? Nasl::Identifier) 165 | assert(arg.expr.is_a? Nasl::Integer) 166 | assert_equal(arg.expr, call.arg['a']) 167 | 168 | arg = call.args[1] 169 | assert_equal(:anonymous, arg.type) 170 | assert(arg.expr.is_a? Nasl::String) 171 | 172 | arg = call.args[2] 173 | assert_equal(:named, arg.type) 174 | assert(arg.name.is_a? Nasl::Identifier) 175 | assert(arg.expr.is_a? Nasl::Lvalue) 176 | assert_equal(arg.expr, call.arg['c']) 177 | 178 | arg = call.args[3] 179 | assert_equal(:anonymous, arg.type) 180 | assert(arg.expr.is_a? Nasl::Call) 181 | end 182 | end 183 | -------------------------------------------------------------------------------- /test/unit/parser/test_array.rb: -------------------------------------------------------------------------------- 1 | ################################################################################ 2 | # Copyright (c) 2011-2014, Tenable Network Security 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 TestArray < Test::Unit::TestCase 28 | include Nasl::Test 29 | 30 | def test_empty 31 | tree = parse("foo = {};") 32 | assert_not_nil(tree) 33 | 34 | arrays = tree.all(:Array) 35 | assert_not_nil(arrays) 36 | assert_equal(1, arrays.length) 37 | 38 | array = arrays.first 39 | assert_not_nil(array) 40 | assert_equal(0, array.pairs.length) 41 | end 42 | 43 | def test_integers 44 | tree = parse("foo = {1:02, 3:4, 5:0x6};") 45 | assert_not_nil(tree) 46 | 47 | arrays = tree.all(:Array) 48 | assert_not_nil(arrays) 49 | assert_equal(1, arrays.length) 50 | 51 | array = arrays.first 52 | assert_not_nil(array) 53 | assert_equal(3, array.pairs.length) 54 | 55 | assert_equal(1, array.pairs[0].key.value) 56 | assert_equal(array.keys[1], array.pairs[0].value) 57 | pair = array.pairs[0] 58 | assert(pair.key.is_a? Nasl::Integer) 59 | assert(pair.value.is_a? Nasl::Integer) 60 | assert_equal(2, pair.value.value) 61 | 62 | assert_equal(3, array.pairs[1].key.value) 63 | assert_equal(array.keys[3], array.pairs[1].value) 64 | pair = array.pairs[1] 65 | assert(pair.key.is_a? Nasl::Integer) 66 | assert(pair.value.is_a? Nasl::Integer) 67 | assert_equal(4, pair.value.value) 68 | 69 | assert_equal(5, array.pairs[2].key.value) 70 | assert_equal(array.keys[5], array.pairs[2].value) 71 | pair = array.pairs[2] 72 | assert(pair.key.is_a? Nasl::Integer) 73 | assert(pair.value.is_a? Nasl::Integer) 74 | assert_equal(6, pair.value.value) 75 | end 76 | 77 | def test_string 78 | tree = parse(%q|foo = {'a':"b", "c":'d'};|) 79 | assert_not_nil(tree) 80 | 81 | arrays = tree.all(:Array) 82 | assert_not_nil(arrays) 83 | assert_equal(1, arrays.length) 84 | 85 | array = arrays.first 86 | assert_not_nil(array) 87 | assert_equal(2, array.pairs.length) 88 | 89 | pair = array.pairs[0] 90 | assert(pair.key.is_a? Nasl::String) 91 | assert_equal(:DATA, pair.key.type) 92 | assert_equal('a', pair.key.text) 93 | assert(pair.value.is_a? Nasl::String) 94 | assert_equal(:STRING, pair.value.type) 95 | assert_equal('b', pair.value.text) 96 | assert_equal(pair.value, array.keys['a']) 97 | 98 | pair = array.pairs[1] 99 | assert(pair.key.is_a? Nasl::String) 100 | assert_equal(:STRING, pair.key.type) 101 | assert_equal('c', pair.key.text) 102 | assert(pair.value.is_a? Nasl::String) 103 | assert_equal(:DATA, pair.value.type) 104 | assert_equal('d', pair.value.text) 105 | assert_equal(pair.value, array.keys['c']) 106 | end 107 | 108 | def test_mixed 109 | tree = parse(%q|foo = {'a':1, 2:"b"};|) 110 | assert_not_nil(tree) 111 | 112 | arrays = tree.all(:Array) 113 | assert_not_nil(arrays) 114 | assert_equal(1, arrays.length) 115 | 116 | array = arrays.first 117 | assert_not_nil(array) 118 | assert_equal(2, array.pairs.length) 119 | 120 | pair = array.pairs[0] 121 | assert(pair.key.is_a? Nasl::String) 122 | assert_equal(:DATA, pair.key.type) 123 | assert_equal('a', pair.key.text) 124 | assert(pair.value.is_a? Nasl::Integer) 125 | assert_equal(1, pair.value.value) 126 | assert_equal(pair.value, array.keys['a']) 127 | 128 | pair = array.pairs[1] 129 | assert(pair.key.is_a? Nasl::Integer) 130 | assert_equal(2, pair.key.value) 131 | assert(pair.value.is_a? Nasl::String) 132 | assert_equal(:STRING, pair.value.type) 133 | assert_equal('b', pair.value.text) 134 | assert_equal(pair.value, array.keys[2]) 135 | end 136 | 137 | # A single trailing comma in an array literal is valid, but multiple is not. 138 | def test_single_trailing_comma 139 | tree = parse(%q|foo = {'a':1, 2:"b",};|) 140 | assert_not_nil(tree) 141 | 142 | arrays = tree.all(:Array) 143 | assert_not_nil(arrays) 144 | assert_equal(1, arrays.length) 145 | 146 | array = arrays.first 147 | assert_not_nil(array) 148 | assert_equal(2, array.pairs.length) 149 | 150 | pair = array.pairs[0] 151 | assert(pair.key.is_a? Nasl::String) 152 | assert_equal(:DATA, pair.key.type) 153 | assert_equal('a', pair.key.text) 154 | assert(pair.value.is_a? Nasl::Integer) 155 | assert_equal(1, pair.value.value) 156 | assert_equal(pair.value, array.keys['a']) 157 | 158 | pair = array.pairs[1] 159 | assert(pair.key.is_a? Nasl::Integer) 160 | assert_equal(2, pair.key.value) 161 | assert(pair.value.is_a? Nasl::String) 162 | assert_equal(:STRING, pair.value.type) 163 | assert_equal('b', pair.value.text) 164 | assert_equal(pair.value, array.keys[2]) 165 | end 166 | 167 | def test_multiple_trailing_comma 168 | fail_parse(%q|foo = {'a':1, 2:"b",,};|) 169 | end 170 | 171 | def test_empty_array_with_comma 172 | fail_parse(%q|return {,};|) 173 | end 174 | 175 | def test_array_with_unquoted_key 176 | parse(%q|return {a:@foo, b:"hello", c:3};|) 177 | end 178 | end 179 | --------------------------------------------------------------------------------