]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/shavs_test2.rb
[blake] fixing bugs and removing bug compatibility mode
[avr-crypto-lib.git] / host / shavs_test2.rb
1 #!/usr/bin/ruby
2 # shavs_test.rb
3 =begin
4     This file is part of the AVR-Crypto-Lib.
5     Copyright (C) 2008, 2009  Daniel Otte (daniel.otte@rub.de)
6
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.
11
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.
16
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/>.
19 =end
20
21 $debug = true
22 $debug = false
23 require 'rubygems'
24 require 'serialport'
25 require 'getopt/std'
26
27 $buffer_size = 0
28
29 def readconfigfile(fname, conf)
30   section = "default"
31   if not File.exists?(fname)
32     return conf
33   end
34   file = File.open(fname, "r")
35   until file.eof
36     line = file.gets()
37         next if /[\s]*#/.match(line)
38         if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
39           section=m[1]
40           conf[m[1]] = Hash.new
41           next
42         end
43         next if not /=/.match(line)
44         m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
45         conf[section][m[1]] = m[2]
46   end
47   file.close()
48   return conf
49 end
50
51 def reset_system
52   $sp.print("exit\r")
53   sleep 0.1
54   $sp.print("exit\r")
55   sleep 0.1
56 end
57
58 def scan_system
59   algos = Hash.new
60   $sp.print("shavs_list\r")
61   while true
62     line=$sp.gets()
63     return algos if /^>$/.match(line)
64     if m = /[\*\ ]([a-z]):[\s]*([a-zA-Z0-9+_-]+)/.match(line)
65       algos[m[2]]=m[1]
66     end
67   end
68 end
69
70 def init_system(algo_select)
71   $sp.print("echo off \r")
72   print("DBG i: " + "echo off \r"+"\n") if $debug
73  sleep 1
74   $sp.print("shavs_set #{algo_select}\r")
75   print("DBG i: " + "shavs_set #{$algo_select} \r"+"\n") # if $debug
76   sleep 1
77   $sp.print("shavs_test1 \r")
78   print("DBG i: " + "shavs_test1 \r"+"\n") if $debug
79   begin
80     line=$sp.gets()
81   end while not m=/buffer_size[\s]*=[\s]*0x([0-9A-Fa-f]*)/.match(line)
82   $buffer_size = m[1].to_i(16)
83 end
84
85 def get_md
86   begin
87     line = $sp.gets()
88         line = "" if line==nil
89         puts("DBG got: "+line) if $debug
90   end while not /[\s]*MD[\s]*=.*/.match(line)
91   return line
92 end
93
94 def send_md(md_string)
95   $sp.print("Msg = ")
96   for i in 0..md_string.length-1
97     $sp.print(md_string[i].chr)
98 #       print("DBG s: "+ md_string[i].chr) if $debug
99         if((i%($buffer_size*2)==0)&&(i!=0))
100           begin
101                 line=$sp.gets()
102           end while not /\./.match(line)
103         end
104   end
105 end
106
107 def run_test(filename, skip=0)
108   nerrors = 0
109   line=1
110   if not File.exist?(filename)
111         puts("ERROR file "+filename+" does not exist!")
112         return nerrors
113   end
114   pos = 0
115   file = File.new(filename, "r");
116   until file.eof
117     begin
118       lb=file.gets()
119     end while not (file.eof or (/[\s]*Len[\s]*=.*/.match(lb)))
120     len = /[\s]*Len[\s]*=[\s]*([0-9]*)/.match(lb)[1].to_i
121     puts("DBG sending: "+lb) if $debug
122         return if file.eof
123         if(skip>0)
124           skip -= 1
125           redo
126         end
127         $sp.print(lb.strip)
128         $sp.print("\r")
129     begin
130           lb=file.gets()
131     end while not (file.eof or (m=/[\s]*Msg[\s]*=[\s]*([0-9a-fA-F]*)/.match(lb)))
132     return if file.eof
133     puts("DBG sending: "+lb) if $debug
134         send_md(m[1])
135         avr_md = get_md()
136     begin
137           lb=file.gets()
138     end while not /[\s]*MD[\s]*=.*/.match(lb)
139         a = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(lb))[1];
140         b = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(avr_md))[1];
141         a.upcase!
142         b.upcase!
143         printf("\n%4d (%4d) [%5d]: ", line, (line-1)*$linewidth, len) if (pos%$linewidth==0 and $linewidth!=0)
144         line += 1               if (pos%$linewidth==0 and $linewidth!=0)
145         #sleep(1)
146         #putc((a==b)?'*':'!')
147         if(a==b)
148           putc('*')
149         else
150           putc('!')
151           printf("<%d>",len)
152           nerrors += 1
153         end
154         pos += 1
155   end
156   return nerrors.to_i
157 end
158
159 conf = Hash.new
160 conf = readconfigfile("/etc/testport.conf", conf)
161 conf = readconfigfile("~/.testport.conf", conf)
162 conf = readconfigfile("testport.conf", conf)
163 #puts conf.inspect
164
165 puts("serial port interface version: " + SerialPort::VERSION);
166 $linewidth = 64
167 params = { "baud"       => conf["PORT"]["baud"].to_i,
168             "data_bits" => conf["PORT"]["databits"].to_i,
169             "stop_bits" => conf["PORT"]["stopbits"].to_i,
170             "parity"    => SerialPort::NONE }
171 params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
172 params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
173 params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
174 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
175
176 puts("\nPort: "+conf["PORT"]["port"]+"@"    +
177                 params["baud"].to_s      +
178                 " "                      +
179                 params["data_bits"].to_s +
180                 conf["PORT"]["paraty"][0,1].upcase +
181                 params["stop_bits"].to_s +
182                 "\n")
183
184 $sp = SerialPort.new(conf["PORT"]["port"], params)
185
186 $sp.read_timeout=1000; # 5 minutes
187 $sp.flow_control = SerialPort::SOFT
188
189 reset_system()
190 algos=scan_system()
191 #puts algos.inspect
192
193 algos.sort.each do |algoa|
194   algo = algoa[0]
195   if conf[algo]==nil
196     puts("No test-set defined for #{algo} \r\n")
197     next
198   else
199         i=0
200         logfile=File.open(conf["PORT"]["testlogbase"]+algo+".txt", "a")
201         while conf[algo]["file_#{i}"] != nil
202           puts("Testing #{algo} with #{conf[algo]["file_#{i}"]}")
203           reset_system()
204           init_system(algoa[1])
205           nerrors=run_test(conf[algo]["file_#{i}"], 0)
206       if nerrors == 0
207         puts("\n[ok]")
208         logfile.puts("[ok] "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
209       else
210         puts("\n[errors: "+ nerrors.to_s() +"]")
211         logfile.puts("[error] "+nerrors.to_s+" "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
212       end
213       i += 1
214     end
215     logfile.close()
216   end
217 end
218
219 =begin
220 nerrors = 0
221 for i in (5..(ARGV.size-1))
222   nerrors = run_test(ARGV[i])
223   if nerrors == 0
224     puts("\n[ok]")
225   else
226     puts("\n[errors: "+ nerrors.to_s() +"]")
227   end
228 end
229  $sp.print("EXIT\r");
230
231 #exit(0);
232 =end
233