summaryrefslogtreecommitdiffstats
path: root/test/unit/lib/redmine/ciphering_test.rb
blob: 1981a40078deda4b1e88b88b3e40a6d9dd65d86a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-2023  Jean-Philippe Lang
#
# 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 2
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require File.expand_path('../../../../test_helper', __FILE__)

class Redmine::CipheringTest < ActiveSupport::TestCase
  fixtures :auth_sources

  def test_password_should_be_encrypted
    Redmine::Configuration.with 'database_cipher_key' => 'secret' do
      plaintext_password = "THIS_IS_A_32_BYTES_LONG_PASSWORD"
      r = Repository::Subversion.create!(:password => plaintext_password, :url => 'file:///tmp', :identifier => 'svn')
      assert_equal plaintext_password, r.password
      assert r.read_attribute(:password).match(/\Aaes-256-cbc:.+\Z/)
    end
  end

  def test_password_should_be_clear_with_blank_key
    Redmine::Configuration.with 'database_cipher_key' => '' do
      r = Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'svn')
      assert_equal 'foo', r.password
      assert_equal 'foo', r.read_attribute(:password)
    end
  end

  def test_password_should_be_clear_with_nil_key
    Redmine::Configuration.with 'database_cipher_key' => nil do
      r = Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'svn')
      assert_equal 'foo', r.password
      assert_equal 'foo', r.read_attribute(:password)
    end
  end

  def test_blank_password_should_be_clear
    Redmine::Configuration.with 'database_cipher_key' => 'secret' do
      r = Repository::Subversion.create!(:password => '', :url => 'file:///tmp', :identifier => 'svn')
      assert_equal '', r.password
      assert_equal '', r.read_attribute(:password)
    end
  end

  def test_unciphered_password_should_be_readable
    Redmine::Configuration.with 'database_cipher_key' => nil do
      r = Repository::Subversion.create!(:password => 'clear', :url => 'file:///tmp', :identifier => 'svn')
    end

    Redmine::Configuration.with 'database_cipher_key' => 'secret' do
      r = Repository.order('id DESC').first
      assert_equal 'clear', r.password
    end
  end

  def test_ciphered_password_with_no_cipher_key_configured_should_be_returned_ciphered
    Redmine::Configuration.with 'database_cipher_key' => 'secret' do
      r = Repository::Subversion.create!(:password => 'clear', :url => 'file:///tmp', :identifier => 'svn')
    end

    Redmine::Configuration.with 'database_cipher_key' => '' do
      r = Repository.order('id DESC').first
      # password can not be deciphered
      assert_nothing_raised do
        assert r.password.match(/\Aaes-256-cbc:.+\Z/)
      end
    end
  end

  def test_encrypt_all
    Repository.delete_all
    Redmine::Configuration.with 'database_cipher_key' => nil do
      Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'foo')
      Repository::Subversion.create!(:password => 'bar', :url => 'file:///tmp', :identifier => 'bar')
    end

    Redmine::Configuration.with 'database_cipher_key' => 'secret' do
      assert Repository.encrypt_all(:password)
      r = Repository.order('id DESC').first
      assert_equal 'bar', r.password
      assert r.read_attribute(:password).match(/\Aaes-256-cbc:.+\Z/)
    end
  end

  def test_decrypt_all
    Repository.delete_all
    Redmine::Configuration.with 'database_cipher_key' => 'secret' do
      Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'foo')
      Repository::Subversion.create!(:password => 'bar', :url => 'file:///tmp', :identifier => 'bar')

      assert Repository.decrypt_all(:password)
      r = Repository.order('id DESC').first
      assert_equal 'bar', r.password
      assert_equal 'bar', r.read_attribute(:password)
    end
  end

  def test_encrypt_all_and_decrypt_all_should_skip_validation
    auth_source = auth_sources(:auth_sources_001)
    # validator checks if AuthSource#host is present
    auth_source.update_column(:host, nil)
    assert AuthSource.encrypt_all(:account_password)
    assert AuthSource.decrypt_all(:account_password)
  end
end