4 This file is part of the AVR-Crypto-Lib.
5 Copyright (C) 2008, 2009 Daniel Otte (daniel.otte@rub.de)
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 $conffile_check = Hash.new
29 $conffile_check.default = 0
31 def readconfigfile(fname, conf)
32 return conf if $conffile_check[fname]==1
33 $conffile_check[fname]=1
35 if not File.exists?(fname)
38 file = File.open(fname, "r")
41 next if /[\s]*#/.match(line)
42 if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
47 next if not /=/.match(line)
48 m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
50 Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
52 conf[section][m[1]] = m[2]
68 $sp.print("shavs_list\r")
71 return algos if /^>$/.match(line)
72 if m = /[\*\ ]([a-z]):[\s]*([a-zA-Z0-9+_-]+)/.match(line)
78 def init_system(algo_select)
79 $sp.print("echo off \r")
80 print("DBG i: " + "echo off \r"+"\n") if $debug
82 $sp.print("shavs_set #{algo_select}\r")
83 print("DBG i: " + "shavs_set #{$algo_select} \r"+"\n") # if $debug
85 $sp.print("shavs_test1 \r")
86 print("DBG i: " + "shavs_test1 \r"+"\n") if $debug
89 end while not m=/buffer_size[\s]*=[\s]*0x([0-9A-Fa-f]*)/.match(line)
90 $buffer_size = m[1].to_i(16)
96 line = "" if line==nil
97 puts("DBG got: "+line) if $debug
98 end while not /[\s]*MD[\s]*=.*/.match(line)
102 def send_md(md_string)
104 for i in 0..md_string.length-1
105 $sp.print(md_string[i].chr)
106 # print("DBG s: "+ md_string[i].chr) if $debug
108 if((i%($buffer_size*2)==0)&&(i!=0))
111 end while not /\./.match(line)
116 def run_test(filename, skip=0)
119 if not File.exist?(filename)
120 puts("ERROR file "+filename+" does not exist!")
124 file = File.new(filename, "r");
128 end while not (file.eof or (/[\s]*Len[\s]*=.*/.match(lb)))
129 len = /[\s]*Len[\s]*=[\s]*([0-9]*)/.match(lb)[1].to_i
130 puts("DBG sending: "+lb) if $debug
140 end while not (file.eof or (m=/[\s]*Msg[\s]*=[\s]*([0-9a-fA-F]*)/.match(lb)))
142 puts("DBG sending: "+lb) if $debug
147 end while not /[\s]*MD[\s]*=.*/.match(lb)
148 a = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(lb))[1];
149 b = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(avr_md))[1];
152 printf("\n%4d (%4d) [%5d]: ", line, (line-1)*$linewidth, len) if (pos%$linewidth==0 and $linewidth!=0)
153 line += 1 if (pos%$linewidth==0 and $linewidth!=0)
155 #putc((a==b)?'*':'!')
161 printf("\nError @%05d: %s \n != %s - ",len, a, b)
169 opts = Getopt::Std.getopts("s:f:i:hdca")
172 conf = readconfigfile("/etc/testport.conf", conf)
173 conf = readconfigfile("~/.testport.conf", conf)
174 conf = readconfigfile("testport.conf", conf)
175 conf = readconfigfile(opts["f"], conf) if opts["f"]
179 puts("serial port interface version: " + SerialPort::VERSION);
181 params = { "baud" => conf["PORT"]["baud"].to_i,
182 "data_bits" => conf["PORT"]["databits"].to_i,
183 "stop_bits" => conf["PORT"]["stopbits"].to_i,
184 "parity" => SerialPort::NONE }
185 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
186 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
187 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
188 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
190 puts("\nPort: "+conf["PORT"]["port"]+"@" +
191 params["baud"].to_s +
193 params["data_bits"].to_s +
194 conf["PORT"]["paraty"][0,1].upcase +
195 params["stop_bits"].to_s +
198 $sp = SerialPort.new(conf["PORT"]["port"], params)
200 $sp.read_timeout=1000; # 5 minutes
201 $sp.flow_control = SerialPort::SOFT
208 algos_rev = algos.invert
209 algo_tasks = Array.new
210 opts["s"].each_byte{ |x|
212 algo_tasks << [algos_rev[x.chr],x.chr]
216 algo_tasks=algos.sort
219 algo_tasks.each do |algoa|
222 puts("No test-set defined for #{algo} \r\n")
226 logfile=File.open(conf["PORT"]["testlogbase"]+algo+".txt", "a")
227 while conf[algo]["file_#{i}"] != nil
228 puts("Testing #{algo} with #{conf[algo]["file_#{i}"]}")
230 init_system(algoa[1])
232 skip=opts["i"].to_i if opts["i"]
233 nerrors=run_test(conf[algo]["file_#{i}"], skip)
236 logfile.puts("[ok] "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
238 puts("\n[errors: "+ nerrors.to_s() +"]")
239 logfile.puts("[error] "+nerrors.to_s+" "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
249 for i in (5..(ARGV.size-1))
250 nerrors = run_test(ARGV[i])
254 puts("\n[errors: "+ nerrors.to_s() +"]")