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; 33 34 import static org.junit.Assert.assertFalse; 35 import static org.junit.Assert.assertNotNull; 36 import static org.junit.Assert.assertTrue; 37 38 import com.google.api.client.http.HttpHeaders; 39 import com.google.api.client.http.HttpResponseException; 40 import com.google.api.client.json.GenericJson; 41 import com.google.api.client.json.JsonFactory; 42 import com.google.api.client.json.gson.GsonFactory; 43 import com.google.auth.http.AuthHttpConstants; 44 import com.google.common.base.Splitter; 45 import com.google.common.collect.Lists; 46 import java.io.ByteArrayInputStream; 47 import java.io.IOException; 48 import java.io.InputStream; 49 import java.io.UnsupportedEncodingException; 50 import java.net.URLDecoder; 51 import java.text.SimpleDateFormat; 52 import java.util.Calendar; 53 import java.util.Date; 54 import java.util.HashMap; 55 import java.util.List; 56 import java.util.Map; 57 import javax.annotation.Nullable; 58 59 /** Utilities for test code under com.google.auth. */ 60 public class TestUtils { 61 62 private static final JsonFactory JSON_FACTORY = GsonFactory.getDefaultInstance(); 63 assertContainsBearerToken(Map<String, List<String>> metadata, String token)64 public static void assertContainsBearerToken(Map<String, List<String>> metadata, String token) { 65 assertNotNull(metadata); 66 assertNotNull(token); 67 assertTrue("Bearer token not found", hasBearerToken(metadata, token)); 68 } 69 assertNotContainsBearerToken( Map<String, List<String>> metadata, String token)70 public static void assertNotContainsBearerToken( 71 Map<String, List<String>> metadata, String token) { 72 assertNotNull(metadata); 73 assertNotNull(token); 74 assertFalse("Bearer token found", hasBearerToken(metadata, token)); 75 } 76 hasBearerToken(Map<String, List<String>> metadata, String token)77 private static boolean hasBearerToken(Map<String, List<String>> metadata, String token) { 78 String expectedValue = AuthHttpConstants.BEARER + " " + token; 79 List<String> authorizations = metadata.get(AuthHttpConstants.AUTHORIZATION); 80 assertNotNull("Authorization headers not found", authorizations); 81 return authorizations.contains(expectedValue); 82 } 83 jsonToInputStream(GenericJson json)84 public static InputStream jsonToInputStream(GenericJson json) throws IOException { 85 json.setFactory(JSON_FACTORY); 86 String text = json.toPrettyString(); 87 return new ByteArrayInputStream(text.getBytes("UTF-8")); 88 } 89 stringToInputStream(String text)90 public static InputStream stringToInputStream(String text) { 91 try { 92 return new ByteArrayInputStream(text.getBytes("UTF-8")); 93 } catch (UnsupportedEncodingException e) { 94 throw new RuntimeException("Unexpected encoding exception", e); 95 } 96 } 97 parseQuery(String query)98 public static Map<String, String> parseQuery(String query) throws IOException { 99 Map<String, String> map = new HashMap<>(); 100 Iterable<String> entries = Splitter.on('&').split(query); 101 for (String entry : entries) { 102 List<String> sides = Lists.newArrayList(Splitter.on('=').split(entry)); 103 if (sides.size() != 2) { 104 throw new IOException("Invalid Query String"); 105 } 106 String key = URLDecoder.decode(sides.get(0), "UTF-8"); 107 String value = URLDecoder.decode(sides.get(1), "UTF-8"); 108 map.put(key, value); 109 } 110 return map; 111 } 112 errorJson(String message)113 public static String errorJson(String message) throws IOException { 114 GenericJson errorResponse = new GenericJson(); 115 errorResponse.setFactory(JSON_FACTORY); 116 GenericJson errorObject = new GenericJson(); 117 errorObject.put("message", message); 118 errorResponse.put("error", errorObject); 119 return errorResponse.toPrettyString(); 120 } 121 buildHttpResponseException( String error, @Nullable String errorDescription, @Nullable String errorUri)122 public static HttpResponseException buildHttpResponseException( 123 String error, @Nullable String errorDescription, @Nullable String errorUri) 124 throws IOException { 125 GenericJson json = new GenericJson(); 126 json.setFactory(GsonFactory.getDefaultInstance()); 127 json.set("error", error); 128 if (errorDescription != null) { 129 json.set("error_description", errorDescription); 130 } 131 if (errorUri != null) { 132 json.set("error_uri", errorUri); 133 } 134 return new HttpResponseException.Builder( 135 /* statusCode= */ 400, /* statusMessage= */ "statusMessage", new HttpHeaders()) 136 .setContent(json.toPrettyString()) 137 .build(); 138 } 139 getDefaultExpireTime()140 public static String getDefaultExpireTime() { 141 Calendar calendar = Calendar.getInstance(); 142 calendar.setTime(new Date()); 143 calendar.add(Calendar.SECOND, 300); 144 return new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").format(calendar.getTime()); 145 } 146 TestUtils()147 private TestUtils() {} 148 } 149