github.com/minio/console@v1.4.1/pkg/utils/utils_test.go (about) 1 // This file is part of MinIO Console Server 2 // Copyright (c) 2022 MinIO, Inc. 3 // 4 // This program is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Affero General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // This program is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Affero General Public License for more details. 13 // 14 // You should have received a copy of the GNU Affero General Public License 15 // along with this program. If not, see <http://www.gnu.org/licenses/>. 16 17 package utils 18 19 import ( 20 "context" 21 "testing" 22 ) 23 24 func TestDecodeInput(t *testing.T) { 25 type args struct { 26 s string 27 } 28 tests := []struct { 29 name string 30 args args 31 want string 32 wantErr bool 33 }{ 34 { 35 name: "chinese characters", 36 args: args{ 37 s: "5bCP6aO85by+5bCP6aO85by+5bCP6aO85by+L+Wwj+mjvOW8vuWwj+mjvOW8vuWwj+mjvOW8vg==", 38 }, 39 want: "小飼弾小飼弾小飼弾/小飼弾小飼弾小飼弾", 40 wantErr: false, 41 }, 42 { 43 name: "spaces and & symbol", 44 args: args{ 45 s: "YSBhIC0gYSBhICYgYSBhIC0gYSBhIGE=", 46 }, 47 want: "a a - a a & a a - a a a", 48 wantErr: false, 49 }, 50 { 51 name: "the infamous fly me to the moon", 52 args: args{ 53 s: "MDIlMjAtJTIwRkxZJTIwTUUlMjBUTyUyMFRIRSUyME1PT04lMjA=", 54 }, 55 want: "02%20-%20FLY%20ME%20TO%20THE%20MOON%20", 56 wantErr: false, 57 }, 58 { 59 name: "random symbols", 60 args: args{ 61 s: "IUAjJCVeJiooKV8r", 62 }, 63 want: "!@#$%^&*()_+", 64 wantErr: false, 65 }, 66 { 67 name: "name with / symbols", 68 args: args{ 69 s: "dGVzdC90ZXN0Mi/lsI/po7zlvL7lsI/po7zlvL7lsI/po7zlvL4uanBn", 70 }, 71 want: "test/test2/小飼弾小飼弾小飼弾.jpg", 72 wantErr: false, 73 }, 74 { 75 name: "decoding fails", 76 args: args{ 77 s: "this should fail", 78 }, 79 want: "", 80 wantErr: true, 81 }, 82 } 83 for _, tt := range tests { 84 t.Run(tt.name, func(_ *testing.T) { 85 got, err := DecodeBase64(tt.args.s) 86 if (err != nil) != tt.wantErr { 87 t.Errorf("DecodeBase64() error = %v, wantErr %v", err, tt.wantErr) 88 return 89 } 90 if got != tt.want { 91 t.Errorf("DecodeBase64() got = %v, want %v", got, tt.want) 92 } 93 }) 94 } 95 } 96 97 func TestClientIPFromContext(t *testing.T) { 98 type args struct { 99 ctx context.Context 100 } 101 tests := []struct { 102 name string 103 args args 104 want string 105 }{ 106 { 107 name: "working return", 108 args: args{ 109 ctx: context.Background(), 110 }, 111 want: "127.0.0.1", 112 }, 113 { 114 name: "context contains ip", 115 args: args{ 116 ctx: context.WithValue(context.Background(), ContextClientIP, "10.0.0.1"), 117 }, 118 want: "10.0.0.1", 119 }, 120 } 121 for _, tt := range tests { 122 t.Run(tt.name, func(_ *testing.T) { 123 if got := ClientIPFromContext(tt.args.ctx); got != tt.want { 124 t.Errorf("ClientIPFromContext() = %v, want %v", got, tt.want) 125 } 126 }) 127 } 128 }