4 This file is part of the AVR-Crypto-Lib.
5 Copyright (C) 2008, 2009 Daniel Otte (daniel.otte@rub.de)
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.
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.
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/>.
29 $conffile_check = Hash.new
30 $conffile_check.default = 0
32 ################################################################################
34 ################################################################################
36 def readconfigfile(fname, conf)
37 return conf if $conffile_check[fname]==1
38 $conffile_check[fname]=1
40 if not File.exists?(fname)
43 file = File.open(fname, "r")
46 next if /[\s]*#/.match(line)
47 if m=/\[[\s]*([^\s]*)[\s]*\]/.match(line)
52 next if not /=/.match(line)
53 m=/[\s]*([^\s]*)[\s]*=[\s]*([^\s]*)/.match(line)
55 Dir.glob(m[2]){ |fn| conf = readconfigfile(fn, conf) }
57 conf[section][m[1]] = m[2]
64 ################################################################################
66 ################################################################################
75 ################################################################################
77 ################################################################################
79 def init_system(test_prog)
80 $sp.print("echo off \r")
81 print("DBG i: " + "echo off \r"+"\n") if $debug
83 $sp.print("#{test_prog}\r")
84 print("DBG i: " + "#{test_prog} \r"+"\n") if $debug
89 ################################################################################
91 ################################################################################
92 def screen_progress(v)
94 printf("\n%5d [%04d]: ", $testno, $size)
98 $linepos = ($linepos+1)%$linewidth
101 ################################################################################
103 ################################################################################
108 line = "" if line==nil
109 puts("DBG got: "+line) if $debug
110 if /^Error:.*/.match(line)
114 end while not /[\s]*enter a:[\s]*/.match(line)
115 $sp.print(a.to_s(16)+" ")
118 line = "" if line==nil
119 puts("DBG got: "+line) if $debug
120 if /^Error:.*/.match(line)
124 end while not /[\s]*enter b:[\s]*/.match(line)
125 $sp.print(b.to_s(16)+" ")
128 line = "" if line==nil
129 puts("DBG got: "+line) if $debug
130 if /^Error:.*/.match(line)
134 end while not m=/[\s]*([-]?[0-9a-fA-F]*)[\s]+\+[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
139 if(a_== a && b_ == b && c_ == (a+b))
140 $logfile.printf("[pass]: %s\n", line)
143 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
144 $logfile.printf(" ; should %s + %s = %s\n", a.to_s(16), b.to_s(16), (a+b).to_s(16))
150 ################################################################################
152 ################################################################################
157 line = "" if line==nil
158 puts("DBG got: "+line) if $debug
159 if /^Error:.*/.match(line)
163 end while not /[\s]*enter a:[\s]*/.match(line)
164 $sp.print(a.to_s(16)+" ")
167 line = "" if line==nil
168 puts("DBG got: "+line) if $debug
169 if /^Error:.*/.match(line)
173 end while not /[\s]*enter b:[\s]*/.match(line)
174 $sp.print(b.to_s(16)+" ")
177 line = "" if line==nil
178 puts("DBG got: "+line) if $debug
179 if /^Error:.*/.match(line)
183 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+\*[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
188 if(a_== a && b_ == b && c_ == (a*b))
189 $logfile.printf("[pass]: %s\n", line)
192 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
193 $logfile.printf(" ; should %s * %s = %s\n", a.to_s(16), b.to_s(16), (a*b).to_s(16))
199 ################################################################################
201 ################################################################################
206 line = "" if line==nil
207 puts("DBG got: "+line) if $debug
208 if /^Error:.*/.match(line)
212 end while not /[\s]*enter a:[\s]*/.match(line)
213 $sp.print(a.to_s(16)+" ")
216 line = "" if line==nil
217 puts("DBG got: "+line) if $debug
218 if /^Error:.*/.match(line)
222 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]*\*\*2[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
226 if(a_== a && c_ == (a**2))
227 $logfile.printf("[pass]: %s\n", line)
230 $logfile.printf("[fail (%s%s)]: %s", (a==a_)?"":"a", (c_==a**2)?"":"c",line)
231 $logfile.printf(" ; should %s **2 = %s\n", a.to_s(16), (a**2).to_s(16))
237 ################################################################################
239 ################################################################################
244 line = "" if line==nil
245 puts("DBG got: "+line) if $debug
246 if /^Error:.*/.match(line)
250 end while not /[\s]*enter a:[\s]*/.match(line)
251 $sp.print(a.to_s(16)+" ")
254 line = "" if line==nil
255 puts("DBG got: "+line) if $debug
256 if /^Error:.*/.match(line)
260 end while not /[\s]*enter b:[\s]*/.match(line)
261 $sp.print(b.to_s(16)+" ")
264 line = "" if line==nil
265 puts("DBG got: "+line) if $debug
266 if /^Error:.*/.match(line)
270 end while not m=/[\s]*([+-]?[0-9a-fA-F]*)[\s]+%[\s]+([+-]?[0-9a-fA-F]*)[\s]*=[\s]*([+-]?[0-9a-fA-F]*)/.match(line)
275 if(a_== a && b_ == b && c_ == (a%b))
276 $logfile.printf("[pass]: %s\n", line)
279 $logfile.printf("[fail (%s%s%s)]: %s", (a==a_)?"":"a", (b==b_)?"":"b", (c_==a+b)?"":"c",line)
280 $logfile.printf(" ; should %s %% %s = %s\n", a.to_s(16), b.to_s(16), (a%b).to_s(16))
286 ################################################################################
288 ################################################################################
290 def run_test_add(skip=0)
298 a = rand(256**length_a_B) * s_a
299 b = rand(256**length_a_B) * s_b
308 b_size = rand(length_b_B+1)
309 a = rand(256**length_a_B) * s_a
310 b = rand(256**b_size) * s_b
319 end while length_a_B<4096/8
322 ################################################################################
324 ################################################################################
326 def run_test_mul(skip=0)
334 a = rand(256**length_a_B) * s_a
335 b = rand(256**length_a_B) * s_b
344 b_size = rand(length_b_B+1)
345 a = rand(256**length_a_B) * s_a
346 b = rand(256**b_size) * s_b
354 end while length_a_B<4096/8
357 ################################################################################
359 ################################################################################
361 def run_test_square(skip=0)
366 a = rand(256**length_a_B)
371 end while length_a_B<4096/8
374 ################################################################################
376 ################################################################################
378 def run_test_reduce(skip=0)
384 a = rand(256**length_a_B)
385 b = rand(256**length_a_B)+1
386 v = reduce_test(a, b)
390 b_size = rand(length_b_B+1)
391 a = rand(256**length_a_B)
392 b = rand(256**b_size)+1
393 v = reduce_test(a, b)
398 end while length_a_B<4096/8
401 ################################################################################
403 ################################################################################
405 opts = Getopt::Std.getopts("s:f:i:a:hd")
408 conf = readconfigfile("/etc/testport.conf", conf)
409 conf = readconfigfile("~/.testport.conf", conf)
410 conf = readconfigfile("testport.conf", conf)
411 conf = readconfigfile(opts["f"], conf) if opts["f"]
415 puts("serial port interface version: " + SerialPort::VERSION);
419 params = { "baud" => conf["PORT"]["baud"].to_i,
420 "data_bits" => conf["PORT"]["databits"].to_i,
421 "stop_bits" => conf["PORT"]["stopbits"].to_i,
422 "parity" => SerialPort::NONE }
423 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
424 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
425 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
426 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
428 puts("\nPort: "+conf["PORT"]["port"]+"@" +
429 params["baud"].to_s +
431 params["data_bits"].to_s +
432 conf["PORT"]["paraty"][0,1].upcase +
433 params["stop_bits"].to_s +
436 $sp = SerialPort.new(conf["PORT"]["port"], params)
438 $sp.read_timeout=1000; # 5 minutes
439 $sp.flow_control = SerialPort::SOFT
447 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
448 if File.exists?(logfilename)
451 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
453 end while(File.exists?(logfilename))
455 File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'),
456 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
459 File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'),
460 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
461 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
463 $logfile = File.open(logfilename, 'w')
464 printf("logfile: %s\n", logfilename)
466 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
467 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
468 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
471 tests['a'] = proc {|x| run_test_add(x) }
472 tests['m'] = proc {|x| run_test_mul(x) }
473 tests['s'] = proc {|x| run_test_square(x) }
474 tests['r'] = proc {|x| run_test_reduce(x) }
476 init_str['a'] = 'add-test'
477 init_str['m'] = 'mul-test'
478 init_str['s'] = 'square-test'
479 init_str['r'] = 'reduce-test'
484 opts['a'].each_char do |x|
487 init_system(init_str[x])
488 tests[x].call(opts['s']?opts['s'].to_i():0)
490 puts "no test defiened for '#{x}'"
494 'amsr'.each_char do |x|
497 init_system(init_str[x])
498 tests[x].call(opts['s']?opts['s'].to_i():0)
500 puts "no test defiened for '#{x}'"