aboutsummaryrefslogtreecommitdiff
path: root/weed/iam/helpers_test.go
blob: c9913d28afeedde1f6755f17c19fb92bc588d45b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package iam

import (
	"testing"

	"github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
	"github.com/stretchr/testify/assert"
)

func TestHash(t *testing.T) {
	input := "test"
	result := Hash(&input)
	assert.NotEmpty(t, result)
	assert.Len(t, result, 40) // SHA1 hex is 40 chars

	// Same input should produce same hash
	result2 := Hash(&input)
	assert.Equal(t, result, result2)

	// Different input should produce different hash
	different := "different"
	result3 := Hash(&different)
	assert.NotEqual(t, result, result3)
}

func TestGenerateRandomString(t *testing.T) {
	// Valid generation
	result, err := GenerateRandomString(10, CharsetUpper)
	assert.NoError(t, err)
	assert.Len(t, result, 10)

	// Different calls should produce different results (with high probability)
	result2, err := GenerateRandomString(10, CharsetUpper)
	assert.NoError(t, err)
	assert.NotEqual(t, result, result2)

	// Invalid length
	_, err = GenerateRandomString(0, CharsetUpper)
	assert.Error(t, err)

	_, err = GenerateRandomString(-1, CharsetUpper)
	assert.Error(t, err)

	// Empty charset
	_, err = GenerateRandomString(10, "")
	assert.Error(t, err)
}

func TestGenerateAccessKeyId(t *testing.T) {
	keyId, err := GenerateAccessKeyId()
	assert.NoError(t, err)
	assert.Len(t, keyId, AccessKeyIdLength)
}

func TestGenerateSecretAccessKey(t *testing.T) {
	secretKey, err := GenerateSecretAccessKey()
	assert.NoError(t, err)
	assert.Len(t, secretKey, SecretAccessKeyLength)
}

func TestStringSlicesEqual(t *testing.T) {
	tests := []struct {
		a        []string
		b        []string
		expected bool
	}{
		{[]string{"a", "b", "c"}, []string{"a", "b", "c"}, true},
		{[]string{"c", "b", "a"}, []string{"a", "b", "c"}, true}, // Order independent
		{[]string{"a", "b"}, []string{"a", "b", "c"}, false},
		{[]string{}, []string{}, true},
		{nil, nil, true},
		{[]string{"a"}, []string{"b"}, false},
	}

	for _, test := range tests {
		result := StringSlicesEqual(test.a, test.b)
		assert.Equal(t, test.expected, result)
	}
}

func TestMapToStatementAction(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{StatementActionAdmin, s3_constants.ACTION_ADMIN},
		{StatementActionWrite, s3_constants.ACTION_WRITE},
		{StatementActionRead, s3_constants.ACTION_READ},
		{StatementActionList, s3_constants.ACTION_LIST},
		{StatementActionDelete, s3_constants.ACTION_DELETE_BUCKET},
		{"unknown", ""},
	}

	for _, test := range tests {
		result := MapToStatementAction(test.input)
		assert.Equal(t, test.expected, result)
	}
}

func TestMapToIdentitiesAction(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{s3_constants.ACTION_ADMIN, StatementActionAdmin},
		{s3_constants.ACTION_WRITE, StatementActionWrite},
		{s3_constants.ACTION_READ, StatementActionRead},
		{s3_constants.ACTION_LIST, StatementActionList},
		{s3_constants.ACTION_DELETE_BUCKET, StatementActionDelete},
		{"unknown", ""},
	}

	for _, test := range tests {
		result := MapToIdentitiesAction(test.input)
		assert.Equal(t, test.expected, result)
	}
}

func TestMaskAccessKey(t *testing.T) {
	tests := []struct {
		input    string
		expected string
	}{
		{"AKIAIOSFODNN7EXAMPLE", "AKIA***"},
		{"AKIA", "AKIA"},
		{"AKI", "AKI"},
		{"", ""},
	}

	for _, test := range tests {
		result := MaskAccessKey(test.input)
		assert.Equal(t, test.expected, result)
	}
}