github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/utils/license_test.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"io/ioutil"
     8  	"os"
     9  	"testing"
    10  
    11  	"github.com/stretchr/testify/assert"
    12  	"github.com/stretchr/testify/require"
    13  )
    14  
    15  func TestValidateLicense(t *testing.T) {
    16  	b1 := []byte("junk")
    17  	ok, _ := ValidateLicense(b1)
    18  	require.False(t, ok, "should have failed - bad license")
    19  
    20  	b2 := []byte("junkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunk")
    21  	ok, _ = ValidateLicense(b2)
    22  	require.False(t, ok, "should have failed - bad license")
    23  }
    24  
    25  func TestGetLicenseFileLocation(t *testing.T) {
    26  	fileName := GetLicenseFileLocation("")
    27  	require.NotEmpty(t, fileName, "invalid default file name")
    28  
    29  	fileName = GetLicenseFileLocation("mattermost.mattermost-license")
    30  	require.Equal(t, fileName, "mattermost.mattermost-license", "invalid file name")
    31  }
    32  
    33  func TestGetLicenseFileFromDisk(t *testing.T) {
    34  	t.Run("missing file", func(t *testing.T) {
    35  		fileBytes := GetLicenseFileFromDisk("thisfileshouldnotexist.mattermost-license")
    36  		assert.Empty(t, fileBytes, "invalid bytes")
    37  	})
    38  
    39  	t.Run("not a license file", func(t *testing.T) {
    40  		f, err := ioutil.TempFile("", "TestGetLicenseFileFromDisk")
    41  		require.NoError(t, err)
    42  		defer os.Remove(f.Name())
    43  		ioutil.WriteFile(f.Name(), []byte("not a license"), 0777)
    44  
    45  		fileBytes := GetLicenseFileFromDisk(f.Name())
    46  		require.NotEmpty(t, fileBytes, "should have read the file")
    47  
    48  		success, _ := ValidateLicense(fileBytes)
    49  		assert.False(t, success, "should have been an invalid file")
    50  	})
    51  }