• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015, 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.assertNull;
36 
37 import com.google.api.client.json.GenericJson;
38 import com.google.auth.TestUtils;
39 import java.io.IOException;
40 import java.io.InputStream;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.junit.runners.JUnit4;
44 
45 /** Unit tests for ClientId */
46 @RunWith(JUnit4.class)
47 public class ClientIdTest {
48   private static final String CLIENT_ID = "ya29.1.AADtN_UtlxN3PuGAxrN2XQnZTVRvDyVWnYq4I6dws";
49   private static final String CLIENT_SECRET = "jakuaL9YyieakhECKL2SwZcu";
50 
51   @Test
constructor()52   public void constructor() {
53     ClientId clientId =
54         ClientId.newBuilder().setClientId(CLIENT_ID).setClientSecret(CLIENT_SECRET).build();
55 
56     assertEquals(CLIENT_ID, clientId.getClientId());
57     assertEquals(CLIENT_SECRET, clientId.getClientSecret());
58   }
59 
60   @Test(expected = NullPointerException.class)
constructor_nullClientId_throws()61   public void constructor_nullClientId_throws() {
62     ClientId.newBuilder().setClientSecret(CLIENT_SECRET).build();
63   }
64 
65   @Test
constructor_nullClientSecret()66   public void constructor_nullClientSecret() {
67     ClientId clientId = ClientId.newBuilder().setClientId(CLIENT_ID).build();
68     assertEquals(CLIENT_ID, clientId.getClientId());
69     assertNull(clientId.getClientSecret());
70   }
71 
72   @Test
fromJson_web()73   public void fromJson_web() throws IOException {
74     GenericJson json = writeClientIdJson("web", CLIENT_ID, CLIENT_SECRET);
75 
76     ClientId clientId = ClientId.fromJson(json);
77 
78     assertEquals(CLIENT_ID, clientId.getClientId());
79     assertEquals(CLIENT_SECRET, clientId.getClientSecret());
80   }
81 
82   @Test
fromJson_installed()83   public void fromJson_installed() throws IOException {
84     GenericJson json = writeClientIdJson("installed", CLIENT_ID, CLIENT_SECRET);
85 
86     ClientId clientId = ClientId.fromJson(json);
87 
88     assertEquals(CLIENT_ID, clientId.getClientId());
89     assertEquals(CLIENT_SECRET, clientId.getClientSecret());
90   }
91 
92   @Test
fromJson_installedNoSecret()93   public void fromJson_installedNoSecret() throws IOException {
94     GenericJson json = writeClientIdJson("installed", CLIENT_ID, null);
95 
96     ClientId clientId = ClientId.fromJson(json);
97 
98     assertEquals(CLIENT_ID, clientId.getClientId());
99     assertNull(clientId.getClientSecret());
100   }
101 
102   @Test(expected = IOException.class)
fromJson_invalidType_throws()103   public void fromJson_invalidType_throws() throws IOException {
104     GenericJson json = writeClientIdJson("invalid", CLIENT_ID, null);
105 
106     ClientId.fromJson(json);
107   }
108 
109   @Test(expected = IOException.class)
fromJson_noClientId_throws()110   public void fromJson_noClientId_throws() throws IOException {
111     GenericJson json = writeClientIdJson("web", null, null);
112 
113     ClientId.fromJson(json);
114   }
115 
116   @Test(expected = IOException.class)
fromJson_zeroLengthClientId_throws()117   public void fromJson_zeroLengthClientId_throws() throws IOException {
118     GenericJson json = writeClientIdJson("web", "", null);
119 
120     ClientId.fromJson(json);
121   }
122 
123   @Test
fromResource()124   public void fromResource() throws IOException {
125     ClientId clientId = ClientId.fromResource(ClientIdTest.class, "/client_secret.json");
126 
127     assertEquals(CLIENT_ID, clientId.getClientId());
128     assertEquals(CLIENT_SECRET, clientId.getClientSecret());
129   }
130 
131   @Test(expected = NullPointerException.class)
fromResource_badResource()132   public void fromResource_badResource() throws IOException {
133     ClientId.fromResource(ClientIdTest.class, "invalid.json");
134   }
135 
136   @Test
fromStream()137   public void fromStream() throws IOException {
138     String text =
139         "{"
140             + "\"web\": {"
141             + "\"client_id\" : \""
142             + CLIENT_ID
143             + "\","
144             + "\"client_secret\" : \""
145             + CLIENT_SECRET
146             + "\""
147             + "}"
148             + "}";
149     InputStream stream = TestUtils.stringToInputStream(text);
150 
151     ClientId clientId = ClientId.fromStream(stream);
152 
153     assertEquals(CLIENT_ID, clientId.getClientId());
154     assertEquals(CLIENT_SECRET, clientId.getClientSecret());
155   }
156 
157   @Test
fromStream_invalidJson_doesNotThrow()158   public void fromStream_invalidJson_doesNotThrow() throws IOException {
159     String invalidJson =
160         "{"
161             + "\"web\": {"
162             + "\"client_id\" : \""
163             + CLIENT_ID
164             + "\","
165             + "\"client_secret\" : \""
166             + CLIENT_SECRET
167             + "\""
168             + "}"; // No closing brace
169     InputStream stream = TestUtils.stringToInputStream(invalidJson);
170 
171     ClientId.fromStream(stream);
172   }
173 
writeClientIdJson(String type, String clientId, String clientSecret)174   private GenericJson writeClientIdJson(String type, String clientId, String clientSecret) {
175     GenericJson json = new GenericJson();
176     GenericJson details = new GenericJson();
177     if (clientId != null) {
178       details.put("client_id", clientId);
179     }
180     if (clientSecret != null) {
181       details.put("client_secret", clientSecret);
182     }
183     json.put(type, details);
184     return json;
185   }
186 }
187