You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ciphering_test.rb 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2022 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../../../test_helper', __FILE__)
  19. class Redmine::CipheringTest < ActiveSupport::TestCase
  20. fixtures :auth_sources
  21. def test_password_should_be_encrypted
  22. Redmine::Configuration.with 'database_cipher_key' => 'secret' do
  23. plaintext_password = "THIS_IS_A_32_BYTES_LONG_PASSWORD"
  24. r = Repository::Subversion.create!(:password => plaintext_password, :url => 'file:///tmp', :identifier => 'svn')
  25. assert_equal plaintext_password, r.password
  26. assert r.read_attribute(:password).match(/\Aaes-256-cbc:.+\Z/)
  27. end
  28. end
  29. def test_password_should_be_clear_with_blank_key
  30. Redmine::Configuration.with 'database_cipher_key' => '' do
  31. r = Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'svn')
  32. assert_equal 'foo', r.password
  33. assert_equal 'foo', r.read_attribute(:password)
  34. end
  35. end
  36. def test_password_should_be_clear_with_nil_key
  37. Redmine::Configuration.with 'database_cipher_key' => nil do
  38. r = Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'svn')
  39. assert_equal 'foo', r.password
  40. assert_equal 'foo', r.read_attribute(:password)
  41. end
  42. end
  43. def test_blank_password_should_be_clear
  44. Redmine::Configuration.with 'database_cipher_key' => 'secret' do
  45. r = Repository::Subversion.create!(:password => '', :url => 'file:///tmp', :identifier => 'svn')
  46. assert_equal '', r.password
  47. assert_equal '', r.read_attribute(:password)
  48. end
  49. end
  50. def test_unciphered_password_should_be_readable
  51. Redmine::Configuration.with 'database_cipher_key' => nil do
  52. r = Repository::Subversion.create!(:password => 'clear', :url => 'file:///tmp', :identifier => 'svn')
  53. end
  54. Redmine::Configuration.with 'database_cipher_key' => 'secret' do
  55. r = Repository.order('id DESC').first
  56. assert_equal 'clear', r.password
  57. end
  58. end
  59. def test_ciphered_password_with_no_cipher_key_configured_should_be_returned_ciphered
  60. Redmine::Configuration.with 'database_cipher_key' => 'secret' do
  61. r = Repository::Subversion.create!(:password => 'clear', :url => 'file:///tmp', :identifier => 'svn')
  62. end
  63. Redmine::Configuration.with 'database_cipher_key' => '' do
  64. r = Repository.order('id DESC').first
  65. # password can not be deciphered
  66. assert_nothing_raised do
  67. assert r.password.match(/\Aaes-256-cbc:.+\Z/)
  68. end
  69. end
  70. end
  71. def test_encrypt_all
  72. Repository.delete_all
  73. Redmine::Configuration.with 'database_cipher_key' => nil do
  74. Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'foo')
  75. Repository::Subversion.create!(:password => 'bar', :url => 'file:///tmp', :identifier => 'bar')
  76. end
  77. Redmine::Configuration.with 'database_cipher_key' => 'secret' do
  78. assert Repository.encrypt_all(:password)
  79. r = Repository.order('id DESC').first
  80. assert_equal 'bar', r.password
  81. assert r.read_attribute(:password).match(/\Aaes-256-cbc:.+\Z/)
  82. end
  83. end
  84. def test_decrypt_all
  85. Repository.delete_all
  86. Redmine::Configuration.with 'database_cipher_key' => 'secret' do
  87. Repository::Subversion.create!(:password => 'foo', :url => 'file:///tmp', :identifier => 'foo')
  88. Repository::Subversion.create!(:password => 'bar', :url => 'file:///tmp', :identifier => 'bar')
  89. assert Repository.decrypt_all(:password)
  90. r = Repository.order('id DESC').first
  91. assert_equal 'bar', r.password
  92. assert_equal 'bar', r.read_attribute(:password)
  93. end
  94. end
  95. def test_encrypt_all_and_decrypt_all_should_skip_validation
  96. auth_source = auth_sources(:auth_sources_001)
  97. # validator checks if AuthSource#host is present
  98. auth_source.update_column(:host, nil)
  99. assert AuthSource.encrypt_all(:account_password)
  100. assert AuthSource.decrypt_all(:account_password)
  101. end
  102. end