github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/api/filesystem_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package api
    26  
    27  import (
    28  	"bytes"
    29  	"context"
    30  	"io/ioutil"
    31  	"os"
    32  	"path/filepath"
    33  	"testing"
    34  
    35  	"github.com/ethereum/go-ethereum/common"
    36  	"github.com/ethereum/go-ethereum/swarm/storage"
    37  )
    38  
    39  var testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test")
    40  
    41  func testFileSystem(t *testing.T, f func(*FileSystem, bool)) {
    42  	testAPI(t, func(api *API, toEncrypt bool) {
    43  		f(NewFileSystem(api), toEncrypt)
    44  	})
    45  }
    46  
    47  func readPath(t *testing.T, parts ...string) string {
    48  	file := filepath.Join(parts...)
    49  	content, err := ioutil.ReadFile(file)
    50  
    51  	if err != nil {
    52  		t.Fatalf("unexpected error reading '%v': %v", file, err)
    53  	}
    54  	return string(content)
    55  }
    56  
    57  func TestApiDirUpload0(t *testing.T) {
    58  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
    59  		api := fs.api
    60  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt)
    61  		if err != nil {
    62  			t.Fatalf("unexpected error: %v", err)
    63  		}
    64  		content := readPath(t, "testdata", "test0", "index.html")
    65  		resp := testGet(t, api, bzzhash, "index.html")
    66  		exp := expResponse(content, "text/html; charset=utf-8", 0)
    67  		checkResponse(t, resp, exp)
    68  
    69  		content = readPath(t, "testdata", "test0", "index.css")
    70  		resp = testGet(t, api, bzzhash, "index.css")
    71  		exp = expResponse(content, "text/css", 0)
    72  		checkResponse(t, resp, exp)
    73  
    74  		addr := storage.Address(common.Hex2Bytes(bzzhash))
    75  		_, _, _, _, err = api.Get(context.TODO(), NOOPDecrypt, addr, "")
    76  		if err == nil {
    77  			t.Fatalf("expected error: %v", err)
    78  		}
    79  
    80  		downloadDir := filepath.Join(testDownloadDir, "test0")
    81  		defer os.RemoveAll(downloadDir)
    82  		err = fs.Download(bzzhash, downloadDir)
    83  		if err != nil {
    84  			t.Fatalf("unexpected error: %v", err)
    85  		}
    86  		newbzzhash, err := fs.Upload(downloadDir, "", toEncrypt)
    87  		if err != nil {
    88  			t.Fatalf("unexpected error: %v", err)
    89  		}
    90  //
    91  		if !toEncrypt && bzzhash != newbzzhash {
    92  			t.Fatalf("download %v reuploaded has incorrect hash, expected %v, got %v", downloadDir, bzzhash, newbzzhash)
    93  		}
    94  	})
    95  }
    96  
    97  func TestApiDirUploadModify(t *testing.T) {
    98  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
    99  		api := fs.api
   100  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt)
   101  		if err != nil {
   102  			t.Errorf("unexpected error: %v", err)
   103  			return
   104  		}
   105  
   106  		addr := storage.Address(common.Hex2Bytes(bzzhash))
   107  		addr, err = api.Modify(context.TODO(), addr, "index.html", "", "")
   108  		if err != nil {
   109  			t.Errorf("unexpected error: %v", err)
   110  			return
   111  		}
   112  		index, err := ioutil.ReadFile(filepath.Join("testdata", "test0", "index.html"))
   113  		if err != nil {
   114  			t.Errorf("unexpected error: %v", err)
   115  			return
   116  		}
   117  		ctx := context.TODO()
   118  		hash, wait, err := api.Store(ctx, bytes.NewReader(index), int64(len(index)), toEncrypt)
   119  		if err != nil {
   120  			t.Errorf("unexpected error: %v", err)
   121  			return
   122  		}
   123  		err = wait(ctx)
   124  		if err != nil {
   125  			t.Errorf("unexpected error: %v", err)
   126  			return
   127  		}
   128  		addr, err = api.Modify(context.TODO(), addr, "index2.html", hash.Hex(), "text/html; charset=utf-8")
   129  		if err != nil {
   130  			t.Errorf("unexpected error: %v", err)
   131  			return
   132  		}
   133  		addr, err = api.Modify(context.TODO(), addr, "img/logo.png", hash.Hex(), "text/html; charset=utf-8")
   134  		if err != nil {
   135  			t.Errorf("unexpected error: %v", err)
   136  			return
   137  		}
   138  		bzzhash = addr.Hex()
   139  
   140  		content := readPath(t, "testdata", "test0", "index.html")
   141  		resp := testGet(t, api, bzzhash, "index2.html")
   142  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   143  		checkResponse(t, resp, exp)
   144  
   145  		resp = testGet(t, api, bzzhash, "img/logo.png")
   146  		exp = expResponse(content, "text/html; charset=utf-8", 0)
   147  		checkResponse(t, resp, exp)
   148  
   149  		content = readPath(t, "testdata", "test0", "index.css")
   150  		resp = testGet(t, api, bzzhash, "index.css")
   151  		exp = expResponse(content, "text/css", 0)
   152  		checkResponse(t, resp, exp)
   153  
   154  		_, _, _, _, err = api.Get(context.TODO(), nil, addr, "")
   155  		if err == nil {
   156  			t.Errorf("expected error: %v", err)
   157  		}
   158  	})
   159  }
   160  
   161  func TestApiDirUploadWithRootFile(t *testing.T) {
   162  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
   163  		api := fs.api
   164  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html", toEncrypt)
   165  		if err != nil {
   166  			t.Errorf("unexpected error: %v", err)
   167  			return
   168  		}
   169  
   170  		content := readPath(t, "testdata", "test0", "index.html")
   171  		resp := testGet(t, api, bzzhash, "")
   172  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   173  		checkResponse(t, resp, exp)
   174  	})
   175  }
   176  
   177  func TestApiFileUpload(t *testing.T) {
   178  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
   179  		api := fs.api
   180  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "", toEncrypt)
   181  		if err != nil {
   182  			t.Errorf("unexpected error: %v", err)
   183  			return
   184  		}
   185  
   186  		content := readPath(t, "testdata", "test0", "index.html")
   187  		resp := testGet(t, api, bzzhash, "index.html")
   188  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   189  		checkResponse(t, resp, exp)
   190  	})
   191  }
   192  
   193  func TestApiFileUploadWithRootFile(t *testing.T) {
   194  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
   195  		api := fs.api
   196  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html", toEncrypt)
   197  		if err != nil {
   198  			t.Errorf("unexpected error: %v", err)
   199  			return
   200  		}
   201  
   202  		content := readPath(t, "testdata", "test0", "index.html")
   203  		resp := testGet(t, api, bzzhash, "")
   204  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   205  		checkResponse(t, resp, exp)
   206  	})
   207  }