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