Added test cases for parseLine

pull/13/head
Chris Bednarski 9 years ago
parent 92d4245f14
commit 197eff7a04

@ -6,5 +6,5 @@ test: build
go test -coverprofile=coverage.out
go tool cover -html=coverage.out -o coverage.html
install: build
cp hostess /usr/sbin/hostess
install: build test
cp main /usr/sbin/hostess

@ -1,7 +1,8 @@
package main
import (
// "github.com/cbednarski/hostess"
// "fmt"
"github.com/cbednarski/hostess"
"os"
)
@ -14,5 +15,7 @@ func getArgs() []string {
}
func main() {
hostfile := hostess.NewHostfile(hostess.GetHostsPath())
hostfile.Load()
hostfile.Parse()
}

@ -37,6 +37,63 @@ ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
`
func TrimWS(s string) string {
return strings.Trim(s, " \n\t")
}
var line_parser = regexp.MustCompile(``)
func parseLine(line string) []Hostname {
// 1. Split on # to discard comments.
// - What if it's a legit line but commented out?
// - Disabled lines should have # at the beginning of the line
// 2. Split on first space to find the IP
// - Pass to LooksLikeIpv4 or LooksLikeIpv6 to check
// - If it doesn't look like either of these then it's probably a random
// comment.
// 3. Split remainder of line on whitespace to find
// domain names
// - Watch out for comments.
hostnames := make([]Hostname, 0)
return hostnames
}
func LooksLikeIpv4(ip string) bool {
return false
}
func LooksLikeIpv6(ip string) bool {
return false
}
func ContainsHostname(hostnames []Hostname, hostname Hostname) bool {
for _, v := range hostnames {
if v.Ip == hostname.Ip && v.Domain == hostname.Domain {
return true
}
}
return false
}
func ContainsDomain(hostnames []Hostname, domain string) bool {
for _, v := range hostnames {
if v.Domain == domain {
return true
}
}
return false
}
func ContainsIp(hostnames []Hostname, ip string) bool {
for _, v := range hostnames {
if v.Ip == ip {
return true
}
}
return false
}
type Hostname struct {
Domain string
Ip string
@ -60,16 +117,12 @@ type Hostfile struct {
data string
}
func TrimWS(s string) string {
return strings.Trim(s, " \n\t")
}
// NewHostFile creates a new Hostfile object from the specified file.
func NewHostfile(path string) *Hostfile {
return &Hostfile{path, make(map[string]*Hostname), ""}
}
func (h *Hostfile) ReadFile(path string) string {
func (h *Hostfile) Load() string {
data, err := ioutil.ReadFile(h.Path)
if err != nil {
fmt.Println("Can't read ", h.Path)
@ -79,24 +132,10 @@ func (h *Hostfile) ReadFile(path string) string {
return h.data
}
var line_parser = regexp.MustCompile(``)
func parseLine(line string) (Hostname, error) {
// 1. Split on # to discard comments.
// 2. Split on first space to find the IP
// 3. Split remainder of line on whitespace to find
// domain names
// 4. Validate the IP (maybe -- could be ipv4 or ipv6)
hostname := Hostname{}
if false {
return hostname, errors.New("Can't parse hostname")
func (h *Hostfile) Parse() {
for _, v := range strings.Split(h.data, "\n") {
fmt.Println(v)
}
return hostname, nil
}
func (h *Hostfile) Read(hostfile string) []Hostname {
var hosts = make([]Hostname, 0)
return hosts
}
func getSortedMapKeys(m map[string][]string) []string {
@ -269,8 +308,3 @@ func GetHostsPath() string {
}
return path
}
func Hostess() {
hostfile := NewHostfile(GetHostsPath())
hostfile.ReadFile(hostfile.Path)
}

@ -168,3 +168,43 @@ func TestListDomainsByIp(t *testing.T) {
t.Errorf("Expected localhost and devsite. Got %s", names2)
}
}
func TestParseLine(t *testing.T) {
var hosts = []Hostname{}
// Commented stuff
hosts = parseLine("# The following lines are desirable for IPv6 capable hosts")
if len(hosts) > 0 {
t.Error("Expected to find zero hostnames")
}
hosts = parseLine("#66.33.99.11 test.domain.com")
if !ContainsHostname(hosts, Hostname{"test.domain.com", "66.33.99.11", false}) {
t.Error("Expected to find test.domain.com (disabled)")
}
hosts = parseLine("# 66.33.99.11 test.domain.com domain.com")
if !ContainsHostname(hosts, Hostname{"test.domain.com", "66.33.99.11", false}) ||
!ContainsHostname(hosts, Hostname{"domain.com", "66.33.99.11", false}) {
t.Error("Expected to find domain.com and test.domain.com (disabled)")
}
// Not Commented stuff
hosts = parseLine("255.255.255.255 broadcasthost test.domain.com domain.com")
if !ContainsHostname(hosts, Hostname{"broadcasthost", "255.255.255.255", true}) ||
!ContainsHostname(hosts, Hostname{"test.domain.com", "255.255.255.255", true}) ||
!ContainsHostname(hosts, Hostname{"domain.com", "255.255.255.255", true}) {
t.Error("Expected to find broadcasthost, domain.com, and test.domain.com (enabled)")
}
// Ipv6 stuff
hosts = parseLine("::1 localhost")
if !ContainsHostname(hosts, Hostname{"localhost", "::1", true}) {
t.Error("Expected to find localhost ipv6 (enabled)")
}
hosts = parseLine("ff02::1 ip6-allnodes")
if !ContainsHostname(hosts, Hostname{"ip6-allnodes", "ff02::1", true}) {
t.Error("Expected to find ip6-allnodes ipv6 (enabled)")
}
}

Loading…
Cancel
Save