• 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.assertFalse;
36 import static org.junit.Assert.assertSame;
37 import static org.junit.Assert.assertTrue;
38 
39 import com.google.api.client.util.Clock;
40 import java.io.BufferedReader;
41 import java.io.IOException;
42 import java.io.InputStreamReader;
43 import java.io.PrintWriter;
44 import java.net.ServerSocket;
45 import java.net.Socket;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.junit.runners.JUnit4;
49 
50 /** Unit tests for CloudShellCredentials */
51 @RunWith(JUnit4.class)
52 public class CloudShellCredentialsTest extends BaseSerializationTest {
53 
54   @Test
refreshAccessToken()55   public void refreshAccessToken() throws IOException {
56     final ServerSocket authSocket = new ServerSocket(0);
57     try {
58       Runnable serverTask =
59           new Runnable() {
60             @Override
61             public void run() {
62               try {
63                 Socket clientSocket = authSocket.accept();
64                 BufferedReader input =
65                     new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
66                 String lines = input.readLine();
67                 lines += '\n' + input.readLine();
68                 assertEquals(lines, CloudShellCredentials.GET_AUTH_TOKEN_REQUEST);
69 
70                 PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
71                 out.println("32\n[\"email\", \"project-id\", \"token\"]");
72               } catch (Exception reThrown) {
73                 throw new RuntimeException(reThrown);
74               }
75             }
76           };
77       Thread serverThread = new Thread(serverTask);
78       serverThread.start();
79 
80       GoogleCredentials creds =
81           CloudShellCredentials.newBuilder().setAuthPort(authSocket.getLocalPort()).build();
82       assertEquals("token", creds.refreshAccessToken().getTokenValue());
83     } finally {
84       authSocket.close();
85     }
86   }
87 
88   @Test
equals_true()89   public void equals_true() throws IOException {
90     GoogleCredentials credentials = CloudShellCredentials.newBuilder().setAuthPort(42).build();
91     GoogleCredentials otherCredentials = CloudShellCredentials.newBuilder().setAuthPort(42).build();
92     assertTrue(credentials.equals(otherCredentials));
93     assertTrue(otherCredentials.equals(credentials));
94   }
95 
96   @Test
equals_false_authPort()97   public void equals_false_authPort() throws IOException {
98     GoogleCredentials credentials = CloudShellCredentials.newBuilder().setAuthPort(42).build();
99     GoogleCredentials otherCredentials = CloudShellCredentials.newBuilder().setAuthPort(43).build();
100     assertFalse(credentials.equals(otherCredentials));
101     assertFalse(otherCredentials.equals(credentials));
102   }
103 
104   @Test
toString_containsFields()105   public void toString_containsFields() throws IOException {
106     String expectedToString = String.format("CloudShellCredentials{authPort=%d}", 42);
107     GoogleCredentials credentials = CloudShellCredentials.newBuilder().setAuthPort(42).build();
108     assertEquals(expectedToString, credentials.toString());
109   }
110 
111   @Test
hashCode_equals()112   public void hashCode_equals() throws IOException {
113     GoogleCredentials credentials = CloudShellCredentials.newBuilder().setAuthPort(42).build();
114     GoogleCredentials otherCredentials = CloudShellCredentials.newBuilder().setAuthPort(42).build();
115     assertEquals(credentials.hashCode(), otherCredentials.hashCode());
116   }
117 
118   @Test
serialize()119   public void serialize() throws IOException, ClassNotFoundException {
120     GoogleCredentials credentials = CloudShellCredentials.newBuilder().setAuthPort(42).build();
121     GoogleCredentials deserializedCredentials = serializeAndDeserialize(credentials);
122     assertEquals(credentials, deserializedCredentials);
123     assertEquals(credentials.hashCode(), deserializedCredentials.hashCode());
124     assertEquals(credentials.toString(), deserializedCredentials.toString());
125     assertSame(deserializedCredentials.clock, Clock.SYSTEM);
126   }
127 
128   @Test
toBuilder()129   public void toBuilder() {
130     CloudShellCredentials credentials =
131         CloudShellCredentials.newBuilder()
132             .setAuthPort(42)
133             .setQuotaProjectId("quota-project")
134             .build();
135 
136     assertEquals(42, credentials.getAuthPort());
137     assertEquals("quota-project", credentials.getQuotaProjectId());
138 
139     CloudShellCredentials secondCredentials = credentials.toBuilder().build();
140 
141     assertEquals(credentials, secondCredentials);
142   }
143 }
144