2
0

node_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright © 2022 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package node_test
  16. import (
  17. "context"
  18. "time"
  19. "github.com/ipfs/go-log"
  20. "github.com/libp2p/go-libp2p-core/peer"
  21. . "github.com/onsi/ginkgo/v2"
  22. . "github.com/onsi/gomega"
  23. "github.com/mudler/edgevpn/pkg/blockchain"
  24. "github.com/mudler/edgevpn/pkg/logger"
  25. . "github.com/mudler/edgevpn/pkg/node"
  26. )
  27. var _ = Describe("Node", func() {
  28. // Trigger key rotation on a low frequency to test everything works in between
  29. token := GenerateNewConnectionData(25).Base64()
  30. l := Logger(logger.New(log.LevelFatal))
  31. Context("Configuration", func() {
  32. It("fails if is not valid", func() {
  33. _, err := New(FromBase64(true, true, " "), WithStore(&blockchain.MemoryStore{}), l)
  34. Expect(err).To(HaveOccurred())
  35. _, err = New(FromBase64(true, true, token), WithStore(&blockchain.MemoryStore{}), l)
  36. Expect(err).ToNot(HaveOccurred())
  37. })
  38. })
  39. Context("Connection", func() {
  40. It("see each other node ID", func() {
  41. ctx, cancel := context.WithCancel(context.Background())
  42. defer cancel()
  43. e, _ := New(FromBase64(true, true, token), WithStore(&blockchain.MemoryStore{}), l)
  44. e2, _ := New(FromBase64(true, true, token), WithStore(&blockchain.MemoryStore{}), l)
  45. e.Start(ctx)
  46. e2.Start(ctx)
  47. Eventually(func() []peer.ID {
  48. return e.Host().Network().Peers()
  49. }, 240*time.Second, 1*time.Second).Should(ContainElement(e2.Host().ID()))
  50. })
  51. It("nodes can write to the ledger", func() {
  52. ctx, cancel := context.WithCancel(context.Background())
  53. defer cancel()
  54. e, _ := New(FromBase64(true, true, token), WithStore(&blockchain.MemoryStore{}), WithDiscoveryInterval(10*time.Second), l)
  55. e2, _ := New(FromBase64(true, true, token), WithStore(&blockchain.MemoryStore{}), WithDiscoveryInterval(10*time.Second), l)
  56. e.Start(ctx)
  57. e2.Start(ctx)
  58. l, err := e.Ledger()
  59. Expect(err).ToNot(HaveOccurred())
  60. l2, err := e2.Ledger()
  61. Expect(err).ToNot(HaveOccurred())
  62. l.Announce(ctx, 2*time.Second, func() { l.Add("foo", map[string]interface{}{"bar": "baz"}) })
  63. Eventually(func() string {
  64. var s string
  65. v, exists := l2.GetKey("foo", "bar")
  66. if exists {
  67. v.Unmarshal(&s)
  68. }
  69. return s
  70. }, 240*time.Second, 1*time.Second).Should(Equal("baz"))
  71. })
  72. })
  73. Context("connection gater", func() {
  74. It("blacklists", func() {
  75. ctx, cancel := context.WithCancel(context.Background())
  76. defer cancel()
  77. e, _ := New(
  78. WithBlacklist("1.1.1.1/32", "1.1.1.0/24"),
  79. FromBase64(true, true, token),
  80. WithStore(&blockchain.MemoryStore{}),
  81. l,
  82. )
  83. e.Start(ctx)
  84. addrs := e.ConnectionGater().ListBlockedAddrs()
  85. peers := e.ConnectionGater().ListBlockedPeers()
  86. subs := e.ConnectionGater().ListBlockedSubnets()
  87. Expect(len(addrs)).To(Equal(0))
  88. Expect(len(peers)).To(Equal(0))
  89. Expect(len(subs)).To(Equal(2))
  90. ips := []string{}
  91. for _, s := range subs {
  92. ips = append(ips, s.String())
  93. }
  94. Expect(ips).To(ContainElements("1.1.1.1/32", "1.1.1.0/24"))
  95. })
  96. })
  97. })