github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/swarm/api/uri_test.go (about)

     1  
     2  //此源码被清华学神尹成大魔王专业翻译分析并修改
     3  //尹成QQ77025077
     4  //尹成微信18510341407
     5  //尹成所在QQ群721929980
     6  //尹成邮箱 yinc13@mails.tsinghua.edu.cn
     7  //尹成毕业于清华大学,微软区块链领域全球最有价值专家
     8  //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620
     9  //
    10  //
    11  //
    12  //
    13  //
    14  //
    15  //
    16  //
    17  //
    18  //
    19  //
    20  //
    21  //
    22  //
    23  //
    24  
    25  package api
    26  
    27  import (
    28  	"bytes"
    29  	"reflect"
    30  	"testing"
    31  
    32  	"github.com/ethereum/go-ethereum/swarm/storage"
    33  )
    34  
    35  func TestParseURI(t *testing.T) {
    36  	type test struct {
    37  		uri                       string
    38  		expectURI                 *URI
    39  		expectErr                 bool
    40  		expectRaw                 bool
    41  		expectImmutable           bool
    42  		expectList                bool
    43  		expectHash                bool
    44  		expectDeprecatedRaw       bool
    45  		expectDeprecatedImmutable bool
    46  		expectValidKey            bool
    47  		expectAddr                storage.Address
    48  	}
    49  	tests := []test{
    50  		{
    51  			uri:       "",
    52  			expectErr: true,
    53  		},
    54  		{
    55  			uri:       "foo",
    56  			expectErr: true,
    57  		},
    58  		{
    59  			uri:       "bzz",
    60  			expectErr: true,
    61  		},
    62  		{
    63  			uri:       "bzz:",
    64  			expectURI: &URI{Scheme: "bzz"},
    65  		},
    66  		{
    67  			uri:             "bzz-immutable:",
    68  			expectURI:       &URI{Scheme: "bzz-immutable"},
    69  			expectImmutable: true,
    70  		},
    71  		{
    72  			uri:       "bzz-raw:",
    73  			expectURI: &URI{Scheme: "bzz-raw"},
    74  			expectRaw: true,
    75  		},
    76  		{
    77  			uri:       "bzz:/",
    78  			expectURI: &URI{Scheme: "bzz"},
    79  		},
    80  		{
    81  			uri:       "bzz:/abc123",
    82  			expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
    83  		},
    84  		{
    85  			uri:       "bzz:/abc123/path/to/entry",
    86  			expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
    87  		},
    88  		{
    89  			uri:       "bzz-raw:/",
    90  			expectURI: &URI{Scheme: "bzz-raw"},
    91  			expectRaw: true,
    92  		},
    93  		{
    94  			uri:       "bzz-raw:/abc123",
    95  			expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123"},
    96  			expectRaw: true,
    97  		},
    98  		{
    99  			uri:       "bzz-raw:/abc123/path/to/entry",
   100  			expectURI: &URI{Scheme: "bzz-raw", Addr: "abc123", Path: "path/to/entry"},
   101  			expectRaw: true,
   102  		},
   103  		{
   104  uri:       "bzz://
   105  			expectURI: &URI{Scheme: "bzz"},
   106  		},
   107  		{
   108  uri:       "bzz://
   109  			expectURI: &URI{Scheme: "bzz", Addr: "abc123"},
   110  		},
   111  		{
   112  uri:       "bzz://
   113  			expectURI: &URI{Scheme: "bzz", Addr: "abc123", Path: "path/to/entry"},
   114  		},
   115  		{
   116  			uri:        "bzz-hash:",
   117  			expectURI:  &URI{Scheme: "bzz-hash"},
   118  			expectHash: true,
   119  		},
   120  		{
   121  			uri:        "bzz-hash:/",
   122  			expectURI:  &URI{Scheme: "bzz-hash"},
   123  			expectHash: true,
   124  		},
   125  		{
   126  			uri:        "bzz-list:",
   127  			expectURI:  &URI{Scheme: "bzz-list"},
   128  			expectList: true,
   129  		},
   130  		{
   131  			uri:        "bzz-list:/",
   132  			expectURI:  &URI{Scheme: "bzz-list"},
   133  			expectList: true,
   134  		},
   135  		{
   136  uri: "bzz-raw://
   137  			expectURI: &URI{Scheme: "bzz-raw",
   138  				Addr: "4378d19c26590f1a818ed7d6a62c3809e149b0999cab5ce5f26233b3b423bf8c",
   139  			},
   140  			expectValidKey: true,
   141  			expectRaw:      true,
   142  			expectAddr: storage.Address{67, 120, 209, 156, 38, 89, 15, 26,
   143  				129, 142, 215, 214, 166, 44, 56, 9,
   144  				225, 73, 176, 153, 156, 171, 92, 229,
   145  				242, 98, 51, 179, 180, 35, 191, 140,
   146  			},
   147  		},
   148  	}
   149  	for _, x := range tests {
   150  		actual, err := Parse(x.uri)
   151  		if x.expectErr {
   152  			if err == nil {
   153  				t.Fatalf("expected %s to error", x.uri)
   154  			}
   155  			continue
   156  		}
   157  		if err != nil {
   158  			t.Fatalf("error parsing %s: %s", x.uri, err)
   159  		}
   160  		if !reflect.DeepEqual(actual, x.expectURI) {
   161  			t.Fatalf("expected %s to return %#v, got %#v", x.uri, x.expectURI, actual)
   162  		}
   163  		if actual.Raw() != x.expectRaw {
   164  			t.Fatalf("expected %s raw to be %t, got %t", x.uri, x.expectRaw, actual.Raw())
   165  		}
   166  		if actual.Immutable() != x.expectImmutable {
   167  			t.Fatalf("expected %s immutable to be %t, got %t", x.uri, x.expectImmutable, actual.Immutable())
   168  		}
   169  		if actual.List() != x.expectList {
   170  			t.Fatalf("expected %s list to be %t, got %t", x.uri, x.expectList, actual.List())
   171  		}
   172  		if actual.Hash() != x.expectHash {
   173  			t.Fatalf("expected %s hash to be %t, got %t", x.uri, x.expectHash, actual.Hash())
   174  		}
   175  		if x.expectValidKey {
   176  			if actual.Address() == nil {
   177  				t.Fatalf("expected %s to return a valid key, got nil", x.uri)
   178  			} else {
   179  				if !bytes.Equal(x.expectAddr, actual.Address()) {
   180  					t.Fatalf("expected %s to be decoded to %v", x.expectURI.Addr, x.expectAddr)
   181  				}
   182  			}
   183  		}
   184  	}
   185  }