|  | 
| 1 | 1 | package servers | 
| 2 | 2 | 
 | 
| 3 | 3 | import ( | 
|  | 4 | +	"context" | 
|  | 5 | +	"encoding/json" | 
| 4 | 6 | 	"fmt" | 
| 5 | 7 | 	"net/http" | 
| 6 | 8 | 	"net/http/httptest" | 
| @@ -139,3 +141,53 @@ func (s *lanternServerManagerMock) ServeHTTP(w http.ResponseWriter, r *http.Requ | 
| 139 | 141 | 
 | 
| 140 | 142 | 	w.WriteHeader(http.StatusNotFound) | 
| 141 | 143 | } | 
|  | 144 | + | 
|  | 145 | +func TestAddServerWithSingBoxJSON(t *testing.T) { | 
|  | 146 | +	dataPath := t.TempDir() | 
|  | 147 | +	manager := &Manager{ | 
|  | 148 | +		servers: Servers{ | 
|  | 149 | +			SGLantern: Options{ | 
|  | 150 | +				Outbounds: make([]option.Outbound, 0), | 
|  | 151 | +				Endpoints: make([]option.Endpoint, 0), | 
|  | 152 | +				Locations: make(map[string]C.ServerLocation), | 
|  | 153 | +			}, | 
|  | 154 | +			SGUser: Options{ | 
|  | 155 | +				Outbounds: make([]option.Outbound, 0), | 
|  | 156 | +				Endpoints: make([]option.Endpoint, 0), | 
|  | 157 | +				Locations: make(map[string]C.ServerLocation), | 
|  | 158 | +			}, | 
|  | 159 | +		}, | 
|  | 160 | +		optsMaps: map[ServerGroup]map[string]any{ | 
|  | 161 | +			SGLantern: make(map[string]any), | 
|  | 162 | +			SGUser:    make(map[string]any), | 
|  | 163 | +		}, | 
|  | 164 | +		serversFile:      filepath.Join(dataPath, common.ServersFileName), | 
|  | 165 | +		fingerprintsFile: filepath.Join(dataPath, trustFingerprintFileName), | 
|  | 166 | +	} | 
|  | 167 | + | 
|  | 168 | +	ctx := context.Background() | 
|  | 169 | +	jsonConfig := ` | 
|  | 170 | +	{ | 
|  | 171 | +		"outbounds": [ | 
|  | 172 | +			{ | 
|  | 173 | +               "type": "shadowsocks", | 
|  | 174 | +               "tag": "ss-out", | 
|  | 175 | +               "server": "127.0.0.1", | 
|  | 176 | +               "server_port": 8388, | 
|  | 177 | +               "method": "chacha20-ietf-poly1305", | 
|  | 178 | +               "password": "randompasswordwith24char", | 
|  | 179 | +               "network": "tcp" | 
|  | 180 | +            } | 
|  | 181 | +		] | 
|  | 182 | +	}` | 
|  | 183 | + | 
|  | 184 | +	t.Run("adding server with a sing-box json config should work", func(t *testing.T) { | 
|  | 185 | +		require.NoError(t, manager.AddServerWithSingboxJSON(ctx, []byte(jsonConfig))) | 
|  | 186 | +	}) | 
|  | 187 | +	t.Run("using a empty config should return an error", func(t *testing.T) { | 
|  | 188 | +		require.Error(t, manager.AddServerWithSingboxJSON(ctx, []byte{})) | 
|  | 189 | +	}) | 
|  | 190 | +	t.Run("providing a json that doesn't have any endpoints or outbounds should return a error", func(t *testing.T) { | 
|  | 191 | +		require.Error(t, manager.AddServerWithSingboxJSON(ctx, json.RawMessage("{}"))) | 
|  | 192 | +	}) | 
|  | 193 | +} | 
0 commit comments