# Copyright (C) 2022 Greenbone AG
#
# SPDX-License-Identifier: GPL-3.0-or-later
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from pathlib import Path

from troubadix.plugin import LinterError
from troubadix.plugins.grammar import CheckGrammar

from . import PluginTestCase


class CheckNewlinesTestCase(PluginTestCase):
    def test_ok(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"summary", value:"Foo Bar.");\n'
            '  script_tag(name:"vuldetect", value:"Sends multiple HTTP GET '
            'requests and checks the responses.");\n'
            '  script_tag(name:"insight", value:"Determine which file systems '
            "do not need to be supported based on the actual scenario and "
            "disable mounting for these file systems through "
            'configuration.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
        )
        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 0)

    def test_grammar(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"summary", value:"Foo Bar.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
            "# is prone to a security bypass vulnerabilities\n"
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: is prone to a security bypass vulnerabilities\n"
            "- Full line: # is prone to a security bypass vulnerabilities",
            results[0].message,
        )

    def test_grammar2(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"summary", value:"Foo Bar.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
            "# is prone to a security bypass vulnerabilities\n"
            "# refer the Reference\n"
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 2)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: is prone to a security bypass vulnerabilities\n"
            "- Full line: # is prone to a security bypass vulnerabilities",
            results[0].message,
        )

        self.assertIsInstance(results[1], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: refer the Reference\n"
            "- Full line: # refer the Reference",
            results[1].message,
        )

    def test_grammar3(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"summary", value:"Foo Bar.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
            '  script_tag(name:"summary", value:"Adobe Digital Edition is '
            'prone a to denial of service (DoS) vulnerability.");\n'
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: is prone a\n"
            '- Full line: script_tag(name:"summary", value:"Adobe Digital '
            'Edition is prone a to denial of service (DoS) vulnerability.");',
            results[0].message,
        )

    def test_grammar4(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"summary", value:"Foo Bar.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
            '  script_tag(name:"summary", value:"Splunk Enterprise is prone an '
            'open redirect vulnerability.");\n'
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: is prone an\n"
            '- Full line: script_tag(name:"summary", value:"Splunk Enterprise '
            'is prone an open redirect vulnerability.");',
            results[0].message,
        )

    def test_grammar5(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"summary", value:"Foo Bar.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
            '  script_tag(name:"vuldetect", value:"Sends multiple HTTP GET '
            'request and checks the responses.");\n'
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: multiple HTTP GET request\n"
            '- Full line: script_tag(name:"vuldetect", value:"Sends multiple '
            'HTTP GET request and checks the responses.");',
            results[0].message,
        )

    def test_grammar6(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"summary", value:"Foo Bar is prone to multiple '
            'unknown vulnerability.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: multiple unknown vulnerability.\n"
            '- Full line: script_tag(name:"summary", value:"Foo Bar is prone '
            'to multiple unknown vulnerability.");',
            results[0].message,
        )

    def test_grammar7(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"summary", value:"Foo Bar is prone to a to a '
            'remote denial-of-service vulnerability.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: to a to a\n"
            '- Full line: script_tag(name:"summary", value:"Foo Bar is prone '
            'to a to a remote denial-of-service vulnerability.");',
            results[0].message,
        )

    def test_grammar8(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"insight", value:"- CVE-2022-31702: Command '
            'injection in the in the vRNI REST API.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: in the in the\n"
            '- Full line: script_tag(name:"insight", value:"- CVE-2022-31702: '
            'Command injection in the in the vRNI REST API.");',
            results[0].message,
        )

    def test_grammar9(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"solution", value:"Update to version to version '
            '1.2.3 or later.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: to version to version\n"
            '- Full line: script_tag(name:"solution", value:"Update to version '
            'to version 1.2.3 or later.");',
            results[0].message,
        )

    def test_grammar10(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"impact", value:"Successful exploitation may '
            "allows an attacker to run arbitrary code on the affected IP "
            'cameras.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: may allows\n"
            '- Full line: script_tag(name:"impact", value:"Successful '
            "exploitation may allows an attacker to run arbitrary code on the "
            'affected IP cameras.");',
            results[0].message,
        )

    def test_grammar11(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"impact", value:"Inadequate checks in '
            "com_contact could allowed mail submission\n"
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: could allowed\n"
            '- Full line: script_tag(name:"impact", value:"Inadequate checks '
            "in com_contact could allowed mail submission",
            results[0].message,
        )

    def test_grammar12(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"impact", value:"This allow an attacker to gain '
            "administrative access to the\n"
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
        )

        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 1)
        self.assertIsInstance(results[0], LinterError)
        self.assertEqual(
            "VT/Include has the following grammar problem:\n"
            "- Hit: This allow\n"
            '- Full line: script_tag(name:"impact", value:"This allow an '
            "attacker to gain administrative access to the",
            results[0].message,
        )

    def test_grammar_fp(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"summary", value:"Foo Bar.'
            ' a multiple keyboard .");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
        )
        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 0)

    def test_grammar_fp1(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"summary", value:"Foo Bar is prone to '
            ' multiple cross-site request forgery (CSRF) vulnerabilities.");\n'
            '  script_tag(name:"insight", value:"A Cross Site Request '
            ' Forgery flaw exists.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
        )
        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 0)

    def test_grammar_fp2(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"insight", value:"Nadav Markus and Or Cohen of '
            "Palo Alto Networks discovered\n"
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
        )
        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 0)

    def test_grammar_fp3(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"insight", value:"*snip* connection string to '
            'provide\nproperties that are not on this allow list.");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
        )
        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 0)

    def test_grammar_fp4(self):
        nasl_file = Path(__file__).parent / "test.nasl"
        content = (
            '  script_tag(name:"cvss_base", value:"4.0");\n'
            '  script_tag(name:"insight", value:"*snip*\n'
            "  control of a remote NFS server to create a setuid root "
            "executable on\n  the exported filesystem of the remote NFS "
            "server.  If this filesystem\n  was mounted with the default "
            'hosts map, it would allow the user to\n  *snip*");\n'
            '  script_tag(name:"solution_type", value:"VendorFix");\n'
            '  script_tag(name:"solution", value:"meh");\n'
        )
        fake_context = self.create_file_plugin_context(nasl_file=nasl_file, file_content=content)
        plugin = CheckGrammar(fake_context)

        results = list(plugin.run())

        self.assertEqual(len(results), 0)
