github.com/digdeepmining/go-atheios@v1.5.13-0.20180902133602-d5687a2e6f43/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  	"fmt"
    22  	"io"
    23  	"strings"
    24  	"testing"
    25  
    26  	"github.com/atheioschain/go-atheios/swarm/storage"
    27  )
    28  
    29  func manifest(paths ...string) (manifestReader storage.LazySectionReader) {
    30  	var entries []string
    31  	for _, path := range paths {
    32  		entry := fmt.Sprintf(`{"path":"%s"}`, path)
    33  		entries = append(entries, entry)
    34  	}
    35  	manifest := fmt.Sprintf(`{"entries":[%s]}`, strings.Join(entries, ","))
    36  	return &storage.LazyTestSectionReader{
    37  		SectionReader: io.NewSectionReader(strings.NewReader(manifest), 0, int64(len(manifest))),
    38  	}
    39  }
    40  
    41  func testGetEntry(t *testing.T, path, match string, paths ...string) *manifestTrie {
    42  	quitC := make(chan bool)
    43  	trie, err := readManifest(manifest(paths...), nil, nil, quitC)
    44  	if err != nil {
    45  		t.Errorf("unexpected error making manifest: %v", err)
    46  	}
    47  	checkEntry(t, path, match, trie)
    48  	return trie
    49  }
    50  
    51  func checkEntry(t *testing.T, path, match string, trie *manifestTrie) {
    52  	entry, fullpath := trie.getEntry(path)
    53  	if match == "-" && entry != nil {
    54  		t.Errorf("expected no match for '%s', got '%s'", path, fullpath)
    55  	} else if entry == nil {
    56  		if match != "-" {
    57  			t.Errorf("expected entry '%s' to match '%s', got no match", match, path)
    58  		}
    59  	} else if fullpath != match {
    60  		t.Errorf("incorrect entry retrieved for '%s'. expected path '%v', got '%s'", path, match, fullpath)
    61  	}
    62  }
    63  
    64  func TestGetEntry(t *testing.T) {
    65  	// file system manifest always contains regularized paths
    66  	testGetEntry(t, "a", "a", "a")
    67  	testGetEntry(t, "b", "-", "a")
    68  	testGetEntry(t, "/a//", "a", "a")
    69  	// fallback
    70  	testGetEntry(t, "/a", "", "")
    71  	testGetEntry(t, "/a/b", "a/b", "a/b")
    72  	// longest/deepest math
    73  	testGetEntry(t, "a/b", "-", "a", "a/ba", "a/b/c")
    74  	testGetEntry(t, "a/b", "a/b", "a", "a/b", "a/bb", "a/b/c")
    75  	testGetEntry(t, "//a//b//", "a/b", "a", "a/b", "a/bb", "a/b/c")
    76  }
    77  
    78  func TestDeleteEntry(t *testing.T) {
    79  
    80  }