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 ################################################################################
241 def run_test_add(skip=0)
249 a = rand(256**length_a_B) * s_a
250 b = rand(256**length_a_B) * s_b
259 b_size = rand(length_b_B+1)
260 a = rand(256**length_a_B) * s_a
261 b = rand(256**b_size) * s_b
270 end while length_a_B<4096/8
273 ################################################################################
275 ################################################################################
277 def run_test_mul(skip=0)
285 a = rand(256**length_a_B) * s_a
286 b = rand(256**length_a_B) * s_b
295 b_size = rand(length_b_B+1)
296 a = rand(256**length_a_B) * s_a
297 b = rand(256**b_size) * s_b
305 end while length_a_B<4096/8
308 ################################################################################
310 ################################################################################
312 def run_test_square(skip=0)
317 a = rand(256**length_a_B)
322 end while length_a_B<4096/8
325 ################################################################################
327 ################################################################################
329 opts = Getopt::Std.getopts("s:f:i:a:hd")
332 conf = readconfigfile("/etc/testport.conf", conf)
333 conf = readconfigfile("~/.testport.conf", conf)
334 conf = readconfigfile("testport.conf", conf)
335 conf = readconfigfile(opts["f"], conf) if opts["f"]
339 puts("serial port interface version: " + SerialPort::VERSION);
343 params = { "baud" => conf["PORT"]["baud"].to_i,
344 "data_bits" => conf["PORT"]["databits"].to_i,
345 "stop_bits" => conf["PORT"]["stopbits"].to_i,
346 "parity" => SerialPort::NONE }
347 params["paraty"] = SerialPort::ODD if conf["PORT"]["paraty"].downcase == "odd"
348 params["paraty"] = SerialPort::EVEN if conf["PORT"]["paraty"].downcase == "even"
349 params["paraty"] = SerialPort::MARK if conf["PORT"]["paraty"].downcase == "mark"
350 params["paraty"] = SerialPort::SPACE if conf["PORT"]["paraty"].downcase == "space"
352 puts("\nPort: "+conf["PORT"]["port"]+"@" +
353 params["baud"].to_s +
355 params["data_bits"].to_s +
356 conf["PORT"]["paraty"][0,1].upcase +
357 params["stop_bits"].to_s +
360 $sp = SerialPort.new(conf["PORT"]["port"], params)
362 $sp.read_timeout=1000; # 5 minutes
363 $sp.flow_control = SerialPort::SOFT
371 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
372 if File.exists?(logfilename)
375 logfilename = sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i,'.txt')
377 end while(File.exists?(logfilename))
379 File.move(sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-2,'.txt'),
380 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',i-1,'.txt'), true)
383 File.move(sprintf('%s%s', conf['PORT']['testlogbase'],'bigint.txt'),
384 sprintf('%s%04d%s', conf['PORT']['testlogbase']+'bigint_',1,'.txt'), true)
385 logfilename = conf['PORT']['testlogbase']+'bigint.txt'
387 $logfile = File.open(logfilename, 'w')
388 printf("logfile: %s\n", logfilename)
390 $logfile.printf("bigint test from: %s\n", Time.now.to_s)
391 $logfile.printf("skip = %s\n", opts['s']) if opts['s']
392 $logfile.printf("seed = 0x%X\n", 0xdeadbeef)
395 tests['a'] = proc {|x| run_test_add(x) }
396 tests['m'] = proc {|x| run_test_mul(x) }
397 tests['s'] = proc {|x| run_test_square(x) }
399 init_str['a'] = 'add-test'
400 init_str['m'] = 'mul-test'
401 init_str['s'] = 'square-test'
406 opts['a'].each_char do |x|
409 init_system(init_str[x])
410 tests[x].call(opts['s']?opts['s'].to_i():0)
412 puts "no test defiened for '#{x}'"
416 'ams'.each_char do |x|
419 init_system(init_str[x])
420 tests[x].call(opts['s']?opts['s'].to_i():0)
422 puts "no test defiened for '#{x}'"