github.com/psyb0t/mattermost-server@v4.6.1-0.20180125161845-5503a1351abf+incompatible/utils/license_test.go (about)

     1  // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
     2  // See License.txt for license information.
     3  
     4  package utils
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/mattermost/mattermost-server/model"
    10  )
    11  
    12  func TestSetLicense(t *testing.T) {
    13  	l1 := &model.License{}
    14  	l1.Features = &model.Features{}
    15  	l1.Customer = &model.Customer{}
    16  	l1.StartsAt = model.GetMillis() - 1000
    17  	l1.ExpiresAt = model.GetMillis() + 100000
    18  	if ok := SetLicense(l1); !ok {
    19  		t.Fatal("license should have worked")
    20  	}
    21  
    22  	l2 := &model.License{}
    23  	l2.Features = &model.Features{}
    24  	l2.Customer = &model.Customer{}
    25  	l2.StartsAt = model.GetMillis() - 1000
    26  	l2.ExpiresAt = model.GetMillis() - 100
    27  	if ok := SetLicense(l2); ok {
    28  		t.Fatal("license should have failed")
    29  	}
    30  
    31  	l3 := &model.License{}
    32  	l3.Features = &model.Features{}
    33  	l3.Customer = &model.Customer{}
    34  	l3.StartsAt = model.GetMillis() + 10000
    35  	l3.ExpiresAt = model.GetMillis() + 100000
    36  	if ok := SetLicense(l3); !ok {
    37  		t.Fatal("license should have passed")
    38  	}
    39  }
    40  
    41  func TestValidateLicense(t *testing.T) {
    42  	b1 := []byte("junk")
    43  	if ok, _ := ValidateLicense(b1); ok {
    44  		t.Fatal("should have failed - bad license")
    45  	}
    46  
    47  	LoadLicense(b1)
    48  
    49  	b2 := []byte("junkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunkjunk")
    50  	if ok, _ := ValidateLicense(b2); ok {
    51  		t.Fatal("should have failed - bad license")
    52  	}
    53  }
    54  
    55  func TestClientLicenseEtag(t *testing.T) {
    56  	etag1 := GetClientLicenseEtag(false)
    57  
    58  	SetClientLicense(map[string]string{"SomeFeature": "true", "IsLicensed": "true"})
    59  
    60  	etag2 := GetClientLicenseEtag(false)
    61  	if etag1 == etag2 {
    62  		t.Fatal("etags should not match")
    63  	}
    64  
    65  	SetClientLicense(map[string]string{"SomeFeature": "true", "IsLicensed": "false"})
    66  
    67  	etag3 := GetClientLicenseEtag(false)
    68  	if etag2 == etag3 {
    69  		t.Fatal("etags should not match")
    70  	}
    71  }
    72  
    73  func TestGetSanitizedClientLicense(t *testing.T) {
    74  	l1 := &model.License{}
    75  	l1.Features = &model.Features{}
    76  	l1.Customer = &model.Customer{}
    77  	l1.Customer.Name = "TestName"
    78  	l1.StartsAt = model.GetMillis() - 1000
    79  	l1.ExpiresAt = model.GetMillis() + 100000
    80  	SetLicense(l1)
    81  
    82  	m := GetSanitizedClientLicense()
    83  
    84  	if _, ok := m["Name"]; ok {
    85  		t.Fatal("should have been sanatized")
    86  	}
    87  }
    88  
    89  func TestGetLicenseFileLocation(t *testing.T) {
    90  	fileName := GetLicenseFileLocation("")
    91  	if len(fileName) == 0 {
    92  		t.Fatal("invalid default file name")
    93  	}
    94  
    95  	fileName = GetLicenseFileLocation("mattermost.mattermost-license")
    96  	if fileName != "mattermost.mattermost-license" {
    97  		t.Fatal("invalid file name")
    98  	}
    99  }
   100  
   101  func TestGetLicenseFileFromDisk(t *testing.T) {
   102  	fileBytes := GetLicenseFileFromDisk("thisfileshouldnotexist.mattermost-license")
   103  	if len(fileBytes) > 0 {
   104  		t.Fatal("invalid bytes")
   105  	}
   106  
   107  	fileBytes = GetLicenseFileFromDisk(FindConfigFile("config.json"))
   108  	if len(fileBytes) == 0 { // a valid bytes but should be a fail license
   109  		t.Fatal("invalid bytes")
   110  	}
   111  
   112  	if success, _ := ValidateLicense(fileBytes); success {
   113  		t.Fatal("should have been an invalid file")
   114  	}
   115  }