github.com/alanchchen/go-ethereum@v1.6.6-0.20170601190819-6171d01b1195/swarm/api/manifest_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  	// "encoding/json"
    21  	"bytes"
    22  	"encoding/json"
    23  	"fmt"
    24  	"io"
    25  	"strings"
    26  	"testing"
    27  
    28  	"github.com/ethereum/go-ethereum/swarm/storage"
    29  )
    30  
    31  func manifest(paths ...string) (manifestReader storage.LazySectionReader) {
    32  	var entries []string
    33  	for _, path := range paths {
    34  		entry := fmt.Sprintf(`{"path":"%s"}`, path)
    35  		entries = append(entries, entry)
    36  	}
    37  	manifest := fmt.Sprintf(`{"entries":[%s]}`, strings.Join(entries, ","))
    38  	return &storage.LazyTestSectionReader{
    39  		SectionReader: io.NewSectionReader(strings.NewReader(manifest), 0, int64(len(manifest))),
    40  	}
    41  }
    42  
    43  func testGetEntry(t *testing.T, path, match string, paths ...string) *manifestTrie {
    44  	quitC := make(chan bool)
    45  	trie, err := readManifest(manifest(paths...), nil, nil, quitC)
    46  	if err != nil {
    47  		t.Errorf("unexpected error making manifest: %v", err)
    48  	}
    49  	checkEntry(t, path, match, trie)
    50  	return trie
    51  }
    52  
    53  func checkEntry(t *testing.T, path, match string, trie *manifestTrie) {
    54  	entry, fullpath := trie.getEntry(path)
    55  	if match == "-" && entry != nil {
    56  		t.Errorf("expected no match for '%s', got '%s'", path, fullpath)
    57  	} else if entry == nil {
    58  		if match != "-" {
    59  			t.Errorf("expected entry '%s' to match '%s', got no match", match, path)
    60  		}
    61  	} else if fullpath != match {
    62  		t.Errorf("incorrect entry retrieved for '%s'. expected path '%v', got '%s'", path, match, fullpath)
    63  	}
    64  }
    65  
    66  func TestGetEntry(t *testing.T) {
    67  	// file system manifest always contains regularized paths
    68  	testGetEntry(t, "a", "a", "a")
    69  	testGetEntry(t, "b", "-", "a")
    70  	testGetEntry(t, "/a//", "a", "a")
    71  	// fallback
    72  	testGetEntry(t, "/a", "", "")
    73  	testGetEntry(t, "/a/b", "a/b", "a/b")
    74  	// longest/deepest math
    75  	testGetEntry(t, "a/b", "-", "a", "a/ba", "a/b/c")
    76  	testGetEntry(t, "a/b", "a/b", "a", "a/b", "a/bb", "a/b/c")
    77  	testGetEntry(t, "//a//b//", "a/b", "a", "a/b", "a/bb", "a/b/c")
    78  }
    79  
    80  func TestDeleteEntry(t *testing.T) {
    81  
    82  }
    83  
    84  // TestAddFileWithManifestPath tests that adding an entry at a path which
    85  // already exists as a manifest just adds the entry to the manifest rather
    86  // than replacing the manifest with the entry
    87  func TestAddFileWithManifestPath(t *testing.T) {
    88  	// create a manifest containing "ab" and "ac"
    89  	manifest, _ := json.Marshal(&Manifest{
    90  		Entries: []ManifestEntry{
    91  			{Path: "ab", Hash: "ab"},
    92  			{Path: "ac", Hash: "ac"},
    93  		},
    94  	})
    95  	reader := &storage.LazyTestSectionReader{
    96  		SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))),
    97  	}
    98  	trie, err := readManifest(reader, nil, nil, nil)
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	checkEntry(t, "ab", "ab", trie)
   103  	checkEntry(t, "ac", "ac", trie)
   104  
   105  	// now add path "a" and check we can still get "ab" and "ac"
   106  	entry := &manifestTrieEntry{}
   107  	entry.Path = "a"
   108  	entry.Hash = "a"
   109  	trie.addEntry(entry, nil)
   110  	checkEntry(t, "ab", "ab", trie)
   111  	checkEntry(t, "ac", "ac", trie)
   112  	checkEntry(t, "a", "a", trie)
   113  }