brakeman.rb 6.5 KB
Newer Older
J
Justin Collins 已提交
1 2
require 'tempfile'

3 4
class BrakemanTests < Test::Unit::TestCase
  def test_exception_on_no_application
5
    assert_raise Brakeman::NoApplication do
6 7 8 9 10
      Brakeman.run "/tmp#{rand}" #better not exist
    end
  end
end

J
Justin Collins 已提交
11
class UtilTests < Test::Unit::TestCase
12
  def setup
J
Justin Collins 已提交
13
    @ruby_parser = RubyParser
14 15
  end

J
Justin Collins 已提交
16
  def util
J
Justin Collins 已提交
17
    Class.new.extend Brakeman::Util
J
Justin Collins 已提交
18 19 20
  end

  def test_cookies?
21
    assert util.cookies?(@ruby_parser.new.parse 'cookies[:x][:y][:z]')
J
Justin Collins 已提交
22 23 24
  end

  def test_params?
25
    assert util.params?(@ruby_parser.new.parse 'params[:x][:y][:z]')
J
Justin Collins 已提交
26 27
  end
end
J
Justin Collins 已提交
28

29 30
class BaseCheckTests < Test::Unit::TestCase
  FakeTracker = Struct.new(:config)
31
  FakeAppTree = Struct.new(:root)
32 33 34

  def setup
    @tracker = FakeTracker.new
35 36
    app_tree = FakeAppTree.new
    @check = Brakeman::BaseCheck.new app_tree, @tracker
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  end

  def version_between? version, high, low
    @tracker.config = { :rails_version => version }
    @check.send(:version_between?, high, low)
  end

  def test_version_between
    assert version_between?("2.3.8", "2.3.0", "2.3.8")
    assert version_between?("2.3.8", "2.3.0", "2.3.14")
    assert version_between?("2.3.8", "1.0.0", "5.0.0")
  end

  def test_version_not_between
    assert_equal false, version_between?("3.2.1", "2.0.0", "3.0.0")
    assert_equal false, version_between?("3.2.1", "3.0.0", "3.2.0")
    assert_equal false, version_between?("0.0.0", "3.0.0", "3.2.0")
  end

  def test_version_between_longer
    assert_equal false, version_between?("1.0.1.2", "1.0.0", "1.0.1")
  end
59 60 61 62

  def test_version_between_pre_release
    assert version_between?("3.2.9.rc2", "3.2.5", "4.0.0")
  end
63

64
end
J
Justin Collins 已提交
65

66
class ConfigTests < Test::Unit::TestCase
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  
  def setup
    Brakeman.instance_variable_set(:@quiet, false)
  end
  
  # method from test-unit: http://test-unit.rubyforge.org/test-unit/en/Test/Unit/Util/Output.html#capture_output-instance_method
  def capture_output
    require 'stringio'

    output = StringIO.new
    error = StringIO.new
    stdout_save, stderr_save = $stdout, $stderr
    $stdout, $stderr = output, error
    begin
      yield
      [output.string, error.string]
    ensure
      $stdout, $stderr = stdout_save, stderr_save
    end
  end
  
J
Justin Collins 已提交
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
  def test_quiet_option_from_file
    config = Tempfile.new("config")

    config.write <<-YAML.strip
    ---
    :quiet: true
    YAML

    config.close

    options = {
      :config_file => config.path,
      :app_path => "/tmp" #doesn't need to be real
    }

103 104 105 106
    assert_equal "", capture_output {
      final_options = Brakeman.set_options(options)

      config.unlink
J
Justin Collins 已提交
107

108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
      assert final_options[:quiet], "Expected quiet option to be true, but was #{final_options[:quiet]}"
    }[1]
  end
  
  def test_quiet_option_from_commandline
    config = Tempfile.new("config")

    config.write <<-YAML.strip
    ---
    app_path: "/tmp"
    YAML

    config.close

    options = {
      :config_file => config.path,
      :quiet => true,
      :app_path => "/tmp" #doesn't need to be real
    }
    
    assert_equal "", capture_output {
      final_options = Brakeman.set_options(options)
    }[1]
