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
|
require 'spec_helper'
describe Cleanroom do
let(:klass) do
Class.new do
NULL = Object.new.freeze unless defined?(NULL)
include Cleanroom
def method_1(val = NULL)
if val.equal?(NULL)
@method_1
else
@method_1 = val
end
end
expose :method_1
def method_2(val = NULL)
if val.equal?(NULL)
@method_2
else
@method_2 = val
end
end
expose :method_2
def method_3
@method_3 = true
end
end
end
let(:instance) { klass.new }
describe '#evaluate_file' do
let(:path) { tmp_path('file.rb') }
before do
File.open(path, 'w') do |f|
f.write <<-EOH.gsub(/^ {10}/, '')
method_1 'hello'
method_2 false
EOH
end
end
it 'evaluates the file' do
instance.evaluate_file(path)
expect(instance.method_1).to eq('hello')
expect(instance.method_2).to be(false)
end
end
describe '#evaluate' do
let(:contents) do
<<-EOH.gsub(/^ {8}/, '')
method_1 'hello'
method_2 false
EOH
end
it 'evaluates the file' do
instance.evaluate(contents)
expect(instance.method_1).to eq('hello')
expect(instance.method_2).to be(false)
end
end
describe 'security' do
it 'restricts access to __instance__' do
expect {
instance.evaluate("__instance__")
}.to raise_error(Cleanroom::InaccessibleError)
end
it 'restricts access to __instance__ using :send' do
expect {
instance.evaluate("send(:__instance__)")
}.to raise_error(Cleanroom::InaccessibleError)
end
it 'restricts access to defining new methods' do
expect {
instance.evaluate <<-EOH.gsub(/^ {12}/, '')
self.class.class_eval do
def new_method
__instance__.method_3
end
end
EOH
}.to raise_error(Cleanroom::InaccessibleError)
expect(instance.instance_variables).to_not include(:@method_3)
end
end
end
|