]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/shavs_test.rb
tweak for BMW
[avr-crypto-lib.git] / host / shavs_test.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
26 def init_system
27 #  sleep 1
28   $sp.print("exit\r")
29   sleep 0.1
30   $sp.print("exit\r")
31   sleep 0.1
32   $sp.print("echo off \r")
33   print("DBG i: " + "echo off \r"+"\n") if $debug
34 #  line = $sp.readlines()
35 #  print("DBG 0.0: ")
36 #  print(line)
37   sleep 1
38   $sp.print("shavs_set #{$algo_select}\r")
39   print("DBG i: " + "shavs_set #{$algo_select} \r"+"\n") # if $debug
40 #  line = $sp.readlines()
41 #  print("DBG 0.1: ")
42 #  print(line)
43   sleep 1
44   $sp.print("shavs_test1 \r")
45   print("DBG i: " + "shavs_test1 \r"+"\n") if $debug
46 #  line = $sp.readlines()
47 #  print("DBG 0.2: ")
48 #  print(line)
49 end
50
51 def get_md
52   begin
53     line = $sp.gets()
54         line = "" if line==nil
55         puts("DBG g: "+line) if $debug
56   end while not /[\s]*MD[\s]*=.*/.match(line)
57   return line
58 end
59
60 def send_md(md_string)
61   for i in 0..md_string.length-1
62     $sp.print(md_string[i].chr)
63 #       print("DBG s: "+ md_string[i].chr) if $debug
64         if(i%20==19)
65                 sleep(0.15)
66         end
67   end
68 end
69
70 def run_test(filename)
71   nerrors = 0
72   line=1
73   if not File.exist?(filename)
74         puts("ERROR file "+filename+" does not exist!")
75   end
76   pos = 0
77   file = File.new(filename, "r");
78   until file.eof
79     sleep(0.5)
80     begin
81       lb=file.gets()
82     end while not (file.eof or (/[\s]*Len[\s]*=.*/.match(lb)))
83     puts("DBG sending: "+lb) if $debug
84         return if file.eof
85         $sp.print(lb.strip)
86         $sp.print("\r")
87     begin
88           lb=file.gets()
89     end while not (file.eof or (/[\s]*Msg[\s]*=.*/.match(lb)))
90     return if file.eof
91     puts("DBG sending: "+lb) if $debug
92         send_md(lb.strip)
93         avr_md = get_md()
94     begin
95           lb=file.gets()
96     end while not /[\s]*MD[\s]*=.*/.match(lb)
97         a = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(lb))[1];
98         b = (/[\s]*MD[\s]*=[\s]*([0-9a-fA-F]*).*/.match(avr_md))[1];
99         a.upcase!
100         b.upcase!
101         printf("\n%4d (%4d): ", line, (line-1)*$linewidth) if (pos%$linewidth==0 and $linewidth!=0)
102         line += 1               if (pos%$linewidth==0 and $linewidth!=0)
103         #putc((a==b)?'*':'!')
104         if(a==b)
105           putc('*')
106         else
107           putc('!')
108           printf("\nshould: %s\ngot:   %s\n",lb,avr_md)
109           nerrors += 1
110         end
111         pos += 1
112   end
113   return nerrors.to_i
114 end
115
116 if ARGV.size < 6
117   STDERR.print <<EOF
118   Usage: ruby #{$0} port bps nbits stopb algo_select testfile ...
119 EOF
120   exit(1)
121 end
122
123 puts("\nPort: "+ARGV[0]+ "@"+ARGV[1]+" "+ARGV[2]+"N"+ARGV[3]+"\n");
124 puts("serial port interface version: " + SerialPort::VERSION);
125 $linewidth = 64
126 $params = { "baud"      => ARGV[1].to_i,
127             "data_bits" => ARGV[2].to_i,
128             "stop_bits" => ARGV[3].to_i,
129             "parity"    => SerialPort::NONE }
130 $sp = SerialPort.new(ARGV[0], $params)
131 #$sp = SerialPort.new(ARGV[0], ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i, SerialPort::NONE);
132
133 $sp.read_timeout=1000; # 5 minutes
134 $sp.flow_control = SerialPort::SOFT
135 $algo_select = ARGV[4]
136 #irb
137
138 init_system()
139
140 nerrors = 0
141 for i in (5..(ARGV.size-1))
142   nerrors = run_test(ARGV[i])
143   if nerrors == 0
144     puts("\n[ok]")
145   else
146     puts("\n[errors: "+ nerrors.to_s() +"]")
147   end
148 end
149  $sp.print("EXIT\r");
150
151 #exit(0);
152
153