GenVectorTests 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env ruby
  2. require 'optparse'
  3. require 'json'
  4. require File.join(File.dirname(__FILE__), '../Spec/vectors', 'vectorparser')
  5. @test_files = ["kdf_short", "password_short"]
  6. @options = {}
  7. # Returns the text for a JS hash from a hash
  8. def NSDictionaryForHash(hash)
  9. "@{\n" + hash.collect { |key, value| %Q( @"#{key}": @"#{value}") }.join(",\n") + "}"
  10. end
  11. # Output the file header to output stream
  12. def outputHeader(output)
  13. output << <<-HEADER
  14. // Automatically Generated by GenVectorTests
  15. HEADER
  16. end
  17. # Output the tests for a given filename to the output stream
  18. def outputTestsForFile(output, name)
  19. VectorParser.new(@options[:v3_directory] + "/" + name).vectors.each do |vector|
  20. output << <<-TEST_CASE
  21. test("#{vector["title"]}", function() {
  22. verify_#{name}(#{JSON.dump vector});
  23. });
  24. TEST_CASE
  25. end
  26. end
  27. # Output the footer to the output stream
  28. def outputFooter(output)
  29. output<< <<-FOOTER
  30. FOOTER
  31. end
  32. ###################
  33. ### MAIN
  34. ###################
  35. opt_parser = OptionParser.new do |opt|
  36. opt.banner = "Usage: GenVectorTest -o VectorTests.m -3 v3_directory"
  37. opt.separator ""
  38. opt.on("-o","--output PATH","path to output code") do |output_path|
  39. @options[:output_path] = output_path
  40. end
  41. opt.on("-3", "-3 PATH", "path to v3 directory") do |v3_directory|
  42. @options[:v3_directory] = v3_directory
  43. end
  44. end
  45. opt_parser.parse!
  46. raise OptionParser::MissingArgument if @options[:output_path].nil?
  47. raise OptionParser::MissingArgument if @options[:v3_directory].nil?
  48. File.open(@options[:output_path], "w") do |output|
  49. outputHeader(output)
  50. @test_files.each do |file|
  51. outputTestsForFile(output, file)
  52. end
  53. outputFooter(output)
  54. end