From 1e051771c155ae627446d3b4838d75c7e3364490 Mon Sep 17 00:00:00 2001 From: Alex Shulgin Date: Tue, 13 Aug 2013 08:38:25 -0700 Subject: [PATCH] Fix Message comparison method to ensure there's no side-effects. Comparing a Message w/o message_id set to another object would set it's Message-ID to a temporary value. Ditto for the object compared to. --- lib/mail/message.rb | 9 ++++++--- spec/mail/message_spec.rb | 30 ++++++++++-------------------- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/lib/mail/message.rb b/lib/mail/message.rb index 11a42d407..8dfa97c15 100644 --- a/lib/mail/message.rb +++ b/lib/mail/message.rb @@ -357,9 +357,12 @@ def ==(other) else self_message_id, other_message_id = self.message_id, other.message_id self.message_id, other.message_id = '', '' - result = self.encoded == other.encoded - self.message_id = "<#{self_message_id}>" if self_message_id - other.message_id = "<#{other_message_id}>" if other_message_id + begin + result = self.encoded == other.encoded + ensure + self.message_id = self_message_id + other.message_id = other_message_id + end result end end diff --git a/spec/mail/message_spec.rb b/spec/mail/message_spec.rb index a033d1363..70d750631 100644 --- a/spec/mail/message_spec.rb +++ b/spec/mail/message_spec.rb @@ -1340,44 +1340,34 @@ def message_with_iso_8859_1_charset m1 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there") m2 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there") m1.should eq m2 + # confirm there are no side-effects in the comparison + m1.message_id.should be_nil + m2.message_id.should be_nil end it "should ignore the message id value if self has a nil message id" do m1 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there") m2 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there") m1.should eq m2 + # confirm there are no side-effects in the comparison + m1.message_id.should be_nil + m2.message_id.should eq '1234@test.lindsaar.net' end it "should ignore the message id value if other has a nil message id" do m1 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there") m2 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there") m1.should eq m2 + # confirm there are no side-effects in the comparison + m1.message_id.should eq '1234@test.lindsaar.net' + m2.message_id.should be_nil end it "should not be == if both emails have different Message IDs" do m1 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <4321@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there") m2 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there") m1.should_not eq m2 - end - - it "should preserve the message id of self if set" do - m1 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there") - m2 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there") - (m1 == m2).should be_true # confirm the side-effects of the comparison - m1.message_id.should eq '1234@test.lindsaar.net' - end - - it "should preserve the message id of other if set" do - m1 = Mail.new("To: mikel@test.lindsaar.net\r\nSubject: Yo!\r\n\r\nHello there") - m2 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there") - (m1 == m2).should be_true # confirm the side-effects of the comparison - m2.message_id.should eq '1234@test.lindsaar.net' - end - - it "should preserve the message id of both if set" do - m1 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <4321@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there") - m2 = Mail.new("To: mikel@test.lindsaar.net\r\nMessage-ID: <1234@test.lindsaar.net>\r\nSubject: Yo!\r\n\r\nHello there") - (m1 == m2).should be_false # confirm the side-effects of the comparison + # confirm there are no side-effects in the comparison m1.message_id.should eq '4321@test.lindsaar.net' m2.message_id.should eq '1234@test.lindsaar.net' end