From 727ca33ff7b0eddad90650615b636a2e7b909198 Mon Sep 17 00:00:00 2001 From: Artem Los Date: Wed, 6 Mar 2019 10:25:39 +0100 Subject: [PATCH] Update HasFeature to use JSON array all the way --- .../java/io/cryptolens/methods/Helpers.java | 24 +++++++++---------- src/test/java/io/cryptolens/HelpersTest.java | 8 +++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/main/java/io/cryptolens/methods/Helpers.java b/src/main/java/io/cryptolens/methods/Helpers.java index c7b163b..467714b 100644 --- a/src/main/java/io/cryptolens/methods/Helpers.java +++ b/src/main/java/io/cryptolens/methods/Helpers.java @@ -1,9 +1,8 @@ package io.cryptolens.methods; -import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonParser; -import com.google.gson.reflect.TypeToken; + import io.cryptolens.internal.BasicResult; import io.cryptolens.models.ActivatedMachine; import io.cryptolens.models.LicenseKey; @@ -13,11 +12,9 @@ import oshi.hardware.HardwareAbstractionLayer; import oshi.software.os.OperatingSystem; -import java.lang.reflect.Type; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; -import java.util.HashMap; -import java.util.HashSet; + /** * A collection of helper methods that operate on a license key. @@ -209,10 +206,11 @@ public class MyClass /** *

Uses the notes field to determine if a certain feature exists (instead of the 8 feature flags).

- *

Formatting:

The notes field needs to be formatted as a JSON array of strings or objects that contain

- * For example, ["f1", "f2"] means f1 and f2 are true. You can also have feature bundling, eg. ["f1", {"f2": ["voice","image"]}], + *

Formatting:

The notes field needs to be formatted as a JSON array of strings or of JSON arrays + * where the first element specifies the feature name and the second element is a list of features.

+ * For example, ["f1", "f2"] means f1 and f2 are true. You can also have feature bundling, eg. ["f1", ["f2",["voice","image"]]], * which means that f1 and f2 are true, as well as f2.limited and f2.image. You can set any depth, eg. you can have - * ["f1", {"f2":[{"voice":["all"]}, "image"]}] means f2.voice.all is true as well as f2.voice and f2. + * ["f1", ["f2",[["voice",["all"]], "image"]]] means f2.voice.all is true as well as f2.voice and f2. * The dots symbol is used to specify the "sub-features". *

* @param licenseKey a license key object. @@ -227,13 +225,14 @@ public static boolean HasFeature(LicenseKey licenseKey, String featureName) { boolean found = false; for(int i = 0; i < featurePath.length; i++) { + found = false; int index = -1; for(int j = 0; j < array.size(); j++) { - if(!array.get(j).isJsonObject() && array.get(j).getAsString().equals(featurePath[i])) { + if(!array.get(j).isJsonArray() && array.get(j).getAsString().equals(featurePath[i])) { found = true; break; - } else if (array.get(j).isJsonObject() && array.get(j).getAsJsonObject().keySet().contains(featurePath[i])){ + } else if (array.get(j).isJsonArray() && array.get(j).getAsJsonArray().get(0).getAsString().equals(featurePath[i])){ found = true; index = j; break; @@ -244,8 +243,9 @@ public static boolean HasFeature(LicenseKey licenseKey, String featureName) { } if(i + 1 < featurePath.length && index != -1) { // still have some sub features to go through. - array = array.get(index).getAsJsonObject().get(featurePath[i]).getAsJsonArray(); - found = false; + // TODO: need to check if it's actually a json array or null? + // TODO: try catch + array = array.get(index).getAsJsonArray().get(1).getAsJsonArray(); } } diff --git a/src/test/java/io/cryptolens/HelpersTest.java b/src/test/java/io/cryptolens/HelpersTest.java index fad510d..dbb2e15 100644 --- a/src/test/java/io/cryptolens/HelpersTest.java +++ b/src/test/java/io/cryptolens/HelpersTest.java @@ -36,17 +36,21 @@ public static Test suite() public void testApp() { LicenseKey license = new LicenseKey(); - license.Notes = "[\"test\", {\"module\":[\"A\"]}]"; + license.Notes = "[\"test\", [\"module\",[\"A\"]]]"; assertTrue(Helpers.HasFeature(license, "test")); assertTrue(Helpers.HasFeature(license, "module")); assertTrue(Helpers.HasFeature(license, "module.A")); assertFalse(Helpers.HasFeature(license, "module.B")); - license.Notes = "[\"f1\", {\"f2\":[{\"voice\":[\"all\"]}]}]"; + license.Notes = "[\"f1\", [\"f2\",[[\"voice\",[\"all\"]]]]]"; assertTrue(Helpers.HasFeature(license, "f2.voice.all")); + assertFalse(Helpers.HasFeature(license, "f2.voice.all.test")); + assertFalse(Helpers.HasFeature(license, "f2.voice.all.test.a")); + assertFalse(Helpers.HasFeature(license, "f2.A.all.test")); + assertFalse(Helpers.HasFeature(license, "aa.voice.all.test")); String[] featurePath = "moduleA.video".split(".");