Add some Message specs

bug/bundler_fix
jvazquez-r7 2014-08-20 09:31:49 -05:00
parent e4b586a96d
commit e5fc0a007f
1 changed files with 137 additions and 7 deletions

View File

@ -10,24 +10,132 @@ describe Rex::MIME::Message do
described_class.new
end
let(:raw_message) do
message = "MIME-Version: 1.0\r\n"
message << "Content-Type: multipart/mixed; boundary=\"_Part_12_3195573780_381739540\"\r\n"
message << "Subject: Pull Request\r\n"
message << "Date: Wed,20 Aug 2014 08:45:38 -0500\r\n"
message << "Message-ID: <WRobqc7gEyQVIQwEkLS7FN3ZNhS1Xj9pU2szC24rggMg@tqUqGjjSLEvssbwm>\r\n"
message << "From: contributor@msfdev.int\r\n"
message << "To: msfdev@msfdev.int\r\n"
message << "\r\n"
message << "--_Part_12_3195573780_381739540\r\n"
message << "Content-Disposition: inline; filename=\"content\"\r\n"
message << "Content-Type: application/octet-stream; name=\"content\"\r\n"
message << "Content-Transfer-Encoding: base64\r\n"
message << "\r\n"
message << "Q29udGVudHM=\r\n"
message << "\r\n"
message << "--_Part_12_3195573780_381739540--\r\n"
message
end
let(:regexp_mail) do
regex = "MIME-Version: 1.0\r\n"
regex << "Content-Type: multipart/mixed; boundary=\"_Part_.*\"\r\n"
regex << "Subject: Pull Request\r\n"
regex << "Date: .*\r\n"
regex << "Message-ID: <.*@.*>\r\n"
regex << "From: contributor@msfdev.int\r\n"
regex << "To: msfdev@msfdev.int\r\n"
regex << "\r\n"
regex << "--_Part_.*\r\n"
regex << "Content-Disposition: inline\r\n"
regex << "Content-Type: text/plain\r\n"
regex << "Content-Transfer-Encoding: base64\r\n"
regex << "\r\n"
regex << "Q29udGVudHM=\r\n"
regex << "\r\n"
regex << "--_Part_.*--\r\n"
Regexp.new(regex)
end
let(:regexp_web) do
regex = "\r\n"
regex << "--_Part_.*\r\n"
regex << "Content-Disposition: form-data; name=\"action\"\r\n"
regex << "\r\n"
regex << "save\r\n"
regex << "--_Part_.*\r\n"
regex << "Content-Disposition: form-data; name=\"file\"; filename=\"test.txt\"\r\n"
regex << "Content-Type: application/octet-stream\r\n"
regex << "\r\n"
regex << "Contents\r\n"
regex << "--_Part_.*\r\n"
regex << "Content-Disposition: form-data; name=\"title\"\r\n"
regex << "\r\n"
regex << "Title\r\n"
regex << "--_Part_.*--\r\n"
Regexp.new(regex)
end
describe "#initialize" do
subject(:header_class) do
subject(:message_class) do
described_class.allocate
end
it "creates a new Rex::MIME::Header" do
header_class.send(:initialize)
expect(header_class.header).to be_a(Rex::MIME::Header)
message_class.send(:initialize)
expect(message_class.header).to be_a(Rex::MIME::Header)
end
it "creates an empty array of parts" do
header_class.send(:initialize)
expect(header_class.parts).to be_empty
message_class.send(:initialize)
expect(message_class.parts).to be_empty
end
it "creates a random bound" do
header_class.send(:initialize)
expect(header_class.bound).to include('_Part_')
message_class.send(:initialize)
expect(message_class.bound).to include('_Part_')
end
it "allows to populate headers from argument" do
message_class.send(:initialize, raw_message)
expect(message_class.header.headers.length).to eq(7)
end
it "allows to create a MIME-Version header from argument" do
message_class.send(:initialize, raw_message)
expect(message_class.header.find('MIME-Version')).to eq(['MIME-Version', '1.0'])
end
it "allows to create a Content-Type header from argument" do
message_class.send(:initialize, raw_message)
expect(message_class.header.find('Content-Type')).to eq(['Content-Type', "multipart/mixed; boundary=\"_Part_12_3195573780_381739540\""])
end
it "allows to create a Subject header from argument" do
message_class.send(:initialize, raw_message)
expect(message_class.header.find('Subject')).to eq(['Subject', 'Pull Request'])
end
it "allows to create a Date header from argument" do
message_class.send(:initialize, raw_message)
expect(message_class.header.find('Date')).to eq(['Date', 'Wed,20 Aug 2014 08:45:38 -0500'])
end
it "allows to create a Message-ID header from argument" do
message_class.send(:initialize, raw_message)
expect(message_class.header.find('Message-ID')).to eq(['Message-ID', '<WRobqc7gEyQVIQwEkLS7FN3ZNhS1Xj9pU2szC24rggMg@tqUqGjjSLEvssbwm>'])
end
it "allows to create a From header from argument" do
message_class.send(:initialize, raw_message)
expect(message_class.header.find('From')).to eq(['From', 'contributor@msfdev.int'])
end
it "allows to create a To header from argument" do
message_class.send(:initialize, raw_message)
expect(message_class.header.find('To')).to eq(['To', 'msfdev@msfdev.int'])
end
it "allows to populate parts from argument" do
message_class.send(:initialize, raw_message)
p "#{message_class.parts.inspect}"
expect(message_class.parts.length).to eq(1)
end
end
@ -264,4 +372,26 @@ describe Rex::MIME::Message do
end
end
describe "#to_s" do
it "returns \\r\\n if Rex::MIME::Message is empty" do
expect(subject.to_s).to eq("\r\n")
end
it "generates valid MIME email messages" do
subject.mime_defaults
subject.from = "contributor@msfdev.int"
subject.to = "msfdev@msfdev.int"
subject.subject = "Pull Request"
subject.add_part(Rex::Text.encode_base64("Contents", "\r\n"), "text/plain", "base64", "inline")
expect(regexp_mail.match(subject.to_s)).to_not be_nil
end
it "generates valid MIME web forms" do
subject.add_part("save", nil, nil, "form-data; name=\"action\"")
subject.add_part("Contents", "application/octet-stream", nil, "form-data; name=\"file\"; filename=\"test.txt\"")
subject.add_part("Title", nil, nil, "form-data; name=\"title\"")
expect(regexp_web.match(subject.to_s)).to_not be_nil
end
end
end