files_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 services_test
  16. import (
  17. "context"
  18. "fmt"
  19. "io/ioutil"
  20. "os"
  21. "time"
  22. "github.com/ipfs/go-log"
  23. . "github.com/onsi/ginkgo/v2"
  24. . "github.com/onsi/gomega"
  25. "github.com/mudler/edgevpn/pkg/blockchain"
  26. "github.com/mudler/edgevpn/pkg/logger"
  27. node "github.com/mudler/edgevpn/pkg/node"
  28. . "github.com/mudler/edgevpn/pkg/services"
  29. )
  30. var _ = Describe("File services", func() {
  31. token := node.GenerateNewConnectionData(25).Base64()
  32. logg := logger.New(log.LevelError)
  33. l := node.Logger(logg)
  34. e2, _ := node.New(
  35. node.WithDiscoveryInterval(10*time.Second),
  36. node.WithNetworkService(AliveNetworkService(2*time.Second, 4*time.Second, 15*time.Minute)),
  37. node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
  38. Context("File sharing", func() {
  39. It("sends and receive files between two nodes", func() {
  40. ctx, cancel := context.WithCancel(context.Background())
  41. defer cancel()
  42. fileUUID := "test"
  43. f, err := ioutil.TempFile("", "test")
  44. Expect(err).ToNot(HaveOccurred())
  45. defer os.RemoveAll(f.Name())
  46. ioutil.WriteFile(f.Name(), []byte("testfile"), os.ModePerm)
  47. // First node expose a file
  48. opts, err := ShareFile(logg, 10*time.Second, fileUUID, f.Name())
  49. Expect(err).ToNot(HaveOccurred())
  50. opts = append(opts, node.FromBase64(true, true, token), node.WithStore(&blockchain.MemoryStore{}), l)
  51. e, _ := node.New(opts...)
  52. e.Start(ctx)
  53. e2.Start(ctx)
  54. Eventually(func() string {
  55. ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
  56. defer cancel()
  57. f, err := ioutil.TempFile("", "test")
  58. Expect(err).ToNot(HaveOccurred())
  59. defer os.RemoveAll(f.Name())
  60. ll, _ := e2.Ledger()
  61. ll1, _ := e.Ledger()
  62. By(fmt.Sprint(ll.CurrentData(), ll.LastBlock().Index, ll1.CurrentData()))
  63. ReceiveFile(ctx, ll, e2, logg, 2*time.Second, fileUUID, f.Name())
  64. b, _ := ioutil.ReadFile(f.Name())
  65. return string(b)
  66. }, 190*time.Second, 1*time.Second).Should(Equal("testfile"))
  67. })
  68. })
  69. })