github.com/sberex/go-sberex@v1.8.2-0.20181113200658-ed96ac38f7d7/rpc/utils_test.go (about)

     1  // This file is part of the go-sberex library. The go-sberex library is 
     2  // free software: you can redistribute it and/or modify it under the terms 
     3  // of the GNU Lesser General Public License as published by the Free 
     4  // Software Foundation, either version 3 of the License, or (at your option)
     5  // any later version.
     6  //
     7  // The go-sberex library is distributed in the hope that it will be useful, 
     8  // but WITHOUT ANY WARRANTY; without even the implied warranty of
     9  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser 
    10  // General Public License <http://www.gnu.org/licenses/> for more details.
    11  
    12  package rpc
    13  
    14  import (
    15  	"strings"
    16  	"testing"
    17  )
    18  
    19  func TestNewID(t *testing.T) {
    20  	hexchars := "0123456789ABCDEFabcdef"
    21  	for i := 0; i < 100; i++ {
    22  		id := string(NewID())
    23  		if !strings.HasPrefix(id, "0x") {
    24  			t.Fatalf("invalid ID prefix, want '0x...', got %s", id)
    25  		}
    26  
    27  		id = id[2:]
    28  		if len(id) == 0 || len(id) > 32 {
    29  			t.Fatalf("invalid ID length, want len(id) > 0 && len(id) <= 32), got %d", len(id))
    30  		}
    31  
    32  		for i := 0; i < len(id); i++ {
    33  			if strings.IndexByte(hexchars, id[i]) == -1 {
    34  				t.Fatalf("unexpected byte, want any valid hex char, got %c", id[i])
    35  			}
    36  		}
    37  	}
    38  }