github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/swarm/api/filesystem_test.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"io/ioutil"
    23  	"os"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	"github.com/PlatONnetwork/PlatON-Go/common"
    28  	"github.com/PlatONnetwork/PlatON-Go/swarm/storage"
    29  )
    30  
    31  var testDownloadDir, _ = ioutil.TempDir(os.TempDir(), "bzz-test")
    32  
    33  func testFileSystem(t *testing.T, f func(*FileSystem, bool)) {
    34  	testAPI(t, func(api *API, toEncrypt bool) {
    35  		f(NewFileSystem(api), toEncrypt)
    36  	})
    37  }
    38  
    39  func readPath(t *testing.T, parts ...string) string {
    40  	file := filepath.Join(parts...)
    41  	content, err := ioutil.ReadFile(file)
    42  
    43  	if err != nil {
    44  		t.Fatalf("unexpected error reading '%v': %v", file, err)
    45  	}
    46  	return string(content)
    47  }
    48  
    49  func TestApiDirUpload0(t *testing.T) {
    50  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
    51  		api := fs.api
    52  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt)
    53  		if err != nil {
    54  			t.Fatalf("unexpected error: %v", err)
    55  		}
    56  		content := readPath(t, "testdata", "test0", "index.html")
    57  		resp := testGet(t, api, bzzhash, "index.html")
    58  		exp := expResponse(content, "text/html; charset=utf-8", 0)
    59  		checkResponse(t, resp, exp)
    60  
    61  		content = readPath(t, "testdata", "test0", "index.css")
    62  		resp = testGet(t, api, bzzhash, "index.css")
    63  		exp = expResponse(content, "text/css", 0)
    64  		checkResponse(t, resp, exp)
    65  
    66  		addr := storage.Address(common.Hex2Bytes(bzzhash))
    67  		_, _, _, _, err = api.Get(context.TODO(), NOOPDecrypt, addr, "")
    68  		if err == nil {
    69  			t.Fatalf("expected error: %v", err)
    70  		}
    71  
    72  		downloadDir := filepath.Join(testDownloadDir, "test0")
    73  		defer os.RemoveAll(downloadDir)
    74  		err = fs.Download(bzzhash, downloadDir)
    75  		if err != nil {
    76  			t.Fatalf("unexpected error: %v", err)
    77  		}
    78  		newbzzhash, err := fs.Upload(downloadDir, "", toEncrypt)
    79  		if err != nil {
    80  			t.Fatalf("unexpected error: %v", err)
    81  		}
    82  		// TODO: currently the hash is not deterministic in the encrypted case
    83  		if !toEncrypt && bzzhash != newbzzhash {
    84  			t.Fatalf("download %v reuploaded has incorrect hash, expected %v, got %v", downloadDir, bzzhash, newbzzhash)
    85  		}
    86  	})
    87  }
    88  
    89  func TestApiDirUploadModify(t *testing.T) {
    90  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
    91  		api := fs.api
    92  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "", toEncrypt)
    93  		if err != nil {
    94  			t.Errorf("unexpected error: %v", err)
    95  			return
    96  		}
    97  
    98  		addr := storage.Address(common.Hex2Bytes(bzzhash))
    99  		addr, err = api.Modify(context.TODO(), addr, "index.html", "", "")
   100  		if err != nil {
   101  			t.Errorf("unexpected error: %v", err)
   102  			return
   103  		}
   104  		index, err := ioutil.ReadFile(filepath.Join("testdata", "test0", "index.html"))
   105  		if err != nil {
   106  			t.Errorf("unexpected error: %v", err)
   107  			return
   108  		}
   109  		ctx := context.TODO()
   110  		hash, wait, err := api.Store(ctx, bytes.NewReader(index), int64(len(index)), toEncrypt)
   111  		if err != nil {
   112  			t.Errorf("unexpected error: %v", err)
   113  			return
   114  		}
   115  		err = wait(ctx)
   116  		if err != nil {
   117  			t.Errorf("unexpected error: %v", err)
   118  			return
   119  		}
   120  		addr, err = api.Modify(context.TODO(), addr, "index2.html", hash.Hex(), "text/html; charset=utf-8")
   121  		if err != nil {
   122  			t.Errorf("unexpected error: %v", err)
   123  			return
   124  		}
   125  		addr, err = api.Modify(context.TODO(), addr, "img/logo.png", hash.Hex(), "text/html; charset=utf-8")
   126  		if err != nil {
   127  			t.Errorf("unexpected error: %v", err)
   128  			return
   129  		}
   130  		bzzhash = addr.Hex()
   131  
   132  		content := readPath(t, "testdata", "test0", "index.html")
   133  		resp := testGet(t, api, bzzhash, "index2.html")
   134  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   135  		checkResponse(t, resp, exp)
   136  
   137  		resp = testGet(t, api, bzzhash, "img/logo.png")
   138  		exp = expResponse(content, "text/html; charset=utf-8", 0)
   139  		checkResponse(t, resp, exp)
   140  
   141  		content = readPath(t, "testdata", "test0", "index.css")
   142  		resp = testGet(t, api, bzzhash, "index.css")
   143  		exp = expResponse(content, "text/css", 0)
   144  		checkResponse(t, resp, exp)
   145  
   146  		_, _, _, _, err = api.Get(context.TODO(), nil, addr, "")
   147  		if err == nil {
   148  			t.Errorf("expected error: %v", err)
   149  		}
   150  	})
   151  }
   152  
   153  func TestApiDirUploadWithRootFile(t *testing.T) {
   154  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
   155  		api := fs.api
   156  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0"), "index.html", toEncrypt)
   157  		if err != nil {
   158  			t.Errorf("unexpected error: %v", err)
   159  			return
   160  		}
   161  
   162  		content := readPath(t, "testdata", "test0", "index.html")
   163  		resp := testGet(t, api, bzzhash, "")
   164  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   165  		checkResponse(t, resp, exp)
   166  	})
   167  }
   168  
   169  func TestApiFileUpload(t *testing.T) {
   170  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
   171  		api := fs.api
   172  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "", toEncrypt)
   173  		if err != nil {
   174  			t.Errorf("unexpected error: %v", err)
   175  			return
   176  		}
   177  
   178  		content := readPath(t, "testdata", "test0", "index.html")
   179  		resp := testGet(t, api, bzzhash, "index.html")
   180  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   181  		checkResponse(t, resp, exp)
   182  	})
   183  }
   184  
   185  func TestApiFileUploadWithRootFile(t *testing.T) {
   186  	testFileSystem(t, func(fs *FileSystem, toEncrypt bool) {
   187  		api := fs.api
   188  		bzzhash, err := fs.Upload(filepath.Join("testdata", "test0", "index.html"), "index.html", toEncrypt)
   189  		if err != nil {
   190  			t.Errorf("unexpected error: %v", err)
   191  			return
   192  		}
   193  
   194  		content := readPath(t, "testdata", "test0", "index.html")
   195  		resp := testGet(t, api, bzzhash, "")
   196  		exp := expResponse(content, "text/html; charset=utf-8", 0)
   197  		checkResponse(t, resp, exp)
   198  	})
   199  }