github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/swarm/api/manifest_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 12:09:47</date>
    10  //</624342670021496832>
    11  
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  //
    25  //
    26  //
    27  
    28  package api
    29  
    30  import (
    31  	"bytes"
    32  	"encoding/json"
    33  	"fmt"
    34  	"io"
    35  	"net/http"
    36  	"strings"
    37  	"testing"
    38  
    39  	"github.com/ethereum/go-ethereum/swarm/storage"
    40  )
    41  
    42  func manifest(paths ...string) (manifestReader storage.LazySectionReader) {
    43  	var entries []string
    44  	for _, path := range paths {
    45  		entry := fmt.Sprintf(`{"path":"%s"}`, path)
    46  		entries = append(entries, entry)
    47  	}
    48  	manifest := fmt.Sprintf(`{"entries":[%s]}`, strings.Join(entries, ","))
    49  	return &storage.LazyTestSectionReader{
    50  		SectionReader: io.NewSectionReader(strings.NewReader(manifest), 0, int64(len(manifest))),
    51  	}
    52  }
    53  
    54  func testGetEntry(t *testing.T, path, match string, multiple bool, paths ...string) *manifestTrie {
    55  	quitC := make(chan bool)
    56  	fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams())
    57  	ref := make([]byte, fileStore.HashSize())
    58  	trie, err := readManifest(manifest(paths...), ref, fileStore, false, quitC, NOOPDecrypt)
    59  	if err != nil {
    60  		t.Errorf("unexpected error making manifest: %v", err)
    61  	}
    62  	checkEntry(t, path, match, multiple, trie)
    63  	return trie
    64  }
    65  
    66  func checkEntry(t *testing.T, path, match string, multiple bool, trie *manifestTrie) {
    67  	entry, fullpath := trie.getEntry(path)
    68  	if match == "-" && entry != nil {
    69  		t.Errorf("expected no match for '%s', got '%s'", path, fullpath)
    70  	} else if entry == nil {
    71  		if match != "-" {
    72  			t.Errorf("expected entry '%s' to match '%s', got no match", match, path)
    73  		}
    74  	} else if fullpath != match {
    75  		t.Errorf("incorrect entry retrieved for '%s'. expected path '%v', got '%s'", path, match, fullpath)
    76  	}
    77  
    78  	if multiple && entry.Status != http.StatusMultipleChoices {
    79  		t.Errorf("Expected %d Multiple Choices Status for path %s, match %s, got %d", http.StatusMultipleChoices, path, match, entry.Status)
    80  	} else if !multiple && entry != nil && entry.Status == http.StatusMultipleChoices {
    81  		t.Errorf("Were not expecting %d Multiple Choices Status for path %s, match %s, but got it", http.StatusMultipleChoices, path, match)
    82  	}
    83  }
    84  
    85  func TestGetEntry(t *testing.T) {
    86  //
    87  	testGetEntry(t, "a", "a", false, "a")
    88  	testGetEntry(t, "b", "-", false, "a")
    89  testGetEntry(t, "/a//
    90  //
    91  	testGetEntry(t, "/a", "", false, "")
    92  	testGetEntry(t, "/a/b", "a/b", false, "a/b")
    93  //
    94  	testGetEntry(t, "read", "read", true, "readme.md", "readit.md")
    95  	testGetEntry(t, "rf", "-", false, "readme.md", "readit.md")
    96  	testGetEntry(t, "readme", "readme", false, "readme.md")
    97  	testGetEntry(t, "readme", "-", false, "readit.md")
    98  	testGetEntry(t, "readme.md", "readme.md", false, "readme.md")
    99  	testGetEntry(t, "readme.md", "-", false, "readit.md")
   100  	testGetEntry(t, "readmeAmd", "-", false, "readit.md")
   101  	testGetEntry(t, "readme.mdffff", "-", false, "readme.md")
   102  	testGetEntry(t, "ab", "ab", true, "ab/cefg", "ab/cedh", "ab/kkkkkk")
   103  	testGetEntry(t, "ab/ce", "ab/ce", true, "ab/cefg", "ab/cedh", "ab/ceuuuuuuuuuu")
   104  	testGetEntry(t, "abc", "abc", true, "abcd", "abczzzzef", "abc/def", "abc/e/g")
   105  	testGetEntry(t, "a/b", "a/b", true, "a", "a/bc", "a/ba", "a/b/c")
   106  	testGetEntry(t, "a/b", "a/b", false, "a", "a/b", "a/bb", "a/b/c")
   107  testGetEntry(t, "//
   108  }
   109  
   110  func TestExactMatch(t *testing.T) {
   111  	quitC := make(chan bool)
   112  	mf := manifest("shouldBeExactMatch.css", "shouldBeExactMatch.css.map")
   113  	fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams())
   114  	ref := make([]byte, fileStore.HashSize())
   115  	trie, err := readManifest(mf, ref, fileStore, false, quitC, nil)
   116  	if err != nil {
   117  		t.Errorf("unexpected error making manifest: %v", err)
   118  	}
   119  	entry, _ := trie.getEntry("shouldBeExactMatch.css")
   120  	if entry.Path != "" {
   121  		t.Errorf("Expected entry to match %s, got: %s", "shouldBeExactMatch.css", entry.Path)
   122  	}
   123  	if entry.Status == http.StatusMultipleChoices {
   124  		t.Errorf("Got status %d, which is unexepcted", http.StatusMultipleChoices)
   125  	}
   126  }
   127  
   128  func TestDeleteEntry(t *testing.T) {
   129  
   130  }
   131  
   132  //
   133  //
   134  //
   135  func TestAddFileWithManifestPath(t *testing.T) {
   136  //
   137  	manifest, _ := json.Marshal(&Manifest{
   138  		Entries: []ManifestEntry{
   139  			{Path: "ab", Hash: "ab"},
   140  			{Path: "ac", Hash: "ac"},
   141  		},
   142  	})
   143  	reader := &storage.LazyTestSectionReader{
   144  		SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))),
   145  	}
   146  	fileStore := storage.NewFileStore(nil, storage.NewFileStoreParams())
   147  	ref := make([]byte, fileStore.HashSize())
   148  	trie, err := readManifest(reader, ref, fileStore, false, nil, NOOPDecrypt)
   149  	if err != nil {
   150  		t.Fatal(err)
   151  	}
   152  	checkEntry(t, "ab", "ab", false, trie)
   153  	checkEntry(t, "ac", "ac", false, trie)
   154  
   155  //
   156  	entry := &manifestTrieEntry{}
   157  	entry.Path = "a"
   158  	entry.Hash = "a"
   159  	trie.addEntry(entry, nil)
   160  	checkEntry(t, "ab", "ab", false, trie)
   161  	checkEntry(t, "ac", "ac", false, trie)
   162  	checkEntry(t, "a", "a", false, trie)
   163  }
   164  
   165  //
   166  //
   167  //
   168  //
   169  //
   170  func TestReadManifestOverSizeLimit(t *testing.T) {
   171  	manifest := make([]byte, manifestSizeLimit+1)
   172  	reader := &storage.LazyTestSectionReader{
   173  		SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))),
   174  	}
   175  	_, err := readManifest(reader, storage.Address{}, nil, false, nil, NOOPDecrypt)
   176  	if err == nil {
   177  		t.Fatal("got no error from readManifest")
   178  	}
   179  //
   180  //
   181  	got := err.Error()
   182  	want := fmt.Sprintf("Manifest size of %v bytes exceeds the %v byte limit", len(manifest), manifestSizeLimit)
   183  	if got != want {
   184  		t.Fatalf("got error mesage %q, expected %q", got, want)
   185  	}
   186  }
   187