aboutsummaryrefslogtreecommitdiffstats
path: root/client-compiler/tests/src/com/vaadin/tools/CvalCheckerTest.java
blob: 51b12f4c7ee52739b7da5a3c4081fa140f969c38 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
 * Copyright 2000-2014 Vaadin Ltd.
 *
 * 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.
 */

package com.vaadin.tools;

import static com.vaadin.tools.CvalAddonsChecker.VAADIN_ADDON_LICENSE;
import static com.vaadin.tools.CvalAddonsChecker.VAADIN_ADDON_NAME;
import static com.vaadin.tools.CvalAddonsChecker.VAADIN_ADDON_TITLE;
import static com.vaadin.tools.CvalAddonsChecker.VAADIN_ADDON_VERSION;
import static com.vaadin.tools.CvalChecker.GRACE_DAYS_MSECS;
import static com.vaadin.tools.CvalChecker.cacheLicenseInfo;
import static com.vaadin.tools.CvalChecker.deleteCache;
import static com.vaadin.tools.CvalChecker.parseJson;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import java.util.prefs.Preferences;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.tools.CvalChecker.CvalInfo;
import com.vaadin.tools.CvalChecker.CvalServer;
import com.vaadin.tools.CvalChecker.InvalidCvalException;
import com.vaadin.tools.CvalChecker.UnreachableCvalServerException;

/**
 * The CvalChecker test.
 */
public class CvalCheckerTest {

    static final String productNameCval = "test.cval";
    static final String productTitleCval = "Vaadin Test";
    static final String productNameAgpl = "test.agpl";
    static final String productTitleAgpl = "Vaadin Test";
    static final String productNameApache = "test.apache";
    static final String VALID_KEY = "valid";
    static final String INVALID_KEY = "invalid";

    static final String responseJson = "{'licenseKey':'" + VALID_KEY + "',"
            + "'licensee':'Test User','type':'normal',"
            + "'expiredEpoch':'1893511225000'," + "'product':{'name':'"
            + productNameCval + "', 'version': 2}}";

    private static ByteArrayOutputStream outContent;

    // A provider returning a valid license if productKey is valid or null if
    // invalid
    static final CvalServer validLicenseProvider = new CvalServer() {
        @Override
        String askServer(String productName, String productKey, int timeout) {
            return VALID_KEY.equals(productKey) ? responseJson : null;
        }
    };
    // A provider returning a valid evaluation license
    static final CvalServer validEvaluationLicenseProvider = new CvalServer() {
        @Override
        String askServer(String productName, String productKey, int timeout) {
            return responseJson.replace("normal", "evaluation");
        }
    };
    // A provider returning an expired license with a server message
    static final CvalServer expiredLicenseProviderWithMessage = new CvalServer() {
        @Override
        String askServer(String productName, String productKey, int timeout) {
            return responseJson
                    .replace("'expired",
                            "'message':'Custom\\\\nServer\\\\nMessage','expired':true,'expired");
        }
    };
    // A provider returning an expired license with a server message
    static final CvalServer expiredLicenseProvider = new CvalServer() {
        @Override
        String askServer(String productName, String productKey, int timeout) {
            return responseJson.replace("'expired", "'expired':true,'expired");
        }
    };
    // A provider returning an expired epoch license
    static final CvalServer expiredEpochLicenseProvider = new CvalServer() {
        @Override
        String askServer(String productName, String productKey, int timeout) {
            long ts = System.currentTimeMillis() - GRACE_DAYS_MSECS - 1000;
            return responseJson.replace("1893511225000", "" + ts);
        }
    };
    // A provider returning an unlimited license
    static final CvalServer unlimitedLicenseProvider = new CvalServer() {
        @Override
        String askServer(String productName, String productKey, int timeout) {
            return responseJson.replaceFirst("1893511225000", "");
        }
    };
    // An unreachable provider
    static final CvalServer unreachableLicenseProvider = new CvalServer() {
        @Override
        String askServer(String productName, String productKey, int timeout)
                throws IOException {
            // Normally there is no route for this ip in public routers, so we
            // should get a timeout.
            licenseUrl = "http://localhost:9999/";
            return super.askServer(productName, productKey, 1000);
        }
    };

