2018-11-22 07:21:43 +00:00
package main
import (
2020-03-09 01:37:31 +00:00
"fmt"
2019-05-22 04:45:07 +00:00
"strconv"
"strings"
2018-11-22 07:21:43 +00:00
"testing"
2019-04-19 08:20:10 +00:00
c "github.com/Azareal/Gosora/common"
2018-11-22 07:21:43 +00:00
)
func TestPreparser ( t * testing . T ) {
2019-07-11 07:20:33 +00:00
miscinit ( t )
if ! c . PluginsInited {
c . InitPlugins ( )
}
2019-10-06 00:34:09 +00:00
l := & METriList { nil }
2018-11-22 07:21:43 +00:00
// 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
2019-10-06 00:34:09 +00:00
l . Add ( "" , "" )
l . Add ( " " , "" )
l . Add ( " hi" , "hi" )
l . Add ( "hi " , "hi" )
l . Add ( "hi" , "hi" )
l . Add ( ":grinning:" , "😀" )
2020-07-26 04:36:40 +00:00
l . Add ( ":grinning: :grinning:" , "😀 😀" )
l . Add ( " :grinning: " , "😀" )
l . Add ( ": :grinning: :" , ": 😀 :" )
l . Add ( "::grinning::" , ":😀:" )
//l.Add("d:grinning:d", "d:grinning:d") // todo
l . Add ( "d :grinning: d" , "d 😀 d" )
2019-10-06 00:34:09 +00:00
l . Add ( "😀" , "😀" )
l . Add ( " " , "" )
l . Add ( "<p>" , "" )
l . Add ( "</p>" , "" )
l . Add ( "<p></p>" , "" )
2018-11-22 07:21:43 +00:00
2019-10-06 00:34:09 +00:00
l . Add ( "<" , "<" )
l . Add ( ">" , ">" )
l . Add ( "<meow>" , "<meow>" )
l . Add ( "<" , "&lt;" )
l . Add ( "&" , "&" )
2018-11-22 07:21:43 +00:00
// Note: strings.TrimSpace strips newlines, if there's nothing before or after them
2019-10-06 00:34:09 +00:00
l . Add ( "<br>" , "" )
l . Add ( "<br />" , "" )
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" , "" )
2018-11-22 07:21:43 +00:00
2019-10-06 00:34:09 +00:00
l . Add ( "ho<br>ho" , "ho\n\nho" )
l . Add ( "ho<br />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" )
2018-11-22 07:21:43 +00:00
2019-10-06 00:34:09 +00:00
l . Add ( "<b></b>" , "<strong></strong>" )
l . Add ( "<b>hi</b>" , "<strong>hi</strong>" )
l . Add ( "<b>h</b>" , "<strong>h</strong>" )
l . Add ( "<s>hi</s>" , "<del>hi</del>" )
l . Add ( "<del>hi</del>" , "<del>hi</del>" )
l . Add ( "<u>hi</u>" , "<u>hi</u>" )
l . Add ( "<em>hi</em>" , "<em>hi</em>" )
l . Add ( "<i>hi</i>" , "<em>hi</em>" )
l . Add ( "<strong>hi</strong>" , "<strong>hi</strong>" )
2019-11-06 21:15:43 +00:00
l . Add ( "<spoiler>hi</spoiler>" , "<spoiler>hi</spoiler>" )
l . Add ( "<g>hi</g>" , "hi" ) // Grammarly fix
2019-10-06 00:34:09 +00:00
l . Add ( "<b><i>hi</i></b>" , "<strong><em>hi</em></strong>" )
l . Add ( "<strong><em>hi</em></strong>" , "<strong><em>hi</em></strong>" )
l . Add ( "<b><i><b>hi</b></i></b>" , "<strong><em><strong>hi</strong></em></strong>" )
l . Add ( "<strong><em><strong>hi</strong></em></strong>" , "<strong><em><strong>hi</strong></em></strong>" )
l . Add ( "<div>hi</div>" , "<div>hi</div>" )
l . Add ( "<span>hi</span>" , "hi" ) // This is stripped since the editor (Trumbowyg) likes blasting useless spans
l . Add ( "<span >hi</span>" , "hi" )
l . Add ( "<span style='background-color: yellow;'>hi</span>" , "hi" )
l . Add ( "<span style='background-color: yellow;'>>hi</span>" , ">hi" )
l . Add ( "<b>hi" , "<strong>hi</strong>" )
l . Add ( "hi</b>" , "hi</b>" )
l . Add ( "</b>" , "</b>" )
l . Add ( "</del>" , "</del>" )
l . Add ( "</strong>" , "</strong>" )
l . Add ( "<b>" , "<strong></strong>" )
l . Add ( "<span style='background-color: yellow;'>hi" , "hi" )
l . Add ( "<span style='background-color:yellow;'>hi" , "hi" )
l . Add ( "hi</span>" , "hi" )
l . Add ( "</span>" , "" )
l . Add ( "<span></span>" , "" )
l . Add ( "<span ></span>" , "" )
l . Add ( "<span><span></span></span>" , "" )
l . Add ( "<span><b></b></span>" , "<strong></strong>" )
l . Add ( "<h1>t</h1>" , "<h2>t</h2>" )
l . Add ( "<h2>t</h2>" , "<h3>t</h3>" )
l . Add ( "<h3>t</h3>" , "<h4>t</h4>" )
l . Add ( "<></>" , "<></>" )
l . Add ( "</><>" , "</><>" )
l . Add ( "<>" , "<>" )
l . Add ( "</>" , "</>" )
l . Add ( "<p>hi</p>" , "hi" )
l . Add ( "<p></p>" , "" )
l . Add ( "<blockquote>hi</blockquote>" , "<blockquote>hi</blockquote>" )
l . Add ( "<blockquote><b>hi</b></blockquote>" , "<blockquote><strong>hi</strong></blockquote>" )
l . Add ( "<blockquote><meow>hi</meow></blockquote>" , "<blockquote><meow>hi</meow></blockquote>" )
l . Add ( "\\<blockquote>hi</blockquote>" , "<blockquote>hi</blockquote>" )
//l.Add("\\\\<blockquote><meow>hi</meow></blockquote>", "\\<blockquote><meow>hi</meow></blockquote>") // TODO: Double escapes should print a literal backslash
//l.Add("<blockquote>hi</blockquote>", "<blockquote>hi</blockquote>") // TODO: Stop double-entitising this
l . Add ( "\\<blockquote>hi</blockquote>\\<blockquote>hi</blockquote>" , "<blockquote>hi</blockquote><blockquote>hi</blockquote>" )
l . Add ( "\\<a itemprop=\"author\">Admin</a>" , "<a itemprop="author">Admin</a>" )
l . Add ( "<blockquote>\\<a itemprop=\"author\">Admin</a></blockquote>" , "<blockquote><a itemprop="author">Admin</a></blockquote>" )
l . Add ( "\n<blockquote>\\<a itemprop=\"author\">Admin</a></blockquote>\n" , "<blockquote><a itemprop="author">Admin</a></blockquote>" )
l . Add ( "tt\n<blockquote>\\<a itemprop=\"author\">Admin</a></blockquote>\ntt" , "tt\n<blockquote><a itemprop="author">Admin</a></blockquote>\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 ( "@元気" , "@元気" )
2019-05-17 08:40:41 +00:00
// TODO: More tests for unicode names?
2019-10-06 00:34:09 +00:00
//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}), "")
2018-11-22 07:21:43 +00:00
// TODO: Do a test with invalid UTF-8 input
2019-10-06 00:34:09 +00:00
for _ , item := range l . Items {
2019-10-13 07:32:05 +00:00
if res := c . PreparseMessage ( item . Msg ) ; res != item . Expects {
2018-11-22 07:21:43 +00:00
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 ) {
2019-07-11 07:20:33 +00:00
miscinit ( t )
if ! c . PluginsInited {
c . InitPlugins ( )
}
2019-10-06 00:34:09 +00:00
l := & METriList { nil }
2018-11-22 07:21:43 +00:00
url := "github.com/Azareal/Gosora"
2020-03-27 21:01:44 +00:00
eurl := "<a rel='ugc'href='//" + url + "'>" + url + "</a>"
2019-10-06 00:34:09 +00:00
l . Add ( "" , "" )
l . Add ( "haha" , "haha" )
2020-07-26 04:36:40 +00:00
l . Add ( ":P" , "😛" )
l . Add ( " :P " , " 😛 " )
l . Add ( ":p" , "😛" )
l . Add ( "d:p" , "d:p" )
l . Add ( ":pd" , "😛d" )
l . Add ( ":pdd" , "😛dd" )
l . Add ( ":pddd" , "😛ddd" )
l . Add ( ":p d" , "😛 d" )
l . Add ( ":p dd" , "😛 dd" )
l . Add ( ":p ddd" , "😛 ddd" )
//l.Add(":p:p:p", "😛😛😛")
l . Add ( ":p:p:p" , "😛:p:p" )
l . Add ( ":p :p" , "😛 😛" )
l . Add ( ":p :p :p" , "😛 😛 😛" )
l . Add ( ":p :p :p :p" , "😛 😛 😛 😛" )
l . Add ( ":p :p :p" , "😛 😛 😛" )
l . Add ( "word:p" , "word:p" )
l . Add ( "word:pword" , "word:pword" )
l . Add ( ":pword" , "😛word" ) // TODO: Change the semantics on this to detect the succeeding character?
l . Add ( "word :p" , "word 😛" )
l . Add ( ":p word" , "😛 word" )
2019-10-06 00:34:09 +00:00
l . Add ( "<b>t</b>" , "<b>t</b>" )
l . Add ( "//" , "//" )
l . Add ( "http://" , "<red>[Invalid URL]</red>" )
2021-04-05 06:03:53 +00:00
//l.Add("https://", "<red>[Invalid URL]</red>")
l . Add ( "ipfs://" , "<red>[Invalid URL]</red>" )
2019-10-06 00:34:09 +00:00
l . Add ( "ftp://" , "<red>[Invalid URL]</red>" )
l . Add ( "git://" , "<red>[Invalid URL]</red>" )
l . Add ( "ssh://" , "ssh://" )
2021-04-05 06:03:53 +00:00
l . Add ( "fff://" , "fff://" )
2019-05-22 04:45:07 +00:00
2019-10-06 00:34:09 +00:00
l . Add ( "// " , "// " )
l . Add ( "// //" , "// //" )
l . Add ( "// // //" , "// // //" )
l . Add ( "http:// " , "<red>[Invalid URL]</red> " )
l . Add ( "https:// " , "<red>[Invalid URL]</red> " )
l . Add ( "ftp:// " , "<red>[Invalid URL]</red> " )
l . Add ( "git:// " , "<red>[Invalid URL]</red> " )
l . Add ( "ssh:// " , "ssh:// " )
2021-04-05 06:03:53 +00:00
l . Add ( "fff:// " , "fff:// " )
2019-05-22 04:45:07 +00:00
2021-02-18 13:02:54 +00:00
l . Add ( "//t" , "<a rel='ugc'href='//t'>t</a>" )
2019-10-06 00:34:09 +00:00
l . Add ( "// t" , "// t" )
l . Add ( "http:// t" , "<red>[Invalid URL]</red> t" )
2019-05-22 04:45:07 +00:00
2021-02-18 13:02:54 +00:00
l . Add ( "g" , "g" )
2021-02-19 02:51:13 +00:00
l . Add ( "g/" , "g/" )
l . Add ( "g//" , "g//" )
2021-02-18 13:02:54 +00:00
l . Add ( "/g" , "/g" )
l . Add ( "/gg" , "/gg" )
l . Add ( "/g/" , "/g/" )
l . Add ( "hi" , "hi" )
l . Add ( "hit" , "hit" )
l . Add ( "hit:" , "hit:" )
2021-02-19 02:51:13 +00:00
l . Add ( "hit:/" , "hit:/" )
l . Add ( "hit://" , "hit://" )
l . Add ( "hit://t" , "hit://t" )
2019-12-31 21:57:54 +00:00
l . Add ( "h" , "h" )
l . Add ( "ht" , "ht" )
l . Add ( "htt" , "htt" )
l . Add ( "http" , "http" )
2019-10-06 00:34:09 +00:00
l . Add ( "http:" , "http:" )
2019-12-31 21:57:54 +00:00
//t l.Add("http:/", "http:/")
//t l.Add("http:/d", "http:/d")
l . Add ( "http:d" , "http:d" )
2019-10-06 00:34:09 +00:00
l . Add ( "https:" , "https:" )
2021-02-19 02:51:13 +00:00
l . Add ( "gttps:" , "gttps:" )
l . Add ( "gttps:/" , "gttps:/" )
l . Add ( "gttps://" , "gttps://" )
2019-10-06 00:34:09 +00:00
l . Add ( "ftp:" , "ftp:" )
l . Add ( "git:" , "git:" )
l . Add ( "ssh:" , "ssh:" )
2019-05-22 04:45:07 +00:00
2019-10-06 00:34:09 +00:00
l . Add ( "http" , "http" )
l . Add ( "https" , "https" )
l . Add ( "ftp" , "ftp" )
l . Add ( "git" , "git" )
l . Add ( "ssh" , "ssh" )
2019-05-22 04:45:07 +00:00
2019-10-06 00:34:09 +00:00
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<br>haha<br>haha" )
2019-10-06 01:49:31 +00:00
l . Add ( "//" + url , eurl )
2020-03-27 21:01:44 +00:00
l . Add ( "//a" , "<a rel='ugc'href='//a'>a</a>" )
l . Add ( " //a" , " <a rel='ugc'href='//a'>a</a>" )
l . Add ( "//a " , "<a rel='ugc'href='//a'>a</a> " )
l . Add ( " //a " , " <a rel='ugc'href='//a'>a</a> " )
l . Add ( "d //a " , "d <a rel='ugc'href='//a'>a</a> " )
l . Add ( "ddd ddd //a " , "ddd ddd <a rel='ugc'href='//a'>a</a> " )
l . Add ( "https://" + url , "<a rel='ugc'href='https://" + url + "'>" + url + "</a>" )
l . Add ( "https://t" , "<a rel='ugc'href='https://t'>t</a>" )
l . Add ( "http://" + url , "<a rel='ugc'href='http://" + url + "'>" + url + "</a>" )
2019-10-06 00:34:09 +00:00
l . Add ( "#http://" + url , "#http://" + url )
l . Add ( "@http://" + url , "<red>[Invalid Profile]</red>ttp://" + url )
2020-03-27 21:01:44 +00:00
l . Add ( "//" + url + "\n" , "<a rel='ugc'href='//" + url + "'>" + url + "</a><br>" )
2019-10-06 01:49:31 +00:00
l . Add ( "\n//" + url , "<br>" + eurl )
l . Add ( "\n//" + url + "\n" , "<br>" + eurl + "<br>" )
l . Add ( "\n//" + url + "\n\n" , "<br>" + eurl + "<br><br>" )
l . Add ( "//" + url + "\n//" + url , eurl + "<br>" + eurl )
2020-07-26 04:36:40 +00:00
l . Add ( "//" + url + " //" + url , eurl + " " + eurl )
l . Add ( "//" + url + " //" + url , eurl + " " + eurl )
//l.Add("//"+url+"//"+url, eurl+""+eurl)
//l.Add("//"+url+"|//"+url, eurl+"|"+eurl)
l . Add ( "//" + url + "|//" + url , "<red>[Invalid URL]</red>|//" + url )
2021-02-18 13:02:54 +00:00
l . Add ( "//" + url + "//" + url , "<a rel='ugc'href='//" + url + "//" + url + "'>" + url + "//" + url + "</a>" )
2019-10-06 01:49:31 +00:00
l . Add ( "//" + url + "\n\n//" + url , eurl + "<br><br>" + eurl )
2018-11-22 07:21:43 +00:00
2019-11-04 11:55:52 +00:00
pre2 := c . Config . SslSchema
c . Config . SslSchema = true
2019-10-13 07:32:05 +00:00
local := func ( u string ) {
s := "//" + c . Site . URL
fs := "http://" + c . Site . URL
2019-11-04 11:55:52 +00:00
if c . Config . SslSchema {
2019-10-13 07:32:05 +00:00
s = "https:" + s
fs = "https://" + c . Site . URL
}
2019-10-13 10:36:50 +00:00
l . Add ( "//" + u , "<a href='" + fs + "'>" + c . Site . URL + "</a>" )
2020-03-27 21:01:44 +00:00
// TODO: Strip redundant slashes?
l . Add ( "//" + u + "/" , "<a href='" + fs + "/'>" + c . Site . URL + "/</a>" )
l . Add ( "//" + u + "//" , "<a href='" + fs + "//'>" + c . Site . URL + "//</a>" )
2019-10-13 10:36:50 +00:00
l . Add ( "//" + u + "\n" , "<a href='" + fs + "'>" + c . Site . URL + "</a><br>" )
l . Add ( "//" + u + "\n//" + u , "<a href='" + fs + "'>" + c . Site . URL + "</a><br><a href='" + fs + "'>" + c . Site . URL + "</a>" )
2019-10-13 07:32:05 +00:00
l . Add ( "http://" + u , "<a href='" + fs + "'>" + c . Site . URL + "</a>" )
l . Add ( "https://" + u , "<a href='" + fs + "'>" + c . Site . URL + "</a>" )
2021-04-05 06:03:53 +00:00
l . Add ( "ipfs://testthis" , "<a rel='ugc'href='ipfs://testthis'>ipfs://testthis</a>" )
2020-03-28 04:48:42 +00:00
2020-04-30 06:28:01 +00:00
l . Add ( "//" + u + "/attachs/sha256hash.webm?sid=1&stype=forums" , "<video controls src=\"" + fs + "/attachs/sha256hash.webm?sid=1&stype=forums\"><a class='attach'href=\"" + fs + "/attachs/sha256hash.webm?sid=1&stype=forums\"download>Attachment</a></video>" )
l . Add ( "//" + u + "/attachs/sha256hash.webm" , "<video controls src=\"" + fs + "/attachs/sha256hash.webm?sid=1&stype=forums\"><a class='attach'href=\"" + fs + "/attachs/sha256hash.webm?sid=1&stype=forums\"download>Attachment</a></video>" )
l . Add ( "//" + u + "/attachs/sha256hash.webm?sid=1" , "<video controls src=\"" + fs + "/attachs/sha256hash.webm?sid=1&stype=forums\"><a class='attach'href=\"" + fs + "/attachs/sha256hash.webm?sid=1&stype=forums\"download>Attachment</a></video>" )
l . Add ( "//" + u + "/attachs/sha256hash.webm?stype=forums" , "<video controls src=\"" + fs + "/attachs/sha256hash.webm?sid=1&stype=forums\"><a class='attach'href=\"" + fs + "/attachs/sha256hash.webm?sid=1&stype=forums\"download>Attachment</a></video>" )
2020-05-05 08:10:19 +00:00
l . Add ( "//" + u + "/attachs/sha256hash.mp3?sid=1&stype=forums" , "<audio controls src=\"" + fs + "/attachs/sha256hash.mp3?sid=1&stype=forums\"><a class='attach'href=\"" + fs + "/attachs/sha256hash.mp3?sid=1&stype=forums\"download>Attachment</a></audio>" )
l . Add ( "//" + u + "/attachs/sha256hash.mp3" , "<audio controls src=\"" + fs + "/attachs/sha256hash.mp3?sid=1&stype=forums\"><a class='attach'href=\"" + fs + "/attachs/sha256hash.mp3?sid=1&stype=forums\"download>Attachment</a></audio>" )
l . Add ( "//" + u + "/attachs/sha256hash.mp3?sid=1" , "<audio controls src=\"" + fs + "/attachs/sha256hash.mp3?sid=1&stype=forums\"><a class='attach'href=\"" + fs + "/attachs/sha256hash.mp3?sid=1&stype=forums\"download>Attachment</a></audio>" )
l . Add ( "//" + u + "/attachs/sha256hash.mp3?stype=forums" , "<audio controls src=\"" + fs + "/attachs/sha256hash.mp3?sid=1&stype=forums\"><a class='attach'href=\"" + fs + "/attachs/sha256hash.mp3?sid=1&stype=forums\"download>Attachment</a></audio>" )
2020-03-27 21:01:44 +00:00
l . Add ( "//" + u + "/attachs/sha256hash.png?sid=1&stype=forums" , "<a href=\"" + fs + "/attachs/sha256hash.png?sid=1&stype=forums\"><img src='" + fs + "/attachs/sha256hash.png?sid=1&stype=forums'class='postImage'></a>" )
l . Add ( "//" + u + "/attachs/sha256hash?sid=1&stype=forums" , "<red>[Invalid URL]</red>" )
l . Add ( "//" + u + "/attachs/s?sid=1&stype=forums" , "<red>[Invalid URL]</red>" )
l . Add ( "//" + u + "/attachs/?sid=1&stype=forums" , "<red>[Invalid URL]</red>" )
l . Add ( "//" + u + "/attachs/sha256hash.?sid=1&stype=forums" , "<red>[Invalid URL]</red>" )
l . Add ( "//" + u + "/attachs?sid=1&stype=forums" , "<red>[Invalid URL]</red>" )
l . Add ( "//" + u + "/attachs/sha256hash.png" , "<a href=\"" + fs + "/attachs/sha256hash.png?sid=1&stype=forums\"><img src='" + fs + "/attachs/sha256hash.png?sid=1&stype=forums'class='postImage'></a>" )
l . Add ( "//" + u + "/attachs/sha256hash.png?sid=1" , "<a href=\"" + fs + "/attachs/sha256hash.png?sid=1&stype=forums\"><img src='" + fs + "/attachs/sha256hash.png?sid=1&stype=forums'class='postImage'></a>" )
l . Add ( "//" + u + "/attachs/sha256hash.png?stype=forums" , "<a href=\"" + fs + "/attachs/sha256hash.png?sid=1&stype=forums\"><img src='" + fs + "/attachs/sha256hash.png?sid=1&stype=forums'class='postImage'></a>" )
2020-03-28 04:48:42 +00:00
2020-06-08 11:13:19 +00:00
//l.Add("//"+u+"/attachs/sha256hash.txt?sid=1&stype=forums", "<a class='attach'href=\""+fs+"/attachs/sha256hash.txt?sid=1&stype=forums\"download>Attachment</a>")
l . Add ( "//" + u + "/attachs/sha256hash.txt?sid=1&stype=forums" , "<div class='attach_box'><div class='attach_info'>" + fs + "/attachs/sha256hash.txt</div><div class='attach_opts'><a class='attach'href=\"" + fs + "/attachs/sha256hash.txt?sid=1&stype=forums\">View</a> / <a class='attach'href=\"" + fs + "/attachs/sha256hash.txt?sid=1&stype=forums\"download>Download</a></div></div>" )
2020-03-28 04:48:42 +00:00
l . Add ( "//example.com/image.png" , "<a href=\"//example.com/image.png\"><img src='//example.com/image.png'class='postImage'></a>" )
l . Add ( "https://example.com/image.png" , "<a href=\"https://example.com/image.png\"><img src='https://example.com/image.png'class='postImage'></a>" )
l . Add ( "http://example.com/image.png" , "<a href=\"http://example.com/image.png\"><img src='http://example.com/image.png'class='postImage'></a>" )
2019-05-23 23:39:24 +00:00
}
local ( "localhost" )
local ( "127.0.0.1" )
local ( "[::1]" )
2020-06-25 07:46:09 +00:00
l . Add ( "https://www.youtube.com/watch?v=lalalalala" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala'>https://www.youtube.com/watch?v=lalalalala</a></noscript>" )
l . Add ( "https://www.youtube.com/watch?v=lalalalala&t=30s" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala?start=30'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala&t=30s'>https://www.youtube.com/watch?v=lalalalala&t=30s</a></noscript>" )
l . Add ( "https://www.youtube.com/watch?v=lalalalala&t=1s" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala'>https://www.youtube.com/watch?v=lalalalala</a></noscript>" )
l . Add ( "https://www.youtube.com/watch?v=lalalalala&t=1" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala'>https://www.youtube.com/watch?v=lalalalala</a></noscript>" )
//l.Add("https://www.youtube.com/watch?v=;","<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/;'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch'>https://www.youtube.com/watch</a></noscript>")
l . Add ( "https://www.youtube.com/watch?v=d;" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/d'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=d'>https://www.youtube.com/watch?v=d</a></noscript>" )
l . Add ( "https://www.youtube.com/watch?v=d;d" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/d'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=d'>https://www.youtube.com/watch?v=d</a></noscript>" )
2019-10-06 01:49:31 +00:00
l . Add ( "https://www.youtube.com/watch?v=alert()" , "<red>[Invalid URL]</red>()" )
l . Add ( "https://www.youtube.com/watch?v=alert()()" , "<red>[Invalid URL]</red>()()" )
l . Add ( "https://www.youtube.com/watch?v=js:alert()" , "<red>[Invalid URL]</red>()" )
l . Add ( "https://www.youtube.com/watch?v='+><script>alert(\"\")</script><+'" , "<red>[Invalid URL]</red>'+><script>alert(\"\")</script><+'" )
l . Add ( "https://www.youtube.com/watch?v='+onready='alert(\"\")'+'" , "<red>[Invalid URL]</red>'+onready='alert(\"\")'+'" )
2020-06-25 07:46:09 +00:00
l . Add ( " https://www.youtube.com/watch?v=lalalalala" , " <iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala'>https://www.youtube.com/watch?v=lalalalala</a></noscript>" )
l . Add ( "https://www.youtube.com/watch?v=lalalalala tt" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala'>https://www.youtube.com/watch?v=lalalalala</a></noscript> tt" )
l . Add ( "https://www.youtube.com/watch?v=lalalalala&d=haha" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala'>https://www.youtube.com/watch?v=lalalalala</a></noscript>" )
l . Add ( "https://gaming.youtube.com/watch?v=lalalalala" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://gaming.youtube.com/watch?v=lalalalala'>https://gaming.youtube.com/watch?v=lalalalala</a></noscript>" )
l . Add ( "https://gaming.youtube.com/watch?v=lalalalala&d=haha" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://gaming.youtube.com/watch?v=lalalalala'>https://gaming.youtube.com/watch?v=lalalalala</a></noscript>" )
l . Add ( "https://youtu.be/lalalalala" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://youtu.be/lalalalala'>https://youtu.be/lalalalala</a></noscript>" )
l . Add ( "https://m.youtube.com/watch?v=lalalalala" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://m.youtube.com/watch?v=lalalalala'>https://m.youtube.com/watch?v=lalalalala</a></noscript>" )
l . Add ( "https://m.youtube.com/watch?v=lalalalala&d=haha" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://m.youtube.com/watch?v=lalalalala'>https://m.youtube.com/watch?v=lalalalala</a></noscript>" )
l . Add ( "http://www.youtube.com/watch?v=lalalalala" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala'>https://www.youtube.com/watch?v=lalalalala</a></noscript>" )
l . Add ( "//www.youtube.com/watch?v=lalalalala" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala'>https://www.youtube.com/watch?v=lalalalala</a></noscript>" )
//l.Add("www.youtube.com/watch?v=lalalalala","<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala'>https://www.youtube.com/watch?v=lalalalala</a></noscript>")
l . Add ( "https://www.nicovideo.jp/watch/sm111111" , "<iframe class='postIframe'src='https://embed.nicovideo.jp/watch/sm111111?jsapi=1&playerId=1'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.nicovideo.jp/watch/sm111111'>https://www.nicovideo.jp/watch/sm111111</a></noscript>" )
//l.Add("www.nicovideo.jp/watch/sm111111", "<iframe class='postIframe'src='https://embed.nicovideo.jp/watch/sm111111?jsapi=1&playerId=1'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.nicovideo.jp/watch/sm111111'>www.nicovideo.jp/watch/sm111111</a></noscript>")
//l.Add("www.nicovideo.jp/watch/smlalalalala", "<a href='www.nicovideo.jp/watch/smlalalalala'>www.nicovideo.jp/watch/smlalalalala</a>")
l . Add ( "https://www.nicovideo.jp/watch/smlalalalala" , "<a rel='ugc'href='https://www.nicovideo.jp/watch/smlalalalala'>www.nicovideo.jp/watch/smlalalalala</a>" )
l . Add ( "//www.youtube.com/watch?v=lalalalala&t=30s" , "<iframe class='postIframe'src='https://www.youtube-nocookie.com/embed/lalalalala?start=30'frameborder=0 allowfullscreen></iframe><noscript><a href='https://www.youtube.com/watch?v=lalalalala&t=30s'>https://www.youtube.com/watch?v=lalalalala&t=30s</a></noscript>" )
2019-05-23 23:39:24 +00:00
2019-10-06 00:34:09 +00:00
l . Add ( "#tid-1" , "<a href='/topic/1'>#tid-1</a>" )
2020-07-26 04:36:40 +00:00
l . Add ( "#tid-1#tid-1" , "<a href='/topic/1'>#tid-1</a>#tid-1" )
2019-10-06 00:34:09 +00:00
l . Add ( "##tid-1" , "##tid-1" )
2020-06-25 07:46:09 +00:00
l . Add ( "#@tid-1" , "#@tid-1" )
2019-10-06 00:34:09 +00:00
l . Add ( "# #tid-1" , "# #tid-1" )
l . Add ( "@ #tid-1" , "<red>[Invalid Profile]</red>#tid-1" )
l . Add ( "@#tid-1" , "<red>[Invalid Profile]</red>tid-1" )
l . Add ( "@ #tid-@" , "<red>[Invalid Profile]</red>#tid-@" )
l . Add ( "#tid-1 #tid-1" , "<a href='/topic/1'>#tid-1</a> <a href='/topic/1'>#tid-1</a>" )
l . Add ( "#tid-0" , "<red>[Invalid Topic]</red>" )
2020-03-27 21:01:44 +00:00
l . Add ( "https://" + url + "/#tid-1" , "<a rel='ugc'href='https://" + url + "/#tid-1'>" + url + "/#tid-1</a>" )
l . Add ( "https://" + url + "/?hi=2" , "<a rel='ugc'href='https://" + url + "/?hi=2'>" + url + "/?hi=2</a>" )
l . Add ( "https://" + url + "/?hi=2#t=1" , "<a rel='ugc'href='https://" + url + "/?hi=2#t=1'>" + url + "/?hi=2#t=1</a>" )
2019-10-06 00:34:09 +00:00
l . Add ( "#fid-1" , "<a href='/forum/1'>#fid-1</a>" )
l . Add ( " #fid-1" , " <a href='/forum/1'>#fid-1</a>" )
l . Add ( "#fid-0" , "<red>[Invalid Forum]</red>" )
l . Add ( " #fid-0" , " <red>[Invalid Forum]</red>" )
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 ( "@@" , "<red>[Invalid Profile]</red>" )
l . Add ( "@@ @@" , "<red>[Invalid Profile]</red> <red>[Invalid Profile]</red>" )
l . Add ( "@@1" , "<red>[Invalid Profile]</red>1" )
l . Add ( "@#1" , "<red>[Invalid Profile]</red>1" )
l . Add ( "@##1" , "<red>[Invalid Profile]</red>#1" )
l . Add ( "@2" , "<red>[Invalid Profile]</red>" )
l . Add ( "@2t" , "<red>[Invalid Profile]</red>t" )
l . Add ( "@2 t" , "<red>[Invalid Profile]</red> t" )
l . Add ( "@2 " , "<red>[Invalid Profile]</red> " )
l . Add ( "@2 @2" , "<red>[Invalid Profile]</red> <red>[Invalid Profile]</red>" )
2020-03-28 04:48:42 +00:00
l . Add ( "@1" , "<a href='/user/admin.1'class='mention'>@Admin</a>" )
l . Add ( " @1" , " <a href='/user/admin.1'class='mention'>@Admin</a>" )
l . Add ( "@1t" , "<a href='/user/admin.1'class='mention'>@Admin</a>t" )
l . Add ( "@1 " , "<a href='/user/admin.1'class='mention'>@Admin</a> " )
l . Add ( "@1 @1" , "<a href='/user/admin.1'class='mention'>@Admin</a> <a href='/user/admin.1'class='mention'>@Admin</a>" )
2019-10-06 00:34:09 +00:00
l . Add ( "@0" , "<red>[Invalid Profile]</red>" )
l . Add ( "@-1" , "<red>[Invalid Profile]</red>1" )
2018-11-22 07:21:43 +00:00
2019-10-13 09:07:05 +00:00
// TODO: Fix this hack and make the results a bit more reproducible, push the tests further in the process.
2021-02-18 13:02:54 +00:00
c . GuestUser . Perms . AutoLink = true
2019-10-06 00:34:09 +00:00
for _ , item := range l . Items {
2020-02-04 11:47:03 +00:00
if res := c . ParseMessage ( item . Msg , 1 , "forums" , nil , nil ) ; res != item . Expects {
2019-10-13 07:32:05 +00:00
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
}
}
2019-11-04 11:55:52 +00:00
c . Config . SslSchema = pre2
2019-10-13 07:32:05 +00:00
l = & METriList { nil }
pre := c . Site . URL // Just in case this is localhost...
2019-11-04 11:55:52 +00:00
pre2 = c . Config . SslSchema
2019-10-13 07:32:05 +00:00
c . Site . URL = "example.com"
2019-11-04 11:55:52 +00:00
c . Config . SslSchema = true
2019-10-13 07:32:05 +00:00
l . Add ( "//" + c . Site . URL , "<a href='https://" + c . Site . URL + "'>" + c . Site . URL + "</a>" )
l . Add ( "//" + c . Site . URL + "\n" , "<a href='https://" + c . Site . URL + "'>" + c . Site . URL + "</a><br>" )
l . Add ( "//" + c . Site . URL + "\n//" + c . Site . URL , "<a href='https://" + c . Site . URL + "'>" + c . Site . URL + "</a><br><a href='https://" + c . Site . URL + "'>" + c . Site . URL + "</a>" )
for _ , item := range l . Items {
2020-02-04 11:47:03 +00:00
if res := c . ParseMessage ( item . Msg , 1 , "forums" , nil , nil ) ; res != item . Expects {
2018-11-22 07:21:43 +00:00
if item . Name != "" {
t . Error ( "Name: " , item . Name )
}
t . Error ( "Testing string '" + item . Msg + "'" )
t . Error ( "Bad output:" , "'" + res + "'" )
t . Error ( "Expected:" , "'" + item . Expects + "'" )
2019-05-22 04:45:07 +00:00
break
}
}
2019-10-13 07:32:05 +00:00
c . Site . URL = pre
2019-11-04 11:55:52 +00:00
c . Config . SslSchema = pre2
2019-05-22 04:45:07 +00:00
2021-02-19 02:51:13 +00:00
l = & METriList { nil }
l . Add ( "//" , "//" )
l . Add ( "//z" , "//z" )
l . Add ( "//" + url , "//" + url )
l . Add ( "https://www.youtube.com/watch?v=lalalalala&t=1" , "https://www.youtube.com/watch?v=lalalalala&t=1" )
l . Add ( "#tid-1" , "<a href='/topic/1'>#tid-1</a>" )
c . GuestUser . Perms . AutoLink = false
for _ , item := range l . Items {
if res := c . ParseMessage ( item . Msg , 1 , "forums" , nil , nil ) ; 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
}
}
2019-05-22 04:45:07 +00:00
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 ) )
} )
2020-02-04 11:47:03 +00:00
res := c . ParseMessage ( "#nnid-1" , 1 , "forums" , nil , nil )
2019-05-22 04:45:07 +00:00
expect := "<a href='/topic/1'>#nnid-1</a>"
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
2018-11-22 07:21:43 +00:00
}
2019-05-22 04:45:07 +00:00
c . WriteURL ( sb , c . BuildTopicURL ( "" , tid ) , "#longidnameneedtooverflowhack-" + strconv . Itoa ( tid ) )
} )
2020-03-09 01:37:31 +00:00
res = c . ParseMessage ( "#longidnameneedtooverflowhack-1" , 1 , "forums" , nil , nil )
2019-05-22 04:45:07 +00:00
expect = "<a href='/topic/1'>#longidnameneedtooverflowhack-1</a>"
if res != expect {
t . Error ( "Bad output:" , "'" + res + "'" )
t . Error ( "Expected:" , "'" + expect + "'" )
2018-11-22 07:21:43 +00:00
}
}
2020-03-09 01:37:31 +00:00
func TestPaginate ( t * testing . T ) {
var plist [ ] int
f := func ( i , want int ) {
expect ( t , plist [ i ] == want , fmt . Sprintf ( "plist[%d] should be %d not %d" , i , want , plist [ i ] ) )
}
plist = c . Paginate ( 1 , 1 , 5 )
expect ( t , len ( plist ) == 1 , fmt . Sprintf ( "len of plist should be 1 not %d" , len ( plist ) ) )
f ( 0 , 1 )
plist = c . Paginate ( 1 , 5 , 5 )
expect ( t , len ( plist ) == 5 , fmt . Sprintf ( "len of plist should be 5 not %d" , len ( plist ) ) )
f ( 0 , 1 )
f ( 1 , 2 )
f ( 2 , 3 )
f ( 3 , 4 )
f ( 4 , 5 )
// TODO: More Paginate() tests
// TODO: Tests for other paginator functions
}