github.com/prysmaticlabs/prysm@v1.4.4/validator/graffiti/parse_graffiti_test.go (about)

     1  package graffiti
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  	"testing"
     8  
     9  	types "github.com/prysmaticlabs/eth2-types"
    10  	"github.com/prysmaticlabs/prysm/shared/hashutil"
    11  	"github.com/prysmaticlabs/prysm/shared/testutil/assert"
    12  	"github.com/prysmaticlabs/prysm/shared/testutil/require"
    13  )
    14  
    15  func TestParseGraffitiFile_Default(t *testing.T) {
    16  	input := []byte(`default: "Mr T was here"`)
    17  
    18  	dirName := t.TempDir() + "somedir"
    19  	err := os.MkdirAll(dirName, os.ModePerm)
    20  	require.NoError(t, err)
    21  	someFileName := filepath.Join(dirName, "somefile.txt")
    22  	require.NoError(t, ioutil.WriteFile(someFileName, input, os.ModePerm))
    23  
    24  	got, err := ParseGraffitiFile(someFileName)
    25  	require.NoError(t, err)
    26  
    27  	wanted := &Graffiti{
    28  		Hash:    hashutil.Hash(input),
    29  		Default: "Mr T was here",
    30  	}
    31  	require.DeepEqual(t, wanted, got)
    32  }
    33  
    34  func TestParseGraffitiFile_Random(t *testing.T) {
    35  	input := []byte(`random:
    36    - "Mr A was here"
    37    - "Mr B was here"
    38    - "Mr C was here"`)
    39  
    40  	dirName := t.TempDir() + "somedir"
    41  	err := os.MkdirAll(dirName, os.ModePerm)
    42  	require.NoError(t, err)
    43  	someFileName := filepath.Join(dirName, "somefile.txt")
    44  	require.NoError(t, ioutil.WriteFile(someFileName, input, os.ModePerm))
    45  
    46  	got, err := ParseGraffitiFile(someFileName)
    47  	require.NoError(t, err)
    48  
    49  	wanted := &Graffiti{
    50  		Hash: hashutil.Hash(input),
    51  		Random: []string{
    52  			"Mr A was here",
    53  			"Mr B was here",
    54  			"Mr C was here",
    55  		},
    56  	}
    57  	require.DeepEqual(t, wanted, got)
    58  }
    59  
    60  func TestParseGraffitiFile_Ordered(t *testing.T) {
    61  	input := []byte(`ordered:
    62    - "Mr D was here"
    63    - "Mr E was here"
    64    - "Mr F was here"`)
    65  
    66  	dirName := t.TempDir() + "somedir"
    67  	err := os.MkdirAll(dirName, os.ModePerm)
    68  	require.NoError(t, err)
    69  	someFileName := filepath.Join(dirName, "somefile.txt")
    70  	require.NoError(t, ioutil.WriteFile(someFileName, input, os.ModePerm))
    71  
    72  	got, err := ParseGraffitiFile(someFileName)
    73  	require.NoError(t, err)
    74  
    75  	wanted := &Graffiti{
    76  		Hash: hashutil.Hash(input),
    77  		Ordered: []string{
    78  			"Mr D was here",
    79  			"Mr E was here",
    80  			"Mr F was here",
    81  		},
    82  	}
    83  	require.DeepEqual(t, wanted, got)
    84  }
    85  
    86  func TestParseGraffitiFile_Validators(t *testing.T) {
    87  	input := []byte(`
    88  specific:
    89    1234: Yolo
    90    555: "What's up"
    91    703727: Meow`)
    92  
    93  	dirName := t.TempDir() + "somedir"
    94  	err := os.MkdirAll(dirName, os.ModePerm)
    95  	require.NoError(t, err)
    96  	someFileName := filepath.Join(dirName, "somefile.txt")
    97  	require.NoError(t, ioutil.WriteFile(someFileName, input, os.ModePerm))
    98  
    99  	got, err := ParseGraffitiFile(someFileName)
   100  	require.NoError(t, err)
   101  
   102  	wanted := &Graffiti{
   103  		Hash: hashutil.Hash(input),
   104  		Specific: map[types.ValidatorIndex]string{
   105  			1234:   "Yolo",
   106  			555:    "What's up",
   107  			703727: "Meow",
   108  		},
   109  	}
   110  	require.DeepEqual(t, wanted, got)
   111  }
   112  
   113  func TestParseGraffitiFile_AllFields(t *testing.T) {
   114  	input := []byte(`default: "Mr T was here"
   115  
   116  random:
   117    - "Mr A was here"
   118    - "Mr B was here"
   119    - "Mr C was here"
   120  
   121  ordered:
   122    - "Mr D was here"
   123    - "Mr E was here"
   124    - "Mr F was here"
   125  
   126  specific:
   127    1234: Yolo
   128    555: "What's up"
   129    703727: Meow`)
   130  
   131  	dirName := t.TempDir() + "somedir"
   132  	err := os.MkdirAll(dirName, os.ModePerm)
   133  	require.NoError(t, err)
   134  	someFileName := filepath.Join(dirName, "somefile.txt")
   135  	require.NoError(t, ioutil.WriteFile(someFileName, input, os.ModePerm))
   136  
   137  	got, err := ParseGraffitiFile(someFileName)
   138  	require.NoError(t, err)
   139  
   140  	wanted := &Graffiti{
   141  		Hash:    hashutil.Hash(input),
   142  		Default: "Mr T was here",
   143  		Random: []string{
   144  			"Mr A was here",
   145  			"Mr B was here",
   146  			"Mr C was here",
   147  		},
   148  		Ordered: []string{
   149  			"Mr D was here",
   150  			"Mr E was here",
   151  			"Mr F was here",
   152  		},
   153  		Specific: map[types.ValidatorIndex]string{
   154  			1234:   "Yolo",
   155  			555:    "What's up",
   156  			703727: "Meow",
   157  		},
   158  	}
   159  	require.DeepEqual(t, wanted, got)
   160  }
   161  
   162  func TestParseHexGraffiti(t *testing.T) {
   163  	tests := []struct {
   164  		name  string
   165  		want  string
   166  		input string
   167  	}{
   168  		{
   169  			name:  "standard",
   170  			want:  "hola mundo!",
   171  			input: "hola mundo!",
   172  		},
   173  		{
   174  			name:  "standard with hex tag",
   175  			want:  "hola mundo!",
   176  			input: "hex:686f6c61206d756e646f21",
   177  		},
   178  		{
   179  			name:  "irregularly cased hex tag",
   180  			want:  "hola mundo!",
   181  			input: "HEX:686f6c61206d756e646f21",
   182  		},
   183  		{
   184  			name:  "hex tag without accompanying data",
   185  			want:  "hex:",
   186  			input: "hex:",
   187  		},
   188  		{
   189  			name:  "Passing non-hex data with hex tag",
   190  			want:  "hex:hola mundo!",
   191  			input: "hex:hola mundo!",
   192  		},
   193  		{
   194  			name:  "unmarked hex input",
   195  			want:  "0x686f6c61206d756e646f21",
   196  			input: "0x686f6c61206d756e646f21",
   197  		},
   198  		{
   199  			name:  "Properly tagged hex data with 0x prefix",
   200  			want:  "hola mundo!",
   201  			input: "hex:0x686f6c61206d756e646f21",
   202  		},
   203  		{
   204  			name:  "hex tag with 0x prefix and no other data",
   205  			want:  "hex:0x",
   206  			input: "hex:0x",
   207  		},
   208  		{
   209  			name:  "hex tag with 0x prefix and invalid hex data",
   210  			want:  "hex:0xhola mundo",
   211  			input: "hex:0xhola mundo",
   212  		},
   213  	}
   214  
   215  	for _, tt := range tests {
   216  		t.Run(tt.name, func(t *testing.T) {
   217  			out := ParseHexGraffiti(tt.input)
   218  			assert.Equal(t, out, tt.want)
   219  		})
   220  	}
   221  }