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.2KB

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