2016-08-22 18:31:50 -06:00
|
|
|
package js
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2017-03-27 16:01:12 -06:00
|
|
|
"unicode"
|
2016-08-22 18:31:50 -06:00
|
|
|
|
2017-07-20 17:41:15 -04:00
|
|
|
"github.com/tdewolff/minify"
|
|
|
|
minjson "github.com/tdewolff/minify/json"
|
2016-08-22 18:31:50 -06:00
|
|
|
)
|
|
|
|
|
2016-12-16 13:10:27 -07:00
|
|
|
const (
|
2017-05-25 14:25:39 -04:00
|
|
|
testDir = "pkg/js/parse_tests"
|
|
|
|
errorDir = "pkg/js/error_tests"
|
2016-12-16 13:10:27 -07:00
|
|
|
)
|
2016-08-22 18:31:50 -06:00
|
|
|
|
2016-12-16 13:10:27 -07:00
|
|
|
func init() {
|
2017-05-25 14:25:39 -04:00
|
|
|
os.Chdir("../..") // go up a directory so we helpers.js is in a consistent place.
|
2016-12-16 13:10:27 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsedFiles(t *testing.T) {
|
2016-08-22 18:31:50 -06:00
|
|
|
files, err := ioutil.ReadDir(testDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
2018-01-09 12:53:16 -05:00
|
|
|
// run all js files that start with a number. Skip others.
|
2017-03-27 16:01:12 -06:00
|
|
|
if filepath.Ext(f.Name()) != ".js" || !unicode.IsNumber(rune(f.Name()[0])) {
|
2016-08-22 18:31:50 -06:00
|
|
|
continue
|
|
|
|
}
|
2017-07-20 17:41:15 -04:00
|
|
|
m := minify.New()
|
|
|
|
m.AddFunc("json", minjson.Minify)
|
2017-04-19 13:13:28 -06:00
|
|
|
t.Run(f.Name(), func(t *testing.T) {
|
2019-01-29 10:29:00 -05:00
|
|
|
conf, err := ExecuteJavascript(string(filepath.Join(testDir, f.Name())), true)
|
2017-04-19 13:13:28 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
actualJSON, err := json.MarshalIndent(conf, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-20 17:41:15 -04:00
|
|
|
actualJSON, err = m.Bytes("json", actualJSON)
|
2017-04-19 13:13:28 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-20 17:41:15 -04:00
|
|
|
expectedFile := filepath.Join(testDir, f.Name()[:len(f.Name())-3]+".json")
|
|
|
|
expectedData, err := ioutil.ReadFile(expectedFile)
|
2017-04-19 13:13:28 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-20 17:41:15 -04:00
|
|
|
|
|
|
|
expectedJSON, err := m.String("json", string(expectedData))
|
2017-04-19 13:13:28 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-13 11:46:43 -05:00
|
|
|
es := string(expectedJSON)
|
|
|
|
as := string(actualJSON)
|
|
|
|
if es != as {
|
|
|
|
t.Errorf("Expected and actual json don't match: %s", f.Name())
|
|
|
|
t.Logf("Expected(%v): %s", len(es), es)
|
|
|
|
t.Logf("Actual (%v): %s", len(as), as)
|
2017-04-19 13:13:28 -06:00
|
|
|
}
|
|
|
|
})
|
2016-12-16 13:10:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrors(t *testing.T) {
|
2017-04-11 23:02:57 -06:00
|
|
|
tests := []struct{ desc, text string }{
|
|
|
|
{"old dsp style", `D("foo.com","reg","dsp")`},
|
|
|
|
{"MX no priority", `D("foo.com","reg",MX("@","test."))`},
|
|
|
|
{"MX reversed", `D("foo.com","reg",MX("@","test.", 5))`},
|
2017-05-19 14:15:57 -04:00
|
|
|
{"CF_REDIRECT With comma", `D("foo.com","reg",CF_REDIRECT("foo.com,","baaa"))`},
|
|
|
|
{"CF_TEMP_REDIRECT With comma", `D("foo.com","reg",CF_TEMP_REDIRECT("foo.com","baa,a"))`},
|
2017-06-05 13:58:02 -04:00
|
|
|
{"Bad cidr", `D(reverse("foo.com"), "reg")`},
|
2017-11-14 21:14:45 -07:00
|
|
|
{"Dup domains", `D("example.org", "reg"); D("example.org", "reg")`},
|
2018-03-22 11:52:52 -04:00
|
|
|
{"Bad NAMESERVER", `D("example.com","reg", NAMESERVER("@","ns1.foo.com."))`},
|
2016-12-16 13:10:27 -07:00
|
|
|
}
|
2017-04-11 23:02:57 -06:00
|
|
|
for _, tst := range tests {
|
|
|
|
t.Run(tst.desc, func(t *testing.T) {
|
|
|
|
if _, err := ExecuteJavascript(tst.text, true); err == nil {
|
|
|
|
t.Fatal("Expected error but found none")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2016-08-22 18:31:50 -06:00
|
|
|
}
|
|
|
|
}
|