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