require 'rubygems' require 'test/unit' require 'net/nntp.rb' require 'mock/mock_socket' module TestNet class TestNNTP < Test::Unit::TestCase def setup unless @log @log = Log4r::Logger.new('net::nntp') fileout = Log4r::FileOutputter.new('net::nntp::fileout', :filename => File.join('logs', 'autotest.log')) fileout.formatter = Log4r::PatternFormatter.new(:pattern => '%d [%5l] : %m') @log.add fileout @log.level = Log4r::ALL end nntp_connect_and_auth('localhost', 119, 'dummy', 'test') end def test_new host = 'localhost' port = '119' assert_nothing_raised do @nntp = Net::NNTP.new(host) end assert_nothing_raised do nntp_new(host, port) end end def test_connect assert_nothing_raised do @nntp.connect end end def test_help help = @nntp.help.join assert_equal '100', help[0..2] assert_equal 473, help.length end def test_authenticate assert_nothing_raised do assert nntp_connect_and_auth('localhost', 119, 'dummy', 'test') end end def test_group assert_nothing_raised do group = @nntp.group('at.linux') assert_kind_of Net::NNTP::Group, group assert_equal 'at.linux', group.name assert_equal 12, group.article_count assert_equal 1363, group.article_first assert_equal 1375, group.article_last end end def test_xhdr headers = @nntp.xhdr('at.linux', 'subject', :from => 1363, :to => 1375) assert_equal 15, headers.length assert_equal '221', headers[0][0..2] assert_equal "1364", headers[2].split[0] header = @nntp.xhdr('at.linux', 'subject', :message_id => "").join assert_equal 49, header.length assert_equal "1375", header.split[0] end def test_xhdr_fails header = @nntp.xhdr('at.linux', '', {}).join assert_equal '502', header[0..2] assert_raise(Net::NNTP::CommandFailedError) do header = @nntp.xhdr('at.xxx', 'subject', {}) end end def test_group_fails assert_raise(Net::NNTP::CommandFailedError) do group = @nntp.group('at.xxx') end end def test_xover overviews = @nntp.xover('at.linux', :from => 1363, :to => 1375) assert_equal 15, overviews.length assert_equal '224', overviews[0][0..2] end def test_over_or_xover assert @nntp.has_xover? assert @nntp.has_over? end def test_xover_fails over = @nntp.xover('at.linux', :from => 1363, :to => 1375) over = @nntp.xover('at.linux', :message_id => "").join assert_equal '502', over[0..2] assert_raises(Net::NNTP::CommandFailedError) do over = @nntp.xover('at.xxx', :message_id => "") end end def test_listgroup list = @nntp.listgroup('at.linux') assert_equal 15, list.length match = /(\d{3}).*f\or\s+(\S+)\s/.match(list[0]) assert_equal "211", match[1] assert_equal "at.linux", match[2] assert_equal "1365\r\n", list[3] end def test_list assert_respond_to @nntp, :list assert_nothing_raised do @nntp.list('overview.fmt') assert_equal 9, @nntp.overview_format.length @nntp.list() assert_not_equal 0, @nntp.grouplist.length assert_not_nil @nntp.grouplist assert_equal 'at.test', @nntp.grouplist['at.test'].name @nntp.list('active') assert_equal 'at.test', @nntp.grouplist['at.test'].name @nntp.list('active.times') @nntp.list('active.times', 'at.*') end assert_raise Net::NNTP::CommandFailedError do @nntp.list('distrib.pat', 'at.*') end end def test_article assert_raises Net::NNTP::CommandFailedError do article = @nntp.article end article = @nntp.article("<5d6be$4625ae23$51df8a12$32566@news.inode.at>") response, id, msgid = article[0].split assert_equal '220', response assert_equal '1392', id assert_equal "<5d6be$4625ae23$51df8a12$32566@news.inode.at>", msgid group = @nntp.group 'at.linux' article = @nntp.article '1392' response, id, msgid = article[0].split assert_equal '220', response assert_equal '1392', id assert_equal "<5d6be$4625ae23$51df8a12$32566@news.inode.at>", msgid end def test_head nntp_connect_and_auth('localhost', 119, 'dummy', 'test') assert_nothing_raised do assert_respond_to @nntp, :head end assert_raise Net::NNTP::CommandFailedError do head = @nntp.head end @nntp.group 'at.linux' @io = [ "221 1430 <462dfa6f$0$23135$9b4e6d93@newsspool1.arcor-online.net> article retrieved - head follows\r\n", "Path: vietwist00.chello.at!newsfeed02.chello.at!newsfeed01.chello.at!newsfeed.arcor.de!newsspool1.arcor-online.net!news.arcor.de.POSTED!not-for-mail\r\n", "Message-ID: <462dfa6f$0$23135$9b4e6d93@newsspool1.arcor-online.net>\r\n", "From: Gerhard Engler \r\n", "Newsgroups: at.linux\r\n", "Subject: Re: udev_node_mknod: /dev/capi Operation not permitted\r\n", "Date: Tue, 24 Apr 2007 14:39:11 +0200\r\n", "References: <4629ee10$0$10187$9b4e6d93@newsspool4.arcor-online.net> \r\n", "Lines: 26\r\n", "User-Agent: Thunderbird 1.5.0.10 (Windows/20070221)\r\n", "MIME-Version: 1.0\r\n", "In-Reply-To: \r\n", "Content-Type: text/plain; charset=ISO-8859-15; format=flowed\r\n", "Content-Transfer-Encoding: 7bit\r\n", "Organization: Arcor\r\n", "NNTP-Posting-Date: 24 Apr 2007 14:39:11 CEST\r\n", "NNTP-Posting-Host: 6cf11d8c.newsspool1.arcor-online.net\r\n", "X-Trace: DXC=Cd=D_AR>:`a^Y=RbYBPl4`ic==]BZ:afn4Fo<]lROoRaFl8W>\BH3Yb7K@fQgPi`FgUTEAfnAR\Ta@JWJ8E:^d', msgid end def test_body assert_raises Net::NNTP::CommandFailedError do body = @nntp.body end body = @nntp.body("<5d6be$4625ae23$51df8a12$32566@news.inode.at>") response, id, msgid = body[0].split assert_equal '222', response assert_equal '1392', id assert_equal "<5d6be$4625ae23$51df8a12$32566@news.inode.at>", msgid group = @nntp.group 'at.linux' body = @nntp.body '1392' response, id, msgid = body[0].split assert_equal '222', response assert_equal '1392', id assert_equal "<5d6be$4625ae23$51df8a12$32566@news.inode.at>", msgid end def test_stat assert_nothing_raised do assert_respond_to @nntp, :stat end end def test_last assert_raise Net::NNTP::ProtocolError do last = @nntp.last end @nntp.group 'at.linux' assert_raise Net::NNTP::CommandFailedError do last = @nntp.last end @nntp.next @nntp.next last = @nntp.last assert_kind_of String, last response, article, msgid, rest = last.split assert_equal "223", response assert_equal "1363", article end def test_next assert_raise Net::NNTP::ProtocolError do anext = @nntp.next end @nntp.group 'at.linux' anext = @nntp.next assert_kind_of String, anext response, article, msgid, rest = anext.split assert_equal "223", response assert_equal "1363", article assert_equal "", msgid anext = @nntp.next response, article, msgid, rest = anext.split assert_equal "1364", article end private def nntp_new(host, port=119) @nntp = Net::NNTP.new(host, port) end def nntp_connect_and_auth(host, port, user, pass) @nntp = Net::NNTP.new(host, port) @nntp.instance_variable_set :@socket_class, MockSocket @nntp.connect @nntp.socket.activate @nntp.authenticate(user, pass) end end end