]> git.cryptolib.org Git - avr-crypto-lib.git/blob - host/get_test.rb
optimizing norx32
[avr-crypto-lib.git] / host / get_test.rb
1 #!/usr/bin/ruby 
2 # get_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 require 'rubygems'
22 require 'serialport'
23
24 $debug = false
25 $progress_dots = false
26
27 def read_line(error_msg=true)
28   begin
29     i = $extended_wait
30     begin
31       s = $sp.gets()
32       i -= 1
33     end while !s && i > 0
34     if s==nil
35       puts("ERROR: read timeout!\n") if error_msg
36       return nil
37     end
38   putc('.') if s.include?(6.chr) && $progress_dots      
39   puts(s.inspect) if $debug
40   end while s == 6.chr
41   s.gsub(/\006/, '')
42 end
43
44 def readTestVector(param)
45   fname=$dir;
46   lb="";
47   buffer="";
48   set=0;
49   vector=0;
50   begin
51     lb = read_line(false)
52         if (m=/unknown command/.match(lb) || m=/[Ee][Rr]{2}[Oo][Rr]/.match(lb))
53       puts("ERROR: "+lb);
54       exit(2);
55     end
56     if(lb==nil)
57       return false;
58     end
59   end while(m=/\*+/.match(lb));
60   
61   buffer += lb;
62   begin
63     lb = read_line()
64     if(lb==nil)
65       return false;
66     end
67     buffer+=lb;
68   end while(m=/\*.*/.match(lb));
69
70   while(!(m=/Test vectors/.match(lb))) 
71     m=/[^:]*:[\s]([A-Za-z0-9_-]*)/.match(lb);
72     if(m) 
73       fname+=m[1]+".";
74     end
75     return false if lb==nil
76     buffer+=lb;
77     lb = read_line();
78   end
79   if(param!="")
80     fname+=param+".";
81   end
82   puts("-> "+fname+"txt");
83   file=File.new(fname+"txt", "w");
84   file.sync = true
85     buffer+=lb;
86     file.write(buffer);
87     begin
88       if (m=/Test\ vectors\ \-\-\ set[\s]+([0-9]+)/.match(lb))
89         set=m[1].to_i;
90         print("\nSet "+m[1]+":");
91       end
92       if (m=/Set [0-9]*, vector#[\s]*([0-9]+):/.match(lb))
93         vector=m[1].to_i;
94         #print(" "+m[1]);
95         if(vector!=0 && vector % $linewidth==0)
96           print("\n      ")
97         end
98         printf(" %4u", vector);
99       end
100       lb=read_line();
101       if(lb==nil)
102         file.close();
103         return false;
104       end
105       file.write(lb);
106     end while(!m=/End of test vectors/.match(lb));
107     puts("\n");
108   file.close();
109   return true
110 end
111
112
113 if ARGV.size < 5
114   STDERR.print <<EOF
115   Usage: ruby #{$0} port bps nbits stopb command [target_dir] [additional specifier]
116 EOF
117   exit(1)
118 end
119
120 command=ARGV[4]+"\r";
121 $dir=(ARGV.size>=6)?ARGV[5]:"";
122 param=(ARGV.size>=7)?ARGV[6]:"";
123
124 puts("\nPort: "+ARGV[0]+ "@"+ARGV[1]+" "+ARGV[2]+"N"+ARGV[3]+"\n");
125 $linewidth = 16
126 $sp = SerialPort.new(ARGV[0], ARGV[1].to_i, ARGV[2].to_i, ARGV[3].to_i, SerialPort::NONE);
127 $sp.read_timeout=1000; # 1 second
128 $extended_wait=100000;
129 $sp.write(command);
130
131 if(readTestVector(param)==false)
132   puts("ERROR: test seems not to be implemented");
133   exit(0); # 3 
134 end
135
136 while(readTestVector(param))
137 end
138
139 exit(0);
140
141