You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

cloak_test.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright (c) 2019 Shivaram Lingamneni
  2. // released under the MIT license
  3. package cloaks
  4. import (
  5. "net"
  6. "reflect"
  7. "testing"
  8. )
  9. func assertEqual(supplied, expected interface{}, t *testing.T) {
  10. if !reflect.DeepEqual(supplied, expected) {
  11. t.Errorf("expected %v but got %v", expected, supplied)
  12. }
  13. }
  14. func easyParseIP(ipstr string) (result net.IP) {
  15. result = net.ParseIP(ipstr)
  16. if result == nil {
  17. panic(ipstr)
  18. }
  19. return
  20. }
  21. func cloakConfForTesting() CloakConfig {
  22. config := CloakConfig{
  23. Enabled: true,
  24. Netname: "oragono",
  25. secret: "_BdVPWB5sray7McbFmeuJL996yaLgG4l9tEyficGXKg",
  26. CidrLenIPv4: 32,
  27. CidrLenIPv6: 64,
  28. NumBits: 80,
  29. }
  30. config.Initialize()
  31. return config
  32. }
  33. func TestCloakDeterminism(t *testing.T) {
  34. config := cloakConfForTesting()
  35. v4ip := easyParseIP("8.8.8.8").To4()
  36. assertEqual(config.ComputeCloak(v4ip), "d2z5guriqhzwazyr.oragono", t)
  37. // use of the 4-in-6 mapping should not affect the cloak
  38. v6mappedIP := v4ip.To16()
  39. assertEqual(config.ComputeCloak(v6mappedIP), "d2z5guriqhzwazyr.oragono", t)
  40. v6ip := easyParseIP("2001:0db8::1")
  41. assertEqual(config.ComputeCloak(v6ip), "w7ren6nxii6f3i3d.oragono", t)
  42. // same CIDR, so same cloak:
  43. v6ipsamecidr := easyParseIP("2001:0db8::2")
  44. assertEqual(config.ComputeCloak(v6ipsamecidr), "w7ren6nxii6f3i3d.oragono", t)
  45. v6ipdifferentcidr := easyParseIP("2001:0db9::1")
  46. // different CIDR, different cloak:
  47. assertEqual(config.ComputeCloak(v6ipdifferentcidr), "ccmptyrjwsxv4f4d.oragono", t)
  48. // cloak values must be sensitive to changes in the secret key
  49. config.SetSecret("HJcXK4lLawxBE4-9SIdPji_21YiL3N5r5f5-SPNrGVY")
  50. assertEqual(config.ComputeCloak(v4ip), "4khy3usk8mfu42pe.oragono", t)
  51. assertEqual(config.ComputeCloak(v6mappedIP), "4khy3usk8mfu42pe.oragono", t)
  52. assertEqual(config.ComputeCloak(v6ip), "mxpk3c83vdxkek9j.oragono", t)
  53. assertEqual(config.ComputeCloak(v6ipsamecidr), "mxpk3c83vdxkek9j.oragono", t)
  54. }
  55. func TestCloakShortv4Cidr(t *testing.T) {
  56. config := CloakConfig{
  57. Enabled: true,
  58. Netname: "oragono",
  59. secret: "_BdVPWB5sray7McbFmeuJL996yaLgG4l9tEyficGXKg",
  60. CidrLenIPv4: 24,
  61. CidrLenIPv6: 64,
  62. NumBits: 60,
  63. }
  64. config.Initialize()
  65. v4ip := easyParseIP("8.8.8.8")
  66. assertEqual(config.ComputeCloak(v4ip), "3cay3zc72tnui.oragono", t)
  67. v4ipsamecidr := easyParseIP("8.8.8.9")
  68. assertEqual(config.ComputeCloak(v4ipsamecidr), "3cay3zc72tnui.oragono", t)
  69. }
  70. func TestCloakZeroBits(t *testing.T) {
  71. config := cloakConfForTesting()
  72. config.NumBits = 0
  73. config.Netname = "example.com"
  74. config.Initialize()
  75. v4ip := easyParseIP("8.8.8.8").To4()
  76. assertEqual(config.ComputeCloak(v4ip), "example.com", t)
  77. }
  78. func TestCloakDisabled(t *testing.T) {
  79. config := cloakConfForTesting()
  80. config.Enabled = false
  81. v4ip := easyParseIP("8.8.8.8").To4()
  82. assertEqual(config.ComputeCloak(v4ip), "", t)
  83. }
  84. func BenchmarkCloaks(b *testing.B) {
  85. config := cloakConfForTesting()
  86. v6ip := easyParseIP("2001:0db8::1")
  87. b.ResetTimer()
  88. for i := 0; i < b.N; i++ {
  89. config.ComputeCloak(v6ip)
  90. }
  91. }
  92. func TestAccountCloak(t *testing.T) {
  93. config := cloakConfForTesting()
  94. // just assert that we get all distinct values
  95. assertEqual(config.ComputeAccountCloak("shivaram"), "8yu8kunudb45ztxm.oragono", t)
  96. assertEqual(config.ComputeAccountCloak("dolph🐬n"), "hhgeqsvzeagv3wjw.oragono", t)
  97. assertEqual(config.ComputeAccountCloak("SHIVARAM"), "bgx32x4r7qzih4uh.oragono", t)
  98. assertEqual(config.ComputeAccountCloak("ed"), "j5autmgxtdjdyzf4.oragono", t)
  99. }
  100. func TestAccountCloakCollisions(t *testing.T) {
  101. config := cloakConfForTesting()
  102. v4ip := easyParseIP("97.97.97.97")
  103. v4cloak := config.ComputeCloak(v4ip)
  104. // "aaaa" is the same bytestring as 97.97.97.97
  105. aaaacloak := config.ComputeAccountCloak("aaaa")
  106. if v4cloak == aaaacloak {
  107. t.Errorf("cloak collision between 97.97.97.97 and aaaa: %s", v4cloak)
  108. }
  109. }
  110. func BenchmarkAccountCloaks(b *testing.B) {
  111. config := cloakConfForTesting()
  112. b.ResetTimer()
  113. for i := 0; i < b.N; i++ {
  114. config.ComputeAccountCloak("shivaram")
  115. }
  116. }