e$B@>;3OB9-$G$9!#e(B
e$B0lEYe([email protected]$B$KAw$j$^$7$?$,!"4{$K8x3+$5$l$F$$$k@H<e@-$Ke(B
e$B$D$$$F$NOC$J$N$G!"e(Bruby-deve$B$G$$$$$N$G$O$J$$$+$H$$$&$3$H$N$h$&$J$N$Ge(B
ruby-deve$B$KAw$jD>$7$F$$$^$9!#e(B
http://sylpheed.sraoss.jp/diary/?date=20080926#p01
e$B$r$_$F!“e(Bnet/pop.rbe$B$be(Bsylpheede$B$HF1$8$h$&$JBP1~$re(B
e$B$9$l$P$$$$$N$G$O$J$$$+$H;W$C$?$N$G$9$,!”$I$&$G$7$g$&$+e(B?
1.8e$B$G$Oe(BNet::POP3Command#apope$B$Ge(B
if /[^!-~]/n =~ @apop_stamp || /@/ !~ @apop_stamp
raise POPAuthenticationError, ‘Invalid APOP server; cannot
login’
end
e$B$N$h$&$J%A%'%C%/$K$J$k$H;W$$$^$9!#e(B
(1.9e$B$G$Oe(Bne$B$r$O$:$9$@$1e(B?)
e$B0J2<$Oe(Btest/net/imap/test_imap.rbe$B$r;29M$K$7$F:n@.$7$?%F%9%H$G$9!#e(B
require ‘net/pop’
require ‘test/unit’
require ‘digest/md5’
class TestPOP < Test::Unit::TestCase
def setup
@users = {‘user’ => ‘pass’ }
@ok_user = ‘user’
@stamp_base = “#{$$}.#{Time.now.to_i}@localhost”
end
def test_pop_auth_ok
pop_test(false) do |pop|
assert_instance_of Net::POP3, pop
assert_nothing_raised do
pop.start(@ok_user, @users[@ok_user])
end
end
end
def test_pop_auth_ng
pop_test(false) do |pop|
assert_instance_of Net::POP3, pop
assert_raise Net::POPAuthenticationError do
pop.start(@ok_user, ‘bad password’)
end
end
end
def test_apop_ok
pop_test(@stamp_base) do |pop|
assert_instance_of Net::APOP, pop
assert_nothing_raised do
pop.start(@ok_user, @users[@ok_user])
end
end
end
def test_apop_ng
pop_test(@stamp_base) do |pop|
assert_instance_of Net::APOP, pop
assert_raise Net::POPAuthenticationError do
pop.start(@ok_user, ‘bad password’)
end
end
end
def test_apop_invalid
pop_test(“\x80”+@stamp_base) do |pop|
assert_instance_of Net::APOP, pop
assert_raise Net::POPAuthenticationError do
pop.start(@ok_user, @users[@ok_user])
end
end
end
def test_apop_invalid_at
pop_test(@stamp_base.sub(‘@’, ‘.’)) do |pop|
assert_instance_of Net::APOP, pop
assert_raise Net::POPAuthenticationError do
pop.start(@ok_user, @users[@ok_user])
end
end
end
def pop_test(apop=false)
host = ‘localhost’
server = TCPServer.new(host, 0)
port = server.addr[1]
thread = Thread.start do
sock = server.accept
begin
pop_server_loop(sock, apop)
ensure
sock.close
end
end
begin
pop = Net::POP3::APOP(apop).new(host, port)
#pop.set_debug_output $stderr
yield pop
ensure
begin
pop.finish
rescue IOError
raise unless $!.message == “POP session not yet started”
end
end
ensure
server.close
thread.value
end
def pop_server_loop(sock, apop)
if apop
sock.print “+OK ready <#{apop}>\r\n”
else
sock.print “+OK ready\r\n”
end
user = nil
while line = sock.gets
case line
when /^USER (.+)\r\n/
user = $1
if @users.key?(user)
sock.print “+OK\r\n”
else
sock.print “-ERR unknown user\r\n”
end
when /^PASS (.+)\r\n/
if @users[user] == $1
sock.print “+OK\r\n”
else
sock.print “-ERR invalid password\r\n”
end
when /^APOP (.+) (.+)\r\n/
user = $1
if apop && Digest::MD5.hexdigest(“<#{apop}>#{@users[user]}”) ==
$2
sock.print “+OK\r\n”
else
sock.print “-ERR authentication failed\r\n”
end
when /^QUIT/
sock.print “+OK bye\r\n”
return
else
sock.print “-ERR command not recognized\r\n”
return
end
end
end
end
END
module Net
class POP3Command
def apop(account, password)
raise POPAuthenticationError, ‘not APOP server; cannot login’
unless @apop_stamp
if /[^!-~]/n =~ @apop_stamp || /@/ !~ @apop_stamp
raise POPAuthenticationError, ‘Invalid APOP server; cannot
login’
end
check_response_auth(critical {
get_response(‘APOP %s %s’,
account,
Digest::MD5.hexdigest(@apop_stamp + password))
})
end
end
end