db_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package db_test
  2. import (
  3. "bytes"
  4. "testing"
  5. "github.com/cloudflare/mitmengine/db"
  6. fp "github.com/cloudflare/mitmengine/fputil"
  7. "github.com/cloudflare/mitmengine/testutil"
  8. )
  9. func TestNewDatabase(t *testing.T) {
  10. _, err := db.NewDatabase(bytes.NewReader(nil))
  11. testutil.Ok(t, err)
  12. }
  13. func TestDatabaseLoad(t *testing.T) {
  14. a, _ := db.NewDatabase(bytes.NewReader(nil))
  15. err := a.Load(bytes.NewReader(nil))
  16. testutil.Ok(t, err)
  17. }
  18. func TestDatabaseAdd(t *testing.T) {
  19. a, _ := db.NewDatabase(bytes.NewReader(nil))
  20. testutil.Equals(t, 0, len(a.RecordMap))
  21. a.Add(db.Record{})
  22. testutil.Equals(t, 1, len(a.RecordMap))
  23. a.Add(db.Record{})
  24. testutil.Equals(t, 2, len(a.RecordMap))
  25. }
  26. func TestDatabaseClear(t *testing.T) {
  27. a, _ := db.NewDatabase(bytes.NewReader(nil))
  28. a.Add(db.Record{})
  29. a.Clear()
  30. testutil.Equals(t, 0, len(a.RecordMap))
  31. a.Add(db.Record{})
  32. testutil.Equals(t, 1, len(a.RecordMap))
  33. }
  34. func TestDatabaseGetByUAFingerprint(t *testing.T) {
  35. var tests = []struct {
  36. in fp.UAFingerprint
  37. out []uint64
  38. }{
  39. {fp.UAFingerprint{}, []uint64(nil)},
  40. {fp.UAFingerprint{BrowserName: 1}, []uint64{0}},
  41. {fp.UAFingerprint{BrowserName: 2}, []uint64(nil)},
  42. }
  43. a, _ := db.NewDatabase(bytes.NewReader(nil))
  44. a.Add(db.Record{UASignature: fp.UASignature{
  45. BrowserName: 1,
  46. }})
  47. for _, test := range tests {
  48. testutil.Equals(t, test.out, a.GetByUAFingerprint(test.in))
  49. }
  50. }
  51. func TestDatabaseGetByRequestFingerprint(t *testing.T) {
  52. var tests = []struct {
  53. in fp.RequestFingerprint
  54. out []uint64
  55. }{
  56. {fp.RequestFingerprint{}, []uint64(nil)},
  57. {fp.RequestFingerprint{Version: 1}, []uint64{0}},
  58. {fp.RequestFingerprint{Version: 2}, []uint64(nil)},
  59. }
  60. a, _ := db.NewDatabase(bytes.NewReader(nil))
  61. a.Add(db.Record{RequestSignature: fp.RequestSignature{
  62. Version: fp.VersionSignature{Expected: 1, Min: 1, Max: 1},
  63. }})
  64. for _, test := range tests {
  65. testutil.Equals(t, test.out, a.GetByRequestFingerprint(test.in))
  66. }
  67. }