github.com/kisexp/xdchain@v0.0.0-20211206025815-490d6b732aa7/plugin/central_test.go (about)

     1  package plugin
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"io"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"net/http/httptest"
    10  	"os"
    11  	"path"
    12  	"runtime"
    13  	"testing"
    14  
    15  	"github.com/stretchr/testify/assert"
    16  )
    17  
    18  func TestCentralClient_PublicKey(t *testing.T) {
    19  	arbitraryServer := newTestServer("/"+DefaultPublicKeyFile, arbitraryPubKey)
    20  	defer arbitraryServer.Close()
    21  	arbitraryConfig := &PluginCentralConfiguration{
    22  		BaseURL:      arbitraryServer.URL,
    23  		PublicKeyURI: DefaultPublicKeyFile,
    24  	}
    25  
    26  	testObject := NewPluginCentralClient(arbitraryConfig)
    27  
    28  	actualValue, err := testObject.PublicKey()
    29  
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	assert.Equal(t, arbitraryPubKey, actualValue)
    34  }
    35  
    36  func TestCentralClient_PublicKey_RelativePath(t *testing.T) {
    37  	arbitraryServer := newTestServer("/aa/"+DefaultPublicKeyFile, arbitraryPubKey)
    38  	defer arbitraryServer.Close()
    39  	arbitraryConfig := &PluginCentralConfiguration{
    40  		BaseURL:      arbitraryServer.URL + "/aa/bb/cc/", // without postfix /, the relative path would be diff
    41  		PublicKeyURI: "../../" + DefaultPublicKeyFile,
    42  	}
    43  
    44  	testObject := NewPluginCentralClient(arbitraryConfig)
    45  
    46  	actualValue, err := testObject.PublicKey()
    47  
    48  	if err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	assert.Equal(t, arbitraryPubKey, actualValue)
    52  }
    53  
    54  func TestCentralClient_PublicKey_withSSL(t *testing.T) {
    55  	arbitraryServer := httptest.NewTLSServer(newMux("/"+DefaultPublicKeyFile, arbitraryPubKey))
    56  	defer arbitraryServer.Close()
    57  	arbitraryConfig := &PluginCentralConfiguration{
    58  		CertFingerprint:       string(arbitraryServer.Certificate().Signature),
    59  		BaseURL:               arbitraryServer.URL,
    60  		PublicKeyURI:          DefaultPublicKeyFile,
    61  		InsecureSkipTLSVerify: true,
    62  	}
    63  
    64  	testObject := NewPluginCentralClient(arbitraryConfig)
    65  
    66  	actualValue, err := testObject.PublicKey()
    67  
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	assert.Equal(t, arbitraryPubKey, actualValue)
    72  }
    73  
    74  func TestCentralClient_PluginSignature(t *testing.T) {
    75  	arbitraryDef := &PluginDefinition{
    76  		Name:    "arbitrary-plugin",
    77  		Version: "1.0.0",
    78  	}
    79  	expectedPath := fmt.Sprintf("/maven/bin/%s/%s/%s-%s-%s-%s-sha256.checksum.asc", arbitraryDef.Name, arbitraryDef.Version, arbitraryDef.Name, arbitraryDef.Version, runtime.GOOS, runtime.GOARCH)
    80  	arbitraryServer := newTestServer(expectedPath, validSignature)
    81  	defer arbitraryServer.Close()
    82  	arbitraryConfig := &PluginCentralConfiguration{
    83  		BaseURL: arbitraryServer.URL,
    84  	}
    85  	arbitraryConfig.SetDefaults()
    86  
    87  	testObject := NewPluginCentralClient(arbitraryConfig)
    88  
    89  	actualValue, err := testObject.PluginSignature(arbitraryDef)
    90  
    91  	if err != nil {
    92  		t.Fatal(err)
    93  	}
    94  	assert.Equal(t, validSignature, actualValue)
    95  }
    96  
    97  func TestCentralClient_PluginDistribution(t *testing.T) {
    98  	tmpDir, err := ioutil.TempDir("", "q-")
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  	defer func() {
   103  		_ = os.RemoveAll(tmpDir)
   104  	}()
   105  	arbitraryDef := &PluginDefinition{
   106  		Name:    "arbitrary-plugin",
   107  		Version: "1.0.0",
   108  	}
   109  	arbitraryData := []byte("arbitrary data")
   110  	expectedPath := fmt.Sprintf("/maven/bin/%s/%s/%s-%s-%s-%s.zip", arbitraryDef.Name, arbitraryDef.Version, arbitraryDef.Name, arbitraryDef.Version, runtime.GOOS, runtime.GOARCH)
   111  	arbitraryServer := newTestServer(expectedPath, arbitraryData)
   112  	defer arbitraryServer.Close()
   113  	arbitraryConfig := &PluginCentralConfiguration{
   114  		BaseURL: arbitraryServer.URL,
   115  	}
   116  	arbitraryConfig.SetDefaults()
   117  
   118  	testObject := NewPluginCentralClient(arbitraryConfig)
   119  
   120  	err = testObject.PluginDistribution(arbitraryDef, path.Join(tmpDir, "download.zip"))
   121  
   122  	assert.NoError(t, err)
   123  }
   124  
   125  func newTestServer(pattern string, returnedData []byte) *httptest.Server {
   126  	return httptest.NewServer(newMux(pattern, returnedData))
   127  }
   128  
   129  func newMux(pattern string, returnedData []byte) http.Handler {
   130  	mux := http.NewServeMux()
   131  	mux.Handle(pattern, http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
   132  		_, err := io.Copy(w, bytes.NewReader(returnedData))
   133  		if err != nil {
   134  			http.Error(w, err.Error(), http.StatusInternalServerError)
   135  		}
   136  	}))
   137  	return mux
   138  }