package main
import (
"strconv"
"strings"
"testing"
c "github.com/Azareal/Gosora/common"
)
func TestPreparser(t *testing.T) {
miscinit(t)
if !c.PluginsInited {
c.InitPlugins()
}
var msgList = &METriList{nil}
// Note: The open tag is evaluated without knowledge of the close tag for efficiency and simplicity, so the parser autofills the associated close tag when it finds an open tag without a partner
msgList.Add("", "")
msgList.Add(" ", "")
msgList.Add(" hi", "hi")
msgList.Add("hi ", "hi")
msgList.Add("hi", "hi")
msgList.Add(":grinning:", "😀")
msgList.Add("😀", "😀")
msgList.Add(" ", "")
msgList.Add("
", "")
msgList.Add("
", "")
msgList.Add("", "")
msgList.Add("<", "<")
msgList.Add(">", ">")
msgList.Add("", "<meow>")
msgList.Add("<", "<")
msgList.Add("&", "&")
// Note: strings.TrimSpace strips newlines, if there's nothing before or after them
msgList.Add("
", "")
msgList.Add("
", "")
msgList.Add("\\n", "\n", "")
msgList.Add("\\n\\n", "\n\n", "")
msgList.Add("\\n\\n\\n", "\n\n\n", "")
msgList.Add("\\r\\n", "\r\n", "") // Windows style line ending
msgList.Add("\\n\\r", "\n\r", "")
msgList.Add("ho
ho", "ho\n\nho")
msgList.Add("ho
ho", "ho\n\nho")
msgList.Add("ho\\nho", "ho\nho", "ho\nho")
msgList.Add("ho\\n\\nho", "ho\n\nho", "ho\n\nho")
//msgList.Add("ho\\n\\n\\n\\nho", "ho\n\n\n\nho", "ho\n\n\nho")
msgList.Add("ho\\r\\nho", "ho\r\nho", "ho\nho") // Windows style line ending
msgList.Add("ho\\n\\rho", "ho\n\rho", "ho\nho")
msgList.Add("", "")
msgList.Add("hi", "hi")
msgList.Add("h", "h")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add("hi
", "<div>hi</div>")
msgList.Add("hi", "hi") // This is stripped since the editor (Trumbowyg) likes blasting useless spans
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add(">hi", ">hi")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi</b>")
msgList.Add("", "</b>")
msgList.Add("", "</del>")
msgList.Add("", "</strong>")
msgList.Add("", "")
msgList.Add("hi", "hi")
msgList.Add("hi", "hi")
msgList.Add("", "")
msgList.Add("", "")
msgList.Add("", "")
msgList.Add("", "")
msgList.Add("", "")
msgList.Add("t
", "t
")
msgList.Add("t
", "t
")
msgList.Add("t
", "t
")
msgList.Add("<>>", "<></>")
msgList.Add("><>", "</><>")
msgList.Add("<>", "<>")
msgList.Add(">", "</>")
msgList.Add("hi
", "hi")
msgList.Add("", "")
msgList.Add("hi
", "hi
")
msgList.Add("hi
", "hi
")
msgList.Add("hi
", "<meow>hi</meow>
")
msgList.Add("\\hi
", "<blockquote>hi</blockquote>")
//msgList.Add("\\\\hi
", "\\<meow>hi</meow>
") // TODO: Double escapes should print a literal backslash
//msgList.Add("<blockquote>hi</blockquote>", "<blockquote>hi</blockquote>") // TODO: Stop double-entitising this
msgList.Add("\\hi
\\hi
", "<blockquote>hi</blockquote><blockquote>hi</blockquote>")
msgList.Add("\\Admin", "<a itemprop="author">Admin</a>")
msgList.Add("\\Admin
", "<a itemprop="author">Admin</a>
")
msgList.Add("\n\\Admin
\n", "<a itemprop="author">Admin</a>
")
msgList.Add("tt\n\\Admin
\ntt", "tt\n<a itemprop="author">Admin</a>
\ntt")
msgList.Add("@", "@")
msgList.Add("@Admin", "@1")
msgList.Add("@Bah", "@Bah")
msgList.Add(" @Admin", "@1")
msgList.Add("\n@Admin", "@1")
msgList.Add("@Admin\n", "@1")
msgList.Add("@Admin\ndd", "@1\ndd")
msgList.Add("d@Admin", "d@Admin")
msgList.Add("\\@Admin", "@Admin")
msgList.Add("@元気", "@元気")
// TODO: More tests for unicode names?
//msgList.Add("\\\\@Admin", "@1")
//msgList.Add("byte 0", string([]byte{0}), "")
msgList.Add("byte 'a'", string([]byte{'a'}), "a")
//msgList.Add("byte 255", string([]byte{255}), "")
//msgList.Add("rune 0", string([]rune{0}), "")
// TODO: Do a test with invalid UTF-8 input
for _, item := range msgList.Items {
res := c.PreparseMessage(item.Msg)
if res != item.Expects {
if item.Name != "" {
t.Error("Name: ", item.Name)
}
t.Error("Testing string '" + item.Msg + "'")
t.Error("Bad output:", "'"+res+"'")
//t.Error("Ouput in bytes:", []byte(res))
t.Error("Expected:", "'"+item.Expects+"'")
}
}
}
func TestParser(t *testing.T) {
miscinit(t)
if !c.PluginsInited {
c.InitPlugins()
}
var msgList = &METriList{nil}
url := "github.com/Azareal/Gosora"
msgList.Add("", "")
msgList.Add("haha", "haha")
msgList.Add("t", "t")
msgList.Add("//", "[Invalid URL]")
msgList.Add("http://", "[Invalid URL]")
msgList.Add("https://", "[Invalid URL]")
msgList.Add("ftp://", "[Invalid URL]")
msgList.Add("git://", "[Invalid URL]")
msgList.Add("ssh://", "ssh://")
msgList.Add("// ", "[Invalid URL] ")
msgList.Add("// //", "[Invalid URL] [Invalid URL]")
msgList.Add("http:// ", "[Invalid URL] ")
msgList.Add("https:// ", "[Invalid URL] ")
msgList.Add("ftp:// ", "[Invalid URL] ")
msgList.Add("git:// ", "[Invalid URL] ")
msgList.Add("ssh:// ", "ssh:// ")
msgList.Add("// t", "[Invalid URL] t")
msgList.Add("http:// t", "[Invalid URL] t")
msgList.Add("http:", "http:")
msgList.Add("https:", "https:")
msgList.Add("ftp:", "ftp:")
msgList.Add("git:", "git:")
msgList.Add("ssh:", "ssh:")
msgList.Add("http", "http")
msgList.Add("https", "https")
msgList.Add("ftp", "ftp")
msgList.Add("git", "git")
msgList.Add("ssh", "ssh")
msgList.Add("ht", "ht")
msgList.Add("htt", "htt")
msgList.Add("ft", "ft")
msgList.Add("gi", "gi")
msgList.Add("ss", "ss")
msgList.Add("haha\nhaha\nhaha", "haha
haha
haha")
msgList.Add("//"+url, "//"+url+"")
msgList.Add("//a", "//a")
msgList.Add("https://"+url, "https://"+url+"")
msgList.Add("https://t", "https://t")
msgList.Add("http://"+url, "http://"+url+"")
msgList.Add("#http://"+url, "#http://"+url)
msgList.Add("@http://"+url, "[Invalid Profile]ttp://"+url)
msgList.Add("//"+url+"\n", "//"+url+"
")
msgList.Add("\n//"+url, "
//"+url+"")
msgList.Add("\n//"+url+"\n", "
//"+url+"
")
msgList.Add("\n//"+url+"\n\n", "
//"+url+"
")
msgList.Add("//"+url+"\n//"+url, "//"+url+"
//"+url+"")
msgList.Add("//"+url+"\n\n//"+url, "//"+url+"
//"+url+"")
msgList.Add("//"+c.Site.URL, "//"+c.Site.URL+"")
msgList.Add("//"+c.Site.URL+"\n", "//"+c.Site.URL+"
")
msgList.Add("//"+c.Site.URL+"\n//"+c.Site.URL, "//"+c.Site.URL+"
//"+c.Site.URL+"")
var local = func(url string) {
msgList.Add("//"+url, "//"+url+"")
msgList.Add("//"+url+"\n", "//"+url+"
")
msgList.Add("//"+url+"\n//"+url, "//"+url+"
//"+url+"")
}
local("localhost")
local("127.0.0.1")
local("[::1]")
msgList.Add("https://www.youtube.com/watch?v=lalalalala","")
//msgList.Add("https://www.youtube.com/watch?v=;","")
msgList.Add("https://www.youtube.com/watch?v=d;","")
msgList.Add("https://www.youtube.com/watch?v=d;d","")
msgList.Add("https://www.youtube.com/watch?v=alert()","[Invalid URL]()")
msgList.Add("https://www.youtube.com/watch?v=js:alert()","[Invalid URL]()")
msgList.Add("https://www.youtube.com/watch?v='+><+'","[Invalid URL]'+><+'")
msgList.Add("https://www.youtube.com/watch?v='+onready='alert(\"\")'+'","[Invalid URL]'+onready='alert(\"\")'+'")
msgList.Add(" https://www.youtube.com/watch?v=lalalalala"," ")
msgList.Add("https://www.youtube.com/watch?v=lalalalala tt"," tt")
msgList.Add("https://www.youtube.com/watch?v=lalalalala&d=haha","")
msgList.Add("https://gaming.youtube.com/watch?v=lalalalala","")
msgList.Add("https://gaming.youtube.com/watch?v=lalalalala&d=haha","")
msgList.Add("https://m.youtube.com/watch?v=lalalalala","")
msgList.Add("https://m.youtube.com/watch?v=lalalalala&d=haha","")
msgList.Add("http://www.youtube.com/watch?v=lalalalala","")
msgList.Add("//www.youtube.com/watch?v=lalalalala","")
//msgList.Add("www.youtube.com/watch?v=lalalalala","")
msgList.Add("#tid-1", "#tid-1")
msgList.Add("##tid-1", "##tid-1")
msgList.Add("# #tid-1", "# #tid-1")
msgList.Add("@ #tid-1", "[Invalid Profile]#tid-1")
msgList.Add("@#tid-1", "[Invalid Profile]tid-1")
msgList.Add("@ #tid-@", "[Invalid Profile]#tid-@")
msgList.Add("#tid-1 #tid-1", "#tid-1 #tid-1")
msgList.Add("#tid-0", "[Invalid Topic]")
msgList.Add("https://"+url+"/#tid-1", "https://"+url+"/#tid-1")
msgList.Add("https://"+url+"/?hi=2", "https://"+url+"/?hi=2")
msgList.Add("#fid-1", "#fid-1")
msgList.Add(" #fid-1", " #fid-1")
msgList.Add("#fid-0", "[Invalid Forum]")
msgList.Add(" #fid-0", " [Invalid Forum]")
msgList.Add("#", "#")
msgList.Add("# ", "# ")
msgList.Add(" @", " @")
msgList.Add(" #", " #")
msgList.Add("#@", "#@")
msgList.Add("#@ ", "#@ ")
msgList.Add("#@1", "#@1")
msgList.Add("#f", "#f")
msgList.Add("#ff", "#ff")
msgList.Add("#ffffid-0", "#ffffid-0")
//msgList.Add("#ffffid-0", "#ffffid-0")
msgList.Add("#nid-0", "#nid-0")
msgList.Add("#nnid-0", "#nnid-0")
msgList.Add("@@", "[Invalid Profile]")
msgList.Add("@@ @@", "[Invalid Profile] [Invalid Profile]")
msgList.Add("@@1", "[Invalid Profile]1")
msgList.Add("@#1", "[Invalid Profile]1")
msgList.Add("@##1", "[Invalid Profile]#1")
msgList.Add("@2", "[Invalid Profile]")
msgList.Add("@2t", "[Invalid Profile]t")
msgList.Add("@2 t", "[Invalid Profile] t")
msgList.Add("@2 ", "[Invalid Profile] ")
msgList.Add("@2 @2", "[Invalid Profile] [Invalid Profile]")
msgList.Add("@1", "@Admin")
msgList.Add(" @1", " @Admin")
msgList.Add("@1t", "@Admint")
msgList.Add("@1 ", "@Admin ")
msgList.Add("@1 @1", "@Admin @Admin")
msgList.Add("@0", "[Invalid Profile]")
msgList.Add("@-1", "[Invalid Profile]1")
for _, item := range msgList.Items {
res := c.ParseMessage(item.Msg, 1, "forums")
if res != item.Expects {
if item.Name != "" {
t.Error("Name: ", item.Name)
}
t.Error("Testing string '" + item.Msg + "'")
t.Error("Bad output:", "'"+res+"'")
t.Error("Expected:", "'"+item.Expects+"'")
break
}
}
c.AddHashLinkType("nnid-", func(sb *strings.Builder, msg string, i *int) {
tid, intLen := c.CoerceIntString(msg[*i:])
*i += intLen
topic, err := c.Topics.Get(tid)
if err != nil || !c.Forums.Exists(topic.ParentID) {
sb.Write(c.InvalidTopic)
return
}
c.WriteURL(sb, c.BuildTopicURL("", tid), "#nnid-"+strconv.Itoa(tid))
})
res := c.ParseMessage("#nnid-1", 1, "forums")
expect := "#nnid-1"
if res != expect {
t.Error("Bad output:", "'"+res+"'")
t.Error("Expected:", "'"+expect+"'")
}
c.AddHashLinkType("longidnameneedtooverflowhack-", func(sb *strings.Builder, msg string, i *int) {
tid, intLen := c.CoerceIntString(msg[*i:])
*i += intLen
topic, err := c.Topics.Get(tid)
if err != nil || !c.Forums.Exists(topic.ParentID) {
sb.Write(c.InvalidTopic)
return
}
c.WriteURL(sb, c.BuildTopicURL("", tid), "#longidnameneedtooverflowhack-"+strconv.Itoa(tid))
})
res = c.ParseMessage("#longidnameneedtooverflowhack-1", 1, "forums")
expect = "#longidnameneedtooverflowhack-1"
if res != expect {
t.Error("Bad output:", "'"+res+"'")
t.Error("Expected:", "'"+expect+"'")
}
}