Skip to content

Commit

Permalink
ParseTokenString Unit Test
Browse files Browse the repository at this point in the history
  • Loading branch information
saithsab877 committed Dec 6, 2024
1 parent 7ff1f2d commit 2aa65f7
Showing 1 changed file with 140 additions and 0 deletions.
140 changes: 140 additions & 0 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -971,3 +971,143 @@ func TestConnectionCodeContext(t *testing.T) {
assert.Equal(t, 500, nextCalled)
})
}

func TestParseTokenString(t *testing.T) {
tests := []struct {
name string
input string
expectedTs uint32
expectedTime []byte
expectedSig []byte
expectedError error
}{
{
name: "Valid Token Without Prefix",
input: base64.URLEncoding.EncodeToString(append([]byte{0, 0, 0, 1}, []byte("sig")...)),
expectedTs: 1,
expectedTime: []byte{0, 0, 0, 1},
expectedSig: []byte("sig"),
expectedError: nil,
},
{
name: "Valid Token With Prefix",
input: "." + base64.URLEncoding.EncodeToString(append([]byte{0, 0, 0, 1}, []byte("sig")...)),
expectedTs: 1,
expectedTime: []byte(base64.URLEncoding.EncodeToString([]byte{0, 0, 0, 1})),
expectedSig: []byte("sig"),
expectedError: nil,
},
{
name: "Minimum Length Token",
input: base64.URLEncoding.EncodeToString(append([]byte{0, 0, 0, 1}, []byte("s")...)),
expectedTs: 1,
expectedTime: []byte{0, 0, 0, 1},
expectedSig: []byte("s"),
expectedError: nil,
},
{
name: "Token Just Below Minimum Length",
input: base64.URLEncoding.EncodeToString([]byte{0, 0, 0, 1}),
expectedTs: 0,
expectedTime: nil,
expectedSig: nil,
expectedError: errors.New("invalid signature (too short)"),
},
{
name: "Invalid Base64 String",
input: "invalid_base64",
expectedTs: 0,
expectedTime: nil,
expectedSig: nil,
expectedError: base64.CorruptInputError(12),
},
{
name: "Empty String",
input: "",
expectedTs: 0,
expectedTime: nil,
expectedSig: nil,
expectedError: errors.New("invalid signature (too short)"),
},
{
name: "Token with Invalid Characters",
input: "!!invalid!!",
expectedTs: 0,
expectedTime: nil,
expectedSig: nil,
expectedError: base64.CorruptInputError(0),
},
{
name: "Large Token",
input: base64.URLEncoding.EncodeToString(append([]byte{0, 0, 0, 1}, make([]byte, 1000)...)),
expectedTs: 1,
expectedTime: []byte{0, 0, 0, 1},
expectedSig: make([]byte, 1000),
expectedError: nil,
},
{
name: "Token with Special Characters",
input: base64.URLEncoding.EncodeToString(append([]byte{0, 0, 0, 1}, []byte("!@#$%^&*()")...)),
expectedTs: 1,
expectedTime: []byte{0, 0, 0, 1},
expectedSig: []byte("!@#$%^&*()"),
expectedError: nil,
},
{
name: "Token with Non-UTF8 Characters",
input: "." + base64.URLEncoding.EncodeToString(append([]byte{0, 0, 0, 1}, []byte{0xff, 0xfe, 0xfd}...)),
expectedTs: 1,
expectedTime: []byte(base64.URLEncoding.EncodeToString([]byte{0, 0, 0, 1})),
expectedSig: []byte{0xff, 0xfe, 0xfd},
expectedError: nil,
},
{
name: "Token with Leading and Trailing Whitespace",
input: " " + base64.URLEncoding.EncodeToString(append([]byte{0, 0, 0, 1}, []byte("sig")...)) + " ",
expectedTs: 1,
expectedTime: []byte{0, 0, 0, 1},
expectedSig: []byte("sig"),
expectedError: nil,
},
{
name: "Token with Mixed Case Sensitivity",
input: base64.URLEncoding.EncodeToString(append([]byte{0, 0, 0, 1}, []byte("SiG")...)),
expectedTs: 1,
expectedTime: []byte{0, 0, 0, 1},
expectedSig: []byte("SiG"),
expectedError: nil,
},
{
name: "Token with Padding Characters",
input: base64.URLEncoding.EncodeToString(append([]byte{0, 0, 0, 1}, []byte("sig")...)),
expectedTs: 1,
expectedTime: []byte{0, 0, 0, 1},
expectedSig: []byte("sig"),
expectedError: nil,
},
{
name: "Token with Embedded Null Bytes",
input: base64.URLEncoding.EncodeToString(append([]byte{0, 0, 0, 1}, []byte{0, 0, 0}...)),
expectedTs: 1,
expectedTime: []byte{0, 0, 0, 1},
expectedSig: []byte{0, 0, 0},
expectedError: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ts, timeBuf, sig, err := ParseTokenString(strings.TrimSpace(tt.input))

assert.Equal(t, tt.expectedTs, ts)
assert.Equal(t, tt.expectedTime, timeBuf)
assert.Equal(t, tt.expectedSig, sig)
if tt.expectedError != nil {
assert.Error(t, err)
assert.Equal(t, tt.expectedError.Error(), err.Error())
} else {
assert.NoError(t, err)
}
})
}
}

0 comments on commit 2aa65f7

Please sign in to comment.