Skip to content Search Apps Documentation Source Content File Folder Download Copy Actions State String Boolean Number Struct Map Slice Pointer Function Closure Reference Nil Package Type Interface Unknown

Community realm. Read the code first. Community realm, deployed by its author. Read the code before you interact or send coins.

validate_test.gno

3.65 Kb · 121 lines
  1package gnopm
  2
  3import (
  4	"strings"
  5	"testing"
  6
  7	"gno.land/p/nt/uassert/v0"
  8)
  9
 10func TestValidCommit(t *testing.T) {
 11	sha1 := strings.Repeat("a", 40)
 12	sha256 := strings.Repeat("0", 64)
 13	cases := []struct {
 14		name string
 15		in   string
 16		want bool
 17	}{
 18		{"sha1", sha1, true},
 19		{"sha256", sha256, true},
 20		{"mixed hex", "0123456789abcdef0123456789abcdef01234567", true},
 21		{"empty", "", false},
 22		{"too short", strings.Repeat("a", 39), false},
 23		{"between the two lengths", strings.Repeat("a", 41), false},
 24		{"uppercase", strings.Repeat("A", 40), false},
 25		{"non hex", strings.Repeat("g", 40), false},
 26		{"abbreviated", "0123456", false},
 27	}
 28	for _, tc := range cases {
 29		uassert.Equal(t, tc.want, ValidCommit(tc.in), tc.name)
 30	}
 31}
 32
 33func TestValidRepo(t *testing.T) {
 34	cases := []struct {
 35		name string
 36		in   string
 37		want bool
 38	}{
 39		{"github", "https://github.com/gnolang/gno", true},
 40		{"no path", "https://example.com", true},
 41		{"trailing slash", "https://example.com/x/", true},
 42		{"port is not a host character here", "https://example.com:8443/x", false},
 43
 44		{"empty", "", false},
 45		{"http", "http://github.com/gnolang/gno", false},
 46		{"git scheme", "git://github.com/gnolang/gno", false},
 47		{"ssh scheme", "ssh://[email protected]/gnolang/gno", false},
 48		{"javascript", "javascript:alert(1)", false},
 49		{"data url", "data:text/html,<script>", false},
 50		{"scheme relative", "//github.com/gnolang/gno", false},
 51		{"no host", "https:///gnolang/gno", false},
 52		{"bare host with no dot", "https://localhost/x", false},
 53		{"userinfo disguise", "https://[email protected]/gnolang/gno", false},
 54		{"parent traversal", "https://example.com/../x", false},
 55		{"space", "https://example.com/a b", false},
 56		{"markdown link break-out", "https://example.com/a)[x](y", false},
 57		{"newline", "https://example.com/a\nb", false},
 58		{"bidi override", "https://example.com/a‮b", false},
 59	}
 60	for _, tc := range cases {
 61		uassert.Equal(t, tc.want, ValidRepo(tc.in), tc.name)
 62	}
 63}
 64
 65func TestValidDir(t *testing.T) {
 66	cases := []struct {
 67		name string
 68		in   string
 69		want bool
 70	}{
 71		{"repo root", "", true},
 72		{"one element", "examples", true},
 73		{"nested", "examples/gno.land/p/nt/avl", true},
 74		{"dots in a name", "p/moul.io/x", true},
 75
 76		{"absolute", "/examples", false},
 77		{"trailing slash", "examples/", false},
 78		{"empty element", "a//b", false},
 79		{"dot element", "a/./b", false},
 80		{"parent traversal", "a/../b", false},
 81		{"backslash", "a\\b", false},
 82		{"space", "a b", false},
 83	}
 84	for _, tc := range cases {
 85		uassert.Equal(t, tc.want, ValidDir(tc.in), tc.name)
 86	}
 87}
 88
 89func TestValidRef(t *testing.T) {
 90	cases := []struct {
 91		name string
 92		in   string
 93		want bool
 94	}{
 95		{"absent is legal", "", true},
 96		{"branch", "refs/heads/main", true},
 97		{"tag", "refs/tags/v1.2.0", true},
 98
 99		{"unqualified branch", "main", false},
100		{"double dot", "refs/heads/a..b", false},
101		{"reflog syntax", "refs/heads/a@{1}", false},
102		{"trailing slash", "refs/heads/", false},
103		{"lock suffix", "refs/heads/main.lock", false},
104		{"leading dot", "refs/heads/.hidden", false},
105		{"caret", "refs/heads/a^b", false},
106		{"space", "refs/heads/a b", false},
107
108		// Narrower than git on purpose: these are legal ref names that would
109		// break out of a markdown table cell or an inline-code span, and every
110		// other field in this package is an allowlist so that no consumer has
111		// to escape anything.
112		{"pipe breaks a table cell", "refs/heads/a|b", false},
113		{"backtick breaks a code span", "refs/heads/a`b", false},
114		{"brackets open a link", "refs/heads/a[b]c", false},
115		{"parens close a link", "refs/heads/a(b)c", false},
116		{"hash opens a heading", "refs/heads/a#b", false},
117	}
118	for _, tc := range cases {
119		uassert.Equal(t, tc.want, ValidRef(tc.in), tc.name)
120	}
121}