github.com/grafviktor/keep-my-secret@v0.9.10-0.20230908165355-19f35cce90e5/internal/api/utils/utils_test.go (about) 1 package utils 2 3 import ( 4 "math/rand" 5 "reflect" 6 "strings" 7 "testing" 8 "time" 9 10 "github.com/stretchr/testify/require" 11 ) 12 13 func TestIsPasswordConformsPolicy(t *testing.T) { 14 type args struct { 15 password string 16 } 17 tests := []struct { 18 name string 19 args args 20 want bool 21 }{ 22 { 23 name: "HashedPassword is OK", 24 args: args{password: "1"}, 25 want: true, 26 }, 27 { 28 name: "HashedPassword is empty", 29 args: args{password: ""}, 30 want: false, 31 }, 32 } 33 for _, tt := range tests { 34 t.Run(tt.name, func(t *testing.T) { 35 if got := IsPasswordConformsPolicy(tt.args.password); got != tt.want { 36 t.Errorf("IsPasswordConformsPolicy() = %v, want %v", got, tt.want) 37 } 38 }) 39 } 40 } 41 42 func TestIsUsernameConformsPolicy(t *testing.T) { 43 type args struct { 44 username string 45 } 46 tests := []struct { 47 name string 48 args args 49 want bool 50 }{ 51 { 52 name: "Login is OK", 53 args: args{username: "tony.tester@example.com"}, 54 want: true, 55 }, 56 { 57 name: "Login is empty", 58 args: args{username: ""}, 59 want: false, 60 }, 61 } 62 for _, tt := range tests { 63 t.Run(tt.name, func(t *testing.T) { 64 if got := IsUsernameConformsPolicy(tt.args.username); got != tt.want { 65 t.Errorf("IsUsernameConformsPolicy() = %v, want %v", got, tt.want) 66 } 67 }) 68 } 69 } 70 71 func TestDecrypt(t *testing.T) { 72 type args struct { 73 cipherdata []byte 74 key string 75 } 76 tests := []struct { 77 name string 78 args args 79 want string 80 wantErr bool 81 }{ 82 { 83 name: "Can encrypt and decrypt and confirm the same result", 84 args: args{ 85 cipherdata: []byte("6tXPNaEV&!xC?3>#"), 86 key: "12345", 87 }, 88 want: "6tXPNaEV&!xC?3>#", 89 wantErr: false, 90 }, 91 } 92 for _, tt := range tests { 93 t.Run(tt.name, func(t *testing.T) { 94 encrypted, err := Encrypt(tt.args.cipherdata, tt.args.key) 95 require.NoError(t, err) 96 97 got, err := Decrypt(encrypted, tt.args.key) 98 if (err != nil) != tt.wantErr { 99 t.Errorf("Decrypt() error = %v, wantErr %v", err, tt.wantErr) 100 return 101 } 102 if !reflect.DeepEqual(string(got), tt.want) { 103 t.Errorf("Decrypt() got = %v, want %v", got, tt.want) 104 } 105 }) 106 } 107 108 // Test wrong AES key kength 109 aesKeyLength = 11 110 _, err := Decrypt([]byte("6tXPNaEV&!xC?3>#"), "12345") 111 require.Error(t, err) 112 } 113 114 func TestGenerateRandomPassword(t *testing.T) { 115 // Seed the random number generator with the current time 116 rand.Seed(time.Now().UnixNano()) 117 118 // Generate a random password 119 password := GenerateRandomPassword() 120 121 // Check if the generated password has the correct length 122 expectedLength := aesKeyLength // Assuming aesKeyLength is defined in the same package 123 if len(password) != expectedLength { 124 t.Errorf("Generated password has incorrect length. Expected %d, but got %d", expectedLength, len(password)) 125 } 126 127 // Check if the generated password only contains valid validRandomChars 128 for _, char := range password { 129 if !strings.ContainsRune(validRandomChars, char) { 130 t.Errorf("Generated password contains invalid character: %s", string(char)) 131 } 132 } 133 } 134 135 func BenchmarkGenerateRandomPassword(b *testing.B) { 136 rand.Seed(time.Now().UnixNano()) 137 for i := 0; i < b.N; i++ { 138 _ = GenerateRandomPassword() 139 } 140 } 141 142 func TestNormalizeAESKey(t *testing.T) { 143 // aesKeyLength is defined in utils.go. Redefining, to simplify debugging 144 aesKeyLength = 16 145 // Test cases for key normalization 146 testCases := []struct { 147 input string 148 expected string 149 }{ 150 {"1234", "1234000000000000"}, // Shorter key, should be padded with zeros 151 {"abcdefghijklmnop", "abcdefghijklmnop"}, // Correct length key, should remain unchanged 152 {"abcdefghijklmnopqrstuvwx", "abcdefghijklmnop"}, // Longer key, should be truncated 153 } 154 155 for _, tc := range testCases { 156 t.Run(tc.input, func(t *testing.T) { 157 normalizedKey := normalizeAESKey(tc.input) 158 159 if normalizedKey != tc.expected { 160 t.Errorf("Expected normalized key: %s, but got: %s", tc.expected, normalizedKey) 161 } 162 }) 163 } 164 }