Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add canonicalization tests based on rfc

  • Loading branch information...
commit b519c71776f53b9fe305b1e4dba2eb30a59853cd 1 parent 299941c
@jhawthorn authored
Showing with 94 additions and 0 deletions.
  1. +12 −0 Rakefile
  2. +55 −0 test/canonicalization_test.rb
  3. +27 −0 test/test_helper.rb
View
12 Rakefile
@@ -1,2 +1,14 @@
+require 'rake/testtask'
require 'bundler'
+
+task :default => [:test]
+
+desc 'Run tests.'
+Rake::TestTask.new(:test) do |t|
+ t.libs << 'lib' << 'test'
+ t.pattern = 'test/**/*_test.rb'
+ t.verbose = true
+end
+
Bundler::GemHelper.install_tasks
+
View
55 test/canonicalization_test.rb
@@ -0,0 +1,55 @@
+
+require 'test_helper'
+
+class CanonicalizationTest < Test::Unit::TestCase
+ # from section 3.4.6 of rfc4871
+ def setup
+ @input = <<-eos.rfc_format
+ A: <SP> X <CRLF>
+ B <SP> : <SP> Y <HTAB><CRLF>
+ <HTAB> Z <SP><SP><CRLF>
+ <CRLF>
+ <SP> C <SP><CRLF>
+ D <SP><HTAB><SP> E <CRLF>
+ <CRLF>
+ <CRLF>
+ eos
+ @mail = Dkim::SignedMail.new(@input)
+ @mail.signable_headers = ['A', 'B']
+ end
+ def test_relaxed_header
+ @mail.header_canonicalization = 'relaxed'
+ expected_header = <<-eos.rfc_format
+ a:X <CRLF>
+ b:Y <SP> Z <CRLF>
+ eos
+ assert_equal expected_header, @mail.canonical_header
+ end
+ def test_relaxed_body
+ @mail.body_canonicalization = 'relaxed'
+ expected_body = <<-eos.rfc_format
+ <SP> C <CRLF>
+ D <SP> E <CRLF>
+ eos
+ assert_equal expected_body, @mail.canonical_body
+ end
+
+ def test_simple_header
+ @mail.header_canonicalization = 'simple'
+ expected_header = <<-eos.rfc_format
+ A: <SP> X <CRLF>
+ B <SP> : <SP> Y <HTAB><CRLF>
+ <HTAB> Z <SP><SP><CRLF>
+ eos
+ assert_equal expected_header, @mail.canonical_header
+ end
+ def test_simple_body
+ @mail.body_canonicalization = 'simple'
+ expected_body = <<-eos.rfc_format
+ <SP> C <SP><CRLF>
+ D <SP><HTAB><SP> E <CRLF>
+ eos
+ assert_equal expected_body, @mail.canonical_body
+ end
+end
+
View
27 test/test_helper.rb
@@ -0,0 +1,27 @@
+
+require 'test/unit'
+require 'dkim'
+
+class String
+ # Parse the format used in rfc4871
+ #
+ # In the following examples, actual whitespace is used only for
+ # clarity. The actual input and output text is designated using
+ # bracketed descriptors: "<SP>" for a space character, "<HTAB>" for a
+ # tab character, and "<CRLF>" for a carriage-return/line-feed sequence.
+ # For example, "X <SP> Y" and "X<SP>Y" represent the same three
+ # characters.
+ def rfc_format
+ str = self.dup
+ str.gsub!(/\s/,'')
+ str.gsub!(/<SP>/,' ')
+ str.gsub!(/<CRLF>/,"\r\n")
+ str.gsub!(/<HTAB>/,"\t")
+ str
+ end
+end
+
+# examples used in rfc
+Dkim::domain = 'example.com'
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.