diff --git a/src/main/java/com/uid2/client/KeyContainer.java b/src/main/java/com/uid2/client/KeyContainer.java index f7ef2e7..0d204fa 100644 --- a/src/main/java/com/uid2/client/KeyContainer.java +++ b/src/main/java/com/uid2/client/KeyContainer.java @@ -116,19 +116,19 @@ public long getTokenExpirySeconds() { return tokenExpirySeconds; } - public long getMaxBidstreamLifetimeSeconds() { + long getMaxBidstreamLifetimeSeconds() { return maxBidstreamLifetimeSeconds; } - public long getMaxSharingLifetimeSeconds() { + long getMaxSharingLifetimeSeconds() { return maxSharingLifetimeSeconds; } - public long getAllowClockSkewSeconds() { + long getAllowClockSkewSeconds() { return allowClockSkewSeconds; } - public IdentityScope getIdentityScope() { + IdentityScope getIdentityScope() { return identityScope; } } diff --git a/src/main/java/com/uid2/client/PrivacyBits.java b/src/main/java/com/uid2/client/PrivacyBits.java index 51d6b23..7196df8 100644 --- a/src/main/java/com/uid2/client/PrivacyBits.java +++ b/src/main/java/com/uid2/client/PrivacyBits.java @@ -2,7 +2,7 @@ import java.util.BitSet; -public class PrivacyBits { + class PrivacyBits { // Bit 0 is legacy and is no longer in use private final int bitClientSideGenerated = 1; diff --git a/src/main/java/com/uid2/client/TokenHelper.java b/src/main/java/com/uid2/client/TokenHelper.java index 36ee9bf..423686e 100644 --- a/src/main/java/com/uid2/client/TokenHelper.java +++ b/src/main/java/com/uid2/client/TokenHelper.java @@ -5,7 +5,7 @@ import java.time.Instant; import java.util.concurrent.atomic.AtomicReference; -public class TokenHelper { +class TokenHelper { private final Uid2Helper uid2Helper; private final Uid2ClientHelper uid2ClientHelper; private final AtomicReference container = new AtomicReference<>(null);; diff --git a/src/test/java/com/uid2/client/AdvertisingTokenBuilder.java b/src/test/java/com/uid2/client/AdvertisingTokenBuilder.java new file mode 100644 index 0000000..6261f0a --- /dev/null +++ b/src/test/java/com/uid2/client/AdvertisingTokenBuilder.java @@ -0,0 +1,105 @@ +package com.uid2.client; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; +import java.util.Base64; + +import static com.uid2.client.TestData.*; + +class AdvertisingTokenBuilder { + TokenVersionForTesting version = TokenVersionForTesting.V4; + String rawUid = EXAMPLE_UID; + Key masterKey = MASTER_KEY; + Key siteKey = SITE_KEY; + int siteId = SITE_ID; + int privacyBits = PrivacyBitsBuilder.Builder().WithAllFlagsDisabled().Build(); + Instant expiry = Instant.now().plus(1, ChronoUnit.HOURS); + IdentityScope identityScope = IdentityScope.UID2; + Instant generated = Instant.now(); + + static AdvertisingTokenBuilder builder() { + return new AdvertisingTokenBuilder(); + } + + AdvertisingTokenBuilder withVersion(TokenVersionForTesting version) + { + this.version = version; + return this; + } + + AdvertisingTokenBuilder withRawUid(String rawUid) + { + this.rawUid = rawUid; + return this; + } + + AdvertisingTokenBuilder withMasterKey(Key masterKey) + { + this.masterKey = masterKey; + return this; + } + + AdvertisingTokenBuilder withSiteKey(Key siteKey) + { + this.siteKey = siteKey; + return this; + } + + AdvertisingTokenBuilder withPrivacyBits(int privacyBits) + { + this.privacyBits = privacyBits; + return this; + } + + AdvertisingTokenBuilder withExpiry(Instant expiry) + { + this.expiry = expiry; + return this; + } + + AdvertisingTokenBuilder withScope(IdentityScope identityScope) + { + this.identityScope = identityScope; + return this; + } + + AdvertisingTokenBuilder withGenerated(Instant generated) + { + this.generated = generated; + return this; + } + + + String build() throws Exception { + Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams().WithPrivacyBits(privacyBits).withTokenExpiry(expiry).WithTokenGenerated(generated); + + params.identityScope = identityScope.value; + String token; + switch (version) { + case V2: + token = Base64.getEncoder().encodeToString(Uid2TokenGenerator.generateUid2TokenV2(rawUid, MASTER_KEY, SITE_ID, SITE_KEY, params)); + break; + case V3: + token = Uid2TokenGenerator.generateUid2TokenV3(rawUid, MASTER_KEY, SITE_ID, SITE_KEY, params); + break; + case V4: + token = Uid2TokenGenerator.generateUid2TokenV4(rawUid, MASTER_KEY, SITE_ID, SITE_KEY, params); + break; + default: + throw new Uid2Exception("Invalid token UID2 version: " + version); + } + + IdentityType identityType = IdentityType.Email; + if (version != TokenVersionForTesting.V2) + { + char firstChar = rawUid.charAt(0); + if (firstChar == 'F' || firstChar == 'B') + identityType = IdentityType.Phone; + } + + + EncryptionTestsV4.validateAdvertisingToken(token, identityScope, identityType, version); + return token; + } +} + diff --git a/src/test/java/com/uid2/client/AdvertisingTokenBuilderTest.java b/src/test/java/com/uid2/client/AdvertisingTokenBuilderTest.java new file mode 100644 index 0000000..1df5b90 --- /dev/null +++ b/src/test/java/com/uid2/client/AdvertisingTokenBuilderTest.java @@ -0,0 +1,92 @@ +package com.uid2.client; + +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.time.temporal.ChronoUnit; + +import static com.uid2.client.TestData.*; +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class AdvertisingTokenBuilderTest { + private final int V2TokenLength = 180; + private final int V3TokenLength = 220; + private final int V4TokenLength = 218; + private final Key someKey = new Key(75, 10, Instant.now().plus(42, ChronoUnit.SECONDS), Instant.now().plus(42, ChronoUnit.HOURS), Instant.now().plus(42, ChronoUnit.HOURS), getMasterSecret()); + + + @Test + public void createsTokenOfDesiredVersion_V2() throws Exception { + String token = AdvertisingTokenBuilder.builder().withVersion(TokenVersionForTesting.V2).build(); + assertEquals(V2TokenLength, token.length()); + } + + @Test + public void createsTokenOfDesiredVersion_V3() throws Exception { + String token = AdvertisingTokenBuilder.builder().withVersion(TokenVersionForTesting.V3).build(); + assertEquals(V3TokenLength, token.length()); + } + + @Test + public void createsTokenOfDesiredVersion_V4() throws Exception { + String token = AdvertisingTokenBuilder.builder().withVersion(TokenVersionForTesting.V4).build(); + assertEquals(V4TokenLength, token.length()); + } + + @Test + public void createsTokenOfDesiredVersion_ByDefaultCreatesV4Token() throws Exception { + String token = AdvertisingTokenBuilder.builder().build(); + assertEquals(V4TokenLength, token.length()); + } + + @Test + public void builderSetterTests_Version() { + AdvertisingTokenBuilder builder = AdvertisingTokenBuilder.builder().withVersion(TokenVersionForTesting.V3); + assertEquals(TokenVersionForTesting.V3, builder.version); + } + + @Test + public void builderSetterTests_RawUid() { + String rawUid = "raw uid"; + AdvertisingTokenBuilder builder = AdvertisingTokenBuilder.builder().withRawUid(rawUid); + assertEquals(rawUid, builder.rawUid); + } + + @Test + public void builderSetterTests_MasterKey() { + AdvertisingTokenBuilder builder = AdvertisingTokenBuilder.builder().withMasterKey(someKey); + assertEquals(someKey, builder.masterKey); + } + + @Test + public void builderSetterTests_SiteKey() { + AdvertisingTokenBuilder builder = AdvertisingTokenBuilder.builder().withSiteKey(someKey); + assertEquals(someKey, builder.siteKey); + } + + @Test + public void builderSetterTests_SiteId() { + AdvertisingTokenBuilder builder = AdvertisingTokenBuilder.builder(); + assertEquals(SITE_ID, builder.siteId); + } + + @Test + public void builderSetterTests_PrivacyBits() { + int privacyBits = 42; + AdvertisingTokenBuilder builder = AdvertisingTokenBuilder.builder().withPrivacyBits(privacyBits); + assertEquals(privacyBits, builder.privacyBits); + } + + @Test + public void builderSetterTests_Expiry() { + Instant expiry = Instant.now().plus(42, ChronoUnit.HOURS); + AdvertisingTokenBuilder builder = AdvertisingTokenBuilder.builder().withExpiry(expiry); + assertEquals(expiry, builder.expiry); + } + + @Test + public void builderSetterTests_Scope() { + AdvertisingTokenBuilder builder = AdvertisingTokenBuilder.builder(); + assertEquals(IdentityScope.UID2, builder.identityScope); + } +} diff --git a/src/test/java/com/uid2/client/BidstreamClientTests.java b/src/test/java/com/uid2/client/BidstreamClientTests.java index 73ca46c..a1e802f 100644 --- a/src/test/java/com/uid2/client/BidstreamClientTests.java +++ b/src/test/java/com/uid2/client/BidstreamClientTests.java @@ -2,7 +2,6 @@ import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import com.uid2.client.builder.PrivacyBitsBuilder; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -14,7 +13,6 @@ import java.time.Instant; import java.time.temporal.ChronoUnit; import java.util.Arrays; -import java.util.Base64; import java.util.stream.Stream; import static com.uid2.client.EncryptionTestsV4.validateAdvertisingToken; @@ -28,15 +26,15 @@ public class BidstreamClientTests { @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void smokeTest(IdentityScope identityScope, int tokenVersion) throws Exception { - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, Uid2TokenGenerator.defaultParams()); + public void smokeTest(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { + String advertisingToken = AdvertisingTokenBuilder.builder().withScope(identityScope).withVersion(tokenVersion).build(); callAndVerifyRefreshJson(identityScope); decryptAndAssertSuccess(advertisingToken, tokenVersion); @@ -44,36 +42,35 @@ public void smokeTest(IdentityScope identityScope, int tokenVersion) throws Exce @ParameterizedTest @CsvSource({ - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void phoneTest(IdentityScope identityScope, int tokenVersion) throws Exception { + public void phoneTest(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { String rawUidPhone = "BEOGxroPLdcY7LrSiwjY52+X05V0ryELpJmoWAyXiwbZ"; - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, rawUidPhone, Uid2TokenGenerator.defaultParams()); + String advertisingToken = AdvertisingTokenBuilder.builder().withRawUid(rawUidPhone).withScope(identityScope).withVersion(tokenVersion).build(); callAndVerifyRefreshJson(identityScope); DecryptionResponse decryptionResponse = bidstreamClient.decryptTokenIntoRawUid(advertisingToken, null); assertTrue(decryptionResponse.isSuccess()); assertEquals(rawUidPhone, decryptionResponse.getUid()); - assertEquals(tokenVersion, decryptionResponse.getAdvertisingTokenVersion()); + assertEquals(tokenVersion.ordinal() + 2, decryptionResponse.getAdvertisingTokenVersion()); assertEquals(IdentityType.Phone, decryptionResponse.getIdentityType()); } @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void tokenLifetimeTooLongForBidstream(IdentityScope identityScope, int tokenVersion) throws Exception { - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenExpiry = Instant.now().plus(3, ChronoUnit.DAYS).plus(1, ChronoUnit.MINUTES); - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, params); + public void tokenLifetimeTooLongForBidstream(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { + Instant tokenExpiry = Instant.now().plus(3, ChronoUnit.DAYS).plus(1, ChronoUnit.MINUTES); + String advertisingToken = AdvertisingTokenBuilder.builder().withExpiry(tokenExpiry).withScope(identityScope).withVersion(tokenVersion).build(); callAndVerifyRefreshJson(identityScope); DecryptionResponse decryptionResponse = bidstreamClient.decryptTokenIntoRawUid(advertisingToken, null); @@ -82,17 +79,16 @@ public void tokenLifetimeTooLongForBidstream(IdentityScope identityScope, int to @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void tokenGeneratedInTheFutureToSimulateClockSkew(IdentityScope identityScope, int tokenVersion) throws Exception { - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenGenerated = Instant.now().plus(31, ChronoUnit.MINUTES); - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, params); + public void tokenGeneratedInTheFutureToSimulateClockSkew(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { + Instant tokenGenerated = Instant.now().plus(31, ChronoUnit.MINUTES); + String advertisingToken = AdvertisingTokenBuilder.builder().withGenerated(tokenGenerated).withScope(identityScope).withVersion(tokenVersion).build(); callAndVerifyRefreshJson(identityScope); DecryptionResponse decryptionResponse = bidstreamClient.decryptTokenIntoRawUid(advertisingToken, null); @@ -101,48 +97,46 @@ public void tokenGeneratedInTheFutureToSimulateClockSkew(IdentityScope identityS @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void tokenGeneratedInTheFutureWithinAllowedClockSkew(IdentityScope identityScope, int tokenVersion) throws Exception { - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenGenerated = Instant.now().plus(30, ChronoUnit.MINUTES); - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, params); + public void tokenGeneratedInTheFutureWithinAllowedClockSkew(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { + Instant tokenGenerated = Instant.now().plus(30, ChronoUnit.MINUTES); + String advertisingToken = AdvertisingTokenBuilder.builder().withGenerated(tokenGenerated).withScope(identityScope).withVersion(tokenVersion).build(); callAndVerifyRefreshJson(identityScope); decryptAndAssertSuccess(advertisingToken, tokenVersion); } @ParameterizedTest - @ValueSource(ints = {2, 3, 4}) - public void legacyResponseFromOldOperator(int tokenVersion) throws Exception { + @ValueSource(strings = {"V2", "V3", "V4"}) + public void legacyResponseFromOldOperator(TokenVersionForTesting tokenVersion) throws Exception { RefreshResponse refreshResponse = bidstreamClient.refreshJson(keySetToJsonForSharing(MASTER_KEY, SITE_KEY)); assertTrue(refreshResponse.isSuccess()); - String advertisingToken = getAdvertisingToken(IdentityScope.UID2, tokenVersion, EXAMPLE_UID, Uid2TokenGenerator.defaultParams()); + String advertisingToken = AdvertisingTokenBuilder.builder().withVersion(tokenVersion).build(); decryptAndAssertSuccess(advertisingToken, tokenVersion); } @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void tokenGeneratedInTheFutureLegacyClient(IdentityScope identityScope, int tokenVersion) throws Exception { + public void tokenGeneratedInTheFutureLegacyClient(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { UID2Client client = new UID2Client("ep", "ak", CLIENT_SECRET, identityScope); client.refreshJson(keyBidstreamResponse(identityScope, MASTER_KEY, SITE_KEY)); - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenGenerated = Instant.now().plus(31, ChronoUnit.MINUTES); - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, params); + Instant tokenGenerated = Instant.now().plus(31, ChronoUnit.MINUTES); + String advertisingToken = AdvertisingTokenBuilder.builder().withGenerated(tokenGenerated).withScope(identityScope).withVersion(tokenVersion).build(); DecryptionResponse decryptionResponse = client.decrypt(advertisingToken); assertSuccess(decryptionResponse, tokenVersion); @@ -150,20 +144,19 @@ public void tokenGeneratedInTheFutureLegacyClient(IdentityScope identityScope, i @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void tokenLifetimeTooLongLegacyClient(IdentityScope identityScope, int tokenVersion) throws Exception { + public void tokenLifetimeTooLongLegacyClient(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { UID2Client client = new UID2Client("ep", "ak", CLIENT_SECRET, identityScope); client.refreshJson(keyBidstreamResponse(identityScope, MASTER_KEY, SITE_KEY)); - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenExpiry = Instant.now().plus(3, ChronoUnit.DAYS).plus(1, ChronoUnit.MINUTES); - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, params); + Instant tokenExpiry = Instant.now().plus(3, ChronoUnit.DAYS).plus(1, ChronoUnit.MINUTES); + String advertisingToken = AdvertisingTokenBuilder.builder().withExpiry(tokenExpiry).withScope(identityScope).withVersion(tokenVersion).build(); DecryptionResponse decryptionResponse = client.decrypt(advertisingToken); assertSuccess(decryptionResponse, tokenVersion); @@ -174,7 +167,7 @@ public void tokenLifetimeTooLongLegacyClient(IdentityScope identityScope, int to public void identityScopeAndType_TestCases(String uid, IdentityScope identityScope, IdentityType identityType) throws Exception { callAndVerifyRefreshJson(identityScope); - String advertisingToken = getAdvertisingToken(identityScope, 4, uid, Uid2TokenGenerator.defaultParams()); + String advertisingToken = AdvertisingTokenBuilder.builder().withRawUid(uid).withScope(identityScope).build(); DecryptionResponse res = bidstreamClient.decryptTokenIntoRawUid(advertisingToken, null); assertTrue(res.isSuccess()); assertEquals(uid, res.getUid()); @@ -193,20 +186,18 @@ private static Stream data_IdentityScopeAndType_TestCases() { @ParameterizedTest @CsvSource({ - "example.com, 2", - "example.org, 2", - "example.com, 4", - "example.org, 4", - "example.com, 4", - "example.org, 4" + "example.com, V2", + "example.org, V2", + "example.com, V4", + "example.org, V4", + "example.com, V4", + "example.org, V4" }) - public void TokenIsCstgDerivedTest(String domainName, int tokenVersion) throws Exception { + public void TokenIsCstgDerivedTest(String domainName, TokenVersionForTesting tokenVersion) throws Exception { callAndVerifyRefreshJson(IdentityScope.UID2); int privacyBits = PrivacyBitsBuilder.Builder().WithClientSideGenerated(true).Build(); - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenPrivacyBits = privacyBits; - String advertisingToken = getAdvertisingToken(IdentityScope.UID2, tokenVersion, EXAMPLE_UID, params); + String advertisingToken = AdvertisingTokenBuilder.builder().withVersion(tokenVersion).withPrivacyBits(privacyBits).build(); validateAdvertisingToken(advertisingToken, IdentityScope.UID2, IdentityType.Email, tokenVersion); DecryptionResponse res = bidstreamClient.decryptTokenIntoRawUid(advertisingToken, domainName); @@ -219,14 +210,14 @@ public void TokenIsCstgDerivedTest(String domainName, int tokenVersion) throws E // tests below taken from EncryptionTestsV4.cs above "// Sharing tests" comment (but excluding deprecated EncryptData/DecryptData methods) and modified to use BidstreamClient and the new JSON /key/bidstream response @Test public void emptyKeyContainer() throws Exception { - String advertisingToken = getAdvertisingToken(IdentityScope.UID2, 4, EXAMPLE_UID, Uid2TokenGenerator.defaultParams()); + String advertisingToken = AdvertisingTokenBuilder.builder().build(); DecryptionResponse res = bidstreamClient.decryptTokenIntoRawUid(advertisingToken, null); assertEquals(DecryptionStatus.NOT_INITIALIZED, res.getStatus()); } @Test public void expiredKeyContainer() throws Exception { - String advertisingToken = getAdvertisingToken(IdentityScope.UID2, 4, EXAMPLE_UID, Uid2TokenGenerator.defaultParams()); + String advertisingToken = AdvertisingTokenBuilder.builder().build(); Key masterKeyExpired = new Key(MASTER_KEY_ID, -1, NOW, NOW.minus(2, ChronoUnit.HOURS), NOW.minus(1, ChronoUnit.HOURS), getMasterSecret()); Key siteKeyExpired = new Key(SITE_KEY_ID, SITE_ID, NOW, NOW.minus(2, ChronoUnit.HOURS), NOW.minus(1, ChronoUnit.HOURS), getSiteSecret()); @@ -239,7 +230,7 @@ public void expiredKeyContainer() throws Exception { @Test public void notAuthorizedForMasterKey() throws Exception { - String advertisingToken = getAdvertisingToken(IdentityScope.UID2, 4, EXAMPLE_UID, Uid2TokenGenerator.defaultParams()); + String advertisingToken = AdvertisingTokenBuilder.builder().build(); Key anotherMasterKey = new Key(MASTER_KEY_ID + SITE_KEY_ID + 1, -1, NOW, NOW, NOW.plus(1, ChronoUnit.HOURS), getMasterSecret()); Key anotherSiteKey = new Key(MASTER_KEY_ID + SITE_KEY_ID + 2, SITE_ID, NOW, NOW, NOW.plus(1, ChronoUnit.HOURS), getSiteSecret()); @@ -252,7 +243,7 @@ public void notAuthorizedForMasterKey() throws Exception { @Test public void invalidPayload() throws Exception { - String payload = getAdvertisingToken(IdentityScope.UID2, 4, EXAMPLE_UID, Uid2TokenGenerator.defaultParams()); + String payload = AdvertisingTokenBuilder.builder().build(); byte[] payloadInBytes = Uid2Base64UrlCoder.decode(payload); String advertisingToken = Uid2Base64UrlCoder.encode(Arrays.copyOfRange(payloadInBytes, 0, payloadInBytes.length - 1)); bidstreamClient.refreshJson(keyBidstreamResponse(IdentityScope.UID2, MASTER_KEY, SITE_KEY)); @@ -263,10 +254,10 @@ public void invalidPayload() throws Exception { @Test public void tokenExpiryAndCustomNow() throws Exception { final Instant expiry = Instant.parse("2021-03-22T09:01:02Z"); - final Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams().WithTokenGenerated(expiry.minus(60, ChronoUnit.SECONDS)).withTokenExpiry(expiry); + final Instant generated = expiry.minus(60, ChronoUnit.SECONDS); bidstreamClient.refreshJson(keyBidstreamResponse(IdentityScope.UID2, MASTER_KEY, SITE_KEY)); - String advertisingToken = getAdvertisingToken(IdentityScope.UID2, 4, EXAMPLE_UID, params); + String advertisingToken = AdvertisingTokenBuilder.builder().withExpiry(expiry).withGenerated(generated).build(); DecryptionResponse res = bidstreamClient.decryptTokenIntoRawUid(advertisingToken, null, expiry.plus(1, ChronoUnit.SECONDS)); assertEquals(DecryptionStatus.EXPIRED_TOKEN, res.getStatus()); @@ -280,44 +271,30 @@ private void callAndVerifyRefreshJson(IdentityScope identityScope) { assertTrue(refreshResponse.isSuccess()); } - private void decryptAndAssertSuccess(String token, int tokenVersion) { + private void decryptAndAssertSuccess(String token, TokenVersionForTesting tokenVersion) { DecryptionResponse response = bidstreamClient.decryptTokenIntoRawUid(token, null); assertSuccess(response, tokenVersion); } - public static void assertSuccess(DecryptionResponse response, int tokenVersion) { + public static void assertSuccess(DecryptionResponse response, TokenVersionForTesting tokenVersion) { assertTrue(response.isSuccess()); assertEquals(EXAMPLE_UID, response.getUid()); - assertEquals(tokenVersion, response.getAdvertisingTokenVersion()); - assertEquals(tokenVersion, response.getAdvertisingTokenVersion()); - if (tokenVersion != 2) { + assertEquals(tokenVersion.ordinal() + 2, response.getAdvertisingTokenVersion()); + assertEquals(tokenVersion.ordinal() + 2, response.getAdvertisingTokenVersion()); + if (tokenVersion != TokenVersionForTesting.V2) { assertEquals(IdentityType.Email, response.getIdentityType()); } } - public static void assertFails(DecryptionResponse response, int tokenVersion) { + public static void assertFails(DecryptionResponse response, TokenVersionForTesting tokenVersion) { assertFalse(response.isSuccess()); assertEquals(DecryptionStatus.INVALID_TOKEN_LIFETIME, response.getStatus()); - assertEquals(tokenVersion, response.getAdvertisingTokenVersion()); - if (tokenVersion != 2) { + assertEquals(tokenVersion.ordinal() + 2, response.getAdvertisingTokenVersion()); + if (tokenVersion != TokenVersionForTesting.V2) { assertEquals(IdentityType.Email, response.getIdentityType()); } } - public static String getAdvertisingToken(IdentityScope identityScope, int tokenVersion, String uid, Uid2TokenGenerator.Params params) throws Exception { - params.identityScope = identityScope.value; - switch (tokenVersion) { - case 2: - return Base64.getEncoder().encodeToString(Uid2TokenGenerator.generateUid2TokenV2(uid, MASTER_KEY, SITE_ID, SITE_KEY, params)); - case 3: - return Uid2TokenGenerator.generateUid2TokenV3(uid, MASTER_KEY, SITE_ID, SITE_KEY, params); - case 4: - return Uid2TokenGenerator.generateUid2TokenV4(uid, MASTER_KEY, SITE_ID, SITE_KEY, params); - default: - throw new Uid2Exception("Invalid token UID2 version: " + tokenVersion); - } - } - private static String keyBidstreamResponse(IdentityScope identityScope, Key... keys) { JsonArray keyToJson = new JsonArray(); for(Key key : keys) { diff --git a/src/test/java/com/uid2/client/EncryptionTestsV4.java b/src/test/java/com/uid2/client/EncryptionTestsV4.java index b459baf..bdcc80c 100644 --- a/src/test/java/com/uid2/client/EncryptionTestsV4.java +++ b/src/test/java/com/uid2/client/EncryptionTestsV4.java @@ -92,8 +92,8 @@ public void crossPlatformConsistencyCheck_Decrypt() throws Exception { assertEquals(EXAMPLE_UID, res.getUid()); } - public static void validateAdvertisingToken(String advertisingTokenString, IdentityScope identityScope, IdentityType identityType, int tokenVersion) { - if (tokenVersion == 2) { + public static void validateAdvertisingToken(String advertisingTokenString, IdentityScope identityScope, IdentityType identityType, TokenVersionForTesting tokenVersion) { + if (tokenVersion == TokenVersionForTesting.V2) { assertEquals("Ag", advertisingTokenString.substring(0, 2)); return; } @@ -106,7 +106,7 @@ public static void validateAdvertisingToken(String advertisingTokenString, Ident } String secondChar = advertisingTokenString.substring(1, 2); - if (tokenVersion == 3) + if (tokenVersion == TokenVersionForTesting.V3) { assertEquals("3", secondChar); @@ -123,7 +123,7 @@ public static void validateAdvertisingToken(String advertisingTokenString, Ident private static String generateUid2TokenV4(String uid, Key masterKey, long siteId, Key siteKey, Uid2TokenGenerator.Params params) { String advertisingToken = Uid2TokenGenerator.generateUid2TokenV4(uid, masterKey, siteId, siteKey, params); - validateAdvertisingToken(advertisingToken, IdentityScope.UID2, IdentityType.Email, 4); + validateAdvertisingToken(advertisingToken, IdentityScope.UID2, IdentityType.Email, TokenVersionForTesting.V4); return advertisingToken; } diff --git a/src/test/java/com/uid2/client/builder/PrivacyBitsBuilder.java b/src/test/java/com/uid2/client/PrivacyBitsBuilder.java similarity index 72% rename from src/test/java/com/uid2/client/builder/PrivacyBitsBuilder.java rename to src/test/java/com/uid2/client/PrivacyBitsBuilder.java index 8652353..8ac24ee 100644 --- a/src/test/java/com/uid2/client/builder/PrivacyBitsBuilder.java +++ b/src/test/java/com/uid2/client/PrivacyBitsBuilder.java @@ -1,18 +1,18 @@ -package com.uid2.client.builder; +package com.uid2.client; import java.util.BitSet; -public class PrivacyBitsBuilder { +class PrivacyBitsBuilder { private boolean legacyBit; // first bit, doesn't have a meaning any more private boolean isCstgDerived; private boolean isOptedOut; - public static PrivacyBitsBuilder Builder() + static PrivacyBitsBuilder Builder() { return new PrivacyBitsBuilder(); } - public PrivacyBitsBuilder WithAllFlagsEnabled() + PrivacyBitsBuilder WithAllFlagsEnabled() { legacyBit = true; isCstgDerived = true; @@ -21,7 +21,7 @@ public PrivacyBitsBuilder WithAllFlagsEnabled() return this; } - public PrivacyBitsBuilder WithAllFlagsDisabled() + PrivacyBitsBuilder WithAllFlagsDisabled() { legacyBit = false; isCstgDerived = false; @@ -30,19 +30,19 @@ public PrivacyBitsBuilder WithAllFlagsDisabled() return this; } - public PrivacyBitsBuilder WithClientSideGenerated(boolean isCstgDerived) + PrivacyBitsBuilder WithClientSideGenerated(boolean isCstgDerived) { this.isCstgDerived = isCstgDerived; return this; } - public PrivacyBitsBuilder WithOptedOut(boolean isOptedOut) + PrivacyBitsBuilder WithOptedOut(boolean isOptedOut) { this.isOptedOut = isOptedOut; return this; } - public int Build() + int Build() { return FlagsToInt(new boolean[] { legacyBit, isCstgDerived, isOptedOut }); } @@ -58,7 +58,7 @@ private static int FlagsToInt(boolean[] flags) return bitSetToInt(bitSet); } - public static int bitSetToInt(BitSet bitSet) { + static int bitSetToInt(BitSet bitSet) { int intValue = 0; for (int i = 0; i < bitSet.length(); i++) { intValue += (bitSet.get(i) ? 1 : 0) << i; diff --git a/src/test/java/com/uid2/client/builder/PrivacyBitsBuilderTests.java b/src/test/java/com/uid2/client/PrivacyBitsBuilderTests.java similarity index 94% rename from src/test/java/com/uid2/client/builder/PrivacyBitsBuilderTests.java rename to src/test/java/com/uid2/client/PrivacyBitsBuilderTests.java index b166fc4..d99acc5 100644 --- a/src/test/java/com/uid2/client/builder/PrivacyBitsBuilderTests.java +++ b/src/test/java/com/uid2/client/PrivacyBitsBuilderTests.java @@ -1,5 +1,6 @@ -package com.uid2.client.builder; +package com.uid2.client; +import com.uid2.client.PrivacyBitsBuilder; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; diff --git a/src/test/java/com/uid2/client/PrivacyBitsTests.java b/src/test/java/com/uid2/client/PrivacyBitsTests.java new file mode 100644 index 0000000..6751574 --- /dev/null +++ b/src/test/java/com/uid2/client/PrivacyBitsTests.java @@ -0,0 +1,20 @@ +package com.uid2.client; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class PrivacyBitsTests { + @Test + public void ReadsClientSideGeneratedBit_WhenFalse() { + int bits = PrivacyBitsBuilder.Builder().WithAllFlagsEnabled().WithClientSideGenerated(false).Build(); + assertFalse(new PrivacyBits(bits).isClientSideGenerated()); + } + + @Test + public void ReadsClientSideGeneratedBit_WhenTrue() { + int bits = PrivacyBitsBuilder.Builder().WithAllFlagsEnabled().WithClientSideGenerated(true).Build(); + assertTrue(new PrivacyBits(bits).isClientSideGenerated()); + } +} diff --git a/src/test/java/com/uid2/client/SharingClientTests.java b/src/test/java/com/uid2/client/SharingClientTests.java index 5b52628..9c622d0 100644 --- a/src/test/java/com/uid2/client/SharingClientTests.java +++ b/src/test/java/com/uid2/client/SharingClientTests.java @@ -18,7 +18,7 @@ import static org.junit.jupiter.api.Assertions.*; public class SharingClientTests { - public static SharingClient sharingClient = new SharingClient("ap", "ak", CLIENT_SECRET); + private final SharingClient sharingClient = new SharingClient("ap", "ak", CLIENT_SECRET); private static String keySharingResponse(IdentityScope identityScope, Integer callerSiteId, Integer defaultKeysetId, Integer tokenExpirySeconds, Key... keys) throws IOException { if (callerSiteId == null) { @@ -91,22 +91,24 @@ private static int getKeySetId(int siteId) { } } - private void decryptAndAssertSuccess(String advertisingToken, int tokenVersion) { + private void decryptAndAssertSuccess(String advertisingToken, TokenVersionForTesting tokenVersion) { DecryptionResponse decryptionResponse = sharingClient.decryptTokenIntoRawUid(advertisingToken); assertSuccess(decryptionResponse, tokenVersion); } @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void smokeTest(IdentityScope identityScope, int tokenVersion) throws Exception { - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, Uid2TokenGenerator.defaultParams()); + + + public void smokeTest(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { + String advertisingToken = AdvertisingTokenBuilder.builder().withScope(identityScope).withVersion(tokenVersion).build(); RefreshResponse refreshResponse = sharingClient.refreshJson(keySharingResponse(identityScope, MASTER_KEY, SITE_KEY)); assertTrue(refreshResponse.isSuccess()); @@ -116,17 +118,16 @@ public void smokeTest(IdentityScope identityScope, int tokenVersion) throws Exce @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void tokenLifetimeTooLongForSharing(IdentityScope identityScope, int tokenVersion) throws Exception { - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenExpiry = Instant.now().plus(30, ChronoUnit.DAYS).plus(1, ChronoUnit.MINUTES); - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, params); + public void tokenLifetimeTooLongForSharing(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { + Instant tokenExpiry = Instant.now().plus(30, ChronoUnit.DAYS).plus(1, ChronoUnit.MINUTES); + String advertisingToken = AdvertisingTokenBuilder.builder().withExpiry(tokenExpiry).withScope(identityScope).withVersion(tokenVersion).build(); RefreshResponse refreshResponse = sharingClient.refreshJson(keySharingResponse(identityScope, MASTER_KEY, SITE_KEY)); assertTrue(refreshResponse.isSuccess()); @@ -136,17 +137,16 @@ public void tokenLifetimeTooLongForSharing(IdentityScope identityScope, int toke @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void tokenGeneratedInTheFutureToSimulateClockSkew(IdentityScope identityScope, int tokenVersion) throws Exception { - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenGenerated = Instant.now().plus(31, ChronoUnit.MINUTES); - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, params); + public void tokenGeneratedInTheFutureToSimulateClockSkew(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { + Instant tokenGenerated = Instant.now().plus(31, ChronoUnit.MINUTES); + String advertisingToken = AdvertisingTokenBuilder.builder().withGenerated(tokenGenerated).withScope(identityScope).withVersion(tokenVersion).build(); RefreshResponse refreshResponse = sharingClient.refreshJson(keySharingResponse(identityScope, MASTER_KEY, SITE_KEY)); assertTrue(refreshResponse.isSuccess()); @@ -156,17 +156,16 @@ public void tokenGeneratedInTheFutureToSimulateClockSkew(IdentityScope identityS @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void tokenGeneratedInTheFutureWithinAllowedClockSkew(IdentityScope identityScope, int tokenVersion) throws Exception { - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenGenerated = Instant.now().plus(30, ChronoUnit.MINUTES); - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, params); + public void tokenGeneratedInTheFutureWithinAllowedClockSkew(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { + Instant tokenGenerated = Instant.now().plus(30, ChronoUnit.MINUTES); + String advertisingToken = AdvertisingTokenBuilder.builder().withGenerated(tokenGenerated).withScope(identityScope).withVersion(tokenVersion).build(); RefreshResponse refreshResponse = sharingClient.refreshJson(keySharingResponse(identityScope, MASTER_KEY, SITE_KEY)); assertTrue(refreshResponse.isSuccess()); @@ -175,51 +174,50 @@ public void tokenGeneratedInTheFutureWithinAllowedClockSkew(IdentityScope identi @ParameterizedTest @CsvSource({ - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void phoneTest(IdentityScope identityScope, int tokenVersion) throws Exception { + public void phoneTest(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { String rawUidPhone = "BEOGxroPLdcY7LrSiwjY52+X05V0ryELpJmoWAyXiwbZ"; - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, rawUidPhone, Uid2TokenGenerator.defaultParams()); + String advertisingToken = AdvertisingTokenBuilder.builder().withRawUid(rawUidPhone).withScope(identityScope).withVersion(tokenVersion).build(); RefreshResponse refreshResponse = sharingClient.refreshJson(keySharingResponse(identityScope, MASTER_KEY, SITE_KEY)); assertTrue(refreshResponse.isSuccess()); DecryptionResponse decryptionResponse = sharingClient.decryptTokenIntoRawUid(advertisingToken); assertTrue(decryptionResponse.isSuccess()); assertEquals(rawUidPhone, decryptionResponse.getUid()); - assertEquals(tokenVersion, decryptionResponse.getAdvertisingTokenVersion()); + assertEquals(tokenVersion.ordinal() + 2, decryptionResponse.getAdvertisingTokenVersion()); assertEquals(IdentityType.Phone, decryptionResponse.getIdentityType()); } @ParameterizedTest - @ValueSource(ints = {2, 3, 4}) - public void legacyResponseFromOldOperator(int tokenVersion) throws Exception { + @ValueSource(strings = {"V2", "V3", "V4"}) + public void legacyResponseFromOldOperator(TokenVersionForTesting tokenVersion) throws Exception { RefreshResponse refreshResponse = sharingClient.refreshJson(keySetToJsonForSharing(MASTER_KEY, SITE_KEY)); assertTrue(refreshResponse.isSuccess()); - String advertisingToken = getAdvertisingToken(IdentityScope.UID2, tokenVersion, EXAMPLE_UID, Uid2TokenGenerator.defaultParams()); + String advertisingToken = AdvertisingTokenBuilder.builder().withVersion(tokenVersion).build(); decryptAndAssertSuccess(advertisingToken, tokenVersion); } @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) //similar to BidstreamClientTests.TokenGeneratedInTheFutureLegacyClient, but uses KeySharingResponse and Decrypt without domain parameter - public void tokenGeneratedInTheFutureLegacyClient(IdentityScope identityScope, int tokenVersion) throws Exception { + public void tokenGeneratedInTheFutureLegacyClient(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { UID2Client client = new UID2Client("ep", "ak", CLIENT_SECRET, identityScope); client.refreshJson(keySharingResponse(identityScope, MASTER_KEY, SITE_KEY)); - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenGenerated = Instant.now().plus(99, ChronoUnit.DAYS); - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, params); + Instant tokenGenerated = Instant.now().plus(99, ChronoUnit.DAYS); + String advertisingToken = AdvertisingTokenBuilder.builder().withGenerated(tokenGenerated).withScope(identityScope).withVersion(tokenVersion).build(); DecryptionResponse decryptionResponse = client.decrypt(advertisingToken); assertSuccess(decryptionResponse, tokenVersion); @@ -227,20 +225,19 @@ public void tokenGeneratedInTheFutureLegacyClient(IdentityScope identityScope, i @ParameterizedTest @CsvSource({ - "UID2, 2", - "EUID, 2", - "UID2, 3", - "EUID, 3", - "UID2, 4", - "EUID, 4" + "UID2, V2", + "EUID, V2", + "UID2, V3", + "EUID, V3", + "UID2, V4", + "EUID, V4" }) - public void tokenLifetimeTooLongLegacyClient(IdentityScope identityScope, int tokenVersion) throws Exception { + public void tokenLifetimeTooLongLegacyClient(IdentityScope identityScope, TokenVersionForTesting tokenVersion) throws Exception { UID2Client client = new UID2Client("ep", "ak", CLIENT_SECRET, identityScope); client.refreshJson(keySharingResponse(identityScope, MASTER_KEY, SITE_KEY)); - Uid2TokenGenerator.Params params = Uid2TokenGenerator.defaultParams(); - params.tokenExpiry = Instant.now().plus(3, ChronoUnit.DAYS).plus(1, ChronoUnit.MINUTES); - String advertisingToken = getAdvertisingToken(identityScope, tokenVersion, EXAMPLE_UID, params); + Instant tokenExpiry = Instant.now().plus(3, ChronoUnit.DAYS).plus(1, ChronoUnit.MINUTES); + String advertisingToken = AdvertisingTokenBuilder.builder().withExpiry(tokenExpiry).withScope(identityScope).withVersion(tokenVersion).build(); DecryptionResponse decryptionResponse = client.decrypt(advertisingToken); assertSuccess(decryptionResponse, tokenVersion); @@ -261,7 +258,7 @@ private String sharingEncrypt(SharingClient client) { private String sharingEncrypt(SharingClient client, IdentityScope identityScope) { EncryptionDataResponse encrypted = client.encryptRawUidIntoToken(EXAMPLE_UID); assertEquals(EncryptionStatus.SUCCESS, encrypted.getStatus()); - validateAdvertisingToken(encrypted.getEncryptedData(), identityScope, IdentityType.Email, 4); + validateAdvertisingToken(encrypted.getEncryptedData(), identityScope, IdentityType.Email, TokenVersionForTesting .V4); return encrypted.getEncryptedData(); } @@ -299,7 +296,7 @@ public void CanDecryptAnotherClientsEncryptedToken() throws Exception assertTrue(refreshResponse.isSuccess()); DecryptionResponse res = receivingClient.decryptTokenIntoRawUid(advertisingToken); - assertSame(DecryptionStatus.SUCCESS, res.getStatus()); + assertEquals(DecryptionStatus.SUCCESS, res.getStatus()); assertEquals(EXAMPLE_UID, res.getUid()); } diff --git a/src/test/java/com/uid2/client/TestData.java b/src/test/java/com/uid2/client/TestData.java index f4dbde3..b9fda90 100644 --- a/src/test/java/com/uid2/client/TestData.java +++ b/src/test/java/com/uid2/client/TestData.java @@ -7,27 +7,26 @@ import java.time.temporal.ChronoUnit; import java.util.Arrays; import java.util.Base64; -import java.util.stream.Collectors; -public class TestData { - public static final long MASTER_KEY_ID = 164; - public static final long SITE_KEY_ID = 165; - public static final int SITE_ID = 9000; - public static final int SITE_ID2 = 2; - public static final int[] INT_MASTER_SECRET = new int[] { 139, 37, 241, 173, 18, 92, 36, 232, 165, 168, 23, 18, 38, 195, 123, 92, 160, 136, 185, 40, 91, 173, 165, 221, 168, 16, 169, 164, 38, 139, 8, 155 }; - public static final int[] INT_SITE_SECRET = new int[] { 32, 251, 7, 194, 132, 154, 250, 86, 202, 116, 104, 29, 131, 192, 139, 215, 48, 164, 11, 65, 226, 110, 167, 14, 108, 51, 254, 125, 65, 24, 23, 133 }; - public static final Instant NOW = Instant.now(); - public static final Key MASTER_KEY = new Key(MASTER_KEY_ID, -1, NOW.minus(1, ChronoUnit.DAYS), NOW, NOW.plus(1, ChronoUnit.DAYS), getMasterSecret()); - public static final Key SITE_KEY = new Key(SITE_KEY_ID, SITE_ID, NOW.minus(10, ChronoUnit.DAYS), NOW.minus(1, ChronoUnit.DAYS), NOW.plus(1, ChronoUnit.DAYS), getSiteSecret()); - public static final String EXAMPLE_UID = "ywsvDNINiZOVSsfkHpLpSJzXzhr6Jx9Z/4Q0+lsEUvM="; - public static final String EXAMPLE_PHONE_RAW_UID2_V3 = "BFOsW2SkK0egqbfyiALtpti5G/cG+PcEvjkoHl56rEV8"; - public static final String CLIENT_SECRET = "ioG3wKxAokmp+rERx6A4kM/13qhyolUXIu14WN16Spo="; + class TestData { + static final long MASTER_KEY_ID = 164; + static final long SITE_KEY_ID = 165; + static final int SITE_ID = 9000; + static final int SITE_ID2 = 2; + static final int[] INT_MASTER_SECRET = new int[] { 139, 37, 241, 173, 18, 92, 36, 232, 165, 168, 23, 18, 38, 195, 123, 92, 160, 136, 185, 40, 91, 173, 165, 221, 168, 16, 169, 164, 38, 139, 8, 155 }; + static final int[] INT_SITE_SECRET = new int[] { 32, 251, 7, 194, 132, 154, 250, 86, 202, 116, 104, 29, 131, 192, 139, 215, 48, 164, 11, 65, 226, 110, 167, 14, 108, 51, 254, 125, 65, 24, 23, 133 }; + static final Instant NOW = Instant.now(); + static final Key MASTER_KEY = new Key(MASTER_KEY_ID, -1, NOW.minus(1, ChronoUnit.DAYS), NOW, NOW.plus(1, ChronoUnit.DAYS), getMasterSecret()); + static final Key SITE_KEY = new Key(SITE_KEY_ID, SITE_ID, NOW.minus(10, ChronoUnit.DAYS), NOW.minus(1, ChronoUnit.DAYS), NOW.plus(1, ChronoUnit.DAYS), getSiteSecret()); + static final String EXAMPLE_UID = "ywsvDNINiZOVSsfkHpLpSJzXzhr6Jx9Z/4Q0+lsEUvM="; + static final String EXAMPLE_PHONE_RAW_UID2_V3 = "BFOsW2SkK0egqbfyiALtpti5G/cG+PcEvjkoHl56rEV8"; + static final String CLIENT_SECRET = "ioG3wKxAokmp+rERx6A4kM/13qhyolUXIu14WN16Spo="; - public static byte[] getMasterSecret() { + static byte[] getMasterSecret() { return intArrayToByteArray(INT_MASTER_SECRET); } - public static byte[] getSiteSecret() { + static byte[] getSiteSecret() { return intArrayToByteArray(INT_SITE_SECRET); } @@ -39,13 +38,13 @@ private static byte[] intArrayToByteArray(int[] intArray) { return byteArray; } - public static byte[] getTestSecret(int value) { + static byte[] getTestSecret(int value) { byte[] secret = new byte[32]; Arrays.fill(secret, (byte)value); return secret; } - public static String keySetToJson(Key ... keys) throws Exception { + static String keySetToJson(Key ... keys) throws Exception { StringWriter sw = new StringWriter(); JsonWriter writer = new JsonWriter(sw); writer.beginObject(); diff --git a/src/test/java/com/uid2/client/TokenVersionForTesting.java b/src/test/java/com/uid2/client/TokenVersionForTesting.java new file mode 100644 index 0000000..bbbb972 --- /dev/null +++ b/src/test/java/com/uid2/client/TokenVersionForTesting.java @@ -0,0 +1,7 @@ +package com.uid2.client; + +enum TokenVersionForTesting { + V2, + V3, + V4 +} \ No newline at end of file