# spf.test - Copyright (C) 2004 Pat Thoyts <patthoyts@users.sourceforge.net>
#
# Tests for the Tcllib SPF package
#
# -------------------------------------------------------------------------
# See the file "license.terms" for information on usage and redistribution
# of this file, and for a DISCLAIMER OF ALL WARRANTIES.
# -------------------------------------------------------------------------
# RCS: @(#) $Id: spf.test,v 1.1 2004/07/01 12:25:14 patthoyts Exp $

# -------------------------------------------------------------------------
# Initialise the test package
#
if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import ::tcltest::*
}

# -------------------------------------------------------------------------
# Ensure we test _this_ local copy and one installed somewhere else.
#
package forget spf
catch {namespace delete ::spf}
if {[catch {source [file join [file dirname [info script]] spf.tcl]} msg]} {
    puts "skipped [file tail [info script]]: $msg"
    return
}

# -------------------------------------------------------------------------
# Helpers
# -------------------------------------------------------------------------

# These tests do not make any network calls. Instead we emulate the
# DNS query results wiht the following functions.

foreach cmd [list SPF TXT A PTR MX] {
    catch {rename ::spf::$cmd ::spf::tmp_$cmd}
}
proc ::spf::A {name} { return 192.168.0.4 }
proc ::spf::PTR {addr} { return testhost.local.net }
proc ::spf::MX {domain} { return {{10 mx1.local.net} {20 mx2.local.net}} }
proc ::spf::TXT {domain} { return "Only mail from local hosts permitted." }
proc ::spf::SPF {domain} { return "v=spf1 ?all" }

# -------------------------------------------------------------------------
# Tests
# -------------------------------------------------------------------------

test spf-1.1 {a directive: fallthrough} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 a -all"
    } r] $r
} {0 -}

test spf-1.2 {a directive: fallthrough} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 a ?all"
    } r] $r
} {0 ?}

test spf-1.3 {a directive: matching subnet} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 a/24 ?all"
    } r] $r
} {0 +}

test spf-1.3 {a directive: rejected matching subnet} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 -a/24 ?all"
    } r] $r
} {0 ?}

test spf-1.4 {a directive: match host} {
    list [catch {
        spf::Spf 192.168.0.4 local.net "v=spf1 a ?all"
    } r] $r
} {0 +}

test spf-2.1 {mx directive: fail mx} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 mx ?all"
    } r] $r
} {0 ?}

test spf-2.2 {mx directive: match mx subnet} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 mx/24 ?all"
    } r] $r
} {0 +}

test spf-2.3 {mx directive: fail match explict mx} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 mx:mail.local.net ?all"
    } r] $r
} {0 ?}

test spf-2.4 {mx directive: match explict mx} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 mx:mail.local.net/24 ?all"
    } r] $r
} {0 +}

test spf-2.4 {mx directive: match explict mx} {
    list [catch {
        spf::Spf 192.168.0.4 local.net "v=spf1 mx:mail.local.net ?all"
    } r] $r
} {0 +}

test spf-3.1 {ptr directive} {
    list [catch {
        spf::Spf 192.168.0.4 local.net "v=spf1 ptr ?all"
    } r] $r
} {0 +}

test spf-3.2 {ptr directive} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 ptr ?all"
    } r] $r
} {0 ?}

test spf-3.3 {ptr directive} {
    list [catch {
        spf::Spf 192.168.0.4 local.net "v=spf1 ptr:local.net ?all"
    } r] $r
} {0 +}

test spf-3.4 {ptr directive} {
    list [catch {
        spf::Spf 192.168.0.4 local.net "v=spf1 ptr:local.com ?all"
    } r] $r
} {0 ?}

test spf-4.1 {ip4 directive} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 ip4:192.168.0.0/32 ?all"
    } r] $r
} {0 ?}

test spf-4.2 {ip4 directive} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 ip4:192.168.0.0/24 ?all"
    } r] $r
} {0 +}

test spf-4.3 {ip4 directive} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 ip4:192.168.0.0/16 ?all"
    } r] $r
} {0 +}

test spf-4.4 {ip4 directive} {
    list [catch {
        spf::Spf 192.255.0.1 local.net "v=spf1 ip4:192.168.0.0/16 ?all"
    } r] $r
} {0 ?}

test spf-4.5 {ip4 directive} {
    list [catch {
        spf::Spf 192.168.0.1 local.net "v=spf1 ip4:192.168/16 ?all"
    } r] $r
} {0 +}

# -------------------------------------------------------------------------

foreach cmd [list SPF TXT A PTR MX] {
    catch {rename ::spf::$cmd {}}
    catch {rename ::spf::tmp_$cmd ::spf::$cmd}
}

::tcltest::cleanupTests

# Local Variables:
#  mode: tcl
#  indent-tabs-mode: nil
# End:
