ua_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package fp_test
  2. import (
  3. "testing"
  4. fp "github.com/cloudflare/mitmengine/fputil"
  5. "github.com/cloudflare/mitmengine/testutil"
  6. )
  7. var (
  8. anyUAVersionFin = fp.UAVersion{-1, -1, -1}
  9. anyUAVersionSig = fp.UAVersionSignature{Min: anyUAVersionFin, Max: anyUAVersionFin}
  10. )
  11. func TestNewUAFingerprint(t *testing.T) {
  12. var tests = []struct {
  13. in string
  14. out fp.UAFingerprint
  15. }{
  16. {"0:0.0.0:0:0:0.0.0:0:", fp.UAFingerprint{}},
  17. {"0::0:0::0:", fp.UAFingerprint{BrowserVersion: anyUAVersionFin, OSVersion: anyUAVersionFin}},
  18. }
  19. for _, test := range tests {
  20. fingerprint, err := fp.NewUAFingerprint(test.in)
  21. testutil.Ok(t, err)
  22. testutil.Equals(t, test.out, fingerprint)
  23. }
  24. }
  25. func TestUAFingerprintString(t *testing.T) {
  26. var tests = []struct {
  27. in fp.UAFingerprint
  28. out string
  29. }{
  30. {fp.UAFingerprint{}, "0:0.0.0:0:0:0.0.0:0:"},
  31. {fp.UAFingerprint{BrowserVersion: anyUAVersionFin, OSVersion: anyUAVersionFin}, "0::0:0::0:"},
  32. }
  33. for _, test := range tests {
  34. testutil.Equals(t, test.out, test.in.String())
  35. }
  36. }
  37. func TestNewUASignature(t *testing.T) {
  38. var tests = []struct {
  39. in string
  40. out fp.UASignature
  41. }{
  42. {"0:0.0.0:0:0:0.0.0:0:*", fp.UASignature{Quirk: anyStringSig}},
  43. {"0::0:0::0:*", fp.UASignature{BrowserVersion: anyUAVersionSig, OSVersion: anyUAVersionSig, Quirk: anyStringSig}},
  44. {"0:0.0.0:0:0:0.0.0:0:", fp.UASignature{Quirk: emptyStringSig}},
  45. {"0::0:0::0:", fp.UASignature{BrowserVersion: anyUAVersionSig, OSVersion: anyUAVersionSig, Quirk: emptyStringSig}},
  46. }
  47. for _, test := range tests {
  48. uaSignature, err := fp.NewUASignature(test.in)
  49. testutil.Ok(t, err)
  50. testutil.Equals(t, test.out, uaSignature)
  51. }
  52. }
  53. func TestUASignatureString(t *testing.T) {
  54. var tests = []struct {
  55. in fp.UASignature
  56. out string
  57. }{
  58. {fp.UASignature{}, "0:0.0.0:0:0:0.0.0:0:*"},
  59. {fp.UASignature{BrowserVersion: anyUAVersionSig, OSVersion: anyUAVersionSig}, "0::0:0::0:*"},
  60. {fp.UASignature{Quirk: emptyStringSig}, "0:0.0.0:0:0:0.0.0:0:"},
  61. {fp.UASignature{BrowserVersion: anyUAVersionSig, OSVersion: anyUAVersionSig, Quirk: emptyStringSig}, "0::0:0::0:"},
  62. }
  63. for _, test := range tests {
  64. testutil.Equals(t, test.out, test.in.String())
  65. }
  66. }
  67. func TestUASignatureMerge(t *testing.T) {
  68. var tests = []struct {
  69. in1 fp.UASignature
  70. in2 fp.UASignature
  71. out fp.UASignature
  72. }{
  73. {fp.UASignature{}, fp.UASignature{}, fp.UASignature{}},
  74. }
  75. for _, test := range tests {
  76. testutil.Equals(t, test.out, test.in1.Merge(test.in2))
  77. }
  78. }
  79. func TestUAVersionSignatureMerge(t *testing.T) {
  80. var tests = []struct {
  81. in1 fp.UAVersionSignature
  82. in2 fp.UAVersionSignature
  83. out fp.UAVersionSignature
  84. }{
  85. {fp.UAVersionSignature{}, fp.UAVersionSignature{}, fp.UAVersionSignature{}},
  86. {
  87. fp.UAVersionSignature{Min: fp.UAVersion{6, 1, 0}, Max: fp.UAVersion{6, 3, 0}},
  88. fp.UAVersionSignature{Min: fp.UAVersion{10, 0, 0}, Max: fp.UAVersion{10, 0, 0}},
  89. fp.UAVersionSignature{Min: fp.UAVersion{6, 1, 0}, Max: fp.UAVersion{10, 0, 0}},
  90. },
  91. }
  92. for _, test := range tests {
  93. testutil.Equals(t, test.out, test.in1.Merge(test.in2))
  94. }
  95. }
  96. func TestUASignatureMatch(t *testing.T) {
  97. var tests = []struct {
  98. in1 fp.UASignature
  99. in2 fp.UAFingerprint
  100. out fp.Match
  101. }{
  102. {fp.UASignature{}, fp.UAFingerprint{}, fp.MatchPossible},
  103. }
  104. for _, test := range tests {
  105. testutil.Equals(t, test.out, test.in1.Match(test.in2))
  106. }
  107. }
  108. func TestUAVersionSignatureMatch(t *testing.T) {
  109. var tests = []struct {
  110. in1 fp.UAVersionSignature
  111. in2 fp.UAVersion
  112. out fp.Match
  113. }{
  114. {fp.UAVersionSignature{}, fp.UAVersion{}, fp.MatchPossible},
  115. {
  116. fp.UAVersionSignature{Min: fp.UAVersion{6, 1, 0}, Max: fp.UAVersion{10, 0, 0}},
  117. fp.UAVersion{6, 1, 0},
  118. fp.MatchPossible,
  119. },
  120. }
  121. for _, test := range tests {
  122. testutil.Equals(t, test.out, test.in1.Match(test.in2))
  123. }
  124. }