• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.assertNotSame;
37 import static org.junit.Assert.assertSame;
38 import static org.junit.Assert.assertTrue;
39 
40 import java.io.IOException;
41 import java.util.ArrayList;
42 import java.util.Arrays;
43 import java.util.Date;
44 import java.util.List;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.junit.runners.JUnit4;
48 
49 /** Unit tests for AccessToken */
50 @RunWith(JUnit4.class)
51 public class AccessTokenTest extends BaseSerializationTest {
52 
53   private static final String TOKEN = "AccessToken";
54   private static final Date EXPIRATION_DATE = new Date();
55   private static final List<String> SCOPES = Arrays.asList("scope1", "scope2");
56   private static final String SCOPES_STRING = "scope1 scope2";
57 
58   @Test
constructor()59   public void constructor() {
60     AccessToken accessToken = new AccessToken(TOKEN, EXPIRATION_DATE);
61     assertEquals(TOKEN, accessToken.getTokenValue());
62     assertEquals(EXPIRATION_DATE, accessToken.getExpirationTime());
63     assertEquals(EXPIRATION_DATE.getTime(), (long) accessToken.getExpirationTimeMillis());
64     assertEquals(new ArrayList<>(), accessToken.getScopes());
65   }
66 
67   @Test
builder()68   public void builder() {
69     AccessToken accessToken =
70         AccessToken.newBuilder()
71             .setExpirationTime(EXPIRATION_DATE)
72             .setTokenValue(TOKEN)
73             .setScopes(SCOPES_STRING)
74             .build();
75     assertEquals(TOKEN, accessToken.getTokenValue());
76     assertEquals(EXPIRATION_DATE, accessToken.getExpirationTime());
77     assertEquals(EXPIRATION_DATE.getTime(), (long) accessToken.getExpirationTimeMillis());
78     assertEquals(SCOPES, accessToken.getScopes());
79     assertNotSame(SCOPES, accessToken.getScopes());
80 
81     // scopes list
82     accessToken =
83         AccessToken.newBuilder()
84             .setExpirationTime(EXPIRATION_DATE)
85             .setTokenValue(TOKEN)
86             .setScopes(SCOPES)
87             .build();
88     assertEquals(SCOPES, accessToken.getScopes());
89     assertSame(SCOPES, accessToken.getScopes());
90 
91     // single scope
92     accessToken =
93         AccessToken.newBuilder()
94             .setExpirationTime(EXPIRATION_DATE)
95             .setTokenValue(TOKEN)
96             .setScopes("dummy")
97             .build();
98     assertEquals(Arrays.asList("dummy"), accessToken.getScopes());
99 
100     // empty scope
101     accessToken =
102         AccessToken.newBuilder()
103             .setExpirationTime(EXPIRATION_DATE)
104             .setTokenValue(TOKEN)
105             .setScopes("  ")
106             .build();
107     assertEquals(new ArrayList<>(), accessToken.getScopes());
108   }
109 
110   @Test
equals_true()111   public void equals_true() throws IOException {
112     AccessToken accessToken =
113         AccessToken.newBuilder()
114             .setExpirationTime(EXPIRATION_DATE)
115             .setTokenValue(TOKEN)
116             .setScopes(SCOPES)
117             .build();
118 
119     AccessToken otherAccessToken =
120         AccessToken.newBuilder()
121             .setExpirationTime(EXPIRATION_DATE)
122             .setTokenValue(TOKEN)
123             .setTokenValue(TOKEN)
124             .setScopes(SCOPES)
125             .build();
126 
127     assertTrue(accessToken.equals(otherAccessToken));
128     assertTrue(otherAccessToken.equals(accessToken));
129   }
130 
131   @Test
equals_false_scopes()132   public void equals_false_scopes() throws IOException {
133     AccessToken accessToken =
134         AccessToken.newBuilder()
135             .setExpirationTime(EXPIRATION_DATE)
136             .setTokenValue(TOKEN)
137             .setScopes(SCOPES)
138             .build();
139 
140     AccessToken otherAccessToken =
141         AccessToken.newBuilder()
142             .setExpirationTime(EXPIRATION_DATE)
143             .setTokenValue(TOKEN)
144             .setScopes(Arrays.asList("scope1"))
145             .build();
146 
147     assertFalse(accessToken.equals(otherAccessToken));
148     assertFalse(otherAccessToken.equals(accessToken));
149   }
150 
151   @Test
equals_false_token()152   public void equals_false_token() throws IOException {
153     AccessToken accessToken =
154         AccessToken.newBuilder()
155             .setExpirationTime(EXPIRATION_DATE)
156             .setTokenValue(TOKEN)
157             .setScopes(SCOPES)
158             .build();
159 
160     AccessToken otherAccessToken =
161         AccessToken.newBuilder()
162             .setExpirationTime(EXPIRATION_DATE)
163             .setTokenValue("otherToken")
164             .setScopes(SCOPES)
165             .build();
166 
167     assertFalse(accessToken.equals(otherAccessToken));
168     assertFalse(otherAccessToken.equals(accessToken));
169   }
170 
171   @Test
equals_false_expirationDate()172   public void equals_false_expirationDate() throws IOException {
173     AccessToken accessToken =
174         AccessToken.newBuilder()
175             .setExpirationTime(EXPIRATION_DATE)
176             .setTokenValue(TOKEN)
177             .setScopes(SCOPES)
178             .build();
179 
180     AccessToken otherAccessToken =
181         AccessToken.newBuilder()
182             .setExpirationTime(new Date(EXPIRATION_DATE.getTime() + 42))
183             .setTokenValue(TOKEN)
184             .setScopes(SCOPES)
185             .build();
186 
187     assertFalse(accessToken.equals(otherAccessToken));
188     assertFalse(otherAccessToken.equals(accessToken));
189   }
190 
191   @Test
toString_containsFields()192   public void toString_containsFields() {
193     AccessToken accessToken =
194         AccessToken.newBuilder()
195             .setExpirationTime(EXPIRATION_DATE)
196             .setTokenValue(TOKEN)
197             .setScopes(SCOPES)
198             .build();
199     String expectedToString =
200         String.format(
201             "AccessToken{tokenValue=%s, expirationTimeMillis=%d, scopes=%s}",
202             TOKEN, EXPIRATION_DATE.getTime(), SCOPES);
203     assertEquals(expectedToString, accessToken.toString());
204   }
205 
206   @Test
hashCode_equals()207   public void hashCode_equals() throws IOException {
208     AccessToken accessToken =
209         AccessToken.newBuilder()
210             .setExpirationTime(EXPIRATION_DATE)
211             .setTokenValue(TOKEN)
212             .setScopes(SCOPES)
213             .build();
214 
215     AccessToken otherAccessToken =
216         AccessToken.newBuilder()
217             .setExpirationTime(EXPIRATION_DATE)
218             .setTokenValue(TOKEN)
219             .setScopes(SCOPES)
220             .build();
221 
222     assertEquals(accessToken.hashCode(), otherAccessToken.hashCode());
223   }
224 
225   @Test
serialize()226   public void serialize() throws IOException, ClassNotFoundException {
227     AccessToken emptyScopes =
228         AccessToken.newBuilder()
229             .setExpirationTime(EXPIRATION_DATE)
230             .setTokenValue(TOKEN)
231             .setScopes("")
232             .build();
233 
234     AccessToken deserializedAccessToken = serializeAndDeserialize(emptyScopes);
235     assertEquals(emptyScopes, deserializedAccessToken);
236     assertEquals(emptyScopes.hashCode(), deserializedAccessToken.hashCode());
237     assertEquals(emptyScopes.toString(), deserializedAccessToken.toString());
238     assertEquals(new ArrayList<>(), deserializedAccessToken.getScopes());
239 
240     AccessToken accessToken =
241         AccessToken.newBuilder()
242             .setExpirationTime(EXPIRATION_DATE)
243             .setTokenValue(TOKEN)
244             .setScopes(SCOPES)
245             .build();
246 
247     deserializedAccessToken = serializeAndDeserialize(accessToken);
248     assertEquals(accessToken, deserializedAccessToken);
249     assertEquals(accessToken.hashCode(), deserializedAccessToken.hashCode());
250     assertEquals(accessToken.toString(), deserializedAccessToken.toString());
251   }
252 }
253