shell_test.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package embeddedShell
  2. import (
  3. "crypto/sha1"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "path"
  8. "strings"
  9. "testing"
  10. "github.com/ipfs/go-ipfs/assets"
  11. "github.com/ipfs/go-ipfs/blocks/key"
  12. "github.com/ipfs/go-ipfs/core"
  13. "github.com/ipfs/go-ipfs/core/mock"
  14. )
  15. type testShell struct {
  16. key *key.Key
  17. mn *core.IpfsNode
  18. s *Shell
  19. }
  20. func newTestShell(t *testing.T) *testShell {
  21. mn, err := coremock.NewMockNode()
  22. if err != nil {
  23. t.Fatalf("coremock.NewMockNode() failed: %s", err)
  24. }
  25. tk, err := assets.SeedInitDocs(mn)
  26. if err != nil {
  27. t.Fatalf("assets.SeedInitDocs() failed: %s", err)
  28. }
  29. return &testShell{
  30. key: tk,
  31. mn: mn,
  32. s: NewShell(mn),
  33. }
  34. }
  35. func TestCat(t *testing.T) {
  36. ts := newTestShell(t)
  37. rc, err := ts.s.Cat(path.Join("/ipfs/", ts.key.String(), "about"))
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. ioutil.ReadAll(rc)
  42. h := sha1.New()
  43. _, err = io.Copy(h, rc)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. got := h.Sum(nil)
  48. want := "da39a3ee5e6b4b0d3255bfef95601890afd80709"
  49. if want != fmt.Sprintf("%x", got) {
  50. t.Errorf("hash comparison failed\nWant: %s\nGot: %x", want, got)
  51. }
  52. }
  53. func TestAdd(t *testing.T) {
  54. ts := newTestShell(t)
  55. h, err := ts.s.Add(strings.NewReader("Hello, World"))
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. if h != "QmTev1ZgJkHgFYiCX7MgELEDJuMygPNGcinqBa2RmfnGFu" {
  60. t.Fatal("wrong hash from add")
  61. }
  62. }