package oauth2provider import ( "crypto/sha256" "encoding/hex" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestHashOAuth2State(t *testing.T) { t.Parallel() t.Run("EmptyState", func(t *testing.T) { t.Parallel() result := hashOAuth2State("") assert.False(t, result.Valid, "empty state should return invalid NullString") assert.Empty(t, result.String, "empty state should return empty string") }) t.Run("NonEmptyState", func(t *testing.T) { t.Parallel() state := "test-state-value" result := hashOAuth2State(state) require.True(t, result.Valid, "non-empty state should return valid NullString") // Verify it's a proper SHA-256 hash. expected := sha256.Sum256([]byte(state)) assert.Equal(t, hex.EncodeToString(expected[:]), result.String, "state hash should be SHA-256 hex digest") }) t.Run("DifferentStatesProduceDifferentHashes", func(t *testing.T) { t.Parallel() hash1 := hashOAuth2State("state-a") hash2 := hashOAuth2State("state-b") require.True(t, hash1.Valid) require.True(t, hash2.Valid) assert.NotEqual(t, hash1.String, hash2.String, "different states should produce different hashes") }) t.Run("SameStateProducesSameHash", func(t *testing.T) { t.Parallel() hash1 := hashOAuth2State("deterministic") hash2 := hashOAuth2State("deterministic") require.True(t, hash1.Valid) assert.Equal(t, hash1.String, hash2.String, "same state should produce identical hash") }) }