github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/pkg/compiler/const_file_test.go (about)

     1  // Copyright 2020 syzkaller project authors. All rights reserved.
     2  // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file.
     3  
     4  package compiler
     5  
     6  import (
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestConstFile(t *testing.T) {
    15  	type arch struct {
    16  		consts    map[string]uint64
    17  		undefined map[string]bool
    18  		oldFormat string
    19  	}
    20  	arches := map[string]arch{
    21  		"arch1": {
    22  			consts: map[string]uint64{
    23  				"CONST1_ALL_DIFFERENT":   11,
    24  				"CONST2_ALL_THE_SAME":    3,
    25  				"CONST3_SOME_UNDEFINED":  100,
    26  				"CONST5_SOME_UNDEFINED2": 100,
    27  			},
    28  			undefined: map[string]bool{
    29  				"CONST4_ALL_UNDEFINED": true,
    30  			},
    31  			oldFormat: `
    32  CONST1_ALL_DIFFERENT = 11
    33  CONST2_ALL_THE_SAME = 3
    34  CONST3_SOME_UNDEFINED = 100
    35  CONST5_SOME_UNDEFINED2 = 100
    36  # CONST4_ALL_UNDEFINED is not set
    37  `,
    38  		},
    39  		"arch2": {
    40  			consts: map[string]uint64{
    41  				"CONST1_ALL_DIFFERENT":   22,
    42  				"CONST2_ALL_THE_SAME":    3,
    43  				"CONST5_SOME_UNDEFINED2": 100,
    44  			},
    45  			undefined: map[string]bool{
    46  				"CONST4_ALL_UNDEFINED":  true,
    47  				"CONST3_SOME_UNDEFINED": true,
    48  			},
    49  			oldFormat: `
    50  CONST1_ALL_DIFFERENT = 22
    51  CONST2_ALL_THE_SAME = 3
    52  # CONST3_SOME_UNDEFINED is not set
    53  CONST5_SOME_UNDEFINED2 = 100
    54  # CONST4_ALL_UNDEFINED is not set
    55  `,
    56  		},
    57  		"arch3": {
    58  			consts: map[string]uint64{
    59  				"CONST1_ALL_DIFFERENT": 33,
    60  				"CONST2_ALL_THE_SAME":  3,
    61  			},
    62  			undefined: map[string]bool{
    63  				"CONST4_ALL_UNDEFINED":   true,
    64  				"CONST3_SOME_UNDEFINED":  true,
    65  				"CONST5_SOME_UNDEFINED2": true,
    66  			},
    67  			oldFormat: `
    68  CONST1_ALL_DIFFERENT = 33
    69  CONST2_ALL_THE_SAME = 3
    70  # CONST3_SOME_UNDEFINED is not set
    71  # CONST5_SOME_UNDEFINED2 is not set
    72  # CONST4_ALL_UNDEFINED is not set
    73  `,
    74  		},
    75  	}
    76  	const serialized = `# Code generated by syz-sysgen. DO NOT EDIT.
    77  arches = arch1, arch2, arch3
    78  CONST1_ALL_DIFFERENT = arch1:11, arch2:22, arch3:33
    79  CONST2_ALL_THE_SAME = 3
    80  CONST3_SOME_UNDEFINED = arch1:100, arch2:arch3:???
    81  CONST4_ALL_UNDEFINED = ???
    82  CONST5_SOME_UNDEFINED2 = 100, arch3:???
    83  `
    84  	cf := NewConstFile()
    85  	for name, arch := range arches {
    86  		cf.AddArch(name, arch.consts, arch.undefined)
    87  	}
    88  	data := cf.Serialize()
    89  	assert.Equal(t, serialized, string(data))
    90  	assert.True(t, cf.ExistsAny("CONST3_SOME_UNDEFINED"))
    91  	assert.False(t, cf.ExistsAny("CONST4_ALL_UNDEFINED"))
    92  	{
    93  		file, err := os.CreateTemp("", "syz-const")
    94  		if err != nil {
    95  			t.Fatal(err)
    96  		}
    97  		defer file.Close()
    98  		defer os.Remove(file.Name())
    99  		if _, err := file.Write(data); err != nil {
   100  			t.Fatal(err)
   101  		}
   102  		file.Close()
   103  		cf1 := DeserializeConstFile(file.Name(), nil)
   104  		for name, arch := range arches {
   105  			assert.Equal(t, cf1.Arch(name), arch.consts)
   106  		}
   107  	}
   108  	{
   109  		dir := t.TempDir()
   110  		for name, arch := range arches {
   111  			file := filepath.Join(dir, "consts_"+name+".const")
   112  			if err := os.WriteFile(file, []byte(arch.oldFormat), 0600); err != nil {
   113  				t.Fatal(err)
   114  			}
   115  		}
   116  		cf1 := DeserializeConstFile(filepath.Join(dir, "*"), nil)
   117  		for name, arch := range arches {
   118  			assert.Equal(t, cf1.Arch(name), arch.consts)
   119  		}
   120  	}
   121  }