1 /* 2 * Copyright 2016, Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 package com.google.auth.oauth2; 33 34 import static org.junit.Assert.assertEquals; 35 import static org.junit.Assert.assertFalse; 36 import static org.junit.Assert.assertTrue; 37 38 import java.io.IOException; 39 import java.util.Date; 40 import org.junit.Test; 41 import org.junit.runner.RunWith; 42 import org.junit.runners.JUnit4; 43 44 /** Unit tests for AccessToken */ 45 @RunWith(JUnit4.class) 46 public class IdTokenTest extends BaseSerializationTest { 47 48 private static final String TOKEN_1 = 49 "eyJhbGciOiJSUzI1NiIsImtpZCI6IjM0OTRiMWU3ODZjZGFkMDkyZTQyMzc2NmJiZTM3ZjU0ZWQ4N2IyMmQiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhdWQiOiJodHRwczovL2Zvby5iYXIiLCJhenAiOiJzdmMtMi00MjlAbWluZXJhbC1taW51dGlhLTgyMC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsInN1YiI6IjEwMDE0NzEwNjk5Njc2NDQ3OTA4NSIsImVtYWlsIjoic3ZjLTItNDI5QG1pbmVyYWwtbWludXRpYS04MjAuaWFtLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaWF0IjoxNTY1Mzg3NTM4LCJleHAiOjE1NjUzOTExMzh9.foo"; 50 private static final String TOKEN_2 = 51 "eyJhbGciOiJSUzI1NiIsImtpZCI6IjM0OTRiMWU3ODZjZGFkMDkyZTQyMzc2NmJiZTM3ZjU0ZWQ4N2IyMmQiLCJ0eXAiOiJKV1QifQ.eyJpc3MiOiJodHRwczovL2FjY291bnRzLmdvb2dsZS5jb20iLCJhdWQiOiJodHRwczovL2Jhci5mb28iLCJhenAiOiJzdmMtMi00MjlAbWluZXJhbC1taW51dGlhLTgyMC5pYW0uZ3NlcnZpY2VhY2NvdW50LmNvbSIsInN1YiI6IjEwMDE0NzEwNjk5Njc2NDQ3OTA4NSIsImVtYWlsIjoic3ZjLTItNDI5QG1pbmVyYWwtbWludXRpYS04MjAuaWFtLmdzZXJ2aWNlYWNjb3VudC5jb20iLCJlbWFpbF92ZXJpZmllZCI6dHJ1ZSwiaWF0IjoxNTY1Mzg4NjM0LCJleHAiOjE1NjUzOTIyMzR9.foo"; 52 private static final String TOKEN_WITH_0x20 = 53 "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPbmxpbmUgSldUIEJ1aWxkZXIiLCJpYXQiOjE2NjAxNjMxNjAsImV4cCI6MTY5MTY5OTE2MCwiYXVkIjoid3d3LmV4YW1wbGUuY29tIiwic3ViIjoianJvY2tldEBleGFtcGxlLmNvbSIsIkdpdmVuTmFtZSI6ImFibTHag3M0x20ifQ.foo"; 54 private static final Date EXPIRATION_DATE = new Date((long) 1565391138 * 1000); 55 56 @Test constructor()57 public void constructor() throws IOException { 58 IdToken idToken = IdToken.create(TOKEN_1); 59 assertEquals(TOKEN_1, idToken.getTokenValue()); 60 assertEquals(EXPIRATION_DATE, idToken.getExpirationTime()); 61 } 62 63 @Test equals_true()64 public void equals_true() throws IOException { 65 IdToken accessToken = IdToken.create(TOKEN_1); 66 IdToken otherAccessToken = IdToken.create(TOKEN_1); 67 assertTrue(accessToken.equals(otherAccessToken)); 68 assertTrue(otherAccessToken.equals(accessToken)); 69 } 70 71 @Test equals_false_token()72 public void equals_false_token() throws IOException { 73 IdToken accessToken = IdToken.create(TOKEN_1); 74 IdToken otherAccessToken = IdToken.create(TOKEN_2); 75 assertFalse(accessToken.equals(otherAccessToken)); 76 assertFalse(otherAccessToken.equals(accessToken)); 77 } 78 79 @Test toString_test()80 public void toString_test() throws IOException { 81 IdToken accessToken = IdToken.create(TOKEN_1); 82 String expectedToString = 83 String.format( 84 "IdToken{tokenValue=%s, JsonWebSignature=JsonWebSignature{header={\"alg\":\"RS256\",\"kid\":\"3494b1e786cdad092e423766bbe37f54ed87b22d\",\"typ\":\"JWT\"}, payload={\"aud\":\"https://foo.bar\",\"exp\":1565391138,\"iat\":1565387538,\"iss\":\"https://accounts.google.com\",\"sub\":\"100147106996764479085\",\"azp\":\"svc-2-429@mineral-minutia-820.iam.gserviceaccount.com\",\"email\":\"svc-2-429@mineral-minutia-820.iam.gserviceaccount.com\",\"email_verified\":true}}}", 85 TOKEN_1); 86 assertEquals(expectedToString, accessToken.toString()); 87 } 88 89 @Test hashCode_equals()90 public void hashCode_equals() throws IOException { 91 IdToken accessToken = IdToken.create(TOKEN_1); 92 IdToken otherAccessToken = IdToken.create(TOKEN_1); 93 assertEquals(accessToken.hashCode(), otherAccessToken.hashCode()); 94 } 95 96 @Test serialize()97 public void serialize() throws IOException, ClassNotFoundException { 98 IdToken accessToken = IdToken.create(TOKEN_1); 99 IdToken deserializedAccessToken = serializeAndDeserialize(accessToken); 100 assertEquals(accessToken, deserializedAccessToken); 101 assertEquals(accessToken.hashCode(), deserializedAccessToken.hashCode()); 102 assertEquals(accessToken.toString(), deserializedAccessToken.toString()); 103 } 104 105 @Test token_with_0x20()106 public void token_with_0x20() throws IOException { 107 IdToken accessToken = IdToken.create(TOKEN_WITH_0x20); 108 assertEquals(TOKEN_WITH_0x20, accessToken.getTokenValue()); 109 } 110 } 111