github.com/linapex/ethereum-go-chinese@v0.0.0-20190316121929-f8b7a73c3fa1/swarm/api/filesystem_test.go (about)

     1  
     2  //<developer>
     3  //    <name>linapex 曹一峰</name>
     4  //    <email>linapex@163.com</email>
     5  //    <wx>superexc</wx>
     6  //    <qqgroup>128148617</qqgroup>
     7  //    <url>https://jsq.ink</url>
     8  //    <role>pku engineer</role>
     9  //    <date>2019-03-16 19:16:43</date>
    10  //</624450111623139328>
    11  
    12  
    13  package api
    14  
    15  import (
    16  	"bytes"
    17  	"context"
    18  	"io/ioutil"
    19  	"os"
    20  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/swarm/storage"
    25  )
    26  
    27  var testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test")
    28  
    29  func testFileSystem(t *testing.T, f func(*FileSystem, bool)) {
    30  	testAPI(t, func(api *API, toEncrypt bool) {
    31  		f(NewFileSystem(api), toEncrypt)
    32  	})
    33  }
    34  
    35  func readPath(t *testing.T, parts ...string) string {
    36  	file := filepath.Join(parts...)
    37  	content, err := ioutil.ReadFile(file)
    38  
    39  	if err != nil {
    40  		t.Fatalf("unexpected error reading '%v': %v", file, err)
    41  	}
    42  	return string(content)
    43  }
    44  
    45  func TestApiDirUpload0(t *testing.T) {
    46  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
    47  		api := fs.api
    48  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt)
    49  		if err != nil {
    50  			t.Fatalf("unexpected error: %v", err)
    51  		}
    52  		content := readPath(t, "testdata", "test0", "index.html")
    53  		resp := testGet(t, api, bzzhash, "index.html")
    54  		exp := expResponse(content, "text/html; charset=utf-8", 0)
    55  		checkResponse(t, resp, exp)
    56  
    57  		content = readPath(t, "testdata", "test0", "index.css")
    58  		resp = testGet(t, api, bzzhash, "index.css")
    59  		exp = expResponse(content, "text/css; charset=utf-8", 0)
    60  		checkResponse(t, resp, exp)
    61  
    62  		addr := storage.Address(common.Hex2Bytes(bzzhash))
    63  		_, _, _, _, err = api.Get(context.TODO(), NOOPDecrypt, addr, "")
    64  		if err == nil {
    65  			t.Fatalf("expected error: %v", err)
    66  		}
    67  
    68  		downloadDir := filepath.Join(testDownloadDir, "test0")
    69  		defer os.RemoveAll(downloadDir)
    70  		err = fs.Download(bzzhash, downloadDir)
    71  		if err != nil {
    72  			t.Fatalf("unexpected error: %v", err)
    73  		}
    74  		newbzzhash, err := fs.Upload(downloadDir, "", toEncrypt)
    75  		if err != nil {
    76  			t.Fatalf("unexpected error: %v", err)
    77  		}
    78  //TODO:当前哈希在加密的情况下不具有确定性
    79  		if !toEncrypt && bzzhash != newbzzhash {
    80  			t.Fatalf("download %v reuploaded has incorrect hash, expected %v, got %v", downloadDir, bzzhash, newbzzhash)
    81  		}
    82  	})
    83  }
    84  
    85  func TestApiDirUploadModify(t *testing.T) {
    86  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
    87  		api := fs.api
    88  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt)
    89  		if err != nil {
    90  			t.Errorf("unexpected error: %v", err)
    91  			return
    92  		}
    93  
    94  		addr := storage.Address(common.Hex2Bytes(bzzhash))
    95  		addr, err = api.Modify(context.TODO(), addr, "index.html", "", "")
    96  		if err != nil {
    97  			t.Errorf("unexpected error: %v", err)
    98  			return
    99  		}
   100  		index, err := ioutil.ReadFile(filepath.Join("testdata", "test0", "index.html"))
   101  		if err != nil {
   102  			t.Errorf("unexpected error: %v", err)
   103  			return
   104  		}
   105  		ctx := context.TODO()
   106  		hash, wait, err := api.Store(ctx, bytes.NewReader(index), int64(len(index)), toEncrypt)
   107  		if err != nil {
   108  			t.Errorf("unexpected error: %v", err)
   109  			return
   110  		}
   111  		err = wait(ctx)
   112  		if err != nil {
   113  			t.Errorf("unexpected error: %v", err)
   114  			return
   115  		}
   116  		addr, err = api.Modify(context.TODO(), addr, "index2.html", hash.Hex(), "text/html; charset=utf-8")
   117  		if err != nil {
   118  			t.Errorf("unexpected error: %v", err)
   119  			return
   120  		}
   121  		addr, err = api.Modify(context.TODO(), addr, "img/logo.png", hash.Hex(), "text/html; charset=utf-8")
   122  		if err != nil {
   123  			t.Errorf("unexpected error: %v", err)
   124  			return
   125  		}
   126  		bzzhash = addr.Hex()
   127  
   128  		content := readPath(t, "testdata", "test0", "index.html")
   129  		resp := testGet(t, api, bzzhash, "index2.html")
   130  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   131  		checkResponse(t, resp, exp)
   132  
   133  		resp = testGet(t, api, bzzhash, "img/logo.png")
   134  		exp = expResponse(content, "text/html; charset=utf-8", 0)
   135  		checkResponse(t, resp, exp)
   136  
   137  		content = readPath(t, "testdata", "test0", "index.css")
   138  		resp = testGet(t, api, bzzhash, "index.css")
   139  		exp = expResponse(content, "text/css; charset=utf-8", 0)
   140  		checkResponse(t, resp, exp)
   141  
   142  		_, _, _, _, err = api.Get(context.TODO(), nil, addr, "")
   143  		if err == nil {
   144  			t.Errorf("expected error: %v", err)
   145  		}
   146  	})
   147  }
   148  
   149  func TestApiDirUploadWithRootFile(t *testing.T) {
   150  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
   151  		api := fs.api
   152  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html", toEncrypt)
   153  		if err != nil {
   154  			t.Errorf("unexpected error: %v", err)
   155  			return
   156  		}
   157  
   158  		content := readPath(t, "testdata", "test0", "index.html")
   159  		resp := testGet(t, api, bzzhash, "")
   160  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   161  		checkResponse(t, resp, exp)
   162  	})
   163  }
   164  
   165  func TestApiFileUpload(t *testing.T) {
   166  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
   167  		api := fs.api
   168  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "", toEncrypt)
   169  		if err != nil {
   170  			t.Errorf("unexpected error: %v", err)
   171  			return
   172  		}
   173  
   174  		content := readPath(t, "testdata", "test0", "index.html")
   175  		resp := testGet(t, api, bzzhash, "index.html")
   176  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   177  		checkResponse(t, resp, exp)
   178  	})
   179  }
   180  
   181  func TestApiFileUploadWithRootFile(t *testing.T) {
   182  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
   183  		api := fs.api
   184  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html", toEncrypt)
   185  		if err != nil {
   186  			t.Errorf("unexpected error: %v", err)
   187  			return
   188  		}
   189  
   190  		content := readPath(t, "testdata", "test0", "index.html")
   191  		resp := testGet(t, api, bzzhash, "")
   192  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   193  		checkResponse(t, resp, exp)
   194  	})
   195  }
   196