selector_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package features
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "testing"
  7. "time"
  8. "github.com/rs/zerolog"
  9. "github.com/stretchr/testify/require"
  10. )
  11. func TestUnmarshalFeaturesRecord(t *testing.T) {
  12. tests := []struct {
  13. record []byte
  14. }{
  15. {
  16. record: []byte(`{"pq":0}`),
  17. },
  18. {
  19. record: []byte(`{"pq":39}`),
  20. },
  21. {
  22. record: []byte(`{"pq":100}`),
  23. },
  24. {
  25. record: []byte(`{}`), // Unmarshal to default struct if key is not present
  26. },
  27. {
  28. record: []byte(`{"kyber":768}`), // Unmarshal to default struct if key is not present
  29. },
  30. }
  31. for _, test := range tests {
  32. var features featuresRecord
  33. err := json.Unmarshal(test.record, &features)
  34. require.NoError(t, err)
  35. require.Equal(t, featuresRecord{}, features)
  36. }
  37. }
  38. func TestStaticFeatures(t *testing.T) {
  39. pqMode := PostQuantumStrict
  40. selector := newTestSelector(t, &pqMode, time.Millisecond*10)
  41. require.Equal(t, PostQuantumStrict, selector.PostQuantumMode())
  42. // No StaticFeatures configured
  43. selector = newTestSelector(t, nil, time.Millisecond*10)
  44. require.Equal(t, PostQuantumPrefer, selector.PostQuantumMode())
  45. }
  46. func newTestSelector(t *testing.T, pqMode *PostQuantumMode, refreshFreq time.Duration) *FeatureSelector {
  47. accountTag := t.Name()
  48. logger := zerolog.Nop()
  49. resolver := &mockResolver{}
  50. staticFeatures := StaticFeatures{
  51. PostQuantumMode: pqMode,
  52. }
  53. selector, err := newFeatureSelector(context.Background(), accountTag, &logger, resolver, staticFeatures, refreshFreq)
  54. require.NoError(t, err)
  55. return selector
  56. }
  57. type mockResolver struct{}
  58. func (mr *mockResolver) lookupRecord(ctx context.Context) ([]byte, error) {
  59. return nil, fmt.Errorf("mockResolver hasn't implement lookupRecord")
  60. }