github.com/maynardminer/ethereumprogpow@v1.8.23/swarm/api/uri_test.go (about)

     1  // Copyright 2017 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  	"reflect"
    22  	"testing"
    23  
    24  	"github.com/ethereumprogpow/ethereumprogpow/swarm/storage"
    25  )
    26  
    27  func TestParseURI(t *testing.T) {
    28  	type test struct {
    29  		uri             string
    30  		expectURI       *URI
    31  		expectErr       bool
    32  		expectRaw       bool
    33  		expectImmutable bool
    34  		expectList      bool
    35  		expectHash      bool
    36  		expectValidKey  bool
    37  		expectAddr      storage.Address
    38  	}
    39  	tests := []test{
    40  		{
    41  			uri:       "",
    42  			expectErr: true,
    43  		},
    44  		{
    45  			uri:       "foo",
    46  			expectErr: true,
    47  		},
    48  		{
    49  			uri:       "bzz",
    50  			expectErr: true,
    51  		},
    52  		{
    53  			uri:       "bzz:",
    54  			expectURI: &URI{Scheme: "bzz"},
    55  		},
    56  		{
    57  			uri:             "bzz-immutable:",
    58  			expectURI:       &URI{Scheme: "bzz-immutable"},
    59  			expectImmutable: true,
    60  		},
    61  		{
    62  			uri:       "bzz-raw:",
    63  			expectURI: &URI{Scheme: "bzz-raw"},
    64  			expectRaw: true,
    65  		},
    66  		{
    67  			uri:       "bzz:/",
    68  			expectURI: &URI{Scheme: "bzz"},
    69  		},
    70  		{
    71  			uri:       "bzz:/abc123",
    72  			expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
    73  		},
    74  		{
    75  			uri:       "bzz:/abc123/path/to/entry",
    76  			expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
    77  		},
    78  		{
    79  			uri:       "bzz-raw:/",
    80  			expectURI: &URI{Scheme: "bzz-raw"},
    81  			expectRaw: true,
    82  		},
    83  		{
    84  			uri:       "bzz-raw:/abc123",
    85  			expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123"},
    86  			expectRaw: true,
    87  		},
    88  		{
    89  			uri:       "bzz-raw:/abc123/path/to/entry",
    90  			expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123", Path: "path/to/entry"},
    91  			expectRaw: true,
    92  		},
    93  		{
    94  			uri:       "bzz://",
    95  			expectURI: &URI{Scheme: "bzz"},
    96  		},
    97  		{
    98  			uri:       "bzz://abc123",
    99  			expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
   100  		},
   101  		{
   102  			uri:       "bzz://abc123/path/to/entry",
   103  			expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
   104  		},
   105  		{
   106  			uri:        "bzz-hash:",
   107  			expectURI:  &URI{Scheme: "bzz-hash"},
   108  			expectHash: true,
   109  		},
   110  		{
   111  			uri:        "bzz-hash:/",
   112  			expectURI:  &URI{Scheme: "bzz-hash"},
   113  			expectHash: true,
   114  		},
   115  		{
   116  			uri:        "bzz-list:",
   117  			expectURI:  &URI{Scheme: "bzz-list"},
   118  			expectList: true,
   119  		},
   120  		{
   121  			uri:        "bzz-list:/",
   122  			expectURI:  &URI{Scheme: "bzz-list"},
   123  			expectList: true,
   124  		},
   125  		{
   126  			uri: "bzz-raw://4378d19c26590f1a818ed7d6a62c3809e149b0999cab5ce5f26233b3b423bf8c",
   127  			expectURI: &URI{Scheme: "bzz-raw",
   128  				Addr: "4378d19c26590f1a818ed7d6a62c3809e149b0999cab5ce5f26233b3b423bf8c",
   129  			},
   130  			expectValidKey: true,
   131  			expectRaw:      true,
   132  			expectAddr: storage.Address{67, 120, 209, 156, 38, 89, 15, 26,
   133  				129, 142, 215, 214, 166, 44, 56, 9,
   134  				225, 73, 176, 153, 156, 171, 92, 229,
   135  				242, 98, 51, 179, 180, 35, 191, 140,
   136  			},
   137  		},
   138  	}
   139  	for _, x := range tests {
   140  		actual, err := Parse(x.uri)
   141  		if x.expectErr {
   142  			if err == nil {
   143  				t.Fatalf("expected %s to error", x.uri)
   144  			}
   145  			continue
   146  		}
   147  		if err != nil {
   148  			t.Fatalf("error parsing %s: %s", x.uri, err)
   149  		}
   150  		if !reflect.DeepEqual(actual, x.expectURI) {
   151  			t.Fatalf("expected %s to return %#v, got %#v", x.uri, x.expectURI, actual)
   152  		}
   153  		if actual.Raw() != x.expectRaw {
   154  			t.Fatalf("expected %s raw to be %t, got %t", x.uri, x.expectRaw, actual.Raw())
   155  		}
   156  		if actual.Immutable() != x.expectImmutable {
   157  			t.Fatalf("expected %s immutable to be %t, got %t", x.uri, x.expectImmutable, actual.Immutable())
   158  		}
   159  		if actual.List() != x.expectList {
   160  			t.Fatalf("expected %s list to be %t, got %t", x.uri, x.expectList, actual.List())
   161  		}
   162  		if actual.Hash() != x.expectHash {
   163  			t.Fatalf("expected %s hash to be %t, got %t", x.uri, x.expectHash, actual.Hash())
   164  		}
   165  		if x.expectValidKey {
   166  			if actual.Address() == nil {
   167  				t.Fatalf("expected %s to return a valid key, got nil", x.uri)
   168  			} else {
   169  				if !bytes.Equal(x.expectAddr, actual.Address()) {
   170  					t.Fatalf("expected %s to be decoded to %v", x.expectURI.Addr, x.expectAddr)
   171  				}
   172  			}
   173  		}
   174  	}
   175  }