github.com/janotchain/janota@v0.0.0-20220824112012-93ea4c5dee78/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  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"io"
    24  	"net/http"
    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, multiple bool, 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, multiple, trie)
    50  	return trie
    51  }
    52  
    53  func checkEntry(t *testing.T, path, match string, multiple bool, 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  	if multiple && entry.Status != http.StatusMultipleChoices {
    66  		t.Errorf("Expected %d Multiple Choices Status for path %s, match %s, got %d", http.StatusMultipleChoices, path, match, entry.Status)
    67  	} else if !multiple && entry != nil && entry.Status == http.StatusMultipleChoices {
    68  		t.Errorf("Were not expecting %d Multiple Choices Status for path %s, match %s, but got it", http.StatusMultipleChoices, path, match)
    69  	}
    70  }
    71  
    72  func TestGetEntry(t *testing.T) {
    73  	// file system manifest always contains regularized paths
    74  	testGetEntry(t, "a", "a", false, "a")
    75  	testGetEntry(t, "b", "-", false, "a")
    76  	testGetEntry(t, "/a//", "a", false, "a")
    77  	// fallback
    78  	testGetEntry(t, "/a", "", false, "")
    79  	testGetEntry(t, "/a/b", "a/b", false, "a/b")
    80  	// longest/deepest math
    81  	testGetEntry(t, "read", "read", true, "readme.md", "readit.md")
    82  	testGetEntry(t, "rf", "-", false, "readme.md", "readit.md")
    83  	testGetEntry(t, "readme", "readme", false, "readme.md")
    84  	testGetEntry(t, "readme", "-", false, "readit.md")
    85  	testGetEntry(t, "readme.md", "readme.md", false, "readme.md")
    86  	testGetEntry(t, "readme.md", "-", false, "readit.md")
    87  	testGetEntry(t, "readmeAmd", "-", false, "readit.md")
    88  	testGetEntry(t, "readme.mdffff", "-", false, "readme.md")
    89  	testGetEntry(t, "ab", "ab", true, "ab/cefg", "ab/cedh", "ab/kkkkkk")
    90  	testGetEntry(t, "ab/ce", "ab/ce", true, "ab/cefg", "ab/cedh", "ab/ceuuuuuuuuuu")
    91  	testGetEntry(t, "abc", "abc", true, "abcd", "abczzzzef", "abc/def", "abc/e/g")
    92  	testGetEntry(t, "a/b", "a/b", true, "a", "a/bc", "a/ba", "a/b/c")
    93  	testGetEntry(t, "a/b", "a/b", false, "a", "a/b", "a/bb", "a/b/c")
    94  	testGetEntry(t, "//a//b//", "a/b", false, "a", "a/b", "a/bb", "a/b/c")
    95  }
    96  
    97  func TestExactMatch(t *testing.T) {
    98  	quitC := make(chan bool)
    99  	mf := manifest("shouldBeExactMatch.css", "shouldBeExactMatch.css.map")
   100  	trie, err := readManifest(mf, nil, nil, quitC)
   101  	if err != nil {
   102  		t.Errorf("unexpected error making manifest: %v", err)
   103  	}
   104  	entry, _ := trie.getEntry("shouldBeExactMatch.css")
   105  	if entry.Path != "" {
   106  		t.Errorf("Expected entry to match %s, got: %s", "shouldBeExactMatch.css", entry.Path)
   107  	}
   108  	if entry.Status == http.StatusMultipleChoices {
   109  		t.Errorf("Got status %d, which is unexepcted", http.StatusMultipleChoices)
   110  	}
   111  }
   112  
   113  func TestDeleteEntry(t *testing.T) {
   114  
   115  }
   116  
   117  // TestAddFileWithManifestPath tests that adding an entry at a path which
   118  // already exists as a manifest just adds the entry to the manifest rather
   119  // than replacing the manifest with the entry
   120  func TestAddFileWithManifestPath(t *testing.T) {
   121  	// create a manifest containing "ab" and "ac"
   122  	manifest, _ := json.Marshal(&Manifest{
   123  		Entries: []ManifestEntry{
   124  			{Path: "ab", Hash: "ab"},
   125  			{Path: "ac", Hash: "ac"},
   126  		},
   127  	})
   128  	reader := &storage.LazyTestSectionReader{
   129  		SectionReader: io.NewSectionReader(bytes.NewReader(manifest), 0, int64(len(manifest))),
   130  	}
   131  	trie, err := readManifest(reader, nil, nil, nil)
   132  	if err != nil {
   133  		t.Fatal(err)
   134  	}
   135  	checkEntry(t, "ab", "ab", false, trie)
   136  	checkEntry(t, "ac", "ac", false, trie)
   137  
   138  	// now add path "a" and check we can still get "ab" and "ac"
   139  	entry := &manifestTrieEntry{}
   140  	entry.Path = "a"
   141  	entry.Hash = "a"
   142  	trie.addEntry(entry, nil)
   143  	checkEntry(t, "ab", "ab", false, trie)
   144  	checkEntry(t, "ac", "ac", false, trie)
   145  	checkEntry(t, "a", "a", false, trie)
   146  }