Skip to content

Commit

Permalink
add canonicalization tests based on rfc
Browse files Browse the repository at this point in the history
  • Loading branch information
jhawthorn committed May 18, 2011
1 parent 299941c commit b519c71
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 0 deletions.
12 changes: 12 additions & 0 deletions Rakefile
@@ -1,2 +1,14 @@
require 'rake/testtask'
require 'bundler' 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 Bundler::GemHelper.install_tasks

55 changes: 55 additions & 0 deletions 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

27 changes: 27 additions & 0 deletions 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'


0 comments on commit b519c71

Please sign in to comment.