|
24 | 24 | import com.aliyun.oss.common.auth.*; |
25 | 25 | import com.aliyun.oss.common.provider.mock.EcsRamRoleCredentialsFetcherMock; |
26 | 26 | import com.aliyun.oss.common.provider.mock.EcsRamRoleCredentialsFetcherMock.ResponseCategory; |
| 27 | +import com.aliyun.oss.common.utils.DateUtil; |
27 | 28 | import junit.framework.Assert; |
28 | 29 | import org.junit.Test; |
29 | 30 |
|
30 | 31 | import java.io.ByteArrayInputStream; |
| 32 | +import java.util.Date; |
31 | 33 |
|
32 | 34 | public class EcsRamRoleCredentialsProviderTest extends TestBase { |
33 | 35 |
|
@@ -257,4 +259,69 @@ public void testGetCredentialsFromAuthInOss() { |
257 | 259 | Assert.fail(e.getMessage()); |
258 | 260 | } |
259 | 261 | } |
| 262 | + |
| 263 | + @Test |
| 264 | + public void testExpireTime() { |
| 265 | + |
| 266 | + String NORMAL_METADATA_1 = "{" + "\"AccessKeyId\" : \"STS.EgnR2nX****FAf9uuqjHS8Ddt\"," |
| 267 | + + "\"AccessKeySecret\" : \"CJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV\"," |
| 268 | + + "\"SecurityToken\" : \"CAISigJ1q6Ft5B2yfSjIpKTbGYjatahPg6CtQ0CIkXUkZsd/14HPljz2IHBE****AOEetfs2lW1T6P0TlrRtTtpfTEmBbI569s1WqQW+Z5fT5JHo4LZfhoGoRzB9keMGTIyADd/iRfbxJ92PCTmd5AIRrJ****K9JS/HVbSClZ9gaPkOQwC8dkAoLdxKJwxk2qR4XDmrQp****PxhXfKB0dFoxd1jXgFiZ6y2cqB8BHT/jaYo603392ofsj1NJE1ZMglD4nlhbxMG/CfgHIK2X9j77xriaFIwzDDs+yGDkNZixf8aLqEqIM/dV4hPfdjSvMf8qOtj5t1sffJnoHtzBJAIexOT****FVtcH5xchqAAXp1d/dYv+2L+dJDW+2pm1vACD/UlRk93prPkyuU3zH2wnvXBxEi26QnoQSCA+T1yE2wo41V2mS+LSGYN/PC+2Ml1q+JX5DzKgfGrUPt7kU4FeXJDzGh2YaXRGpO7yERKgAc/NukkDNqthMaHntyTeix08DYBuTT6gd3V8XmN8vF\"," |
| 269 | + + "\"Code\" : \"Success\"," |
| 270 | + + "\"Expiration\" :" ; |
| 271 | + |
| 272 | + String NORMAL_METADATA_2 = "{" + "\"AccessKeyId\" : \"STS.AgnR2nX****FAf9uuqjHS8Ddt\"," |
| 273 | + + "\"AccessKeySecret\" : \"AJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV\"," |
| 274 | + + "\"SecurityToken\" : \"AAISigJ1q6Ft5B2yfSjIpKTbGYjatahPg6CtQ0CIkXUkZsd/14HPljz2IHBE****AOEetfs2lW1T6P0TlrRtTtpfTEmBbI569s1WqQW+Z5fT5JHo4LZfhoGoRzB9keMGTIyADd/iRfbxJ92PCTmd5AIRrJ****K9JS/HVbSClZ9gaPkOQwC8dkAoLdxKJwxk2qR4XDmrQp****PxhXfKB0dFoxd1jXgFiZ6y2cqB8BHT/jaYo603392ofsj1NJE1ZMglD4nlhbxMG/CfgHIK2X9j77xriaFIwzDDs+yGDkNZixf8aLqEqIM/dV4hPfdjSvMf8qOtj5t1sffJnoHtzBJAIexOT****FVtcH5xchqAAXp1d/dYv+2L+dJDW+2pm1vACD/UlRk93prPkyuU3zH2wnvXBxEi26QnoQSCA+T1yE2wo41V2mS+LSGYN/PC+2Ml1q+JX5DzKgfGrUPt7kU4FeXJDzGh2YaXRGpO7yERKgAc/NukkDNqthMaHntyTeix08DYBuTT6gd3V8XmN8vF\"," |
| 275 | + + "\"Code\" : \"Success\"," |
| 276 | + + "\"Expiration\" :" ; |
| 277 | + |
| 278 | + try { |
| 279 | + |
| 280 | + EcsRamRoleCredentialsFetcherMock credentialsFetcher = new EcsRamRoleCredentialsFetcherMock( |
| 281 | + TestConfig.OSS_AUTH_SERVER_HOST).withResponseCategory(ResponseCategory.FromExternal); |
| 282 | + |
| 283 | + String meta = NORMAL_METADATA_1 + "\"" + DateUtil.formatAlternativeIso8601Date(new Date()) + "\"}"; |
| 284 | + credentialsFetcher.setExternalData(meta); |
| 285 | + |
| 286 | + EcsRamRoleCredentialsProvider credentialsProvider = new EcsRamRoleCredentialsProvider( |
| 287 | + TestConfig.OSS_AUTH_SERVER_HOST).withCredentialsFetcher(credentialsFetcher); |
| 288 | + |
| 289 | + BasicCredentials credentials = (BasicCredentials) credentialsProvider.getCredentials(); |
| 290 | + Assert.assertEquals(credentials.getAccessKeyId(), "STS.EgnR2nX****FAf9uuqjHS8Ddt"); |
| 291 | + Assert.assertEquals(credentials.getSecretAccessKey(), "CJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV"); |
| 292 | + Assert.assertTrue(credentials.useSecurityToken()); |
| 293 | + Assert.assertTrue(credentials.willSoonExpire()); |
| 294 | + |
| 295 | + meta = NORMAL_METADATA_2 + "\"" + DateUtil.formatAlternativeIso8601Date(new Date()) + "\"}"; |
| 296 | + credentialsFetcher.setExternalData(meta); |
| 297 | + |
| 298 | + credentials = (BasicCredentials) credentialsProvider.getCredentials(); |
| 299 | + Assert.assertEquals(credentials.getAccessKeyId(), "STS.AgnR2nX****FAf9uuqjHS8Ddt"); |
| 300 | + Assert.assertEquals(credentials.getSecretAccessKey(), "AJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV"); |
| 301 | + Assert.assertTrue(credentials.useSecurityToken()); |
| 302 | + Assert.assertTrue(credentials.willSoonExpire()); |
| 303 | + |
| 304 | + long currTime = new Date().getTime() + (3600 * 6 * 15/100 - 100) * 1000; |
| 305 | + meta = NORMAL_METADATA_1 + "\"" + DateUtil.formatAlternativeIso8601Date(new Date(currTime)) + "\"}"; |
| 306 | + credentialsFetcher.setExternalData(meta); |
| 307 | + credentials = (BasicCredentials) credentialsProvider.getCredentials(); |
| 308 | + Assert.assertEquals(credentials.getAccessKeyId(), "STS.EgnR2nX****FAf9uuqjHS8Ddt"); |
| 309 | + Assert.assertEquals(credentials.getSecretAccessKey(), "CJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV"); |
| 310 | + Assert.assertTrue(credentials.useSecurityToken()); |
| 311 | + Assert.assertTrue(credentials.willSoonExpire()); |
| 312 | + |
| 313 | + currTime = new Date().getTime() + (3600 * 6 * 15/100 + 100) * 1000; |
| 314 | + meta = NORMAL_METADATA_1 + "\"" + DateUtil.formatAlternativeIso8601Date(new Date(currTime)) + "\"}"; |
| 315 | + credentialsFetcher.setExternalData(meta); |
| 316 | + credentials = (BasicCredentials) credentialsProvider.getCredentials(); |
| 317 | + Assert.assertEquals(credentials.getAccessKeyId(), "STS.EgnR2nX****FAf9uuqjHS8Ddt"); |
| 318 | + Assert.assertEquals(credentials.getSecretAccessKey(), "CJ7G63EhuZuN8rfSg2Rd****qAgHMhmDuMkp****NPUV"); |
| 319 | + Assert.assertTrue(credentials.useSecurityToken()); |
| 320 | + Assert.assertFalse(credentials.willSoonExpire()); |
| 321 | + |
| 322 | + } catch (Exception e) { |
| 323 | + e.printStackTrace(); |
| 324 | + Assert.fail(e.getMessage()); |
| 325 | + } |
| 326 | + } |
260 | 327 | } |
0 commit comments