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

     1  // Copyright 2021 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 config_test
     5  
     6  import (
     7  	"bytes"
     8  	"testing"
     9  
    10  	"github.com/google/syzkaller/pkg/config"
    11  )
    12  
    13  func TestMergeJSONs(t *testing.T) {
    14  	tests := []struct {
    15  		left   string
    16  		right  string
    17  		result string
    18  	}{
    19  		{
    20  			`{"a":1,"b":2}`,
    21  			`{"b":3,"c":4}`,
    22  			`{"a":1,"b":3,"c":4}`,
    23  		},
    24  		{
    25  			`{"a":1,"b":{"c":{"d":"nested string","e":"another string"}}}`,
    26  			`{"b":{"c":{"d":12345}}}`,
    27  			`{"a":1,"b":{"c":{"d":12345,"e":"another string"}}}`,
    28  		},
    29  		{
    30  			`{}`,
    31  			`{"a":{"b":{"c":0}}}`,
    32  			`{"a":{"b":{"c":0}}}`,
    33  		},
    34  		{
    35  			`{"a":{"b":{"c":0}}}`,
    36  			``,
    37  			`{"a":{"b":{"c":0}}}`,
    38  		},
    39  	}
    40  	for _, test := range tests {
    41  		res, err := config.MergeJSONs([]byte(test.left), []byte(test.right))
    42  		if err != nil {
    43  			t.Errorf("unexpected error: %s", err)
    44  		}
    45  		if !bytes.Equal(res, []byte(test.result)) {
    46  			t.Errorf("expected %s, got %s", test.result, res)
    47  		}
    48  	}
    49  }
    50  
    51  func TestPatchJSON(t *testing.T) {
    52  	tests := []struct {
    53  		left   string
    54  		patch  map[string]interface{}
    55  		result string
    56  	}{
    57  		{
    58  			`{"a":1,"b":2}`,
    59  			map[string]interface{}{"b": "string val"},
    60  			`{"a":1,"b":"string val"}`,
    61  		},
    62  		{
    63  			`{"a":1,"b":2}`,
    64  			map[string]interface{}{
    65  				"a": map[string]interface{}{
    66  					"b": map[string]interface{}{
    67  						"c": 5,
    68  					},
    69  				},
    70  			},
    71  			`{"a":{"b":{"c":5}},"b":2}`,
    72  		},
    73  		{
    74  			`{}`,
    75  			map[string]interface{}{
    76  				"a": map[string]interface{}{
    77  					"b": map[string]interface{}{
    78  						"c": 0,
    79  					},
    80  				},
    81  			},
    82  			`{"a":{"b":{"c":0}}}`,
    83  		},
    84  	}
    85  	for _, test := range tests {
    86  		res, err := config.PatchJSON([]byte(test.left), test.patch)
    87  		if err != nil {
    88  			t.Errorf("unexpected error: %s", err)
    89  		}
    90  		if !bytes.Equal(res, []byte(test.result)) {
    91  			t.Errorf("expected %s, got %s", test.result, res)
    92  		}
    93  	}
    94  }