Skip to content

Commit

Permalink
Add test cases and address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
caroline-ttd committed Apr 9, 2024
1 parent f181454 commit 223cba2
Show file tree
Hide file tree
Showing 13 changed files with 417 additions and 219 deletions.
8 changes: 4 additions & 4 deletions src/main/java/com/uid2/client/KeyContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/uid2/client/PrivacyBits.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/uid2/client/TokenHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<KeyContainer> container = new AtomicReference<>(null);;
Expand Down
105 changes: 105 additions & 0 deletions src/test/java/com/uid2/client/AdvertisingTokenBuilder.java
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 src/test/java/com/uid2/client/AdvertisingTokenBuilderTest.java
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);
}
}
Loading

0 comments on commit 223cba2

Please sign in to comment.