source: dispositivos_moviles/jdigidoc-3.7/jdigidoc/tests/helper/helper_methods.rb @ 16fd839

Last change on this file since 16fd839 was 62396ee, checked in by Antonio Araujo Brett <aaraujo@…>, 11 years ago

Códigos fuentes de la biblioteca jdigidoc usada en Android.

  • Property mode set to 100644
File size: 3.4 KB
Line 
1
2def log line
3  STDOUT.sync = true
4  puts line
5end
6
7def is_windows?
8   RUBY_PLATFORM=~/(win|w)32$/
9end
10
11def config_file_name(name)
12  if(is_windows?)
13    name = "#{name}-win"
14  end
15  "config#{dir_separator}#{name}.cfg"
16end
17
18def path_separator
19  is_windows? ? ";" : ":"
20end
21
22def dir_separator
23  is_windows? ? "\\" : "/"
24end
25
26def 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')}"
31end
32
33def 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}`
36end
37
38def 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
48end
49
50def 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
56end
57
58def 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}")
62end
63
64def diff(file1, file2)
65  `diff \"#{file1.gsub("`",  "\\\\`")}\" \"#{file2.gsub("`",  "\\\\`")}\"`.should == ""
66  should fail if $?.exitstatus != 0
67end
68
69def add(tool, file)
70  exec(tool, "-ddoc-in #{@ddoc} -ddoc-add sample_data/#{file} text/plain -ddoc-out #{@ddoc}")
71end
72
73def sign(tool)
74  exec(tool, "-ddoc-in #{@ddoc} -ddoc-sign 01497 -ddoc-out #{@ddoc}")
75end
76
77def 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
85end
86
87def 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 == ""
91end
92
93def 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)
99end
100
101def sign_for_format(sign_tool, format)
102  create(sign_tool, format)
103  add(sign_tool, @file)
104  sign(sign_tool)
105end
106
107def 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)
114end
115
116def 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)
121end
122
123def 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")
129end
Note: See TracBrowser for help on using the repository browser.