1
0

test_simply.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. from nose.tools import assert_equals, assert_true
  2. from tests.asserts import assert_in
  3. import os
  4. from os import path
  5. import json
  6. import gixy
  7. from ..utils import *
  8. from gixy.core.manager import Manager as Gixy
  9. from gixy.core.plugins_manager import PluginsManager
  10. from gixy.core.config import Config
  11. def setup_module():
  12. pass
  13. def teardown_module():
  14. pass
  15. def test_from_config():
  16. tested_plugins = set()
  17. tested_fp_plugins = set()
  18. conf_dir = path.join(path.dirname(__file__), 'simply')
  19. for plugin in os.listdir(conf_dir):
  20. if plugin in ('.', '..'):
  21. continue
  22. plugin_path = path.join(conf_dir, plugin)
  23. if not path.isdir(plugin_path):
  24. continue
  25. config = {}
  26. if path.exists(path.join(plugin_path, 'config.json')):
  27. with open(path.join(plugin_path, 'config.json'), 'r') as file:
  28. config = json.loads(file.read())
  29. for test_case in os.listdir(plugin_path):
  30. if not test_case.endswith('.conf'):
  31. continue
  32. config_path = path.join(plugin_path, test_case)
  33. if not test_case.endswith('_fp.conf'):
  34. # Not False Positive test
  35. tested_plugins.add(plugin)
  36. test_func = check_configuration
  37. else:
  38. tested_fp_plugins.add(plugin)
  39. test_func = check_configuration_fp
  40. yield test_func, plugin, config_path, config
  41. manager = PluginsManager()
  42. for plugin in manager.plugins:
  43. plugin = plugin.name
  44. assert_true(plugin in tested_plugins,
  45. 'Plugin {name!r} should have at least one simple test config'.format(name=plugin))
  46. assert_true(plugin in tested_fp_plugins,
  47. 'Plugin {name!r} should have at least one simple test config with false positive'.format(name=plugin))
  48. def parse_plugin_options(config_path):
  49. with open(config_path, 'r') as f:
  50. config_line = f.readline()
  51. if config_line.startswith('# Options: '):
  52. return json.loads(config_line[10:])
  53. return None
  54. def yoda_provider(plugin, plugin_options=None):
  55. config = Config(
  56. allow_includes=False,
  57. plugins=[plugin]
  58. )
  59. if plugin_options:
  60. config.set_for(plugin, plugin_options)
  61. return Gixy(config=config)
  62. def check_configuration(plugin, config_path, test_config):
  63. plugin_options = parse_plugin_options(config_path)
  64. with yoda_provider(plugin, plugin_options) as yoda:
  65. yoda.audit(config_path, open(config_path, mode='r'))
  66. results = RawFormatter().format(yoda)
  67. assert_equals(len(results), 1, 'Should have one report')
  68. result = results[0]
  69. if 'severity' in test_config:
  70. if not hasattr(test_config['severity'], '__iter__'):
  71. assert_equals(result['severity'], test_config['severity'])
  72. else:
  73. assert_in(result['severity'], test_config['severity'])
  74. assert_equals(result['plugin'], plugin)
  75. assert_true(result['summary'])
  76. assert_true(result['description'])
  77. assert_true(result['config'])
  78. assert_true(result['help_url'].startswith('https://'),
  79. 'help_url must starts with https://. It\'is URL!')
  80. def check_configuration_fp(plugin, config_path, test_config):
  81. with yoda_provider(plugin) as yoda:
  82. yoda.audit(config_path, open(config_path, mode='r'))
  83. results = RawFormatter().format(yoda)
  84. assert_equals(len(results), 0,
  85. 'False positive configuration must not trigger any plugins')