
from mock import Mock

from twisted.trial import unittest

from txtorcon._microdesc_parser import MicrodescriptorParser


class StateTests(unittest.TestCase):

    def test_two_no_w(self):
        relays = []

        def create_relay(**kw):
            relays.append(kw)
        m = MicrodescriptorParser(create_relay)

        for line in [
                'r fake YkkmgCNRV1/35OPWDvo7+1bmfoo tanLV/4ZfzpYQW0xtGFqAa46foo 2011-12-12 16:29:16 11.11.11.11 443 80',
                'r ekaf foooooooooooooooooooooooooo barbarbarbarbarbarbarbarbar 2011-11-11 16:30:00 22.22.22.22 443 80',
                's Exit Fast Guard HSDir Named Running Stable V2Dir Valid FutureProof',
        ]:
            m.feed_line(line)
        m.done()

        self.assertEqual(2, len(relays))
        self.assertEqual('fake', relays[0]['nickname'])
        self.assertEqual('ekaf', relays[1]['nickname'])
        self.assertEqual('11.11.11.11', relays[0]['ip'])
        self.assertEqual('22.22.22.22', relays[1]['ip'])

        self.assertTrue('bandwidth' not in relays[0])
        self.assertTrue('bandwidth' not in relays[1])
        self.assertTrue('flags' not in relays[0])
        self.assertTrue('flags' in relays[1])
        self.assertTrue('FutureProof' in relays[1]['flags'])

    def test_two_no_w_no_s(self):
        relays = []

        def create_relay(**kw):
            relays.append(kw)
        m = MicrodescriptorParser(create_relay)

        for line in [
                'r fake YkkmgCNRV1/35OPWDvo7+1bmfoo tanLV/4ZfzpYQW0xtGFqAa46foo 2011-12-12 16:29:16 11.11.11.11 443 80',
                'r ekaf foooooooooooooooooooooooooo barbarbarbarbarbarbarbarbar 2011-11-11 16:30:00 22.22.22.22 443 80',
        ]:
            m.feed_line(line)
        m.done()

        self.assertEqual(2, len(relays))
        self.assertEqual('fake', relays[0]['nickname'])
        self.assertEqual('ekaf', relays[1]['nickname'])
        self.assertEqual('11.11.11.11', relays[0]['ip'])
        self.assertEqual('22.22.22.22', relays[1]['ip'])

        self.assertTrue('bandwidth' not in relays[0])
        self.assertTrue('bandwidth' not in relays[1])
        self.assertTrue('flags' not in relays[0])
        self.assertTrue('flags' not in relays[1])

    def test_two(self):
        relays = []

        def create_relay(**kw):
            relays.append(kw)
        m = MicrodescriptorParser(create_relay)

        for line in [
                'r fake YkkmgCNRV1/35OPWDvo7+1bmfoo tanLV/4ZfzpYQW0xtGFqAa46foo 2011-12-12 16:29:16 11.11.11.11 443 80',
                'r ekaf foooooooooooooooooooooooooo barbarbarbarbarbarbarbarbar 2011-11-11 16:30:00 22.22.22.22 443 80',
                's Exit Fast Guard HSDir Named Running Stable V2Dir Valid FutureProof',
                'w Bandwidth=518000',
                'p accept 43,53,79-81',
        ]:
            m.feed_line(line)
        m.done()

        self.assertEqual(2, len(relays))
        self.assertEqual('fake', relays[0]['nickname'])
        self.assertEqual('ekaf', relays[1]['nickname'])
        self.assertEqual('11.11.11.11', relays[0]['ip'])
        self.assertEqual('22.22.22.22', relays[1]['ip'])

        self.assertTrue('bandwidth' not in relays[0])
        self.assertTrue('bandwidth' in relays[1])
        self.assertTrue('flags' not in relays[0])
        self.assertTrue('flags' in relays[1])
        self.assertTrue('FutureProof' in relays[1]['flags'])

@@ -3380,28 +3380,388 @@
 oof' in relays%5B1%5D%5B'flags'%5D)%0A
+%0A    def test_bad_line(self):%0A        relays = %5B%5D%0A%0A        def create_relay(**kw):%0A            relays.append(kw)%0A        m = MicrodescriptorParser(create_relay)%0A%0A        with self.assertRaises(Exception) as ctx:%0A            m.feed_line('x blam')%0A        self.assertTrue('Unknown microdescriptor' in str(ctx.exception))%0A        self.assertEqual(0, len(relays))%0A

test error case