File: AppEngineCredentialsTest.java

package info (click to toggle)
google-auth-java 0.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 1,208 kB
  • sloc: java: 10,579; xml: 751; sh: 21; makefile: 4
file content (280 lines) | stat: -rw-r--r-- 10,459 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * Copyright 2016, Google Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 *
 *    * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *    * Redistributions in binary form must reproduce the above
 * copyright notice, this list of conditions and the following disclaimer
 * in the documentation and/or other materials provided with the
 * distribution.
 *
 *    * Neither the name of Google Inc. nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.google.auth.oauth2;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import com.google.common.collect.ImmutableMap;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.junit.Test;

public class AppEngineCredentialsTest extends BaseSerializationTest {

  private static final String EXPECTED_ACCESS_TOKEN = "ExpectedAccessToken";
  private static final Date EXPECTED_EXPIRATION_DATE =
      new Date(System.currentTimeMillis() + 60L * 60L * 100L);
  private static final byte[] EXPECTED_SIGNATURE = {0xD, 0xE, 0xA, 0xD};
  private static final String EXPECTED_ACCOUNT = "serviceAccount";

  private static final Collection<String> SCOPES =
      Collections.unmodifiableCollection(Arrays.asList("scope1", "scope2"));

  @Test
  public void constructor_usesAppIdentityService() throws IOException {
    Collection<String> scopes = Collections.singleton("SomeScope");
    TestAppEngineCredentials credentials = new TestAppEngineCredentials(scopes);
    List<String> forNameArgs = credentials.getForNameArgs();
    assertEquals(4, forNameArgs.size());
    assertEquals(AppEngineCredentials.APP_IDENTITY_SERVICE_FACTORY_CLASS, forNameArgs.get(0));
    assertEquals(AppEngineCredentials.APP_IDENTITY_SERVICE_CLASS, forNameArgs.get(1));
    assertEquals(AppEngineCredentials.GET_ACCESS_TOKEN_RESULT_CLASS, forNameArgs.get(2));
    assertEquals(AppEngineCredentials.SIGNING_RESULT_CLASS, forNameArgs.get(3));
  }

  @Test
  public void constructor_noAppEngineRuntime_throwsHelpfulLoadError() throws IOException {
    try {
      new TestAppEngineCredentialsNoSdk();
      fail("Credential expected to fail to load if credential class not present.");
    } catch (IOException e) {
      String message = e.getMessage();
      assertTrue(message.contains("Check that the App Engine SDK is deployed."));
      assertTrue(e.getCause() instanceof ClassNotFoundException);
      assertTrue(
          e.getCause()
              .getMessage()
              .contains(AppEngineCredentials.APP_IDENTITY_SERVICE_FACTORY_CLASS));
    }
  }

  @Test
  public void refreshAccessToken_sameAs() throws IOException {
    TestAppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
    AccessToken accessToken = credentials.refreshAccessToken();
    assertEquals(EXPECTED_ACCESS_TOKEN, accessToken.getTokenValue());
    assertEquals(EXPECTED_EXPIRATION_DATE, accessToken.getExpirationTime());
  }

  @Test
  public void getAccount_sameAs() throws IOException {
    TestAppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
    assertEquals(EXPECTED_ACCOUNT, credentials.getAccount());
  }

  @Test
  public void sign_sameAs() throws IOException {
    TestAppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
    assertArrayEquals(EXPECTED_SIGNATURE, credentials.sign("Bytes to sign".getBytes()));
  }

  @Test
  public void createScoped_clonesWithScopes() throws IOException {
    TestAppEngineCredentials credentials = new TestAppEngineCredentials(null);
    assertTrue(credentials.createScopedRequired());
    try {
      credentials.refreshAccessToken();
      fail("Should not be able to use credential without scopes.");
    } catch (Exception expected) {
      // Expected
    }

    GoogleCredentials scopedCredentials = credentials.createScoped(SCOPES);
    assertNotSame(credentials, scopedCredentials);

    AccessToken accessToken = scopedCredentials.refreshAccessToken();
    assertEquals(EXPECTED_ACCESS_TOKEN, accessToken.getTokenValue());
    assertEquals(EXPECTED_EXPIRATION_DATE, accessToken.getExpirationTime());
  }

  @Test
  public void equals_true() throws IOException {
    GoogleCredentials credentials = new TestAppEngineCredentials(SCOPES);
    GoogleCredentials otherCredentials = new TestAppEngineCredentials(SCOPES);
    assertTrue(credentials.equals(credentials));
    assertTrue(credentials.equals(otherCredentials));
    assertTrue(otherCredentials.equals(credentials));
  }

