|
17 | 17 | package io.grpc.netty; |
18 | 18 |
|
19 | 19 | import static com.google.common.truth.Truth.assertThat; |
| 20 | +import static com.google.common.truth.TruthJUnit.assume; |
20 | 21 | import static org.junit.Assert.fail; |
21 | 22 | import static org.mockito.Mockito.mock; |
22 | 23 | import static org.mockito.Mockito.verify; |
|
26 | 27 | import io.grpc.NameResolver; |
27 | 28 | import io.grpc.NameResolver.ServiceConfigParser; |
28 | 29 | import io.grpc.SynchronizationContext; |
| 30 | +import io.grpc.Uri; |
29 | 31 | import io.grpc.internal.FakeClock; |
30 | 32 | import io.grpc.internal.GrpcUtil; |
31 | 33 | import io.netty.channel.unix.DomainSocketAddress; |
32 | 34 | import java.net.SocketAddress; |
33 | 35 | import java.net.URI; |
| 36 | +import java.util.Arrays; |
34 | 37 | import java.util.List; |
35 | 38 | import org.junit.Rule; |
36 | 39 | import org.junit.Test; |
37 | 40 | import org.junit.runner.RunWith; |
38 | | -import org.junit.runners.JUnit4; |
| 41 | +import org.junit.runners.Parameterized; |
| 42 | +import org.junit.runners.Parameterized.Parameter; |
| 43 | +import org.junit.runners.Parameterized.Parameters; |
39 | 44 | import org.mockito.ArgumentCaptor; |
40 | 45 | import org.mockito.Captor; |
41 | 46 | import org.mockito.Mock; |
42 | 47 | import org.mockito.junit.MockitoJUnit; |
43 | 48 | import org.mockito.junit.MockitoRule; |
44 | 49 |
|
45 | 50 | /** Unit tests for {@link UdsNameResolverProvider}. */ |
46 | | -@RunWith(JUnit4.class) |
| 51 | +@RunWith(Parameterized.class) |
47 | 52 | public class UdsNameResolverProviderTest { |
48 | 53 | private static final int DEFAULT_PORT = 887; |
| 54 | + |
| 55 | + @Parameters(name = "enableRfc3986UrisParam={0}") |
| 56 | + public static Iterable<Object[]> data() { |
| 57 | + return Arrays.asList(new Object[][] {{true}, {false}}); |
| 58 | + } |
| 59 | + |
| 60 | + @Parameter public boolean enableRfc3986UrisParam; |
| 61 | + |
49 | 62 | @Rule |
50 | 63 | public final MockitoRule mocks = MockitoJUnit.rule(); |
51 | 64 |
|
@@ -73,38 +86,59 @@ public class UdsNameResolverProviderTest { |
73 | 86 |
|
74 | 87 | @Test |
75 | 88 | public void testUnixRelativePath() { |
76 | | - UdsNameResolver udsNameResolver = |
77 | | - udsNameResolverProvider.newNameResolver(URI.create("unix:sock.sock"), args); |
| 89 | + UdsNameResolver udsNameResolver = newNameResolver("unix:sock.sock", args); |
78 | 90 | DomainSocketAddress domainSocketAddress = startAndGetUniqueResolvedAddress(udsNameResolver); |
79 | 91 | assertThat(domainSocketAddress.path()).isEqualTo("sock.sock"); |
80 | 92 | } |
81 | 93 |
|
82 | 94 | @Test |
83 | 95 | public void testUnixAbsolutePath() { |
84 | | - UdsNameResolver udsNameResolver = |
85 | | - udsNameResolverProvider.newNameResolver(URI.create("unix:/sock.sock"), args); |
| 96 | + UdsNameResolver udsNameResolver = newNameResolver("unix:/sock.sock", args); |
86 | 97 | DomainSocketAddress domainSocketAddress = startAndGetUniqueResolvedAddress(udsNameResolver); |
87 | 98 | assertThat(domainSocketAddress.path()).isEqualTo("/sock.sock"); |
88 | 99 | } |
89 | 100 |
|
90 | 101 | @Test |
91 | 102 | public void testUnixAbsoluteAlternatePath() { |
92 | | - UdsNameResolver udsNameResolver = |
93 | | - udsNameResolverProvider.newNameResolver(URI.create("unix:///sock.sock"), args); |
| 103 | + UdsNameResolver udsNameResolver = newNameResolver("unix:///sock.sock", args); |
94 | 104 | DomainSocketAddress domainSocketAddress = startAndGetUniqueResolvedAddress(udsNameResolver); |
95 | 105 | assertThat(domainSocketAddress.path()).isEqualTo("/sock.sock"); |
96 | 106 | } |
97 | 107 |
|
98 | 108 | @Test |
99 | 109 | public void testUnixPathWithAuthority() { |
100 | 110 | try { |
101 | | - udsNameResolverProvider.newNameResolver(URI.create("unix://localhost/sock.sock"), args); |
| 111 | + newNameResolver("unix://localhost/sock.sock", args); |
102 | 112 | fail("exception expected"); |
103 | 113 | } catch (IllegalArgumentException e) { |
104 | 114 | assertThat(e).hasMessageThat().isEqualTo("authority not supported: localhost"); |
105 | 115 | } |
106 | 116 | } |
107 | 117 |
|
| 118 | + @Test |
| 119 | + public void testUnixAbsolutePathDoesNotIncludeQueryOrFragment() { |
| 120 | + UdsNameResolver udsNameResolver = newNameResolver("unix:///sock.sock?query#fragment", args); |
| 121 | + DomainSocketAddress domainSocketAddress = startAndGetUniqueResolvedAddress(udsNameResolver); |
| 122 | + assertThat(domainSocketAddress.path()).isEqualTo("/sock.sock"); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testUnixRelativePathDoesNotIncludeQueryOrFragment() { |
| 127 | + // This test fails without RFC 3986 support because of a bug in the legacy java.net.URI-based |
| 128 | + // NRP implementation. |
| 129 | + assume().that(enableRfc3986UrisParam).isTrue(); |
| 130 | + |
| 131 | + UdsNameResolver udsNameResolver = newNameResolver("unix:sock.sock?query#fragment", args); |
| 132 | + DomainSocketAddress domainSocketAddress = startAndGetUniqueResolvedAddress(udsNameResolver); |
| 133 | + assertThat(domainSocketAddress.path()).isEqualTo("sock.sock"); |
| 134 | + } |
| 135 | + |
| 136 | + private UdsNameResolver newNameResolver(String uriString, NameResolver.Args args) { |
| 137 | + return enableRfc3986UrisParam |
| 138 | + ? (UdsNameResolver) udsNameResolverProvider.newNameResolver(Uri.create(uriString), args) |
| 139 | + : udsNameResolverProvider.newNameResolver(URI.create(uriString), args); |
| 140 | + } |
| 141 | + |
108 | 142 | private DomainSocketAddress startAndGetUniqueResolvedAddress(UdsNameResolver udsNameResolver) { |
109 | 143 | assertThat(udsNameResolver).isNotNull(); |
110 | 144 | udsNameResolver.start(mockListener); |
|
0 commit comments