ua_test.go 3.4 KB

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