files_test.go 2.4 KB

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