[62396ee] | 1 | |
---|
| 2 | def log line |
---|
| 3 | STDOUT.sync = true |
---|
| 4 | puts line |
---|
| 5 | end |
---|
| 6 | |
---|
| 7 | def is_windows? |
---|
| 8 | RUBY_PLATFORM=~/(win|w)32$/ |
---|
| 9 | end |
---|
| 10 | |
---|
| 11 | def config_file_name(name) |
---|
| 12 | if(is_windows?) |
---|
| 13 | name = "#{name}-win" |
---|
| 14 | end |
---|
| 15 | "config#{dir_separator}#{name}.cfg" |
---|
| 16 | end |
---|
| 17 | |
---|
| 18 | def path_separator |
---|
| 19 | is_windows? ? ";" : ":" |
---|
| 20 | end |
---|
| 21 | |
---|
| 22 | def dir_separator |
---|
| 23 | is_windows? ? "\\" : "/" |
---|
| 24 | end |
---|
| 25 | |
---|
| 26 | def prepare_environment |
---|
| 27 | ENV['CLASSPATH'] = "../jdigidoc/target/classes#{path_separator}../jdigidoc/target/test-classes#{path_separator}../jdigidoc/target/dependency/*" |
---|
| 28 | @tools = {} |
---|
| 29 | @tools[:jdigidoc] = "java ee.sk.test.jdigidoc -config #{config_file_name('jdigidoc')}" |
---|
| 30 | @tools[:cdigidoc] = "cdigidoc -config #{config_file_name('cdigidoc')}" |
---|
| 31 | end |
---|
| 32 | |
---|
| 33 | def create_large_file(sizeInMB) |
---|
| 34 | # todo: create if does not exist |
---|
| 35 | `dd if=/dev/urandom of=sample_data/#{@file} bs=#{1024*1024} count=#{sizeInMB}` |
---|
| 36 | end |
---|
| 37 | |
---|
| 38 | def exec(tool, args) |
---|
| 39 | if tool == :cdigidoc |
---|
| 40 | args.gsub!("-ddoc-validate", "-verify") |
---|
| 41 | args.gsub!("-ddoc-", "-") |
---|
| 42 | end |
---|
| 43 | cmd = "#{@tools[tool]} #{args}" |
---|
| 44 | log "Command: '#{cmd}' was executed." |
---|
| 45 | output = `#{cmd} 2>&1` |
---|
| 46 | $output += "# #{cmd}\n#{output}\n\n" |
---|
| 47 | output |
---|
| 48 | end |
---|
| 49 | |
---|
| 50 | def get_zipping_time |
---|
| 51 | start_time = Time.new |
---|
| 52 | system "zip sample_data/#{@file} sample_data/#{@file}" |
---|
| 53 | time_spent = Time.now - start_time |
---|
| 54 | system "rm -f sample_data/#{@file}.zip" |
---|
| 55 | time_spent |
---|
| 56 | end |
---|
| 57 | |
---|
| 58 | def create(tool, format) |
---|
| 59 | @ddoc = "sample_data/#{@file}.#{ format =~ /BDOC/ ? 'bdoc' : 'ddoc'}" |
---|
| 60 | File.delete @ddoc rescue nil |
---|
| 61 | exec(tool, "-ddoc-new #{format} -ddoc-out #{@ddoc}") |
---|
| 62 | end |
---|
| 63 | |
---|
| 64 | def diff(file1, file2) |
---|
| 65 | `diff \"#{file1.gsub("`", "\\\\`")}\" \"#{file2.gsub("`", "\\\\`")}\"`.should == "" |
---|
| 66 | should fail if $?.exitstatus != 0 |
---|
| 67 | end |
---|
| 68 | |
---|
| 69 | def add(tool, file) |
---|
| 70 | exec(tool, "-ddoc-in #{@ddoc} -ddoc-add sample_data/#{file} text/plain -ddoc-out #{@ddoc}") |
---|
| 71 | end |
---|
| 72 | |
---|
| 73 | def sign(tool) |
---|
| 74 | exec(tool, "-ddoc-in #{@ddoc} -ddoc-sign 01497 -ddoc-out #{@ddoc}") |
---|
| 75 | end |
---|
| 76 | |
---|
| 77 | def verify(tool) |
---|
| 78 | result = exec(tool, "-ddoc-in #{@ddoc} -ddoc-validate") |
---|
| 79 | if tool == :jdigidoc |
---|
| 80 | result.should =~ /Validation --> OK/ |
---|
| 81 | # result.should =~ /^ --> OK$/ |
---|
| 82 | else |
---|
| 83 | result.split($/)[1].should =~ /\|No errors$/ |
---|
| 84 | end |
---|
| 85 | end |
---|
| 86 | |
---|
| 87 | def extract(tool) |
---|
| 88 | File.delete @file rescue nil |
---|
| 89 | exec(tool, "-ddoc-in #{@ddoc} -ddoc-extract D0 #{@file}").should_not =~ /ERROR/ |
---|
| 90 | `diff sample_data/#{@file}.original #{@file}`.should == "" |
---|
| 91 | end |
---|
| 92 | |
---|
| 93 | def sign_and_verify(sign_tool, verify_tool, format) |
---|
| 94 | create(sign_tool, format) |
---|
| 95 | add(sign_tool, @file) |
---|
| 96 | sign(sign_tool) |
---|
| 97 | verify(verify_tool) |
---|
| 98 | extract(verify_tool) |
---|
| 99 | end |
---|
| 100 | |
---|
| 101 | def sign_for_format(sign_tool, format) |
---|
| 102 | create(sign_tool, format) |
---|
| 103 | add(sign_tool, @file) |
---|
| 104 | sign(sign_tool) |
---|
| 105 | end |
---|
| 106 | |
---|
| 107 | def add_sign_and_verify(sign_tool, verify_tool, format) |
---|
| 108 | create(verify_tool, format) |
---|
| 109 | add(verify_tool, @file) |
---|
| 110 | add(sign_tool, "žämpõl.txt") |
---|
| 111 | sign(sign_tool) |
---|
| 112 | verify(verify_tool) |
---|
| 113 | extract(sign_tool) |
---|
| 114 | end |
---|
| 115 | |
---|
| 116 | def encrypt(tool) |
---|
| 117 | File.delete "sample_data/#{@file}.cdoc" rescue nil |
---|
| 118 | files = "sample_data/#{@file} sample_data/#{@file}.cdoc" |
---|
| 119 | args = tool == :jdigidoc ? "-cdoc-recipient cert.pem -cdoc-encrypt #{files}" : "-encrecv cert.pem -encrypt-file #{files} text/plain" |
---|
| 120 | exec(tool, args) |
---|
| 121 | end |
---|
| 122 | |
---|
| 123 | def decrypt_and_verify(tool) |
---|
| 124 | File.delete "sample_data/#{@file}.decrypted" rescue nil |
---|
| 125 | 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" |
---|
| 126 | exec(tool, args) |
---|
| 127 | log `ls -l sample_data/` |
---|
| 128 | diff("sample_data/#{@file}", "sample_data/#{@file}.decrypted") |
---|
| 129 | end |
---|