github.com/google/syzkaller@v0.0.0-20240517125934-c0f1611a36d6/vm/proxyapp/init_test.go (about)

     1  // Copyright 2022 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 proxyapp
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestParseConfig(t *testing.T) {
    13  	tests := []struct {
    14  		name       string
    15  		data       string
    16  		wantConfig *Config
    17  		wantErr    bool
    18  	}{
    19  		{
    20  			name: "test cmd and rpc_server_uri specified Ok",
    21  			data: `{
    22  				"cmd": "/path/to/proxyapp_binary",
    23  				"rpc_server_uri": "127.0.0.1:1234"}`,
    24  			wantConfig: &Config{
    25  				Command:      "/path/to/proxyapp_binary",
    26  				RPCServerURI: "127.0.0.1:1234",
    27  			},
    28  			wantErr: false,
    29  		},
    30  		{
    31  			name: "only cmd specified Ok",
    32  			data: `{"cmd": "/path/to/proxyapp_binary"}`,
    33  			wantConfig: &Config{
    34  				Command:      "/path/to/proxyapp_binary",
    35  				RPCServerURI: "",
    36  			},
    37  			wantErr: false,
    38  		},
    39  		{
    40  			name: "only rpc_server_uri specified Ok",
    41  			data: `{"rpc_server_uri": "127.0.0.1:1234"}`,
    42  			wantConfig: &Config{
    43  				Command:      "",
    44  				RPCServerURI: "127.0.0.1:1234",
    45  			},
    46  			wantErr: false,
    47  		},
    48  		{
    49  			name:       "cmd OR rpc_server_uri are needed",
    50  			data:       `{}`,
    51  			wantConfig: nil,
    52  			wantErr:    true,
    53  		},
    54  		{
    55  			name:       "rpc address format Ok",
    56  			data:       `{"rpc_server_uri": "127.0.0.1:1234"}`,
    57  			wantConfig: &Config{RPCServerURI: "127.0.0.1:1234"},
    58  			wantErr:    false,
    59  		},
    60  		{
    61  			name:       "rpc address format bad",
    62  			data:       `{"rpc_server_uri": "http://127.0.0.1:1234"}`,
    63  			wantConfig: nil,
    64  			wantErr:    true,
    65  		},
    66  		{
    67  			name: "remote plugin config Ok",
    68  			data: `{"rpc_server_uri": "127.0.0.1:1234",
    69  						  "config": {"param": 1}}`,
    70  			wantConfig: &Config{
    71  				RPCServerURI:   "127.0.0.1:1234",
    72  				ProxyAppConfig: []byte(`{"param": 1}`),
    73  			},
    74  			wantErr: false,
    75  		},
    76  		{
    77  			name: "remote plugin config is optional",
    78  			data: `{"rpc_server_uri": "127.0.0.1:1234"}`,
    79  			wantConfig: &Config{
    80  				RPCServerURI:   "127.0.0.1:1234",
    81  				ProxyAppConfig: nil,
    82  			},
    83  			wantErr: false,
    84  		},
    85  	}
    86  
    87  	for _, test := range tests {
    88  		t.Run(test.name, func(tt *testing.T) {
    89  			cfg, err := parseConfig([]byte(test.data))
    90  			assert.Equal(tt, err != nil, test.wantErr)
    91  			assert.Equal(tt, test.wantConfig, cfg)
    92  		})
    93  	}
    94  }
    95  
    96  func TestURIParseErr(t *testing.T) {
    97  	assert.Nil(t, URIParseErr("127.0.0.1:1234"))
    98  	assert.Nil(t, URIParseErr("domain_name:1234"))
    99  
   100  	assert.NotNil(t, URIParseErr("http://domain_name:1234"))
   101  	assert.NotNil(t, URIParseErr("http://127.0.0.1:1234"))
   102  	assert.NotNil(t, URIParseErr("127.0.0.1"))
   103  }