  @Test
  public void equals_false_scopes() throws IOException {
    final Collection<String> emptyScopes = Collections.emptyList();
    Collection<String> scopes = Collections.singleton("SomeScope");
    AppEngineCredentials credentials = new TestAppEngineCredentials(emptyScopes);
    AppEngineCredentials otherCredentials = new TestAppEngineCredentials(scopes);
    assertFalse(credentials.equals(otherCredentials));
    assertFalse(otherCredentials.equals(credentials));
  }

  @Test
  public void toString_containsFields() throws IOException {
    String expectedToString =
        String.format(
            "TestAppEngineCredentials{scopes=[%s], scopesRequired=%b}", "SomeScope", false);
    Collection<String> scopes = Collections.singleton("SomeScope");
    AppEngineCredentials credentials = new TestAppEngineCredentials(scopes);
    assertEquals(expectedToString, credentials.toString());
  }

  @Test
  public void hashCode_equals() throws IOException {
    AppEngineCredentials credentials = new TestAppEngineCredentials(SCOPES);
    assertEquals(credentials.hashCode(), credentials.hashCode());
  }

  @Test
  public void serialize() throws IOException, ClassNotFoundException {
    Collection<String> scopes = Collections.singleton("SomeScope");
    AppEngineCredentials credentials = new TestAppEngineCredentials(scopes);
    GoogleCredentials deserializedCredentials = serializeAndDeserialize(credentials);
    assertEquals(credentials, deserializedCredentials);
    assertEquals(credentials.hashCode(), deserializedCredentials.hashCode());
    assertEquals(credentials.toString(), deserializedCredentials.toString());
  }

  private static class TestSigningResult {

    private final byte[] signature;

    TestSigningResult(byte[] signature) {
      this.signature = signature;
    }

    public byte[] getSignature() {
      return this.signature;
    }
  }

  private static class TestAppIdentityServiceFactory {

    public static TestAppIdentityService getAppIdentityService() {
      return new TestAppIdentityService();
    }
  }

  private static class TestAppIdentityService {

    public TestGetAccessTokenResult getAccessToken(Iterable<String> scopes) {
      return new TestGetAccessTokenResult(EXPECTED_ACCESS_TOKEN, EXPECTED_EXPIRATION_DATE);
    }

    public String getServiceAccountName() {
      return EXPECTED_ACCOUNT;
    }

    public TestSigningResult signForApp(byte[] toSign) {
      return new TestSigningResult(EXPECTED_SIGNATURE);
    }
  }

  private static class TestGetAccessTokenResult {

    private final String accessToken;
    private final Date expirationTime;

    TestGetAccessTokenResult(String accessToken, Date expirationTime) {
      this.accessToken = accessToken;
      this.expirationTime = expirationTime;
    }

    public String getAccessToken() {
      return this.accessToken;
    }

    public Date getExpirationTime() {
      return this.expirationTime;
    }
  }

  private static class TestAppEngineCredentials extends AppEngineCredentials {

    private static final long serialVersionUID = -5191475572296306231L;

    private static final Map<String, Class<?>> TYPES =
        ImmutableMap.of(
            AppEngineCredentials.APP_IDENTITY_SERVICE_FACTORY_CLASS,
            TestAppIdentityServiceFactory.class,
            AppEngineCredentials.APP_IDENTITY_SERVICE_CLASS,
            TestAppIdentityService.class,
            AppEngineCredentials.GET_ACCESS_TOKEN_RESULT_CLASS,
            TestGetAccessTokenResult.class,
            AppEngineCredentials.SIGNING_RESULT_CLASS,
            TestSigningResult.class);
    private List<String> forNameArgs;

    TestAppEngineCredentials(Collection<String> scopes) throws IOException {
      super(scopes);
    }

    @Override
    Class<?> forName(String className) throws ClassNotFoundException {
      if (forNameArgs == null) {
        forNameArgs = new ArrayList<>();
      }
      forNameArgs.add(className);
      Class<?> lookup = TYPES.get(className);
      if (lookup != null) {
        return lookup;
      }
      throw new ClassNotFoundException(className);
    }

    List<String> getForNameArgs() {
      return forNameArgs;
    }
  }

  private static class TestAppEngineCredentialsNoSdk extends AppEngineCredentials {

    private static final long serialVersionUID = -8987103249265111274L;

    TestAppEngineCredentialsNoSdk() throws IOException {
      super(null);
    }

    @Override
    Class<?> forName(String className) throws ClassNotFoundException {
      throw new ClassNotFoundException(className);
    }
  }
}