    private CvalChecker licenseChecker;
    private String licenseName;

    @Before
    public void setup() {
        licenseChecker = new CvalChecker()
                .setLicenseProvider(validLicenseProvider);
        licenseName = CvalChecker.computeLicenseName(productNameCval);
        System.getProperties().remove(licenseName);
        deleteCache(productNameCval);
    }

    @Test
    public void testValidateProduct() throws Exception {
        deleteCache(productNameCval);

        // If the license key in our environment is null, throw an exception
        try {
            licenseChecker.validateProduct(productNameCval, "2.1",
                    productTitleCval);
            Assert.fail();
        } catch (InvalidCvalException expected) {
            Assert.assertEquals(productNameCval, expected.name);
        }
        Assert.assertFalse(cacheExists(productNameCval));

        // If the license key is empty, throw an exception
        System.setProperty(licenseName, "");
        try {
            licenseChecker.validateProduct(productNameCval, "2.1",
                    productTitleCval);
            Assert.fail();
        } catch (InvalidCvalException expected) {
            Assert.assertEquals(productNameCval, expected.name);
        }
        Assert.assertFalse(cacheExists(productNameCval));

        // If license key is invalid, throw an exception
        System.setProperty(licenseName, "invalid");
        try {
            licenseChecker.validateProduct(productNameCval, "2.1",
                    productTitleCval);
            Assert.fail();
        } catch (InvalidCvalException expected) {
            Assert.assertEquals(productNameCval, expected.name);
        }
        Assert.assertFalse(cacheExists(productNameCval));

        // Fail if version is bigger
        System.setProperty(licenseName, VALID_KEY);
        try {
            licenseChecker.validateProduct(productNameCval, "3.0",
                    productTitleCval);
            Assert.fail();
        } catch (InvalidCvalException expected) {
            Assert.assertEquals(productNameCval, expected.name);
        }
        Assert.assertFalse(cacheExists(productNameCval));

        // Success if license key and version are valid
        System.setProperty(licenseName, VALID_KEY);
        licenseChecker
                .validateProduct(productNameCval, "2.1", productTitleCval);
        Assert.assertTrue(cacheExists(productNameCval));

        // Success if license and cache file are valid, although the license
        // server is offline
        licenseChecker.setLicenseProvider(unreachableLicenseProvider);
        licenseChecker
                .validateProduct(productNameCval, "2.1", productTitleCval);
        Assert.assertTrue(cacheExists(productNameCval));

        // Fail if license key changes although cache file were validated
        // previously and it is ok, we are offline
        try {
            System.setProperty(licenseName, INVALID_KEY);
            licenseChecker.validateProduct(productNameCval, "2.1",
                    productTitleCval);
            Assert.fail();
        } catch (InvalidCvalException expected) {
            Assert.fail();
        } catch (UnreachableCvalServerException expected) {
            Assert.assertEquals(productNameCval, expected.name);
        }
        Assert.assertFalse(cacheExists(productNameCval));

        // Fail with unreachable exception if license has never verified and
        // server is offline
        try {
            System.setProperty(licenseName, VALID_KEY);
            licenseChecker.validateProduct(productNameCval, "2.1",
                    productTitleCval);
            Assert.fail();
        } catch (InvalidCvalException expected) {
            Assert.fail();
        } catch (UnreachableCvalServerException expected) {
            Assert.assertEquals(productNameCval, expected.name);
        }
        Assert.assertFalse(cacheExists(productNameCval));

        // Fail when expired flag comes in the server response, although the
        // expired is valid.
        deleteCache(productNameCval);
        licenseChecker.setLicenseProvider(expiredLicenseProviderWithMessage);
        try {
            licenseChecker.validateProduct(productNameCval, "2.1",
                    productTitleCval);
            Assert.fail();
        } catch (InvalidCvalException expected) {
            Assert.assertEquals(productNameCval, expected.name);
            // Check that we use server customized message if it comes
            Assert.assertTrue(expected.getMessage().contains("Custom"));
        }
        Assert.assertTrue(cacheExists(productNameCval));

        // Check an unlimited license
        licenseChecker.setLicenseProvider(unlimitedLicenseProvider);
        licenseChecker
                .validateProduct(productNameCval, "2.1", productTitleCval);
        Assert.assertTrue(cacheExists(productNameCval));

        // Fail if expired flag does not come, but expired epoch is in the past
        System.setProperty(licenseName, VALID_KEY);
        deleteCache(productNameCval);
        licenseChecker.setLicenseProvider(expiredEpochLicenseProvider);
        try {
            licenseChecker.validateProduct(productNameCval, "2.1",
                    productTitleCval);
            Assert.fail();
        } catch (InvalidCvalException expected) {
            Assert.assertEquals(productNameCval, expected.name);
        }
        Assert.assertTrue(cacheExists(productNameCval));
    }

