github.com/jfrog/jfrog-client-go@v1.40.2/utils/regexputils_test.go (about) 1 package utils 2 3 import ( 4 "strings" 5 "testing" 6 7 gofrogio "github.com/jfrog/gofrog/io" 8 "github.com/jfrog/jfrog-client-go/utils/log" 9 ) 10 11 func init() { 12 log.SetLogger(log.NewLogger(log.DEBUG, nil)) 13 } 14 15 func TestRemoveCredentialsFromLine(t *testing.T) { 16 regExpProtocol, err := GetRegExp(CredentialsInUrlRegexp) 17 if err != nil { 18 t.Error(err) 19 } 20 21 tests := []struct { 22 name string 23 regex gofrogio.CmdOutputPattern 24 expectedLine string 25 matched bool 26 }{ 27 {"http", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line http://127.0.0.1:8081/artifactory/path/to/repo", true}, 28 {"https", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true}, 29 {"git", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line git://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line git://127.0.0.1:8081/artifactory/path/to/repo", true}, 30 {"Special characters 1", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://u-s!<e>_r:!p-a&%%s%sword@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true}, 31 {"Special characters 2", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://!user:[p]a(s)sword@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true}, 32 {"http with token", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line http://123456@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line http://127.0.0.1:8081/artifactory/path/to/repo", true}, 33 {"https with token", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://123456@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true}, 34 {"git with token", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line git://123456@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line git://127.0.0.1:8081/artifactory/path/to/repo", true}, 35 {"Special characters 1 with token", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://u-s!<e>_r!p-a&%%s%sword@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true}, 36 {"Special characters 2 with token", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://!user[p]a(s)sword@127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", true}, 37 {"No credentials", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo"}, "This is an example line https://127.0.0.1:8081/artifactory/path/to/repo", false}, 38 {"No http", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "This is an example line"}, "This is an example line", false}, 39 } 40 for _, test := range tests { 41 t.Run(test.name, func(t *testing.T) { 42 test.regex.MatchedResults = test.regex.RegExp.FindStringSubmatch(test.regex.Line) 43 if test.matched && len(test.regex.MatchedResults) > 3 { 44 t.Errorf("Expected to find 3 results, however, found %d.", len(test.regex.MatchedResults)) 45 } 46 if test.matched && test.regex.MatchedResults[0] == "" { 47 t.Error("Expected to find a match.") 48 } 49 if test.matched { 50 actual := RemoveCredentials(test.regex.Line, test.regex.MatchedResults[0]) 51 if !strings.EqualFold(actual, test.expectedLine) { 52 t.Errorf("Expected: %s, The Regex found %s and the masked line: %s", test.expectedLine, test.regex.MatchedResults[0], actual) 53 } 54 } 55 if !test.matched && len(test.regex.MatchedResults) != 0 { 56 t.Error("Expected to find zero match, found:", test.regex.MatchedResults[0]) 57 } 58 }) 59 } 60 } 61 62 func TestReturnErrorOnNotFound(t *testing.T) { 63 regExpProtocol, err := GetRegExp(`^go: ([^\/\r\n]+\/[^\r\n\s:]*).*(404( Not Found)?[\s]?)$`) 64 if err != nil { 65 t.Error(err) 66 } 67 68 tests := []struct { 69 name string 70 regex gofrogio.CmdOutputPattern 71 error bool 72 }{ 73 {"Without Error", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: github.com/jfrog/jfrog-client-go@v0.2.1: This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo"}, false}, 74 {"With Error No Response Message", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: github.com/jfrog/jfrog-client-go@v0.2.1: This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo: 404"}, true}, 75 {"With Error With response message", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: github.com/jfrog/jfrog-client-go@v0.2.1: This is an example line http://user:password@127.0.0.1:8081/artifactory/path/to/repo: 404 Not Found"}, true}, 76 {"On Different Message", gofrogio.CmdOutputPattern{RegExp: regExpProtocol, Line: "go: finding github.com/elazarl/go-bindata-assetfs v0.0.0-20151224045452-57eb5e1fc594"}, false}, 77 } 78 for _, test := range tests { 79 t.Run(test.name, func(t *testing.T) { 80 test.regex.MatchedResults = test.regex.RegExp.FindStringSubmatch(test.regex.Line) 81 if test.error && len(test.regex.MatchedResults) < 3 { 82 t.Errorf("Expected to find at least 3 results, however, found %d.", len(test.regex.MatchedResults)) 83 } 84 if test.error && test.regex.MatchedResults[0] == "" { 85 t.Error("Expected to find 404 not found, found nothing.") 86 } 87 if !test.error && len(test.regex.MatchedResults) != 0 { 88 t.Error("Expected regex to return empty result. Got:", test.regex.MatchedResults[0]) 89 } 90 }) 91 } 92 }