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