### Question:
ChannelzProtoUtil { static Socket toSocket(InternalInstrumented<SocketStats> obj) { SocketStats socketStats = getFuture(obj.getStats()); Socket.Builder builder = Socket.newBuilder() .setRef(toSocketRef(obj)) .setLocal(toAddress(socketStats.local)); if (socketStats.security != null) { builder.setSecurity(toSecurity(socketStats.security)); } if (socketStats.remote != null) { builder.setRemote(toAddress(socketStats.remote)); } builder.setData(extractSocketData(socketStats)); return builder.build(); } private  ChannelzProtoUtil();  static final String SO_LINGER; static final String SO_TIMEOUT; static final String TCP_INFO; }### Answer:
@Test public void toSocket_withDataNoOptions() throws Exception { assertEquals( Socket .newBuilder() .setRef(socketRef) .setLocal(localAddress) .setRemote(remoteAddress) .setData(socketDataWithDataNoSockOpts) .build(), ChannelzProtoUtil.toSocket(socket)); }
@Test public void toSocket_noDataWithOptions() throws Exception { assertEquals( Socket .newBuilder() .setRef(listenSocketRef) .setLocal(listenAddress) .setData( SocketData .newBuilder() .addOption( SocketOption .newBuilder() .setName("listen_option") .setValue("listen_option_value"))) .build(), ChannelzProtoUtil.toSocket(listenSocket)); }
@Test public void toSocket_withDataWithOptions() throws Exception { socket.socketOptions = new SocketOptions(null, null, null, ImmutableMap.of("test_name", "test_value")); assertEquals( Socket .newBuilder() .setRef(socketRef) .setLocal(localAddress) .setRemote(remoteAddress) .setData( SocketData .newBuilder(socketDataWithDataNoSockOpts) .addOption( SocketOption.newBuilder() .setName("test_name").setValue("test_value"))) .build(), ChannelzProtoUtil.toSocket(socket)); }
@Test public void socketSecurityTls() throws Exception { Certificate local = mock(Certificate.class); Certificate remote = mock(Certificate.class); when(local.getEncoded()).thenReturn("localcert".getBytes(Charsets.UTF_8)); when(remote.getEncoded()).thenReturn("remotecert".getBytes(Charsets.UTF_8)); socket.security = new InternalChannelz.Security( new InternalChannelz.Tls("TLS_NULL_WITH_NULL_NULL", local, remote)); assertEquals( Security.newBuilder().setTls( Tls.newBuilder() .setStandardName("TLS_NULL_WITH_NULL_NULL") .setLocalCertificate(ByteString.copyFrom("localcert", Charsets.UTF_8)) .setRemoteCertificate(ByteString.copyFrom("remotecert", Charsets.UTF_8))) .build(), ChannelzProtoUtil.toSocket(socket).getSecurity()); socket.security = new InternalChannelz.Security( new InternalChannelz.Tls("TLS_NULL_WITH_NULL_NULL", null, remote)); assertEquals( Security.newBuilder().setTls( Tls.newBuilder() .setStandardName("TLS_NULL_WITH_NULL_NULL") .setRemoteCertificate(ByteString.copyFrom("remotecert", Charsets.UTF_8))) .build(), ChannelzProtoUtil.toSocket(socket).getSecurity()); socket.security = new InternalChannelz.Security( new InternalChannelz.Tls("TLS_NULL_WITH_NULL_NULL", local, null)); assertEquals( Security.newBuilder().setTls( Tls.newBuilder() .setStandardName("TLS_NULL_WITH_NULL_NULL") .setLocalCertificate(ByteString.copyFrom("localcert", Charsets.UTF_8))) .build(), ChannelzProtoUtil.toSocket(socket).getSecurity()); }
@Test public void socketSecurityOther() throws Exception { Message contents = GetChannelRequest.newBuilder().setChannelId(1).build(); Any packed = Any.pack(contents); socket.security = new InternalChannelz.Security( new InternalChannelz.OtherSecurity("other_security", packed)); assertEquals( Security.newBuilder().setOther( OtherSecurity.newBuilder().setName("other_security").setValue(packed)) .build(), ChannelzProtoUtil.toSocket(socket).getSecurity()); }