package main import ( "strconv" "strings" "testing" c "github.com/Azareal/Gosora/common" ) func TestPreparser(t *testing.T) { miscinit(t) if !c.PluginsInited { c.InitPlugins() } l := &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 l.Add("", "") l.Add(" ", "") l.Add(" hi", "hi") l.Add("hi ", "hi") l.Add("hi", "hi") l.Add(":grinning:", "😀") l.Add("😀", "😀") l.Add(" ", "") l.Add("

", "") l.Add("

", "") l.Add("

", "") l.Add("<", "<") l.Add(">", ">") l.Add("", "<meow>") l.Add("<", "&lt;") l.Add("&", "&") // Note: strings.TrimSpace strips newlines, if there's nothing before or after them l.Add("
", "") l.Add("
", "") l.Add("\\n", "\n", "") l.Add("\\n\\n", "\n\n", "") l.Add("\\n\\n\\n", "\n\n\n", "") l.Add("\\r\\n", "\r\n", "") // Windows style line ending l.Add("\\n\\r", "\n\r", "") l.Add("ho
ho", "ho\n\nho") l.Add("ho
ho", "ho\n\nho") l.Add("ho\\nho", "ho\nho", "ho\nho") l.Add("ho\\n\\nho", "ho\n\nho", "ho\n\nho") //l.Add("ho\\n\\n\\n\\nho", "ho\n\n\n\nho", "ho\n\n\nho") l.Add("ho\\r\\nho", "ho\r\nho", "ho\nho") // Windows style line ending l.Add("ho\\n\\rho", "ho\n\rho", "ho\nho") l.Add("", "") l.Add("hi", "hi") l.Add("h", "h") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("
hi
", "<div>hi</div>") l.Add("hi", "hi") // This is stripped since the editor (Trumbowyg) likes blasting useless spans l.Add("hi", "hi") l.Add("hi", "hi") l.Add(">hi", ">hi") l.Add("hi", "hi") l.Add("hi", "hi</b>") l.Add("", "</b>") l.Add("", "</del>") l.Add("", "</strong>") l.Add("", "") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("hi", "hi") l.Add("", "") l.Add("", "") l.Add("", "") l.Add("", "") l.Add("", "") l.Add("

t

", "

t

") l.Add("

t

", "

t

") l.Add("

t

", "

t

") l.Add("<>", "<></>") l.Add("<>", "</><>") l.Add("<>", "<>") l.Add("", "</>") l.Add("

hi

", "hi") l.Add("

", "") l.Add("
hi
", "
hi
") l.Add("
hi
", "
hi
") l.Add("
hi
", "
<meow>hi</meow>
") l.Add("\\
hi
", "<blockquote>hi</blockquote>") //l.Add("\\\\
hi
", "\\
<meow>hi</meow>
") // TODO: Double escapes should print a literal backslash //l.Add("<blockquote>hi</blockquote>", "<blockquote>hi</blockquote>") // TODO: Stop double-entitising this l.Add("\\
hi
\\
hi
", "<blockquote>hi</blockquote><blockquote>hi</blockquote>") l.Add("\\Admin", "<a itemprop="author">Admin</a>") l.Add("
\\Admin
", "
<a itemprop="author">Admin</a>
") l.Add("\n
\\Admin
\n", "
<a itemprop="author">Admin</a>
") l.Add("tt\n
\\Admin
\ntt", "tt\n
<a itemprop="author">Admin</a>
\ntt") l.Add("@", "@") l.Add("@Admin", "@1") l.Add("@Bah", "@Bah") l.Add(" @Admin", "@1") l.Add("\n@Admin", "@1") l.Add("@Admin\n", "@1") l.Add("@Admin\ndd", "@1\ndd") l.Add("d@Admin", "d@Admin") l.Add("\\@Admin", "@Admin") l.Add("@元気", "@元気") // TODO: More tests for unicode names? //l.Add("\\\\@Admin", "@1") //l.Add("byte 0", string([]byte{0}), "") l.Add("byte 'a'", string([]byte{'a'}), "a") //l.Add("byte 255", string([]byte{255}), "") //l.Add("rune 0", string([]rune{0}), "") // TODO: Do a test with invalid UTF-8 input for _, item := range l.Items { if res := c.PreparseMessage(item.Msg); 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() } l := &METriList{nil} url := "github.com/Azareal/Gosora" eurl := "" + url + "" l.Add("", "") l.Add("haha", "haha") l.Add("t", "t") l.Add("//", "//") l.Add("http://", "[Invalid URL]") l.Add("https://", "[Invalid URL]") l.Add("ftp://", "[Invalid URL]") l.Add("git://", "[Invalid URL]") l.Add("ssh://", "ssh://") l.Add("// ", "// ") l.Add("// //", "// //") l.Add("// // //", "// // //") l.Add("http:// ", "[Invalid URL] ") l.Add("https:// ", "[Invalid URL] ") l.Add("ftp:// ", "[Invalid URL] ") l.Add("git:// ", "[Invalid URL] ") l.Add("ssh:// ", "ssh:// ") l.Add("// t", "// t") l.Add("http:// t", "[Invalid URL] t") l.Add("http:", "http:") l.Add("https:", "https:") l.Add("ftp:", "ftp:") l.Add("git:", "git:") l.Add("ssh:", "ssh:") l.Add("http", "http") l.Add("https", "https") l.Add("ftp", "ftp") l.Add("git", "git") l.Add("ssh", "ssh") l.Add("ht", "ht") l.Add("htt", "htt") l.Add("ft", "ft") l.Add("gi", "gi") l.Add("ss", "ss") l.Add("haha\nhaha\nhaha", "haha
haha
haha") l.Add("//"+url, eurl) l.Add("//a", "a") l.Add(" //a", " a") l.Add("//a ", "a ") l.Add(" //a ", " a ") l.Add("d //a ", "d a ") l.Add("ddd ddd //a ", "ddd ddd a ") l.Add("https://"+url, ""+url+"") l.Add("https://t", "t") l.Add("http://"+url, ""+url+"") l.Add("#http://"+url, "#http://"+url) l.Add("@http://"+url, "[Invalid Profile]ttp://"+url) l.Add("//"+url+"\n", ""+url+"
") l.Add("\n//"+url, "
"+eurl) l.Add("\n//"+url+"\n", "
"+eurl+"
") l.Add("\n//"+url+"\n\n", "
"+eurl+"

") l.Add("//"+url+"\n//"+url, eurl+"
"+eurl) l.Add("//"+url+"\n\n//"+url, eurl+"

"+eurl) pre2 := c.Site.EnableSsl c.Site.EnableSsl = true local := func(u string) { s := "//" + c.Site.URL fs := "http://" + c.Site.URL if c.Site.EnableSsl { s = "https:" + s fs = "https://" + c.Site.URL } l.Add("//"+u, ""+c.Site.URL+"") l.Add("//"+u+"\n", ""+c.Site.URL+"
") l.Add("//"+u+"\n//"+u, ""+c.Site.URL+"
"+c.Site.URL+"") l.Add("http://"+u, ""+c.Site.URL+"") l.Add("https://"+u, ""+c.Site.URL+"") } local("localhost") local("127.0.0.1") local("[::1]") l.Add("https://www.youtube.com/watch?v=lalalalala", "") //l.Add("https://www.youtube.com/watch?v=;","") l.Add("https://www.youtube.com/watch?v=d;", "") l.Add("https://www.youtube.com/watch?v=d;d", "") l.Add("https://www.youtube.com/watch?v=alert()", "[Invalid URL]()") l.Add("https://www.youtube.com/watch?v=alert()()", "[Invalid URL]()()") l.Add("https://www.youtube.com/watch?v=js:alert()", "[Invalid URL]()") l.Add("https://www.youtube.com/watch?v='+><+'", "[Invalid URL]'+><+'") l.Add("https://www.youtube.com/watch?v='+onready='alert(\"\")'+'", "[Invalid URL]'+onready='alert(\"\")'+'") l.Add(" https://www.youtube.com/watch?v=lalalalala", " ") l.Add("https://www.youtube.com/watch?v=lalalalala tt", " tt") l.Add("https://www.youtube.com/watch?v=lalalalala&d=haha", "") l.Add("https://gaming.youtube.com/watch?v=lalalalala", "") l.Add("https://gaming.youtube.com/watch?v=lalalalala&d=haha", "") l.Add("https://m.youtube.com/watch?v=lalalalala", "") l.Add("https://m.youtube.com/watch?v=lalalalala&d=haha", "") l.Add("http://www.youtube.com/watch?v=lalalalala", "") l.Add("//www.youtube.com/watch?v=lalalalala", "") //l.Add("www.youtube.com/watch?v=lalalalala","") l.Add("#tid-1", "#tid-1") l.Add("##tid-1", "##tid-1") l.Add("# #tid-1", "# #tid-1") l.Add("@ #tid-1", "[Invalid Profile]#tid-1") l.Add("@#tid-1", "[Invalid Profile]tid-1") l.Add("@ #tid-@", "[Invalid Profile]#tid-@") l.Add("#tid-1 #tid-1", "#tid-1 #tid-1") l.Add("#tid-0", "[Invalid Topic]") l.Add("https://"+url+"/#tid-1", ""+url+"/#tid-1") l.Add("https://"+url+"/?hi=2", ""+url+"/?hi=2") l.Add("https://"+url+"/?hi=2#t=1", ""+url+"/?hi=2#t=1") l.Add("#fid-1", "#fid-1") l.Add(" #fid-1", " #fid-1") l.Add("#fid-0", "[Invalid Forum]") l.Add(" #fid-0", " [Invalid Forum]") l.Add("#", "#") l.Add("# ", "# ") l.Add(" @", " @") l.Add(" #", " #") l.Add("#@", "#@") l.Add("#@ ", "#@ ") l.Add("#@1", "#@1") l.Add("#f", "#f") l.Add("f#f", "f#f") l.Add("f#", "f#") l.Add("#ff", "#ff") l.Add("#ffffid-0", "#ffffid-0") //l.Add("#ffffid-0", "#ffffid-0") l.Add("#nid-0", "#nid-0") l.Add("#nnid-0", "#nnid-0") l.Add("@@", "[Invalid Profile]") l.Add("@@ @@", "[Invalid Profile] [Invalid Profile]") l.Add("@@1", "[Invalid Profile]1") l.Add("@#1", "[Invalid Profile]1") l.Add("@##1", "[Invalid Profile]#1") l.Add("@2", "[Invalid Profile]") l.Add("@2t", "[Invalid Profile]t") l.Add("@2 t", "[Invalid Profile] t") l.Add("@2 ", "[Invalid Profile] ") l.Add("@2 @2", "[Invalid Profile] [Invalid Profile]") l.Add("@1", "@Admin") l.Add(" @1", " @Admin") l.Add("@1t", "@Admint") l.Add("@1 ", "@Admin ") l.Add("@1 @1", "@Admin @Admin") l.Add("@0", "[Invalid Profile]") l.Add("@-1", "[Invalid Profile]1") // TODO: Fix this hack and make the results a bit more reproducible, push the tests further in the process. for _, item := range l.Items { if res := c.ParseMessage(item.Msg, 1, "forums"); 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.Site.EnableSsl = pre2 l = &METriList{nil} pre := c.Site.URL // Just in case this is localhost... pre2 = c.Site.EnableSsl c.Site.URL = "example.com" c.Site.EnableSsl = true l.Add("//"+c.Site.URL, ""+c.Site.URL+"") l.Add("//"+c.Site.URL+"\n", ""+c.Site.URL+"
") l.Add("//"+c.Site.URL+"\n//"+c.Site.URL, ""+c.Site.URL+"
"+c.Site.URL+"") for _, item := range l.Items { if res := c.ParseMessage(item.Msg, 1, "forums"); 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.Site.URL = pre c.Site.EnableSsl = pre2 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+"'") } }