]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/shavs_test2.rb
release of new testing tool (still unfinished)
[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 #  sleep 1
72   $sp.print("echo off \r")
73   print("DBG i: " + "echo off \r"+"\n") if $debug
74 #  line = $sp.readlines()
75 #  print("DBG 0.0: ")
76 #  print(line)
77   sleep 1
78   $sp.print("shavs_set #{algo_select}\r")
79   print("DBG i: " + "shavs_set #{$algo_select} \r"+"\n") # if $debug
80 #  line = $sp.readlines()
81 #  print("DBG 0.1: ")
82 #  print(line)
83   sleep 1
84   $sp.print("shavs_test1 \r")
85   print("DBG i: " + "shavs_test1 \r"+"\n") if $debug
86   begin
87     line=$sp.gets()
88   end while not m=/buffer_size[\s]*=[\s]*0x([0-9A-Fa-f]*)/.match(line)
89   $buffer_size = m[1].to_i(16)
90 #  line = $sp.readlines()
91 #  print("DBG 0.2: ")
92 #  print(line)
93 end
94
95 def get_md
96   begin
97     line = $sp.gets()
98         line = "" if line==nil
99         puts("DBG got: "+line) if $debug
100   end while not /[\s]*MD[\s]*=.*/.match(line)
101   return line
102 end
103
104 def send_md(md_string)
105   for i in 0..md_string.length-1
106     $sp.print(md_string[i].chr)
107 #       print("DBG s: "+ md_string[i].chr) if $debug
108         if(i%$buffer_size==$buffer_size-1)
109                 begin
110                         line=$sp.gets()
111                 end while not /\./.match(line)
112         end
113   end
114 end
115
116 def run_test(filename)
117   nerrors = 0
118   line=1
119   if not File.exist?(filename)
120         puts("ERROR file "+filename+" does not exist!")
121         return nerrors
122   end
123   pos = 0
124   file = File.new(filename, "r");
125   until file.eof
126     sleep(0.5)
127     begin
128       lb=file.gets()
129     end while not (file.eof or (/[\s]*Len[\s]*=.*/.match(lb)))
130     puts("DBG sending: "+lb) if $debug
131         return if file.eof
132         $sp.print(lb.strip)
133         $sp.print("\r")
134     begin
135           lb=file.gets()
136     end while not (file.eof or (/[\s]*Msg[\s]*=.*/.match(lb)))
137     return if file.eof
138     puts("DBG sending: "+lb) if $debug
139         send_md(lb.strip)
140         avr_md = get_md()
141     begin
142           lb=file.gets()
143     end while not /[\s]*MD[\s]*=.*/.match(lb)
144         a = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(lb))[1];
145         b = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(avr_md))[1];
146         a.upcase!
147         b.upcase!
148         printf("\n%4d (%4d): ", line, (line-1)*$linewidth) if (pos%$linewidth==0 and $linewidth!=0)
149         line += 1               if (pos%$linewidth==0 and $linewidth!=0)
150         sleep(1)
151         #putc((a==b)?'*':'!')
152         if(a==b)
153           putc('*')
154         else
155           putc('!')
156           printf("\nshould: %s\ngot:   %s\n",lb,avr_md)
157           nerrors += 1
158         end
159         pos += 1
160   end
161   return nerrors.to_i
162 end
163
164 conf = Hash.new
165 conf = readconfigfile("/etc/testport.conf", conf)
166 conf = readconfigfile("~/.testport.conf", conf)
167 conf = readconfigfile("testport.conf", conf)
168 puts conf.inspect
169
170 puts("serial port interface version: " + SerialPort::VERSION);
171 $linewidth = 64
172 params = { "baud"       => conf["PORT"]["baud"].to_i,
173             "data_bits" => conf["PORT"]["databits"].to_i,
174             "stop_bits" => conf["PORT"]["stopbits"].to_i,
175             "parity"    => SerialPort::NONE }
176 params["paraty"] = SerialPort::ODD   if conf["PORT"]["paraty"].downcase == "odd"
177 params["paraty"] = SerialPort::EVEN  if conf["PORT"]["paraty"].downcase == "even"
178 params["paraty"] = SerialPort::MARK  if conf["PORT"]["paraty"].downcase == "mark"
179 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
180
181 puts("\nPort: "+conf["PORT"]["port"]+"@"    +
182                 params["baud"].to_s      +
183                 " "                      +
184                 params["data_bits"].to_s +
185                 conf["PORT"]["paraty"][0,1].upcase +
186                 params["stop_bits"].to_s +
187                 "\n")
188
189 $sp = SerialPort.new(conf["PORT"]["port"], params)
190
191 $sp.read_timeout=1000; # 5 minutes
192 $sp.flow_control = SerialPort::SOFT
193 #$algo_select = ARGV[4]
194 #irb
195
196 reset_system()
197 algos=scan_system()
198 puts algos.inspect
199
200 algos.each_key do |algo|
201   if conf[algo]==nil
202     puts("No test-set defined for #{algo} \r\n")
203     next
204   else
205         i=0
206         logfile=File.open(conf["PORT"]["testlogbase"]+algo+".txt", "a")
207         while conf[algo]["file_#{i}"] != nil
208           puts("Testing #{algo} with #{conf[algo]["file_#{i}"]}")
209           reset_system()
210           init_system(algos[algo])
211           nerrors=run_test(conf[algo]["file_#{i}"])
212       if nerrors == 0
213         puts("\n[ok]")
214         logfile.puts("[ok] "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
215       else
216         puts("\n[errors: "+ nerrors.to_s() +"]")
217         logfile.puts("[error] "+nerrors.to_s+" "+conf[algo]["file_#{i}"]+ " ("+Time.now.to_s()+")")
218       end
219       i += 1
220     end
221     logfile.close()
222   end
223 end
224
225 =begin
226 nerrors = 0
227 for i in (5..(ARGV.size-1))
228   nerrors = run_test(ARGV[i])
229   if nerrors == 0
230     puts("\n[ok]")
231   else
232     puts("\n[errors: "+ nerrors.to_s() +"]")
233   end
234 end
235  $sp.print("EXIT\r");
236
237 #exit(0);
238 =end
239