cirandas.net

ref: master

vendor/ezcrypto/test/active_crypto_test.rb


  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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
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
205
206
207
208
209
210
211
$:.unshift(File.dirname(__FILE__) + "/../lib/")
require_relative 'test_helper'
require 'test/unit'
require 'active_crypto'


class User < ActiveRecord::Base
	has_many :secrets
	has_many :groups
	keyholder
end

class Secret < ActiveRecord::Base
	encrypt :name,:email, :key=>:user, :base64=>true
	belongs_to :user
	has_many :children
end

class Child < ActiveRecord::Base
	encrypt :email, :key=>:secret, :base64=>true
	belongs_to :secret
end

class Asset<ActiveRecord::Base
  encrypt :title, :base64=>true
  has_many :caps,:dependent=>:destroy

  def self.create(title,email)
    asset=Asset.new
    asset.set_session_key(EzCrypto::Key.generate)
    asset.title=title
    if asset.save
      asset.share(email)
    else
      nil
    end
  end

  def share(email=nil)
    Cap.create_for_asset(self,email)
  end

end

class AssetRaw<ActiveRecord::Base
  self.table_name = "assets"
end

class Cap < ActiveRecord::Base
  belongs_to :asset
  encrypt :shared_key, :base64=>true

  def self.find_by_key(cap_key)
    cap_key.chop
    hash=Digest::SHA1.hexdigest(cap_key)
    if (cap_key.length>=20) # Sanity check
      cap=self.find_by_key_hash(hash)
      if cap
        cap.set_encoded_key(cap_key)
        cap.asset.set_encoded_key(cap.shared_key)
        cap
      end
    else
      nil
    end
  end

  def self.create_for_asset(asset,email=nil)
    cap=Cap.new
    cap.email=email if email
    cap.asset=asset
    if cap.save
      cap.set_session_key(EzCrypto::Key.generate)
      cap_key=cap.session_key.encode
      cap.key_hash=Digest::SHA1.hexdigest(cap_key)
      cap.shared_key=asset.session_key.encode
      cap.save
      cap_key
    else
      nil
    end
  end

end

class Group < ActiveRecord::Base
	belongs_to :user
	has_many :group_secrets

	encrypt :name,:group_key, :key=>:user	, :base64=>true
end

class GroupSecret < ActiveRecord::Base
	belongs_to :group

	encrypt :title,:body, :key=>:group, :base64=>true

end

class ActiveCryptoTest < Test::Unit::TestCase

  def setup
  end

  def test_key_holder_in_record
    user=User.new
    user.name="bob"
    user.save
  	assert user.kind_of?(ActiveCrypto::KeyHolder)
  	assert user.kind_of?(ActiveRecord::Base)
  	assert user.kind_of?(User)
    assert_nil user.session_key
  	user.enter_password "shhcccc"
  	assert_not_nil user.session_key
  	assert_not_nil user.session_key.encrypt("test")
  end

  def test_encrypted_child
    user=User.new
    user.save
    assert_nil user.session_key
  	user.enter_password "shhcccc"
  	assert_not_nil user.session_key
  	assert user.kind_of?(ActiveCrypto::KeyHolder)
  	assert user.kind_of?(ActiveRecord::Base)
  	assert user.kind_of?(User)

  	jill=user.secrets.create

  	assert_not_nil jill
  	assert jill.kind_of?(ActiveCrypto::AssociationKeyHolder)
  	assert jill.kind_of?(ActiveCrypto::KeyHolder)
  	assert jill.kind_of?(ActiveCrypto::Encrypted)
  	assert jill.kind_of?(ActiveRecord::Base)
  	assert jill.kind_of?(Secret)

  	assert jill.respond_to?(:session_key)

    assert_not_nil jill.user
    assert_not_nil jill.user.session_key


    assert_not_nil jill.session_key
    assert_equal user.session_key,jill.session_key

    jill.name="jill"
    jill.save


    assert_equal "jill",jill.name

    jill=user.secrets.first
    assert_not_nil jill.session_key
    assert_equal user.session_key,jill.session_key
    assert_equal "jill",jill.name

    child=jill.children.create
    child.email="pelle@neubia.com"
    child.save

    assert_not_nil child.secret
    assert_not_nil child.secret.session_key


    assert_not_nil child.session_key
    assert_equal user.session_key,child.session_key

    assert_equal "pelle@neubia.com",child.email

    child=jill.children.first

    assert_not_nil child.secret
    assert_not_nil child.secret.session_key


    assert_not_nil child.session_key
    assert_equal user.session_key,child.session_key

    assert_equal "pelle@neubia.com",child.email

  end

  def test_caps
    key=Asset.create("title","pelle@neubia.com")
    assert_not_nil key
    cap=Cap.find_by_key key
    assert_not_nil cap
    assert_not_nil cap.asset

    assert_equal "title",cap.asset.title
    assert_equal "title",cap.asset["title"]
    assert_equal "pelle@neubia.com",cap.email
    assert_equal "pelle@neubia.com",cap["email"]

    # Non decrypting version
    raw=AssetRaw.find cap.asset.id
    assert_not_equal "title",raw.title
    assert_not_equal "title",raw["title"]

    bob_key=cap.asset.share("bob@bob.com")
    bob_cap=Cap.find_by_key bob_key

    assert_not_equal key,bob_key
    assert_not_nil bob_cap
    assert_not_nil bob_cap.asset
    assert_equal "title",bob_cap.asset.title
    assert_equal "bob@bob.com",bob_cap.email
  end
end