@@ -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 .minus (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,28 @@ 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+ //Note V2 does not have a "token generated" field, therefore v2 tokens can't have a future "token generated" date and are excluded from this test.
108+ @ CsvSource ({
109+ "UID2, V3" ,
110+ "EUID, V3" ,
111+ "UID2, V4" ,
112+ "EUID, V4"
113+ })
89114 public void tokenGeneratedInTheFutureToSimulateClockSkew (IdentityScope identityScope , TokenVersionForTesting tokenVersion ) throws Exception {
90115 Instant tokenGenerated = Instant .now ().plus (31 , ChronoUnit .MINUTES );
91116 String advertisingToken = AdvertisingTokenBuilder .builder ().withGenerated (tokenGenerated ).withScope (identityScope ).withVersion (tokenVersion ).build ();
92- callAndVerifyRefreshJson ( identityScope );
117+ refresh ( keyBidstreamResponse ( identityScope , MASTER_KEY , SITE_KEY ) );
93118
94119 DecryptionResponse decryptionResponse = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
95120 assertFails (decryptionResponse , tokenVersion );
@@ -107,16 +132,15 @@ public void tokenGeneratedInTheFutureToSimulateClockSkew(IdentityScope identityS
107132 public void tokenGeneratedInTheFutureWithinAllowedClockSkew (IdentityScope identityScope , TokenVersionForTesting tokenVersion ) throws Exception {
108133 Instant tokenGenerated = Instant .now ().plus (30 , ChronoUnit .MINUTES );
109134 String advertisingToken = AdvertisingTokenBuilder .builder ().withGenerated (tokenGenerated ).withScope (identityScope ).withVersion (tokenVersion ).build ();
110- callAndVerifyRefreshJson ( identityScope );
135+ refresh ( keyBidstreamResponse ( identityScope , MASTER_KEY , SITE_KEY ) );
111136
112137 decryptAndAssertSuccess (advertisingToken , tokenVersion );
113138 }
114139
115140 @ ParameterizedTest
116141 @ ValueSource (strings = {"V2" , "V3" , "V4" })
117142 public void legacyResponseFromOldOperator (TokenVersionForTesting tokenVersion ) throws Exception {
118- RefreshResponse refreshResponse = bidstreamClient .refreshJson (keySetToJsonForSharing (MASTER_KEY , SITE_KEY ));
119- assertTrue (refreshResponse .isSuccess ());
143+ refresh (keySetToJsonForSharing (MASTER_KEY , SITE_KEY ));
120144 String advertisingToken = AdvertisingTokenBuilder .builder ().withVersion (tokenVersion ).build ();
121145
122146 decryptAndAssertSuccess (advertisingToken , tokenVersion );
@@ -165,7 +189,7 @@ public void tokenLifetimeTooLongLegacyClient(IdentityScope identityScope, TokenV
165189 @ ParameterizedTest
166190 @ MethodSource ("data_IdentityScopeAndType_TestCases" )
167191 public void identityScopeAndType_TestCases (String uid , IdentityScope identityScope , IdentityType identityType ) throws Exception {
168- callAndVerifyRefreshJson ( identityScope );
192+ refresh ( keyBidstreamResponse ( identityScope , MASTER_KEY , SITE_KEY ) );
169193
170194 String advertisingToken = AdvertisingTokenBuilder .builder ().withRawUid (uid ).withScope (identityScope ).build ();
171195 DecryptionResponse res = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
@@ -194,7 +218,7 @@ private static Stream<Arguments> data_IdentityScopeAndType_TestCases() {
194218 "example.org, V4"
195219 })
196220 public void TokenIsCstgDerivedTest (String domainName , TokenVersionForTesting tokenVersion ) throws Exception {
197- callAndVerifyRefreshJson ( IdentityScope .UID2 );
221+ refresh ( keyBidstreamResponse ( IdentityScope .UID2 , MASTER_KEY , SITE_KEY ) );
198222 int privacyBits = PrivacyBitsBuilder .Builder ().WithClientSideGenerated (true ).Build ();
199223
200224 String advertisingToken = AdvertisingTokenBuilder .builder ().withVersion (tokenVersion ).withPrivacyBits (privacyBits ).build ();
@@ -221,8 +245,7 @@ public void expiredKeyContainer() throws Exception {
221245
222246 Key masterKeyExpired = new Key (MASTER_KEY_ID , -1 , NOW , NOW .minus (2 , ChronoUnit .HOURS ), NOW .minus (1 , ChronoUnit .HOURS ), getMasterSecret ());
223247 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 ());
248+ refresh (keyBidstreamResponse (IdentityScope .UID2 , masterKeyExpired , siteKeyExpired ));
226249
227250 DecryptionResponse res = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
228251 assertEquals (DecryptionStatus .KEYS_NOT_SYNCED , res .getStatus ());
@@ -234,8 +257,7 @@ public void notAuthorizedForMasterKey() throws Exception {
234257
235258 Key anotherMasterKey = new Key (MASTER_KEY_ID + SITE_KEY_ID + 1 , -1 , NOW , NOW , NOW .plus (1 , ChronoUnit .HOURS ), getMasterSecret ());
236259 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 ());
260+ refresh (keyBidstreamResponse (IdentityScope .UID2 , anotherMasterKey , anotherSiteKey ));
239261
240262 DecryptionResponse res = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
241263 assertEquals (DecryptionStatus .NOT_AUTHORIZED_FOR_MASTER_KEY , res .getStatus ());
@@ -246,7 +268,7 @@ public void invalidPayload() throws Exception {
246268 String payload = AdvertisingTokenBuilder .builder ().build ();
247269 byte [] payloadInBytes = Uid2Base64UrlCoder .decode (payload );
248270 String advertisingToken = Uid2Base64UrlCoder .encode (Arrays .copyOfRange (payloadInBytes , 0 , payloadInBytes .length - 1 ));
249- bidstreamClient . refreshJson (keyBidstreamResponse (IdentityScope .UID2 , MASTER_KEY , SITE_KEY ));
271+ refresh (keyBidstreamResponse (IdentityScope .UID2 , MASTER_KEY , SITE_KEY ));
250272 DecryptionResponse res = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null );
251273 assertEquals (DecryptionStatus .INVALID_PAYLOAD , res .getStatus ());
252274 }
@@ -256,7 +278,7 @@ public void tokenExpiryAndCustomNow() throws Exception {
256278 final Instant expiry = Instant .parse ("2021-03-22T09:01:02Z" );
257279 final Instant generated = expiry .minus (60 , ChronoUnit .SECONDS );
258280
259- bidstreamClient . refreshJson (keyBidstreamResponse (IdentityScope .UID2 , MASTER_KEY , SITE_KEY ));
281+ refresh (keyBidstreamResponse (IdentityScope .UID2 , MASTER_KEY , SITE_KEY ));
260282 String advertisingToken = AdvertisingTokenBuilder .builder ().withExpiry (expiry ).withGenerated (generated ).build ();
261283
262284 DecryptionResponse res = bidstreamClient .decryptTokenIntoRawUid (advertisingToken , null , expiry .plus (1 , ChronoUnit .SECONDS ));
@@ -266,8 +288,8 @@ public void tokenExpiryAndCustomNow() throws Exception {
266288 assertEquals (EXAMPLE_UID , res .getUid ());
267289 }
268290
269- private void callAndVerifyRefreshJson ( IdentityScope identityScope ) {
270- RefreshResponse refreshResponse = bidstreamClient .refreshJson (keyBidstreamResponse ( identityScope , MASTER_KEY , SITE_KEY ) );
291+ private void refresh ( String json ) {
292+ RefreshResponse refreshResponse = bidstreamClient .refreshJson (json );
271293 assertTrue (refreshResponse .isSuccess ());
272294 }
273295
0 commit comments