github.com/SmartMeshFoundation/Spectrum@v0.0.0-20220621030607-452a266fee1e/swarm/api/uri_test.go (about)

     1  // Copyright 2017 The Spectrum Authors
     2  // This file is part of the Spectrum library.
     3  //
     4  // The Spectrum 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 Spectrum 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 Spectrum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package api
    18  
    19  import (
    20  	"reflect"
    21  	"testing"
    22  )
    23  
    24  func TestParseURI(t *testing.T) {
    25  	type test struct {
    26  		uri                       string
    27  		expectURI                 *URI
    28  		expectErr                 bool
    29  		expectRaw                 bool
    30  		expectImmutable           bool
    31  		expectList                bool
    32  		expectHash                bool
    33  		expectDeprecatedRaw       bool
    34  		expectDeprecatedImmutable bool
    35  	}
    36  	tests := []test{
    37  		{
    38  			uri:       "",
    39  			expectErr: true,
    40  		},
    41  		{
    42  			uri:       "foo",
    43  			expectErr: true,
    44  		},
    45  		{
    46  			uri:       "bzz",
    47  			expectErr: true,
    48  		},
    49  		{
    50  			uri:       "bzz:",
    51  			expectURI: &URI{Scheme: "bzz"},
    52  		},
    53  		{
    54  			uri:             "bzz-immutable:",
    55  			expectURI:       &URI{Scheme: "bzz-immutable"},
    56  			expectImmutable: true,
    57  		},
    58  		{
    59  			uri:       "bzz-raw:",
    60  			expectURI: &URI{Scheme: "bzz-raw"},
    61  			expectRaw: true,
    62  		},
    63  		{
    64  			uri:       "bzz:/",
    65  			expectURI: &URI{Scheme: "bzz"},
    66  		},
    67  		{
    68  			uri:       "bzz:/abc123",
    69  			expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
    70  		},
    71  		{
    72  			uri:       "bzz:/abc123/path/to/entry",
    73  			expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
    74  		},
    75  		{
    76  			uri:       "bzz-raw:/",
    77  			expectURI: &URI{Scheme: "bzz-raw"},
    78  			expectRaw: true,
    79  		},
    80  		{
    81  			uri:       "bzz-raw:/abc123",
    82  			expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123"},
    83  			expectRaw: true,
    84  		},
    85  		{
    86  			uri:       "bzz-raw:/abc123/path/to/entry",
    87  			expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123", Path: "path/to/entry"},
    88  			expectRaw: true,
    89  		},
    90  		{
    91  			uri:       "bzz://",
    92  			expectURI: &URI{Scheme: "bzz"},
    93  		},
    94  		{
    95  			uri:       "bzz://abc123",
    96  			expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
    97  		},
    98  		{
    99  			uri:       "bzz://abc123/path/to/entry",
   100  			expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
   101  		},
   102  		{
   103  			uri:        "bzz-hash:",
   104  			expectURI:  &URI{Scheme: "bzz-hash"},
   105  			expectHash: true,
   106  		},
   107  		{
   108  			uri:        "bzz-hash:/",
   109  			expectURI:  &URI{Scheme: "bzz-hash"},
   110  			expectHash: true,
   111  		},
   112  		{
   113  			uri:        "bzz-list:",
   114  			expectURI:  &URI{Scheme: "bzz-list"},
   115  			expectList: true,
   116  		},
   117  		{
   118  			uri:        "bzz-list:/",
   119  			expectURI:  &URI{Scheme: "bzz-list"},
   120  			expectList: true,
   121  		},
   122  		{
   123  			uri:                 "bzzr:",
   124  			expectURI:           &URI{Scheme: "bzzr"},
   125  			expectDeprecatedRaw: true,
   126  		},
   127  		{
   128  			uri:                 "bzzr:/",
   129  			expectURI:           &URI{Scheme: "bzzr"},
   130  			expectDeprecatedRaw: true,
   131  		},
   132  		{
   133  			uri:                       "bzzi:",
   134  			expectURI:                 &URI{Scheme: "bzzi"},
   135  			expectDeprecatedImmutable: true,
   136  		},
   137  		{
   138  			uri:                       "bzzi:/",
   139  			expectURI:                 &URI{Scheme: "bzzi"},
   140  			expectDeprecatedImmutable: true,
   141  		},
   142  	}
   143  	for _, x := range tests {
   144  		actual, err := Parse(x.uri)
   145  		if x.expectErr {
   146  			if err == nil {
   147  				t.Fatalf("expected %s to error", x.uri)
   148  			}
   149  			continue
   150  		}
   151  		if err != nil {
   152  			t.Fatalf("error parsing %s: %s", x.uri, err)
   153  		}
   154  		if !reflect.DeepEqual(actual, x.expectURI) {
   155  			t.Fatalf("expected %s to return %#v, got %#v", x.uri, x.expectURI, actual)
   156  		}
   157  		if actual.Raw() != x.expectRaw {
   158  			t.Fatalf("expected %s raw to be %t, got %t", x.uri, x.expectRaw, actual.Raw())
   159  		}
   160  		if actual.Immutable() != x.expectImmutable {
   161  			t.Fatalf("expected %s immutable to be %t, got %t", x.uri, x.expectImmutable, actual.Immutable())
   162  		}
   163  		if actual.List() != x.expectList {
   164  			t.Fatalf("expected %s list to be %t, got %t", x.uri, x.expectList, actual.List())
   165  		}
   166  		if actual.Hash() != x.expectHash {
   167  			t.Fatalf("expected %s hash to be %t, got %t", x.uri, x.expectHash, actual.Hash())
   168  		}
   169  		if actual.DeprecatedRaw() != x.expectDeprecatedRaw {
   170  			t.Fatalf("expected %s deprecated raw to be %t, got %t", x.uri, x.expectDeprecatedRaw, actual.DeprecatedRaw())
   171  		}
   172  		if actual.DeprecatedImmutable() != x.expectDeprecatedImmutable {
   173  			t.Fatalf("expected %s deprecated immutable to be %t, got %t", x.uri, x.expectDeprecatedImmutable, actual.DeprecatedImmutable())
   174  		}
   175  	}
   176  }