Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add canonicalization tests based on rfc
- Loading branch information
Showing
3 changed files
with
94 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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 | |||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -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' | |||
|
|||
|