#!/usr/bin/env python
###########################################################################
# guidedogdoctest.py - description                                        #
# ------------------------------                                          #
# begin     : Wed Oct 11 2006                                             #
# copyright : (C) 2006 by Simon Edwards                                   #
# email     : simon@simonzone.com                                         #
#                                                                         #
###########################################################################
#                                                                         #
#   This program is free software; you can redistribute it and/or modify  #
#   it under the terms of the GNU General Public License as published by  #
#   the Free Software Foundation; either version 2 of the License, or     #
#   (at your option) any later version.                                   #
#                                                                         #
###########################################################################

import unittest
import guidedogdoc
import os

TEST_GUIDEDOG_FILENAME = "test_rc.guidedog"

class TestIPRange(unittest.TestCase):

    def setUp(self):
        pass
    
    def testIPRangeTestCreate(self):
        range = guidedogdoc.IPRange()
        self.assert_(range.getType()==guidedogdoc.IPRange.INVALID)

    def testIPRangeTestDomainname(self):
        range = guidedogdoc.IPRange("www.simonzone.com")
        self.assert_(range.getType()==guidedogdoc.IPRange.DOMAIN_NAME)
    
    def testIPRangeTestIp(self):
        range = guidedogdoc.IPRange("123.34.56.78")
        self.assert_(range.getType()==guidedogdoc.IPRange.IP)
    
    def testIPRangeTestIp2(self):
        range = guidedogdoc.IPRange("0.0.0.0")
        self.assert_(range.getType()==guidedogdoc.IPRange.IP)

    def testIPRangeTestIp3(self):
        range = guidedogdoc.IPRange("255.255.255.255")
        self.assert_(range.getType()==guidedogdoc.IPRange.IP)

    def testIPRangeTestIpInvalid(self):
        range = guidedogdoc.IPRange("256.255.255.255")
        self.assert_(range.getType()==guidedogdoc.IPRange.INVALID)

    def testIPRangeTestIpInvalid2(self):
        range = guidedogdoc.IPRange(".255.255.255")
        self.assert_(range.getType()==guidedogdoc.IPRange.INVALID)
    
    def testIPRangeTestIpRange(self):
        range = guidedogdoc.IPRange("123.34.56.78/24")
        self.assert_(range.getType()==guidedogdoc.IPRange.IP_RANGE)

    def testIPRangeTestIpRangeInvalid(self):
        range = guidedogdoc.IPRange("123.34.56.78/33")
        self.assert_(range.getType()==guidedogdoc.IPRange.INVALID)

    def testIPRangeTestIpRange2(self):
        range = guidedogdoc.IPRange("123.34.56.78/255.255.0.0")
        self.assert_(range.getType()==guidedogdoc.IPRange.IP_RANGE)

    def testIPRangeTestIpRange2Invalid(self):
        range = guidedogdoc.IPRange("123.34.56.78/255.255.0.0.0")
        self.assert_(range.getType()==guidedogdoc.IPRange.INVALID)
        
    def testIPRangeTestInvalid(self):
        range = guidedogdoc.IPRange("123.34.56.78.34")
        self.assert_(range.getType()==guidedogdoc.IPRange.INVALID)

    def testIPRangeTestInvalid2(self):
        range = guidedogdoc.IPRange("1.simonzone.com")
        self.assert_(range.getType()==guidedogdoc.IPRange.INVALID)

    def testIPRangeTestInvalid3(self):
        range = guidedogdoc.IPRange("www.simonzone.com ")
        self.assert_(range.getType()==guidedogdoc.IPRange.INVALID)
        
    def testIPRangeTestInvalid4(self):
        range = guidedogdoc.IPRange("www.123.com ")
        self.assert_(range.getType()==guidedogdoc.IPRange.INVALID)
    
