def log line STDOUT.sync = true puts line end def is_windows? RUBY_PLATFORM=~/(win|w)32$/ end def config_file_name(name) if(is_windows?) name = "#{name}-win" end "config#{dir_separator}#{name}.cfg" end def path_separator is_windows? ? ";" : ":" end def dir_separator is_windows? ? "\\" : "/" end def prepare_environment ENV['CLASSPATH'] = "../jdigidoc/target/classes#{path_separator}../jdigidoc/target/test-classes#{path_separator}../jdigidoc/target/dependency/*" @tools = {} @tools[:jdigidoc] = "java ee.sk.test.jdigidoc -config #{config_file_name('jdigidoc')}" @tools[:cdigidoc] = "cdigidoc -config #{config_file_name('cdigidoc')}" end def create_large_file(sizeInMB) # todo: create if does not exist `dd if=/dev/urandom of=sample_data/#{@file} bs=#{1024*1024} count=#{sizeInMB}` end def exec(tool, args) if tool == :cdigidoc args.gsub!("-ddoc-validate", "-verify") args.gsub!("-ddoc-", "-") end cmd = "#{@tools[tool]} #{args}" log "Command: '#{cmd}' was executed." output = `#{cmd} 2>&1` $output += "# #{cmd}\n#{output}\n\n" output end def get_zipping_time start_time = Time.new system "zip sample_data/#{@file} sample_data/#{@file}" time_spent = Time.now - start_time system "rm -f sample_data/#{@file}.zip" time_spent end def create(tool, format) @ddoc = "sample_data/#{@file}.#{ format =~ /BDOC/ ? 'bdoc' : 'ddoc'}" File.delete @ddoc rescue nil exec(tool, "-ddoc-new #{format} -ddoc-out #{@ddoc}") end def diff(file1, file2) `diff \"#{file1.gsub("`", "\\\\`")}\" \"#{file2.gsub("`", "\\\\`")}\"`.should == "" should fail if $?.exitstatus != 0 end def add(tool, file) exec(tool, "-ddoc-in #{@ddoc} -ddoc-add sample_data/#{file} text/plain -ddoc-out #{@ddoc}") end def sign(tool) exec(tool, "-ddoc-in #{@ddoc} -ddoc-sign 01497 -ddoc-out #{@ddoc}") end def verify(tool) result = exec(tool, "-ddoc-in #{@ddoc} -ddoc-validate") if tool == :jdigidoc result.should =~ /Validation --> OK/ # result.should =~ /^ --> OK$/ else result.split($/)[1].should =~ /\|No errors$/ end end def extract(tool) File.delete @file rescue nil exec(tool, "-ddoc-in #{@ddoc} -ddoc-extract D0 #{@file}").should_not =~ /ERROR/ `diff sample_data/#{@file}.original #{@file}`.should == "" end def sign_and_verify(sign_tool, verify_tool, format) create(sign_tool, format) add(sign_tool, @file) sign(sign_tool) verify(verify_tool) extract(verify_tool) end def sign_for_format(sign_tool, format) create(sign_tool, format) add(sign_tool, @file) sign(sign_tool) end def add_sign_and_verify(sign_tool, verify_tool, format) create(verify_tool, format) add(verify_tool, @file) add(sign_tool, "žämpõl.txt") sign(sign_tool) verify(verify_tool) extract(sign_tool) end def encrypt(tool) File.delete "sample_data/#{@file}.cdoc" rescue nil files = "sample_data/#{@file} sample_data/#{@file}.cdoc" args = tool == :jdigidoc ? "-cdoc-recipient cert.pem -cdoc-encrypt #{files}" : "-encrecv cert.pem -encrypt-file #{files} text/plain" exec(tool, args) end def decrypt_and_verify(tool) File.delete "sample_data/#{@file}.decrypted" rescue nil args = tool == :jdigidoc ? "-cdoc-in sample_data/#{@file}.cdoc -cdoc-decrypt 0090 sample_data/#{@file}.decrypted" : "-decrypt-file sample_data/#{@file}.cdoc sample_data/#{@file}.decrypted 0090" exec(tool, args) log `ls -l sample_data/` diff("sample_data/#{@file}", "sample_data/#{@file}.decrypted") end