github.com/masterhung0112/hk_server/v5@v5.0.0-20220302090640-ec71aef15e1c/services/sharedchannel/util_test.go (about) 1 // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. 2 // See LICENSE.txt for license information. 3 4 package sharedchannel 5 6 import ( 7 "strings" 8 "testing" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func Test_mungUsername(t *testing.T) { 14 type args struct { 15 username string 16 remotename string 17 suffix string 18 maxLen int 19 } 20 tests := []struct { 21 name string 22 args args 23 want string 24 }{ 25 {"everything empty", args{username: "", remotename: "", suffix: "", maxLen: 64}, ":"}, 26 27 {"no trunc, no suffix", args{username: "bart", remotename: "example.com", suffix: "", maxLen: 64}, "bart:example.com"}, 28 {"no trunc, suffix", args{username: "bart", remotename: "example.com", suffix: "2", maxLen: 64}, "bart~2:example.com"}, 29 30 {"trunc remote, no suffix", args{username: "bart", remotename: "example1234567890.com", suffix: "", maxLen: 24}, "bart:example123456789..."}, 31 {"trunc remote, suffix", args{username: "bart", remotename: "example1234567890.com", suffix: "2", maxLen: 24}, "bart~2:example1234567..."}, 32 33 {"trunc both, no suffix", args{username: R(24, "A"), remotename: R(24, "B"), suffix: "", maxLen: 24}, "AAAAAAAAA...:BBBBBBBB..."}, 34 {"trunc both, suffix", args{username: R(24, "A"), remotename: R(24, "B"), suffix: "10", maxLen: 24}, "AAAAAA~10...:BBBBBBBB..."}, 35 36 {"trunc user, no suffix", args{username: R(40, "A"), remotename: "abc", suffix: "", maxLen: 24}, "AAAAAAAAAAAAAAAAA...:abc"}, 37 {"trunc user, suffix", args{username: R(40, "A"), remotename: "abc", suffix: "11", maxLen: 24}, "AAAAAAAAAAAAAA~11...:abc"}, 38 39 {"trunc user, remote, no suffix", args{username: R(40, "A"), remotename: "abcdefghijk", suffix: "", maxLen: 24}, "AAAAAAAAA...:abcdefghijk"}, 40 {"trunc user, remote, suffix", args{username: R(40, "A"), remotename: "abcdefghijk", suffix: "19", maxLen: 24}, "AAAAAA~19...:abcdefghijk"}, 41 42 {"short user, long remote, no suffix", args{username: "bart", remotename: R(40, "B"), suffix: "", maxLen: 24}, "bart:BBBBBBBBBBBBBBBB..."}, 43 {"long user, short remote, no suffix", args{username: R(40, "A"), remotename: "abc.com", suffix: "", maxLen: 24}, "AAAAAAAAAAAAA...:abc.com"}, 44 45 {"short user, long remote, suffix", args{username: "bart", remotename: R(40, "B"), suffix: "12", maxLen: 24}, "bart~12:BBBBBBBBBBBBB..."}, 46 {"long user, short remote, suffix", args{username: R(40, "A"), remotename: "abc.com", suffix: "12", maxLen: 24}, "AAAAAAAAAA~12...:abc.com"}, 47 } 48 for _, tt := range tests { 49 t.Run(tt.name, func(t *testing.T) { 50 if got := mungUsername(tt.args.username, tt.args.remotename, tt.args.suffix, tt.args.maxLen); got != tt.want { 51 t.Errorf("mungUsername() = %v, want %v", got, tt.want) 52 } 53 }) 54 } 55 } 56 57 func Test_mungUsernameFuzz(t *testing.T) { 58 // ensure no index out of bounds panic for any combination 59 for i := 0; i < 70; i++ { 60 for j := 0; j < 70; j++ { 61 for k := 0; k < 3; k++ { 62 username := R(i, "A") 63 remotename := R(j, "B") 64 suffix := R(k, "1") 65 66 result := mungUsername(username, remotename, suffix, 64) 67 require.LessOrEqual(t, len(result), 64) 68 } 69 } 70 } 71 } 72 73 // R returns a string with the specified string repeated `count` times. 74 func R(count int, s string) string { 75 return strings.Repeat(s, count) 76 }