55import static org .junit .Assert .assertTrue ;
66
77import java .time .Instant ;
8+ import java .util .ArrayList ;
9+ import java .util .List ;
810
911import com .iab .gdpr .VendorConsent ;
1012import org .hamcrest .Matchers ;
1315import com .iab .gdpr .exception .VendorConsentException ;
1416
1517public 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