github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/ccl/utilccl/licenseccl/license_test.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Licensed as a CockroachDB Enterprise file under the Cockroach Community 4 // License (the "License"); you may not use this file except in compliance with 5 // the License. You may obtain a copy of the License at 6 // 7 // https://github.com/cockroachdb/cockroach/blob/master/licenses/CCL.txt 8 9 package licenseccl 10 11 import ( 12 "fmt" 13 "testing" 14 "time" 15 16 "github.com/cockroachdb/cockroach/pkg/testutils" 17 "github.com/cockroachdb/cockroach/pkg/util/timeutil" 18 "github.com/cockroachdb/cockroach/pkg/util/uuid" 19 ) 20 21 func TestLicense(t *testing.T) { 22 clusterA, _ := uuid.FromString("A0000000-0000-0000-0000-00000000000A") 23 clusterB, _ := uuid.FromString("B0000000-0000-0000-0000-00000000000B") 24 25 clustersA, clustersB := []uuid.UUID{clusterA}, []uuid.UUID{clusterB} 26 27 t0 := timeutil.Unix(0, 0) 28 ts := t0.AddDate(40, 0, 0) 29 after := ts.Add(time.Hour * 24) 30 before := ts.Add(time.Hour * -24) 31 wayAfter := ts.Add(time.Hour * 24 * 365 * 200) 32 33 for i, tc := range []struct { 34 licType License_Type 35 grantedTo []uuid.UUID 36 expiration time.Time 37 checkCluster uuid.UUID 38 checkOrg string 39 checkTime time.Time 40 err string 41 }{ 42 {licType: -1, err: "requires an enterprise license"}, 43 {License_Evaluation, clustersA, ts, clusterA, "", ts, ""}, 44 {License_Enterprise, clustersA, ts, clusterA, "", ts, ""}, 45 {License_NonCommercial, clustersA, ts, clusterA, "", ts, ""}, 46 {License_Evaluation, clustersA, after, clusterA, "", ts, ""}, 47 {License_Evaluation, clustersA, ts, clusterA, "", before, ""}, 48 {License_Evaluation, clustersA, wayAfter, clusterA, "", ts, ""}, 49 50 // expirations. 51 {License_Evaluation, clustersA, ts, clusterA, "", after, "expired"}, 52 {License_Evaluation, clustersA, after, clusterA, "", wayAfter, "expired"}, 53 {License_NonCommercial, clustersA, after, clusterA, "", wayAfter, "expired"}, 54 {License_NonCommercial, clustersA, t0, clusterA, "", wayAfter, ""}, 55 {License_Evaluation, clustersA, t0, clusterA, "", wayAfter, ""}, 56 57 // grace period. 58 {License_Enterprise, clustersA, after, clusterA, "", wayAfter, ""}, 59 60 // mismatch. 61 {License_Enterprise, clustersA, ts, clusterB, "", ts, "not valid for cluster"}, 62 {License_Enterprise, clustersB, ts, clusterA, "", ts, "not valid for cluster"}, 63 {License_Enterprise, append(clustersB, clusterA), ts, clusterA, "", ts, ""}, 64 {License_Enterprise, nil, ts, clusterA, "", ts, "license valid only for"}, 65 {License_Enterprise, nil, ts, clusterA, "tc-17", ts, ""}, 66 } { 67 var lic *License 68 if tc.licType != -1 { 69 s, err := License{ 70 ClusterID: tc.grantedTo, 71 ValidUntilUnixSec: tc.expiration.Unix(), 72 Type: tc.licType, 73 OrganizationName: fmt.Sprintf("tc-%d", i), 74 }.Encode() 75 if err != nil { 76 t.Fatal(err) 77 } 78 79 lic, err = Decode(s) 80 if err != nil { 81 t.Fatal(err) 82 } 83 } 84 if err := lic.Check( 85 tc.checkTime, tc.checkCluster, tc.checkOrg, "", 86 ); !testutils.IsError(err, tc.err) { 87 t.Fatalf("%d: lic for %s to %s, checked by %s at %s.\n got %q", i, 88 tc.grantedTo, tc.expiration, tc.checkCluster, tc.checkTime, err) 89 } 90 } 91 } 92 93 func TestBadLicenseStrings(t *testing.T) { 94 for _, tc := range []struct{ lic, err string }{ 95 {"blah", "invalid license string"}, 96 {"crl-0-&&&&&", "invalid license string"}, 97 {"crl-0-blah", "invalid license string"}, 98 } { 99 if _, err := Decode(tc.lic); !testutils.IsError(err, tc.err) { 100 t.Fatalf("%q: expected err %q, got %v", tc.lic, tc.err, err) 101 } 102 } 103 } 104 105 func TestExpiredLicenseLanguage(t *testing.T) { 106 lic := License{ 107 Type: License_Evaluation, 108 ValidUntilUnixSec: 1, 109 } 110 err := lic.Check(timeutil.Now(), uuid.MakeV4(), "", "RESTORE") 111 expected := "Use of RESTORE requires an enterprise license. Your evaluation license expired on " + 112 "January 1, 1970. If you're interested in getting a new license, please contact " + 113 "subscriptions@cockroachlabs.com and we can help you out." 114 if err == nil || err.Error() != expected { 115 t.Fatalf("expected err %q, got %v", expected, err) 116 } 117 }