    /*
     * Creates a new .jar file with a MANIFEST.MF with all vaadin license info
     * attributes set, and add the .jar to the classpath
     */
    static void addLicensedJarToClasspath(String productName, String licenseType)
            throws Exception {
        // Create a manifest with Vaadin CVAL license
        Manifest testManifest = new Manifest();
        testManifest.getMainAttributes().putValue("Manifest-Version", "1.0");
        testManifest.getMainAttributes().putValue(VAADIN_ADDON_LICENSE,
                licenseType);
        testManifest.getMainAttributes().putValue(VAADIN_ADDON_NAME,
                productName);
        testManifest.getMainAttributes().putValue(VAADIN_ADDON_TITLE,
                "Test " + productName);
        testManifest.getMainAttributes().putValue(VAADIN_ADDON_VERSION, "2");

        // Create a temporary Jar
        String tmpDir = System.getProperty("java.io.tmpdir");
        File testJarFile = new File(tmpDir + "vaadin." + productName + ".jar");
        testJarFile.deleteOnExit();
        JarOutputStream target = new JarOutputStream(new FileOutputStream(
                testJarFile), testManifest);
        target.close();

        // Add the new jar to our classpath (use reflection)
        URL url = new URL("file://" + testJarFile.getAbsolutePath());
        final Method addURL = URLClassLoader.class.getDeclaredMethod("addURL",
                new Class[] { URL.class });
        addURL.setAccessible(true);
        final URLClassLoader urlClassLoader = (URLClassLoader) Thread
                .currentThread().getContextClassLoader();
        addURL.invoke(urlClassLoader, new Object[] { url });
    }

    static boolean cacheExists(String productName) {
        return cachedPreferences(productName) != null;
    }

    static String cachedPreferences(String productName) {
        // ~/Library/Preferences/com.apple.java.util.prefs.plist
        // .java/.userPrefs/com/google/gwt/dev/shell/prefs.xml
        // HKEY_CURRENT_USER\SOFTWARE\JavaSoft\Prefs
        Preferences p = Preferences.userNodeForPackage(CvalInfo.class);
        return p.get(productName, null);
    }

    static void saveCache(String productName, String key, Boolean expired,
            Long expireTs, String type) {
        String json = responseJson.replace(productNameCval, productName);
        if (expired != null && expired) {
            expireTs = System.currentTimeMillis() - GRACE_DAYS_MSECS - 1000;
        }
        if (expireTs != null) {
            json = json.replace("1893511225000", "" + expireTs);
        }
        if (key != null) {
            json = json.replace(VALID_KEY, key);
        }
        if (type != null) {
            json = json.replace("normal", type);

        }
        cacheLicenseInfo(parseJson(json));
    }

    static void captureSystemOut() {
        outContent = new ByteArrayOutputStream();
        System.setOut(new PrintStream(outContent));
    }

    static String readSystemOut() {
        restoreSystemOut();
        return outContent.toString();
    }

    static void restoreSystemOut() {
        System.setOut(new PrintStream(new FileOutputStream(FileDescriptor.out)));
    }
}