2016-08-23 08:31:50 +08:00
|
|
|
package js
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2017-03-28 06:01:12 +08:00
|
|
|
"unicode"
|
2016-08-23 08:31:50 +08:00
|
|
|
|
2017-07-21 05:41:15 +08:00
|
|
|
"github.com/tdewolff/minify"
|
|
|
|
minjson "github.com/tdewolff/minify/json"
|
2016-08-23 08:31:50 +08:00
|
|
|
)
|
|
|
|
|
2016-12-17 04:10:27 +08:00
|
|
|
const (
|
2017-05-26 02:25:39 +08:00
|
|
|
testDir = "pkg/js/parse_tests"
|
|
|
|
errorDir = "pkg/js/error_tests"
|
2016-12-17 04:10:27 +08:00
|
|
|
)
|
2016-08-23 08:31:50 +08:00
|
|
|
|
2016-12-17 04:10:27 +08:00
|
|
|
func init() {
|
2017-05-26 02:25:39 +08:00
|
|
|
os.Chdir("../..") // go up a directory so we helpers.js is in a consistent place.
|
2016-12-17 04:10:27 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestParsedFiles(t *testing.T) {
|
2016-08-23 08:31:50 +08:00
|
|
|
files, err := ioutil.ReadDir(testDir)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
for _, f := range files {
|
2018-01-10 01:53:16 +08:00
|
|
|
// run all js files that start with a number. Skip others.
|
2017-03-28 06:01:12 +08:00
|
|
|
if filepath.Ext(f.Name()) != ".js" || !unicode.IsNumber(rune(f.Name()[0])) {
|
2016-08-23 08:31:50 +08:00
|
|
|
continue
|
|
|
|
}
|
2017-07-21 05:41:15 +08:00
|
|
|
m := minify.New()
|
|
|
|
m.AddFunc("json", minjson.Minify)
|
2017-04-20 03:13:28 +08:00
|
|
|
t.Run(f.Name(), func(t *testing.T) {
|
2019-01-29 23:29:00 +08:00
|
|
|
conf, err := ExecuteJavascript(string(filepath.Join(testDir, f.Name())), true)
|
2017-04-20 03:13:28 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
actualJSON, err := json.MarshalIndent(conf, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-21 05:41:15 +08:00
|
|
|
actualJSON, err = m.Bytes("json", actualJSON)
|
2017-04-20 03:13:28 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-21 05:41:15 +08:00
|
|
|
expectedFile := filepath.Join(testDir, f.Name()[:len(f.Name())-3]+".json")
|
|
|
|
expectedData, err := ioutil.ReadFile(expectedFile)
|
2017-04-20 03:13:28 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-07-21 05:41:15 +08:00
|
|
|
|
|
|
|
expectedJSON, err := m.String("json", string(expectedData))
|
2017-04-20 03:13:28 +08:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2018-12-14 00:46:43 +08: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-20 03:13:28 +08:00
|
|
|
}
|
|
|
|
})
|
2016-12-17 04:10:27 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestErrors(t *testing.T) {
|
2017-04-12 13:02:57 +08: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-20 02:15:57 +08: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-06 01:58:02 +08:00
|
|
|
{"Bad cidr", `D(reverse("foo.com"), "reg")`},
|
2017-11-15 12:14:45 +08:00
|
|
|
{"Dup domains", `D("example.org", "reg"); D("example.org", "reg")`},
|
2018-03-22 23:52:52 +08:00
|
|
|
{"Bad NAMESERVER", `D("example.com","reg", NAMESERVER("@","ns1.foo.com."))`},
|
2016-12-17 04:10:27 +08:00
|
|
|
}
|
2017-04-12 13:02:57 +08: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-23 08:31:50 +08:00
|
|
|
}
|
|
|
|
}
|