@@ -33,9 +33,10 @@ public class BidstreamClientTests {
3333 "UID2, V4" ,
3434 "EUID, V4"
3535 })
36- public void smokeTest (IdentityScope identityScope , TokenVersionForTesting tokenVersion ) throws Exception {
37- String advertisingToken = AdvertisingTokenBuilder .builder ().withScope (identityScope ).withVersion (tokenVersion ).build ();
38- callAndVerifyRefreshJson (identityScope );
36+ public void smokeTestForBidstream (IdentityScope identityScope , TokenVersionForTesting tokenVersion ) throws Exception {
37+ Instant now = Instant .now ();
38+ String advertisingToken = AdvertisingTokenBuilder .builder ().withScope (identityScope ).withVersion (tokenVersion ).withEstablished (now .minus (120 , ChronoUnit .DAYS )).withGenerated (now .plus (-1 , ChronoUnit .DAYS )).withExpiry (now .plus (2 , ChronoUnit .DAYS )).build ();
39+ refresh (keyBidstreamResponse (identityScope , MASTER_KEY , SITE_KEY ));
3940
4041 decryptAndAssertSuccess (advertisingToken , tokenVersion );
4142 }
@@ -50,7 +51,7 @@ public void smokeTest(IdentityScope identityScope, TokenVersionForTesting tokenV
5051 public void phoneTest (IdentityScope identityScope , TokenVersionForTesting tokenVersion ) throws Exception {
5152 String rawUidPhone = "BEOGxroPLdcY7LrSiwjY52+X05V0ryELpJmoWAyXiwbZ" ;
5253 String advertisingToken = AdvertisingTokenBuilder .builder ().withRawUid (rawUidPhone ).withScope (identityScope ).withVersion (tokenVersion ).build ();
53- callAndVerifyRefreshJson ( identityScope );
54+ refresh ( keyBidstreamResponse ( identityScope , MASTER_KEY , SITE_KEY ) );
5455
5556 DecryptionResponse decryptionResponse = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
5657 assertTrue (decryptionResponse .isSuccess ());
@@ -68,13 +69,19 @@ public void phoneTest(IdentityScope identityScope, TokenVersionForTesting tokenV
6869 "UID2, V4" ,
6970 "EUID, V4"
7071 })
71- public void tokenLifetimeTooLongForBidstream (IdentityScope identityScope , TokenVersionForTesting tokenVersion ) throws Exception {
72- Instant tokenExpiry = Instant .now ().plus (3 , ChronoUnit .DAYS ).plus (1 , ChronoUnit .MINUTES );
73- String advertisingToken = AdvertisingTokenBuilder .builder ().withExpiry (tokenExpiry ).withScope (identityScope ).withVersion (tokenVersion ).build ();
74- callAndVerifyRefreshJson (identityScope );
72+ public void tokenLifetimeTooLongForBidstreamButRemainingLifetimeAllowed (IdentityScope identityScope , TokenVersionForTesting tokenVersion ) throws Exception {
73+ Instant generated = Instant .now ().minus (1 , ChronoUnit .DAYS );
74+ Instant tokenExpiry = generated .plus (3 , ChronoUnit .DAYS ).plus (1 , ChronoUnit .MINUTES );
75+ String advertisingToken = AdvertisingTokenBuilder .builder ().withExpiry (tokenExpiry ).withScope (identityScope ).withVersion (tokenVersion ).withGenerated (generated ).build ();
76+ refresh (keyBidstreamResponse (identityScope , MASTER_KEY , SITE_KEY ));
7577
7678 DecryptionResponse decryptionResponse = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
77- assertFails (decryptionResponse , tokenVersion );
79+
80+ if (tokenVersion == TokenVersionForTesting .V2 ) {
81+ assertSuccess (decryptionResponse , tokenVersion );
82+ } else {
83+ assertFails (decryptionResponse , tokenVersion );
84+ }
7885 }
7986
8087 @ ParameterizedTest
@@ -86,10 +93,27 @@ public void tokenLifetimeTooLongForBidstream(IdentityScope identityScope, TokenV
8693 "UID2, V4" ,
8794 "EUID, V4"
8895 })
96+ public void tokenRemainingLifetimeTooLongForBidstream (IdentityScope identityScope , TokenVersionForTesting tokenVersion ) throws Exception {
97+ Instant tokenExpiry = Instant .now ().plus (3 , ChronoUnit .DAYS ).plus (1 , ChronoUnit .MINUTES );
98+ Instant generated = Instant .now ();
99+ String advertisingToken = AdvertisingTokenBuilder .builder ().withExpiry (tokenExpiry ).withScope (identityScope ).withVersion (tokenVersion ).withGenerated (generated ).build ();
100+ refresh (keyBidstreamResponse (identityScope , MASTER_KEY , SITE_KEY ));
101+
102+ DecryptionResponse decryptionResponse = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
103+ assertFails (decryptionResponse , tokenVersion );
104+ }
105+
106+ @ ParameterizedTest
107+ @ CsvSource ({
108+ "UID2, V3" ,
109+ "EUID, V3" ,
110+ "UID2, V4" ,
111+ "EUID, V4"
112+ })
89113 public void tokenGeneratedInTheFutureToSimulateClockSkew (IdentityScope identityScope , TokenVersionForTesting tokenVersion ) throws Exception {
90114 Instant tokenGenerated = Instant .now ().plus (31 , ChronoUnit .MINUTES );
91115 String advertisingToken = AdvertisingTokenBuilder .builder ().withGenerated (tokenGenerated ).withScope (identityScope ).withVersion (tokenVersion ).build ();
92- callAndVerifyRefreshJson ( identityScope );
116+ refresh ( keyBidstreamResponse ( identityScope , MASTER_KEY , SITE_KEY ) );
93117
94118 DecryptionResponse decryptionResponse = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
95119 assertFails (decryptionResponse , tokenVersion );
@@ -107,16 +131,15 @@ public void tokenGeneratedInTheFutureToSimulateClockSkew(IdentityScope identityS
107131 public void tokenGeneratedInTheFutureWithinAllowedClockSkew (IdentityScope identityScope , TokenVersionForTesting tokenVersion ) throws Exception {
108132 Instant tokenGenerated = Instant .now ().plus (30 , ChronoUnit .MINUTES );
109133 String advertisingToken = AdvertisingTokenBuilder .builder ().withGenerated (tokenGenerated ).withScope (identityScope ).withVersion (tokenVersion ).build ();
110- callAndVerifyRefreshJson ( identityScope );
134+ refresh ( keyBidstreamResponse ( identityScope , MASTER_KEY , SITE_KEY ) );
111135
112136 decryptAndAssertSuccess (advertisingToken , tokenVersion );
113137 }
114138
115139 @ ParameterizedTest
116140 @ ValueSource (strings = {"V2" , "V3" , "V4" })
117141 public void legacyResponseFromOldOperator (TokenVersionForTesting tokenVersion ) throws Exception {
118- RefreshResponse refreshResponse = bidstreamClient .refreshJson (keySetToJsonForSharing (MASTER_KEY , SITE_KEY ));
119- assertTrue (refreshResponse .isSuccess ());
142+ refresh (keySetToJsonForSharing (MASTER_KEY , SITE_KEY ));
120143 String advertisingToken = AdvertisingTokenBuilder .builder ().withVersion (tokenVersion ).build ();
121144
122145 decryptAndAssertSuccess (advertisingToken , tokenVersion );
@@ -165,7 +188,7 @@ public void tokenLifetimeTooLongLegacyClient(IdentityScope identityScope, TokenV
165188 @ ParameterizedTest
166189 @ MethodSource ("data_IdentityScopeAndType_TestCases" )
167190 public void identityScopeAndType_TestCases (String uid , IdentityScope identityScope , IdentityType identityType ) throws Exception {
168- callAndVerifyRefreshJson ( identityScope );
191+ refresh ( keyBidstreamResponse ( identityScope , MASTER_KEY , SITE_KEY ) );
169192
170193 String advertisingToken = AdvertisingTokenBuilder .builder ().withRawUid (uid ).withScope (identityScope ).build ();
171194 DecryptionResponse res = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
@@ -194,7 +217,7 @@ private static Stream<Arguments> data_IdentityScopeAndType_TestCases() {
194217 "example.org, V4"
195218 })
196219 public void TokenIsCstgDerivedTest (String domainName , TokenVersionForTesting tokenVersion ) throws Exception {
197- callAndVerifyRefreshJson ( IdentityScope .UID2 );
220+ refresh ( keyBidstreamResponse ( IdentityScope .UID2 , MASTER_KEY , SITE_KEY ) );
198221 int privacyBits = PrivacyBitsBuilder .Builder ().WithClientSideGenerated (true ).Build ();
199222
200223 String advertisingToken = AdvertisingTokenBuilder .builder ().withVersion (tokenVersion ).withPrivacyBits (privacyBits ).build ();
@@ -221,8 +244,7 @@ public void expiredKeyContainer() throws Exception {
221244
222245 Key masterKeyExpired = new Key (MASTER_KEY_ID , -1 , NOW , NOW .minus (2 , ChronoUnit .HOURS ), NOW .minus (1 , ChronoUnit .HOURS ), getMasterSecret ());
223246 Key siteKeyExpired = new Key (SITE_KEY_ID , SITE_ID , NOW , NOW .minus (2 , ChronoUnit .HOURS ), NOW .minus (1 , ChronoUnit .HOURS ), getSiteSecret ());
224- RefreshResponse refreshResponse = bidstreamClient .refreshJson (keyBidstreamResponse (IdentityScope .UID2 , masterKeyExpired , siteKeyExpired ));
225- assertTrue (refreshResponse .isSuccess ());
247+ refresh (keyBidstreamResponse (IdentityScope .UID2 , masterKeyExpired , siteKeyExpired ));
226248
227249 DecryptionResponse res = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
228250 assertEquals (DecryptionStatus .KEYS_NOT_SYNCED , res .getStatus ());
@@ -234,8 +256,7 @@ public void notAuthorizedForMasterKey() throws Exception {
234256
235257 Key anotherMasterKey = new Key (MASTER_KEY_ID + SITE_KEY_ID + 1 , -1 , NOW , NOW , NOW .plus (1 , ChronoUnit .HOURS ), getMasterSecret ());
236258 Key anotherSiteKey = new Key (MASTER_KEY_ID + SITE_KEY_ID + 2 , SITE_ID , NOW , NOW , NOW .plus (1 , ChronoUnit .HOURS ), getSiteSecret ());
237- RefreshResponse refreshResponse = bidstreamClient .refreshJson (keyBidstreamResponse (IdentityScope .UID2 , anotherMasterKey , anotherSiteKey ));
238- assertTrue (refreshResponse .isSuccess ());
259+ refresh (keyBidstreamResponse (IdentityScope .UID2 , anotherMasterKey , anotherSiteKey ));
239260
240261 DecryptionResponse res = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
241262 assertEquals (DecryptionStatus .NOT_AUTHORIZED_FOR_MASTER_KEY , res .getStatus ());
@@ -246,7 +267,7 @@ public void invalidPayload() throws Exception {
246267 String payload = AdvertisingTokenBuilder .builder ().build ();
247268 byte [] payloadInBytes = Uid2Base64UrlCoder .decode (payload );
248269 String advertisingToken = Uid2Base64UrlCoder .encode (Arrays .copyOfRange (payloadInBytes , 0 , payloadInBytes .length - 1 ));
249- bidstreamClient . refreshJson (keyBidstreamResponse (IdentityScope .UID2 , MASTER_KEY , SITE_KEY ));
270+ refresh (keyBidstreamResponse (IdentityScope .UID2 , MASTER_KEY , SITE_KEY ));
250271 DecryptionResponse res = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
251272 assertEquals (DecryptionStatus .INVALID_PAYLOAD , res .getStatus ());
252273 }
@@ -256,7 +277,7 @@ public void tokenExpiryAndCustomNow() throws Exception {
256277 final Instant expiry = Instant .parse ("2021-03-22T09:01:02Z" );
257278 final Instant generated = expiry .minus (60 , ChronoUnit .SECONDS );
258279
259- bidstreamClient . refreshJson (keyBidstreamResponse (IdentityScope .UID2 , MASTER_KEY , SITE_KEY ));
280+ refresh (keyBidstreamResponse (IdentityScope .UID2 , MASTER_KEY , SITE_KEY ));
260281 String advertisingToken = AdvertisingTokenBuilder .builder ().withExpiry (expiry ).withGenerated (generated ).build ();
261282
262283 DecryptionResponse res = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null , expiry .plus (1 , ChronoUnit .SECONDS ));
@@ -266,8 +287,8 @@ public void tokenExpiryAndCustomNow() throws Exception {
266287 assertEquals (EXAMPLE_UID , res .getUid ());
267288 }
268289
269- private void callAndVerifyRefreshJson ( IdentityScope identityScope ) {
270- RefreshResponse refreshResponse = bidstreamClient .refreshJson (keyBidstreamResponse ( identityScope , MASTER_KEY , SITE_KEY ) );
290+ private void refresh ( String json ) {
291+ RefreshResponse refreshResponse = bidstreamClient .refreshJson (json );
271292 assertTrue (refreshResponse .isSuccess ());
272293 }
273294
0 commit comments