summaryrefslogtreecommitdiffstats
path: root/etherpad/src/etherpad/testing/unit_tests/t0002_license_generation.js
blob: 67c79d8209a5c345a82372a2c362626aa127bdf4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/**
 * Copyright 2009 Google Inc.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import("stringutils");
import("sqlbase.sqlobj");

import("etherpad.licensing");

jimport("java.util.Random");

function run() {
  var r = new Random(0);

  function testLicense(name, org, expires, editionId, userQuota) {
    function keydataString() {
      return "{name: "+name+", org: "+org+", expires: "+expires+", editionId: "+editionId+", userQuota: "+userQuota+"}";
    }
    var key = licensing.generateNewKey(name, org, expires, editionId, userQuota);
    var info = licensing.decodeLicenseInfoFromKey(key);
    if (!info) {
      println("Generated key does not decode at all: "+keydataString());
      println("   generated key: "+key);
      throw new Error("Generated key does not decode at all.  See stdout.");
    }
    function testMatch(name, x, y) {
      if (x != y) {
        println("key match error ("+name+"): ["+x+"] != ["+y+"]");
        println("   key data: "+keydataString());
        println("   generated key: "+key);
        println("   decoded key: "+info.toSource());
        throw new Error(name+" mismatch.  see stdout.");
      }
    }
    testMatch("personName", info.personName, name);
    testMatch("orgName", info.organizationName, org);
    testMatch("expires", +info.expiresDate, +expires);
    testMatch("editionName", info.editionName, licensing.getEditionName(editionId));
    testMatch("userQuota", +info.userQuota, +userQuota);
  }

  testLicense("aaron", "test", +(new Date)+1000*60*60*24*30, licensing.getEditionId('PRIVATE_NETWORK_EVALUATION'), 1001);

  for (var editionId = 0; editionId < 3; editionId++) {
    for (var unlimitedUsers = 0; unlimitedUsers <= 1; unlimitedUsers++) {
      for (var noExpiry = 0; noExpiry <= 1; noExpiry++) {
        for (var j = 0; j < 100; j++) {
          var name = stringutils.randomString(1+r.nextInt(39));
          var org = stringutils.randomString(1+r.nextInt(39));
          var expires = null;
          if (noExpiry == 0) {
            expires = +(new Date)+(1000*60*60*24*r.nextInt(100));
          }
          var userQuota = -1;
          if (unlimitedUsers == 1) {
            userQuota = r.nextInt(1e6);
          }

          testLicense(name, org, expires, editionId, userQuota);
        }
      }
    }
  }

  // test that all previously generated keys continue to decode.
  var historicalKeys = sqlobj.selectMulti('eepnet_signups', {}, {});
  historicalKeys.forEach(function(d) {
    var key = d.licenseKey;
    if (key && !licensing.isValidKey(key)) {
      throw new Error("Historical license key no longer validates: "+key);
    }
  });

}