-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f181454
commit 223cba2
Showing
13 changed files
with
417 additions
and
219 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
src/test/java/com/uid2/client/AdvertisingTokenBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
92 changes: 92 additions & 0 deletions
92
src/test/java/com/uid2/client/AdvertisingTokenBuilderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
Oops, something went wrong.