go.temporal.io/server@v1.23.0/common/masker/masker_test.go (about)

     1  // The MIT License
     2  //
     3  // Copyright (c) 2020 Temporal Technologies Inc.  All rights reserved.
     4  //
     5  // Copyright (c) 2020 Uber Technologies, Inc.
     6  //
     7  // Permission is hereby granted, free of charge, to any person obtaining a copy
     8  // of this software and associated documentation files (the "Software"), to deal
     9  // in the Software without restriction, including without limitation the rights
    10  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    11  // copies of the Software, and to permit persons to whom the Software is
    12  // furnished to do so, subject to the following conditions:
    13  //
    14  // The above copyright notice and this permission notice shall be included in
    15  // all copies or substantial portions of the Software.
    16  //
    17  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    18  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    19  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    20  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    21  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    22  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    23  // THE SOFTWARE.
    24  
    25  package masker
    26  
    27  import (
    28  	"fmt"
    29  	"strings"
    30  	"testing"
    31  
    32  	"github.com/stretchr/testify/assert"
    33  )
    34  
    35  func TestMaskStruct(t *testing.T) {
    36  	assert := assert.New(t)
    37  
    38  	type strct struct {
    39  		Login    string
    40  		Password string
    41  	}
    42  
    43  	s1 := strct{
    44  		Login:    "login",
    45  		Password: "password",
    46  	}
    47  	maskedS1 := MaskStruct(s1, DefaultFieldNames)
    48  	assert.Equal("password", s1.Password)
    49  	assert.Equal("******", maskedS1.(*strct).Password)
    50  	assert.Equal("login", maskedS1.(*strct).Login)
    51  
    52  	s2 := &strct{
    53  		Login:    "login",
    54  		Password: "password",
    55  	}
    56  	maskedS2 := MaskStruct(s2, DefaultFieldNames)
    57  	assert.Equal("password", s2.Password)
    58  	assert.Equal("******", maskedS2.(*strct).Password)
    59  	assert.Equal("login", maskedS2.(*strct).Login)
    60  }
    61  
    62  func TestMaskStruct_Nil(t *testing.T) {
    63  	assert := assert.New(t)
    64  
    65  	maskedS1 := MaskStruct(nil, DefaultFieldNames)
    66  	assert.Nil(maskedS1)
    67  
    68  	var nilInterface interface{}
    69  	maskedS2 := MaskStruct(nilInterface, DefaultFieldNames)
    70  	assert.Nil(maskedS2)
    71  
    72  	var nilInt *int
    73  	maskedS3 := MaskStruct(nilInt, DefaultFieldNames)
    74  	assert.Nil(maskedS3)
    75  }
    76  
    77  func TestMaskYaml(t *testing.T) {
    78  	assert := assert.New(t)
    79  	yaml := `persistence:
    80    defaultStore: mysql-default
    81    visibilityStore: mysql-visibility
    82    numHistoryShards: 4
    83    datastores:
    84      mysql-default:
    85        sql:
    86          pluginName: "mysql"
    87          databaseName: "temporal"
    88          connectAddr: "127.0.0.1:3306"
    89          connectProtocol: "tcp"
    90          user: "temporal"
    91          password: "secret"`
    92  
    93  	maskedYaml, err := MaskYaml(yaml, DefaultYAMLFieldNames)
    94  	assert.NoError(err)
    95  	assert.True(strings.Contains(yaml, "secret"))
    96  	assert.False(strings.Contains(maskedYaml, "secret"))
    97  	assert.True(strings.Contains(maskedYaml, "******"))
    98  
    99  	fmt.Println(maskedYaml)
   100  }