class TestGuidedogDoc(unittest.TestCase):

    def setUp(self):
        if os.path.exists(TEST_GUIDEDOG_FILENAME):
            os.remove(TEST_GUIDEDOG_FILENAME)
    
    def testGuidedogDocCreate(self):
        doc = guidedogdoc.GuidedogDoc()
        
    def testGuidedogDocRead(self):
        doc = guidedogdoc.GuidedogDoc()
        doc.openScript("Cpptest_rc.guidedog")

    def testGuidedogDocWrite(self):
        doc = guidedogdoc.GuidedogDoc()
        (rc,errorstring) = doc.saveScript(TEST_GUIDEDOG_FILENAME)
        self.assert_(rc)
        
    def testGuidedogDocWriteDisabled(self):
        doc = guidedogdoc.GuidedogDoc()
        doc.setDisabled(True)
        (rc,errorstring) = doc.saveScript(TEST_GUIDEDOG_FILENAME)
        newdoc = guidedogdoc.GuidedogDoc()
        (rc,errorstring) = newdoc.openScript(TEST_GUIDEDOG_FILENAME)
        self.assert_(newdoc.isDisabled())

    def testGuidedogDocWriteDescription(self):
        doc = guidedogdoc.GuidedogDoc()
        description = "This is a description.\n--Simon\n"
        doc.setDescription(description)
        (rc,errorstring) = doc.saveScript(TEST_GUIDEDOG_FILENAME)
        newdoc = guidedogdoc.GuidedogDoc()
        (rc,errorstring) = newdoc.openScript(TEST_GUIDEDOG_FILENAME)
        self.assert_(newdoc.getDescription()==description)
        
    def testGuidedogDocWriteFlags(self):
        doc = guidedogdoc.GuidedogDoc()
        doc.setRouting(True)
        doc.setMasquerade(True)
        doc.setMasqueradeFTP(True)    
        doc.setMasqueradeIRC(True)
        (rc,errorstring) = doc.saveScript(TEST_GUIDEDOG_FILENAME)
        
        newdoc = guidedogdoc.GuidedogDoc()
        (rc,errorstring) = newdoc.openScript(TEST_GUIDEDOG_FILENAME)
        self.assert_(newdoc.isRouting())
        self.assert_(newdoc.isMasquerade())
        self.assert_(newdoc.isMasqueradeFTP())
        self.assert_(newdoc.isMasqueradeIRC())

    def testGuidedogDocWriteFlags2(self):
        doc = guidedogdoc.GuidedogDoc()
        doc.setRouting(False)
        doc.setMasquerade(False)
        doc.setMasqueradeFTP(False)    
        doc.setMasqueradeIRC(False)
        (rc,errorstring) = doc.saveScript(TEST_GUIDEDOG_FILENAME)
        
        newdoc = guidedogdoc.GuidedogDoc()
        (rc,errorstring) = newdoc.openScript(TEST_GUIDEDOG_FILENAME)
        self.assert_(not newdoc.isRouting())
        self.assert_(not newdoc.isMasquerade())
        self.assert_(not newdoc.isMasqueradeFTP())
        self.assert_(not newdoc.isMasqueradeIRC())
        
    def testGuidedogDocWriteForwardRule(self):
        doc = guidedogdoc.GuidedogDoc()
        
        forwardrule = guidedogdoc.PortForwardRule()
        
        addy = "www.simonzone.com"
        forwardrule.originaladdress = guidedogdoc.IPRange(addy)
        
        port = 80
        forwardrule.originalport = port
        
        comment = "This is the comment."
        forwardrule.comment = comment

        forwardrule.specifyoriginal = True
        forwardrule.specifynewaddress = True
        
        addy2 = "www.slashdot.org"
        forwardrule.newaddress = guidedogdoc.IPRange(addy2)
        forwardrule.iptype = guidedogdoc.PortForwardRule.IPPROTO_UDP
        
        newport = 1080
        forwardrule.newport = newport
        
        doc.forwardrulelist.append(forwardrule)
        (rc,errorstring) = doc.saveScript(TEST_GUIDEDOG_FILENAME)
        
        newdoc = guidedogdoc.GuidedogDoc()
        (rc,errorstring) = newdoc.openScript(TEST_GUIDEDOG_FILENAME)
        self.assert_(len(newdoc.forwardrulelist)==1)
        self.assert_(newdoc.forwardrulelist[0].specifyoriginal)
        self.assert_(newdoc.forwardrulelist[0].originaladdress.getAddress()==addy)
        self.assert_(newdoc.forwardrulelist[0].originalport==port)
        self.assert_(newdoc.forwardrulelist[0].comment==comment)
        self.assert_(newdoc.forwardrulelist[0].specifynewaddress)
        self.assert_(newdoc.forwardrulelist[0].iptype == guidedogdoc.PortForwardRule.IPPROTO_UDP)
        self.assert_(newdoc.forwardrulelist[0].newport==newport)
        self.assert_(newdoc.forwardrulelist[0].newaddress.getAddress()==addy2)
        
    def testGuidedogDocWriteNoMasquerade(self):
        doc = guidedogdoc.GuidedogDoc()
        
        addy = "www.simonzone.com"
        doc.nomasqueradelist.append(guidedogdoc.IPRange(addy))
        
        addy2 = "www.slashdot.org"
        doc.nomasqueradelist.append(guidedogdoc.IPRange(addy2))        
        
        (rc,errorstring) = doc.saveScript(TEST_GUIDEDOG_FILENAME)
        
        newdoc = guidedogdoc.GuidedogDoc()
        (rc,errorstring) = newdoc.openScript(TEST_GUIDEDOG_FILENAME)
        self.assert_(len(newdoc.nomasqueradelist)==2)
        self.assert_(newdoc.nomasqueradelist[0].getAddress()==addy)
        self.assert_(newdoc.nomasqueradelist[1].getAddress()==addy2)
        
if __name__ == '__main__':
    unittest.main()
