db_test.go 1.8 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, a.Len())
  21. a.Add(db.Record{})
  22. testutil.Equals(t, 1, a.Len())
  23. a.Add(db.Record{})
  24. testutil.Equals(t, 2, a.Len())
  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, a.Len())
  31. a.Add(db.Record{})
  32. testutil.Equals(t, 1, a.Len())
  33. }
  34. func TestDatabaseGetByUAFingerprint(t *testing.T) {
  35. var tests = []struct {
  36. in fp.UAFingerprint
  37. out []int
  38. }{
  39. {fp.UAFingerprint{}, []int(nil)},
  40. {fp.UAFingerprint{BrowserName: 1}, []int{0}},
  41. {fp.UAFingerprint{BrowserName: 2}, []int(nil)},
  42. }
  43. a, _ := db.NewDatabase(bytes.NewReader(nil))
  44. var record db.Record
  45. record.Parse("1:0:0:0:0:0:|::::::|::")
  46. a.Add(record)
  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 []int
  55. }{
  56. {fp.RequestFingerprint{}, []int(nil)},
  57. {fp.RequestFingerprint{Version: fp.VersionTLS12}, []int{0}},
  58. {fp.RequestFingerprint{Version: 2}, []int(nil)},
  59. }
  60. a, _ := db.NewDatabase(bytes.NewReader(nil))
  61. var record db.Record
  62. record.Parse("1:0:0:0:0:0:|303::::::|::")
  63. a.Add(record)
  64. for _, test := range tests {
  65. testutil.Equals(t, test.out, a.GetByRequestFingerprint(test.in))
  66. }
  67. }