2015-05-21 17:31:01 -05:00
|
|
|
package parse
|
|
|
|
|
2015-05-21 19:46:42 -05:00
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
)
|
2015-05-21 17:31:01 -05:00
|
|
|
|
|
|
|
func TestStandardAddress(t *testing.T) {
|
|
|
|
for i, test := range []struct {
|
|
|
|
input string
|
|
|
|
host, port string
|
|
|
|
shouldErr bool
|
|
|
|
}{
|
|
|
|
{`localhost`, "localhost", "", false},
|
|
|
|
{`localhost:1234`, "localhost", "1234", false},
|
|
|
|
{`localhost:`, "localhost", "", false},
|
|
|
|
{`0.0.0.0`, "0.0.0.0", "", false},
|
|
|
|
{`127.0.0.1:1234`, "127.0.0.1", "1234", false},
|
|
|
|
{`:1234`, "", "1234", false},
|
|
|
|
{`[::1]`, "::1", "", false},
|
|
|
|
{`[::1]:1234`, "::1", "1234", false},
|
|
|
|
{`:`, "", "", false},
|
|
|
|
{`localhost:http`, "localhost", "http", false},
|
|
|
|
{`localhost:https`, "localhost", "https", false},
|
|
|
|
{`:http`, "", "http", false},
|
|
|
|
{`:https`, "", "https", false},
|
|
|
|
{`http://localhost`, "localhost", "http", false},
|
|
|
|
{`https://localhost`, "localhost", "https", false},
|
|
|
|
{`http://127.0.0.1`, "127.0.0.1", "http", false},
|
|
|
|
{`https://127.0.0.1`, "127.0.0.1", "https", false},
|
|
|
|
{`http://[::1]`, "::1", "http", false},
|
|
|
|
{`http://localhost:1234`, "localhost", "1234", false},
|
|
|
|
{`https://127.0.0.1:1234`, "127.0.0.1", "1234", false},
|
|
|
|
{`http://[::1]:1234`, "::1", "1234", false},
|
|
|
|
{``, "", "", false},
|
|
|
|
{`::1`, "::1", "", true},
|
|
|
|
{`localhost::`, "localhost::", "", true},
|
|
|
|
{`#$%@`, "#$%@", "", true},
|
|
|
|
} {
|
|
|
|
host, port, err := standardAddress(test.input)
|
|
|
|
|
|
|
|
if err != nil && !test.shouldErr {
|
|
|
|
t.Errorf("Test %d: Expected no error, but had error: %v", i, err)
|
|
|
|
}
|
|
|
|
if err == nil && test.shouldErr {
|
|
|
|
t.Errorf("Test %d: Expected error, but had none", i)
|
|
|
|
}
|
|
|
|
|
|
|
|
if host != test.host {
|
|
|
|
t.Errorf("Test %d: Expected host '%s', got '%s'", i, test.host, host)
|
|
|
|
}
|
|
|
|
|
|
|
|
if port != test.port {
|
|
|
|
t.Errorf("Test %d: Expected port '%s', got '%s'", i, test.port, port)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-21 19:46:42 -05:00
|
|
|
|
2015-07-07 23:38:48 -05:00
|
|
|
func TestParseOneAndImport(t *testing.T) {
|
2015-05-22 00:36:17 -05:00
|
|
|
setupParseTests()
|
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
testParseOne := func(input string) (ServerBlock, error) {
|
2015-05-21 19:46:42 -05:00
|
|
|
p := testParser(input)
|
2015-08-03 18:31:10 -05:00
|
|
|
p.Next() // parseOne doesn't call Next() to start, so we must
|
2015-05-21 19:46:42 -05:00
|
|
|
err := p.parseOne()
|
|
|
|
return p.block, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
input string
|
|
|
|
shouldErr bool
|
2015-08-01 14:08:31 -05:00
|
|
|
addresses []Address
|
2015-05-21 19:46:42 -05:00
|
|
|
tokens map[string]int // map of directive name to number of tokens expected
|
|
|
|
}{
|
2015-08-01 14:08:31 -05:00
|
|
|
{`localhost`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", ""},
|
|
|
|
}, map[string]int{}},
|
|
|
|
|
|
|
|
{`localhost
|
2015-08-01 14:08:31 -05:00
|
|
|
dir1`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", ""},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 1,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`localhost:1234
|
2015-08-01 14:08:31 -05:00
|
|
|
dir1 foo bar`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", "1234"},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 3,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`localhost {
|
|
|
|
dir1
|
2015-08-01 14:08:31 -05:00
|
|
|
}`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", ""},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 1,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`localhost:1234 {
|
|
|
|
dir1 foo bar
|
|
|
|
dir2
|
2015-08-01 14:08:31 -05:00
|
|
|
}`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", "1234"},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 3,
|
|
|
|
"dir2": 1,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`http://localhost https://localhost
|
2015-08-01 14:08:31 -05:00
|
|
|
dir1 foo bar`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", "http"},
|
|
|
|
{"localhost", "https"},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 3,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`http://localhost https://localhost {
|
|
|
|
dir1 foo bar
|
2015-08-01 14:08:31 -05:00
|
|
|
}`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", "http"},
|
|
|
|
{"localhost", "https"},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 3,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`http://localhost, https://localhost {
|
|
|
|
dir1 foo bar
|
2015-08-01 14:08:31 -05:00
|
|
|
}`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", "http"},
|
|
|
|
{"localhost", "https"},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 3,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`http://localhost, {
|
2015-08-01 14:08:31 -05:00
|
|
|
}`, true, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", "http"},
|
|
|
|
}, map[string]int{}},
|
|
|
|
|
|
|
|
{`host1:80, http://host2.com
|
|
|
|
dir1 foo bar
|
2015-08-01 14:08:31 -05:00
|
|
|
dir2 baz`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"host1", "80"},
|
|
|
|
{"host2.com", "http"},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 3,
|
|
|
|
"dir2": 2,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`http://host1.com,
|
|
|
|
http://host2.com,
|
2015-08-01 14:08:31 -05:00
|
|
|
https://host3.com`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"host1.com", "http"},
|
|
|
|
{"host2.com", "http"},
|
|
|
|
{"host3.com", "https"},
|
|
|
|
}, map[string]int{}},
|
|
|
|
|
|
|
|
{`http://host1.com:1234, https://host2.com
|
|
|
|
dir1 foo {
|
|
|
|
bar baz
|
|
|
|
}
|
2015-08-01 14:08:31 -05:00
|
|
|
dir2`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"host1.com", "1234"},
|
|
|
|
{"host2.com", "https"},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 6,
|
|
|
|
"dir2": 1,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`127.0.0.1
|
|
|
|
dir1 {
|
|
|
|
bar baz
|
|
|
|
}
|
|
|
|
dir2 {
|
|
|
|
foo bar
|
2015-08-01 14:08:31 -05:00
|
|
|
}`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"127.0.0.1", ""},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 5,
|
|
|
|
"dir2": 5,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`127.0.0.1
|
2015-08-01 14:08:31 -05:00
|
|
|
unknown_directive`, true, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"127.0.0.1", ""},
|
|
|
|
}, map[string]int{}},
|
|
|
|
|
|
|
|
{`localhost
|
|
|
|
dir1 {
|
2015-08-01 14:08:31 -05:00
|
|
|
foo`, true, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", ""},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 3,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`localhost
|
|
|
|
dir1 {
|
2015-08-01 14:08:31 -05:00
|
|
|
}`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", ""},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 3,
|
|
|
|
}},
|
|
|
|
|
|
|
|
{`localhost
|
|
|
|
dir1 {
|
|
|
|
nested {
|
|
|
|
foo
|
|
|
|
}
|
|
|
|
}
|
2015-08-01 14:08:31 -05:00
|
|
|
dir2 foo bar`, false, []Address{
|
2015-05-21 19:46:42 -05:00
|
|
|
{"localhost", ""},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 7,
|
|
|
|
"dir2": 3,
|
|
|
|
}},
|
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
{``, false, []Address{}, map[string]int{}},
|
2015-07-07 23:38:48 -05:00
|
|
|
|
|
|
|
{`localhost
|
|
|
|
dir1 arg1
|
2015-08-01 14:08:31 -05:00
|
|
|
import import_test1.txt`, false, []Address{
|
2015-07-07 23:38:48 -05:00
|
|
|
{"localhost", ""},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 2,
|
|
|
|
"dir2": 3,
|
|
|
|
"dir3": 1,
|
|
|
|
}},
|
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
{`import import_test2.txt`, false, []Address{
|
2015-07-07 23:38:48 -05:00
|
|
|
{"host1", ""},
|
|
|
|
}, map[string]int{
|
|
|
|
"dir1": 1,
|
|
|
|
"dir2": 2,
|
|
|
|
}},
|
2015-08-03 18:31:10 -05:00
|
|
|
|
|
|
|
{``, false, []Address{}, map[string]int{}},
|
|
|
|
|
|
|
|
{`""`, false, []Address{}, map[string]int{}},
|
2015-05-21 19:46:42 -05:00
|
|
|
} {
|
2015-05-22 00:36:17 -05:00
|
|
|
result, err := testParseOne(test.input)
|
2015-05-21 19:46:42 -05:00
|
|
|
|
|
|
|
if test.shouldErr && err == nil {
|
|
|
|
t.Errorf("Test %d: Expected an error, but didn't get one", i)
|
|
|
|
}
|
|
|
|
if !test.shouldErr && err != nil {
|
|
|
|
t.Errorf("Test %d: Expected no error, but got: %v", i, err)
|
|
|
|
}
|
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
if len(result.Addresses) != len(test.addresses) {
|
2015-05-21 19:46:42 -05:00
|
|
|
t.Errorf("Test %d: Expected %d addresses, got %d",
|
2015-08-01 14:08:31 -05:00
|
|
|
i, len(test.addresses), len(result.Addresses))
|
2015-05-21 19:46:42 -05:00
|
|
|
continue
|
|
|
|
}
|
2015-08-01 14:08:31 -05:00
|
|
|
for j, addr := range result.Addresses {
|
|
|
|
if addr.Host != test.addresses[j].Host {
|
2015-05-21 19:46:42 -05:00
|
|
|
t.Errorf("Test %d, address %d: Expected host to be '%s', but was '%s'",
|
2015-08-01 14:08:31 -05:00
|
|
|
i, j, test.addresses[j].Host, addr.Host)
|
2015-05-21 19:46:42 -05:00
|
|
|
}
|
2015-08-01 14:08:31 -05:00
|
|
|
if addr.Port != test.addresses[j].Port {
|
2015-05-21 19:46:42 -05:00
|
|
|
t.Errorf("Test %d, address %d: Expected port to be '%s', but was '%s'",
|
2015-08-01 14:08:31 -05:00
|
|
|
i, j, test.addresses[j].Port, addr.Port)
|
2015-05-21 19:46:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-01 14:08:31 -05:00
|
|
|
if len(result.Tokens) != len(test.tokens) {
|
2015-05-21 19:46:42 -05:00
|
|
|
t.Errorf("Test %d: Expected %d directives, had %d",
|
2015-08-01 14:08:31 -05:00
|
|
|
i, len(test.tokens), len(result.Tokens))
|
2015-05-21 19:46:42 -05:00
|
|
|
continue
|
|
|
|
}
|
2015-08-01 14:08:31 -05:00
|
|
|
for directive, tokens := range result.Tokens {
|
2015-05-21 19:46:42 -05:00
|
|
|
if len(tokens) != test.tokens[directive] {
|
|
|
|
t.Errorf("Test %d, directive '%s': Expected %d tokens, counted %d",
|
|
|
|
i, directive, test.tokens[directive], len(tokens))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-22 00:36:17 -05:00
|
|
|
}
|
|
|
|
|
2015-08-03 18:31:10 -05:00
|
|
|
func TestParseAll(t *testing.T) {
|
|
|
|
setupParseTests()
|
|
|
|
|
|
|
|
testParseAll := func(input string) ([]ServerBlock, error) {
|
|
|
|
p := testParser(input)
|
|
|
|
return p.parseAll()
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, test := range []struct {
|
|
|
|
input string
|
|
|
|
shouldErr bool
|
|
|
|
numBlocks int
|
|
|
|
}{
|
|
|
|
{`localhost`, false, 1},
|
|
|
|
|
|
|
|
{`localhost {
|
|
|
|
dir1
|
|
|
|
}`, false, 1},
|
|
|
|
|
|
|
|
{`http://localhost https://localhost
|
|
|
|
dir1 foo bar`, false, 1},
|
|
|
|
|
|
|
|
{`http://localhost, https://localhost {
|
|
|
|
dir1 foo bar
|
|
|
|
}`, false, 1},
|
|
|
|
|
|
|
|
{`http://host1.com,
|
|
|
|
http://host2.com,
|
|
|
|
https://host3.com`, false, 1},
|
|
|
|
|
|
|
|
{`host1 {
|
|
|
|
}
|
|
|
|
host2 {
|
|
|
|
}`, false, 2},
|
|
|
|
|
|
|
|
{`""`, false, 0},
|
|
|
|
|
|
|
|
{``, false, 0},
|
|
|
|
} {
|
|
|
|
results, err := testParseAll(test.input)
|
|
|
|
|
|
|
|
if test.shouldErr && err == nil {
|
|
|
|
t.Errorf("Test %d: Expected an error, but didn't get one", i)
|
|
|
|
}
|
|
|
|
if !test.shouldErr && err != nil {
|
|
|
|
t.Errorf("Test %d: Expected no error, but got: %v", i, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(results) != test.numBlocks {
|
|
|
|
t.Errorf("Test %d: Expected %d server blocks, got %d",
|
|
|
|
i, test.numBlocks, len(results))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-22 00:36:17 -05:00
|
|
|
func setupParseTests() {
|
|
|
|
// Set up some bogus directives for testing
|
|
|
|
ValidDirectives = map[string]struct{}{
|
|
|
|
"dir1": struct{}{},
|
|
|
|
"dir2": struct{}{},
|
|
|
|
"dir3": struct{}{},
|
|
|
|
}
|
2015-05-21 19:46:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func testParser(input string) parser {
|
|
|
|
buf := strings.NewReader(input)
|
|
|
|
p := parser{Dispenser: NewDispenser("Test", buf)}
|
|
|
|
return p
|
|
|
|
}
|