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