-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from linyows/refactoring
Refactoring
- Loading branch information
Showing
2 changed files
with
154 additions
and
97 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
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 | Diff line number | Diff line change |
---|---|---|
|
@@ -5,73 +5,83 @@ import ( | |
"time" | ||
) | ||
|
||
func TestPairing(t *testing.T) { | ||
func TestSetSenderServerName(t *testing.T) { | ||
var tests = []struct { | ||
arg []byte | ||
expectSenderServer []byte | ||
expectSenderAddr []byte | ||
expectReceiverServer []byte | ||
expectReceiverAddr []byte | ||
arg []byte | ||
expectSenderServer []byte | ||
}{ | ||
{ | ||
arg: []byte("EHLO mx.example.local\r\n"), | ||
expectSenderServer: []byte("mx.example.local"), | ||
}, | ||
{ | ||
arg: []byte("HELO mx.example.local\r\n"), | ||
expectSenderServer: []byte("mx.example.local"), | ||
}, | ||
} | ||
for _, v := range tests { | ||
pipe := &Pipe{afterCommHook: func(b Data, to Direction) {}} | ||
pipe.setSenderServerName(v.arg) | ||
if string(v.expectSenderServer) != string(pipe.sServerName) { | ||
t.Errorf("sender server name expected %s, but got %s", v.expectSenderServer, pipe.sServerName) | ||
} | ||
} | ||
} | ||
|
||
func TestSetSenderMailAddress(t *testing.T) { | ||
var tests = []struct { | ||
arg []byte | ||
expectSenderAddr []byte | ||
}{ | ||
{ | ||
arg: []byte("EHLO mx.example.local\r\n"), | ||
expectSenderServer: []byte("mx.example.local"), | ||
expectSenderAddr: nil, | ||
expectReceiverServer: nil, | ||
expectReceiverAddr: nil, | ||
arg: []byte("MAIL FROM:<[email protected]> SIZE=4095\r\n"), | ||
expectSenderAddr: []byte("[email protected]"), | ||
}, | ||
{ | ||
arg: []byte("HELO mx.example.local\r\n"), | ||
expectSenderServer: []byte("mx.example.local"), | ||
expectSenderAddr: nil, | ||
expectReceiverServer: nil, | ||
expectReceiverAddr: nil, | ||
// Sender Rewriting Scheme | ||
arg: []byte("MAIL FROM:<SRS0=x/[email protected]> SIZE=4095\r\n"), | ||
expectSenderAddr: []byte("SRS0=x/[email protected]"), | ||
}, | ||
{ | ||
arg: []byte("MAIL FROM:<[email protected]> SIZE=4095\r\n"), | ||
expectSenderServer: nil, | ||
expectSenderAddr: []byte("[email protected]"), | ||
expectReceiverServer: nil, | ||
expectReceiverAddr: nil, | ||
// Pipelining | ||
arg: []byte("MAIL FROM:<[email protected]> SIZE=4095\r\nRCPT TO:<[email protected]> ORCPT=rfc822;[email protected]\r\nDATA\r\n"), | ||
expectSenderAddr: []byte("[email protected]"), | ||
}, | ||
} | ||
for _, v := range tests { | ||
pipe := &Pipe{afterCommHook: func(b Data, to Direction) {}} | ||
pipe.setSenderMailAddress(v.arg) | ||
if string(v.expectSenderAddr) != string(pipe.sMailAddr) { | ||
t.Errorf("sender email address expected %s, but got %s", v.expectSenderAddr, pipe.sMailAddr) | ||
} | ||
} | ||
} | ||
|
||
func TestSetReceiverMailAddressAndServerName(t *testing.T) { | ||
var tests = []struct { | ||
arg []byte | ||
expectReceiverServer []byte | ||
expectReceiverAddr []byte | ||
}{ | ||
{ | ||
arg: []byte("RCPT TO:<[email protected]>\r\n"), | ||
expectSenderServer: nil, | ||
expectSenderAddr: nil, | ||
expectReceiverServer: []byte("example.com"), | ||
expectReceiverAddr: []byte("[email protected]"), | ||
}, | ||
{ | ||
// Sender Rewriting Scheme | ||
arg: []byte("MAIL FROM:<SRS0=x/[email protected]> SIZE=4095\r\n"), | ||
expectSenderServer: nil, | ||
expectSenderAddr: []byte("SRS0=x/[email protected]"), | ||
expectReceiverServer: nil, | ||
expectReceiverAddr: nil, | ||
}, | ||
{ | ||
// Pipelining | ||
arg: []byte("MAIL FROM:<[email protected]> SIZE=4095\r\nRCPT TO:<[email protected]> ORCPT=rfc822;[email protected]\r\nDATA\r\n"), | ||
expectSenderServer: nil, | ||
expectSenderAddr: []byte("[email protected]"), | ||
expectReceiverServer: []byte("example.com"), | ||
expectReceiverAddr: []byte("[email protected]"), | ||
}, | ||
} | ||
for _, v := range tests { | ||
pipe := &Pipe{afterCommHook: func(b Data, to Direction) {}} | ||
pipe.pairing(v.arg) | ||
|
||
if v.expectSenderServer != nil && string(v.expectSenderServer) != string(pipe.sServerName) { | ||
t.Errorf("sender server name expected %s, but got %s", v.expectSenderServer, pipe.sServerName) | ||
} | ||
if v.expectSenderAddr != nil && string(v.expectSenderAddr) != string(pipe.sMailAddr) { | ||
t.Errorf("sender email address expected %s, but got %s", v.expectSenderAddr, pipe.sMailAddr) | ||
} | ||
if v.expectReceiverServer != nil && string(v.expectReceiverServer) != string(pipe.rServerName) { | ||
pipe.setReceiverMailAddressAndServerName(v.arg) | ||
if string(v.expectReceiverServer) != string(pipe.rServerName) { | ||
t.Errorf("receiver server name expected %s, but got %s", v.expectReceiverServer, pipe.rServerName) | ||
} | ||
if v.expectReceiverAddr != nil && string(v.expectReceiverAddr) != string(pipe.rMailAddr) { | ||
if string(v.expectReceiverAddr) != string(pipe.rMailAddr) { | ||
t.Errorf("receiver email address expected %s, but got %s", v.expectReceiverAddr, pipe.rMailAddr) | ||
} | ||
} | ||
|
@@ -87,6 +97,16 @@ func TestIsResponseOfEHLOWithStartTLS(t *testing.T) { | |
} | ||
} | ||
|
||
func TestIsResponseOfEHLOWithoutStartTLS(t *testing.T) { | ||
pipe := &Pipe{ | ||
tls: false, | ||
locked: false, | ||
} | ||
if !pipe.isResponseOfEHLOWithoutStartTLS([]byte("250-example.test\r\n250-PIPELINING\r\n250-8BITMIME\r\n250 SIZE 41943040\r\n")) { | ||
t.Errorf("expected true, but got false") | ||
} | ||
} | ||
|
||
func TestIsResponseOfReadyToStartTLS(t *testing.T) { | ||
pipe := &Pipe{ | ||
tls: false, | ||
|