github.com/GuanceCloud/cliutils@v1.1.21/pipeline/ptinput/funcs/fn_hash_test.go (about)

     1  // Unless explicitly stated otherwise all files in this repository are licensed
     2  // under the MIT License.
     3  // This product includes software developed at Guance Cloud (https://www.guance.com/).
     4  // Copyright 2021-present Guance, Inc.
     5  
     6  package funcs
     7  
     8  import (
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/GuanceCloud/cliutils/pipeline/ptinput"
    13  	"github.com/GuanceCloud/cliutils/point"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestHash(t *testing.T) {
    18  	funcs := []*Function{
    19  		FnPtKvsGet,
    20  		FnPtKvsDel,
    21  		FnPtKvsSet,
    22  		FnPtKvsKeys,
    23  
    24  		FnHash,
    25  	}
    26  
    27  	cases := []struct {
    28  		name, pl, in string
    29  		keyName      string
    30  		expect       interface{}
    31  		fail         bool
    32  	}{
    33  		{
    34  			name: "md5",
    35  			pl: `
    36  			sum = hash("abc", "md5")
    37  			pt_kvs_set("result", sum)
    38  			`,
    39  			keyName: "result",
    40  			expect:  "900150983cd24fb0d6963f7d28e17f72",
    41  		},
    42  		{
    43  			name: "xx",
    44  			pl: `
    45  			sum = hash("abc", "xx")
    46  			pt_kvs_set("result", sum)
    47  			`,
    48  			keyName: "result",
    49  			expect:  "",
    50  		},
    51  		{
    52  			name: "sha1",
    53  			pl: `
    54  			sum = hash("abc", "sha1")
    55  			pt_kvs_set("result", sum)
    56  			`,
    57  			keyName: "result",
    58  			expect:  "a9993e364706816aba3e25717850c26c9cd0d89d",
    59  		},
    60  		{
    61  			name: "sha256",
    62  			pl: `
    63  			sum = hash("abc", "sha256")
    64  			pt_kvs_set("result", sum)
    65  			`,
    66  			keyName: "result",
    67  			expect:  "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad",
    68  		},
    69  		{
    70  			name: "sha512",
    71  			pl: `
    72  			sum = hash("abc", "sha512")
    73  			pt_kvs_set("result", sum)
    74  			`,
    75  			keyName: "result",
    76  			expect:  "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
    77  		},
    78  		{
    79  			name: "sha512",
    80  			pl: `
    81  			sum = hash("abc", )
    82  			pt_kvs_set("result", sum)
    83  			`,
    84  			keyName: "result",
    85  			expect:  "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
    86  			fail:    true,
    87  		},
    88  		{
    89  			name: "sha512",
    90  			pl: `
    91  			sum = hash(method= "abc", )
    92  			pt_kvs_set("result", sum)
    93  			`,
    94  			keyName: "result",
    95  			expect:  "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f",
    96  			fail:    true,
    97  		},
    98  	}
    99  
   100  	for idx, tc := range cases {
   101  		t.Run(tc.name, func(t *testing.T) {
   102  			script, err := parseScipt(tc.pl, funcs)
   103  			if err != nil {
   104  				if tc.fail {
   105  					t.Logf("[%d]expect error: %s", idx, err)
   106  				} else {
   107  					t.Errorf("[%d] failed: %s", idx, err)
   108  				}
   109  				return
   110  			}
   111  
   112  			pt := ptinput.NewPlPoint(
   113  				point.Logging, "test", nil, map[string]any{"message": tc.in}, time.Now())
   114  			errR := script.Run(pt, nil)
   115  			if errR != nil {
   116  				t.Fatal(errR.Error())
   117  			}
   118  
   119  			v, _, _ := pt.Get(tc.keyName)
   120  			assert.Equal(t, tc.expect, v)
   121  			t.Logf("[%d] PASS", idx)
   122  
   123  		})
   124  	}
   125  }