J
Justin Collins 已提交
131 132 133 134 135 136 137 138 139 140 141 142
  end

  def test_quiet_option_default
    options = {
      :app_path => "/tmp" #doesn't need to be real
    }

    final_options = Brakeman.set_options(options)

    assert final_options[:quiet], "Expected quiet option to be true, but was #{final_options[:quiet]}"
  end

143
  def test_quiet_command_line_default
J
Justin Collins 已提交
144
    options = {
145
      :quiet => :command_line,
J
Justin Collins 已提交
146 147 148 149 150 151 152
      :app_path => "/tmp" #doesn't need to be real
    }

    final_options = Brakeman.set_options(options)

    assert_nil final_options[:quiet]
  end
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175

  def test_quiet_inconfig_with_command_line
    config = Tempfile.new("config")

    config.write <<-YAML.strip
    ---
    :quiet: true
    YAML

    config.close

    options = {
      :quiet => :command_line,
      :config_file => config.path,
      :app_path => "#{TEST_PATH}/apps/rails4",
      :run_checks => []
    }

    assert_equal "", capture_output {
      Brakeman.run options
      config.unlink
    }[1]
  end
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
  
  def output_format_tester options, expected_options
    output_formats = Brakeman.get_output_formats(options)
    
    assert_equal expected_options, output_formats
  end
  
  def test_output_format
    output_format_tester({}, [:to_s])
    output_format_tester({:output_format => :html}, [:to_html])
    output_format_tester({:output_format => :to_html}, [:to_html])
    output_format_tester({:output_format => :csv}, [:to_csv])
    output_format_tester({:output_format => :to_csv}, [:to_csv])
    output_format_tester({:output_format => :pdf}, [:to_pdf])
    output_format_tester({:output_format => :to_pdf}, [:to_pdf])
    output_format_tester({:output_format => :json}, [:to_json])
    output_format_tester({:output_format => :to_json}, [:to_json])
    output_format_tester({:output_format => :tabs}, [:to_tabs])
    output_format_tester({:output_format => :to_tabs}, [:to_tabs])
    output_format_tester({:output_format => :others}, [:to_s])
    
    output_format_tester({:output_files => ['xx.html', 'xx.pdf']}, [:to_html, :to_pdf])
    output_format_tester({:output_files => ['xx.pdf', 'xx.json']}, [:to_pdf, :to_json])
    output_format_tester({:output_files => ['xx.json', 'xx.tabs']}, [:to_json, :to_tabs])
    output_format_tester({:output_files => ['xx.tabs', 'xx.csv']}, [:to_tabs, :to_csv])
    output_format_tester({:output_files => ['xx.csv', 'xx.xxx']}, [:to_csv, :to_s])
    output_format_tester({:output_files => ['xx.xx', 'xx.xx']}, [:to_s, :to_s])
    output_format_tester({:output_files => ['xx.html', 'xx.pdf', 'xx.csv', 'xx.tabs', 'xx.json']}, [:to_html, :to_pdf, :to_csv, :to_tabs, :to_json])
  end
205
end
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228

class GemProcessorTests < Test::Unit::TestCase
  FakeTracker = Struct.new(:config)

  def setup 
    @tracker = FakeTracker.new({}) 
    @gem_processor = Brakeman::GemProcessor.new @tracker 
    @eol_representations = ["\r\n", "\n"] 
    @gem_locks = @eol_representations.inject({}) {|h, eol| 
      h[eol] = "    paperclip (3.2.1)#    erubis (4.3.1)#     rails (3.2.1.rc2)#    simplecov (1.1)#".gsub('#', eol); h 
    }
  end 

  def test_get_version 
    @gem_locks.each do |eol, gem_lock|      
      assert_equal "4.3.1", @gem_processor.get_version("erubis", gem_lock), "Couldn't match gemlock with eol: #{eol}}"
      assert_equal "3.2.1", @gem_processor.get_version("paperclip", gem_lock), "Couldn't match gemlock with eol: #{eol}"
      assert_equal "3.2.1.rc2", @gem_processor.get_version("rails", gem_lock), "Couldn't match gemlock with eol: #{eol}"  
      assert_equal "1.1", @gem_processor.get_version("simplecov", gem_lock), "Couldn't match gemlock with eol: #{eol}"
    end 
  end 

end