Skip to content

Commit 218a54c

Browse files
authored
Merge pull request #29 from jaikit/bitsize
Handle edge case to correctly count total bit size and calculate byte array
2 parents 97f7e6c + 9ea02a1 commit 218a54c

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

src/main/java/com/iab/gdpr/VendorConsent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private VendorConsent(Builder builder) throws VendorConsentException {
9292
boolean bitsFit = (bitSize % 8) == 0;
9393
this.bits = new Bits(new byte[bitSize / 8 + (bitsFit ? 0 : 1)]);
9494
} else {
95-
int bitSize = GdprConstants.VENDOR_BITFIELD_OFFSET + this.maxVendorId - 1;
95+
int bitSize = GdprConstants.VENDOR_BITFIELD_OFFSET + this.maxVendorId;
9696
boolean bitsFit = (bitSize % 8) == 0;
9797
this.bits = new Bits(new byte[(bitSize / 8 + (bitsFit ? 0 : 1))]);
9898
}

src/test/java/com/iab/gdpr/VendorConsentTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import static org.junit.Assert.assertTrue;
66

77
import java.time.Instant;
8+
import java.util.ArrayList;
9+
import java.util.List;
810

911
import com.iab.gdpr.VendorConsent;
1012
import org.hamcrest.Matchers;
@@ -13,6 +15,48 @@
1315
import com.iab.gdpr.exception.VendorConsentException;
1416

1517
public class VendorConsentTest {
18+
19+
@Test
20+
public void testWithBitSizeMultipleOfEight() {
21+
String consentString = "BOOlLqOOOlLqTABABAENAk-AAAAXx7_______9______9uz_Gv_r_f__3nW8_39P3g_7_O3_7m_-zzV48_lrQV1yPAUCgA";
22+
VendorConsent vendorConsent = VendorConsent.fromBase64String(consentString);
23+
assertTrue(vendorConsent.getMaxVendorId() == 380);
24+
assertTrue(vendorConsent.getBitfield().size() == 380);
25+
assertTrue(vendorConsent.isVendorAllowed(380));
26+
assertFalse(vendorConsent.isVendorAllowed(379));
27+
28+
// test creation of vendor string
29+
VendorConsent consent = VendorConsent.fromBase64String(consentString);
30+
31+
VendorConsent.Builder builder = new VendorConsent.Builder();
32+
builder.withVersion(consent.getVersion());
33+
builder.withConsentRecordCreatedOn(consent.getConsentRecordCreated());
34+
builder.withConsentRecordLastUpdatedOn(consent.getConsentRecordLastUpdated());
35+
36+
builder.withCmpID(consent.getCmpId());
37+
builder.withCmpVersion(consent.getCmpVersion());
38+
builder.withConsentScreenID(consent.getConsentScreen());
39+
builder.withConsentLanguage(consent.getConsentLanguage());
40+
builder.withVendorListVersion(consent.getVendorListVersion());
41+
builder.withAllowedPurposes(consent.getAllowedPurposes());
42+
43+
builder.withMaxVendorId(consent.getMaxVendorId());
44+
builder.withVendorEncodingType(consent.getVendorEncodingType());
45+
List<Integer> vendorConsetBit = new ArrayList<>(consent.getMaxVendorId());
46+
for(int v = 1; v <= consent.getMaxVendorId(); v++) {
47+
if(consent.isVendorAllowed(v)) {
48+
vendorConsetBit.add(v);
49+
}
50+
}
51+
builder.withBitField(vendorConsetBit);
52+
builder.withDefaultConsent(consent.isDefaultConsent());
53+
builder.withRangeEntries(consent.getRangeEntries());
54+
55+
VendorConsent underTest = builder.build();
56+
57+
assertThat(underTest.getConsentString(), Matchers.is(consentString));
58+
}
59+
1660
@Test
1761
public void testBitField() {
1862
String consentString = "BN5lERiOMYEdiAOAWeFRAAYAAaAAptQ";
@@ -164,4 +208,30 @@ public void testRangeEntryConsent() {
164208
assertFalse(consent.isVendorAllowed(3));
165209
assertTrue(consent.isVendorAllowed(27));
166210
}
211+
212+
@Test
213+
public void testLongRangeEntry() {
214+
String consentString = "BOOMzbgOOQww_AtABAFRAb-AAAsvOA3gACAAkABgArgBaAF0AMAA1gBuAH8AQQBSgCoAL8AYQBigDIAM0AaABpgDYAOYAdgA8AB6gD4AQoAiABFQCMAI6ASABIgCTAEqAJeATIBQQCiAKSAU4BVQCtAK-AWYBaQC2ALcAXMAvAC-gGAAYcAxQDGAGQAMsAZsA0ADTAGqANcAbMA4ADjAHKAOiAdQB1gDtgHgAeMA9AD2AHzAP4BAACBAEEAIbAREBEgCKQEXARhZeYA";
215+
VendorConsent consent = VendorConsent.fromBase64String(consentString);
216+
assertThat(consent.getCmpId(), Matchers.is(45));
217+
assertThat(consent.getCmpVersion(), Matchers.is(1));
218+
assertThat(consent.getConsentLanguage(), Matchers.is("FR"));
219+
assertThat(consent.getConsentRecordCreated(), Matchers.is(Instant.ofEpochMilli(15270622944L * 100)));
220+
assertThat(consent.getConsentRecordLastUpdated(), Matchers.is(Instant.ofEpochMilli(15271660607L * 100)));
221+
assertThat(consent.getAllowedPurposes().size(), Matchers.is(5)); assertTrue(consent.isPurposeAllowed(1));
222+
assertTrue(consent.isPurposeAllowed(2));
223+
assertTrue(consent.isPurposeAllowed(3));
224+
assertTrue(consent.isPurposeAllowed(4));
225+
assertTrue(consent.isPurposeAllowed(5));
226+
assertFalse(consent.isPurposeAllowed(6));
227+
assertFalse(consent.isPurposeAllowed(25));
228+
assertFalse(consent.isPurposeAllowed(0));
229+
assertTrue(consent.isVendorAllowed(1));
230+
assertFalse(consent.isVendorAllowed(5));
231+
assertTrue(consent.isVendorAllowed(45));
232+
assertFalse(consent.isVendorAllowed(47));
233+
assertFalse(consent.isVendorAllowed(146));
234+
assertTrue(consent.isVendorAllowed(147));
235+
assertThat(consent.getConsentString(), Matchers.is(consentString));
236+
}
167237
}

0 commit comments

Comments
 (0)