github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/validate/transvalidate_test.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package validate
     6  
     7  import (
     8  	"testing"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils"
    11  )
    12  
    13  func TestIsTransHash(t *testing.T) {
    14  	if IsTransHash("fe611cbee814a8e84c3339186b6dc973947c2dc058cd41a5e3669f3b7f4c980d") {
    15  		t.Error("Passes for hash without 0x")
    16  	}
    17  
    18  	if IsTransHash("0xfe611cbee814a8e84c3339186b6dc973947c2dc058") {
    19  		t.Error("Passes for short hash")
    20  	}
    21  
    22  	if IsTransHash("0xgg611cbee814a8e84c3339186b6dc973947c2dc058cd41a5e3669f3b7f4c980d") {
    23  		t.Error("Passes for invalid hash")
    24  	}
    25  
    26  	if !IsTransHash("0x060e4cf9fa8d34a8b423b5b3691b2541255ff7974ff16699e104edcfb63bd521") {
    27  		t.Error("Fails for a valid transaction hash")
    28  	}
    29  
    30  	// TODO: We don't check if it's a valid transaction hash
    31  	// if IsTransHash("0x0b4c6fb75ded4b90218cf0346b0885e442878f104e1b60bf75d5b6860eeacd53") {
    32  	// 	t.Error("Passes for a block hash")
    33  	// }
    34  }
    35  
    36  func TestIsTransBlockNumAndIndex(t *testing.T) {
    37  	if IsTransBlockNumAndId("notanumber") {
    38  		t.Error("Passes for invalid transaction id")
    39  	}
    40  
    41  	if IsTransBlockNumAndId("") {
    42  		t.Error("Passes for empty transaction id")
    43  	}
    44  
    45  	if !IsTransBlockNumAndId("0.1234") {
    46  		t.Error("Fails at zero block")
    47  	}
    48  
    49  	if !IsTransBlockNumAndId("0xff.0") {
    50  		t.Error("Fails for hex.num")
    51  	}
    52  
    53  	if !IsTransBlockNumAndId("0.0xf") {
    54  		t.Error("Fails for num.hex")
    55  	}
    56  
    57  	if !IsTransBlockNumAndId("0xff.0xf") {
    58  		t.Error("Fails for hex.hex")
    59  	}
    60  
    61  	if !IsTransBlockNumAndId("1000.0") {
    62  		t.Error("Fails for num.num")
    63  	}
    64  }
    65  
    66  func TestIsBlockHashAndId(t *testing.T) {
    67  	if IsTransBlockHashAndId("notanumber") {
    68  		t.Error("Passes for invalid transaction id")
    69  	}
    70  
    71  	if IsTransBlockHashAndId("") {
    72  		t.Error("Passes for empty transaction id")
    73  	}
    74  
    75  	if IsTransBlockHashAndId("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8.1234") {
    76  		t.Error("Passes with too short hash block")
    77  	}
    78  
    79  	if IsTransBlockHashAndId("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3a3.1234") {
    80  		t.Error("Passes with too long hash")
    81  	}
    82  
    83  	if !IsTransBlockHashAndId("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3.0x0") {
    84  		t.Error("Fails for hash.hex")
    85  	}
    86  
    87  	if !IsTransBlockHashAndId("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3.10") {
    88  		t.Error("Fails for hash.num")
    89  	}
    90  }
    91  
    92  func TestValidateTransIdentifiers(t *testing.T) {
    93  	type args struct {
    94  		identifiers []string
    95  		validTypes  ValidArgumentType
    96  		maxRanges   int
    97  	}
    98  	tests := []struct {
    99  		name    string
   100  		args    args
   101  		wantErr bool
   102  	}{
   103  		{
   104  			name: "correct transaction hashes",
   105  			args: args{
   106  				identifiers: []string{
   107  					"0xd3b9663a5f2367cb1ebeff5eab7d45cc24931678a1e96348291db13057ad438f",
   108  					"0x98862798ac03f688cba584931e0fe42bf17f37a7c48f905c6fc4e9f2a0ec7cb4",
   109  				},
   110  				validTypes: ValidArgumentTransHash,
   111  				maxRanges:  1,
   112  			},
   113  			wantErr: false,
   114  		},
   115  		{
   116  			name: "correct blockNum.transId",
   117  			args: args{
   118  				identifiers: []string{
   119  					"10.0",
   120  					"100.1",
   121  					"0x100.2",
   122  				},
   123  				validTypes: ValidArgumentTransBlockNumberAndId,
   124  				maxRanges:  1,
   125  			},
   126  			wantErr: false,
   127  		},
   128  		{
   129  			name: "correct blockNum.transId",
   130  			args: args{
   131  				identifiers: []string{
   132  					"0xd3b9663a5f2367cb1ebeff5eab7d45cc24931678a1e96348291db13057ad438f.0x0",
   133  					"0xd3b9663a5f2367cb1ebeff5eab7d45cc24931678a1e96348291db13057ad438f.1",
   134  				},
   135  				validTypes: ValidArgumentTransBlockHashAndId,
   136  				maxRanges:  1,
   137  			},
   138  			wantErr: false,
   139  		},
   140  		{
   141  			name: "correct mixed identifiers",
   142  			args: args{
   143  				identifiers: []string{
   144  					"0xd3b9663a5f2367cb1ebeff5eab7d45cc24931678a1e96348291db13057ad438f",
   145  					"0x98862798ac03f688cba584931e0fe42bf17f37a7c48f905c6fc4e9f2a0ec7cb4",
   146  					"10.0",
   147  					"100.1",
   148  					"0x100.2",
   149  					"0xd3b9663a5f2367cb1ebeff5eab7d45cc24931678a1e96348291db13057ad438f.0x0",
   150  					"0xd3b9663a5f2367cb1ebeff5eab7d45cc24931678a1e96348291db13057ad438f.1",
   151  				},
   152  				validTypes: ValidArgumentTransHash | ValidArgumentTransBlockHashAndId | ValidArgumentTransBlockNumberAndId,
   153  				maxRanges:  1,
   154  			},
   155  			wantErr: false,
   156  		},
   157  	}
   158  	for _, tt := range tests {
   159  		t.Run(tt.name, func(t *testing.T) {
   160  			if err := ValidateIdentifiers(
   161  				utils.GetTestChain(),
   162  				tt.args.identifiers,
   163  				tt.args.validTypes,
   164  				tt.args.maxRanges,
   165  				nil,
   166  			); (err != nil) != tt.wantErr {
   167  				t.Errorf("ValidateIdentifiers() error = %v, wantErr %v", err, tt.wantErr)
   168  			}
   169  		})
   170  	}
   171  }