1 /* 2 * Copyright 2014 The gRPC Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package io.grpc; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertSame; 21 22 import io.grpc.Status.Code; 23 import java.nio.charset.Charset; 24 import org.junit.Test; 25 import org.junit.runner.RunWith; 26 import org.junit.runners.JUnit4; 27 28 /** Unit tests for {@link Status}. */ 29 @RunWith(JUnit4.class) 30 public class StatusTest { 31 private final Charset ascii = Charset.forName("US-ASCII"); 32 33 @Test verifyExceptionMessage()34 public void verifyExceptionMessage() { 35 assertEquals("UNKNOWN", Status.UNKNOWN.asRuntimeException().getMessage()); 36 assertEquals("CANCELLED: This is a test", 37 Status.CANCELLED.withDescription("This is a test").asRuntimeException().getMessage()); 38 assertEquals("UNKNOWN", Status.UNKNOWN.asException().getMessage()); 39 assertEquals("CANCELLED: This is a test", 40 Status.CANCELLED.withDescription("This is a test").asException().getMessage()); 41 } 42 43 @Test impossibleCodeValue()44 public void impossibleCodeValue() { 45 assertEquals(Code.UNKNOWN, Status.fromCodeValue(-1).getCode()); 46 assertEquals(Code.UNKNOWN, Status.fromCodeValue(17).getCode()); 47 } 48 49 @Test sameCauseReturnsSelf()50 public void sameCauseReturnsSelf() { 51 assertSame(Status.CANCELLED, Status.CANCELLED.withCause(null)); 52 } 53 54 @Test sameDescriptionReturnsSelf()55 public void sameDescriptionReturnsSelf() { 56 assertSame(Status.CANCELLED, Status.CANCELLED.withDescription(null)); 57 assertSame(Status.CANCELLED, Status.CANCELLED.augmentDescription(null)); 58 } 59 60 @Test useObjectHashCode()61 public void useObjectHashCode() { 62 assertEquals(Status.CANCELLED.hashCode(), System.identityHashCode(Status.CANCELLED)); 63 } 64 65 @Test metadataEncode_lowAscii()66 public void metadataEncode_lowAscii() { 67 byte[] b = Status.MESSAGE_KEY.toBytes("my favorite character is \u0000"); 68 assertEquals("my favorite character is %00", new String(b, ascii)); 69 } 70 71 @Test metadataEncode_percent()72 public void metadataEncode_percent() { 73 byte[] b = Status.MESSAGE_KEY.toBytes("my favorite character is %"); 74 assertEquals("my favorite character is %25", new String(b, ascii)); 75 } 76 77 @Test metadataEncode_surrogatePair()78 public void metadataEncode_surrogatePair() { 79 byte[] b = Status.MESSAGE_KEY.toBytes("my favorite character is "); 80 assertEquals("my favorite character is %F0%90%80%81", new String(b, ascii)); 81 } 82 83 @Test metadataEncode_unmatchedHighSurrogate()84 public void metadataEncode_unmatchedHighSurrogate() { 85 byte[] b = Status.MESSAGE_KEY.toBytes("my favorite character is " + ((char) 0xD801)); 86 assertEquals("my favorite character is ?", new String(b, ascii)); 87 } 88 89 @Test metadataEncode_unmatchedLowSurrogate()90 public void metadataEncode_unmatchedLowSurrogate() { 91 byte[] b = Status.MESSAGE_KEY.toBytes("my favorite character is " + ((char)0xDC37)); 92 assertEquals("my favorite character is ?", new String(b, ascii)); 93 } 94 95 @Test metadataEncode_maxSurrogatePair()96 public void metadataEncode_maxSurrogatePair() { 97 byte[] b = Status.MESSAGE_KEY.toBytes( 98 "my favorite character is " + ((char)0xDBFF) + ((char)0xDFFF)); 99 assertEquals("my favorite character is %F4%8F%BF%BF", new String(b, ascii)); 100 } 101 102 @Test metadataDecode_ascii()103 public void metadataDecode_ascii() { 104 String s = Status.MESSAGE_KEY.parseBytes(new byte[]{'H', 'e', 'l', 'l', 'o'}); 105 assertEquals("Hello", s); 106 } 107 108 @Test metadataDecode_percent()109 public void metadataDecode_percent() { 110 String s = Status.MESSAGE_KEY.parseBytes(new byte[]{'H', '%', '6', '1', 'o'}); 111 assertEquals("Hao", s); 112 } 113 114 @Test metadataDecode_percentUnderflow()115 public void metadataDecode_percentUnderflow() { 116 String s = Status.MESSAGE_KEY.parseBytes(new byte[]{'H', '%', '6'}); 117 assertEquals("H%6", s); 118 } 119 120 @Test metadataDecode_surrogate()121 public void metadataDecode_surrogate() { 122 String s = Status.MESSAGE_KEY.parseBytes( 123 new byte[]{'%', 'F', '0', '%', '9', '0', '%', '8', '0', '%', '8', '1'}); 124 assertEquals("", s); 125 } 126 127 @Test metadataDecode_badEncoding()128 public void metadataDecode_badEncoding() { 129 String s = Status.MESSAGE_KEY.parseBytes(new byte[]{'%', 'G', '0'}); 130 assertEquals("%G0", s); 131 } 132 } 133