Skip to content

Commit

Permalink
Fixing issue where WILD XAuthority entries do not have an address. Fi…
Browse files Browse the repository at this point in the history
…xed major bug when writing requests where the header for extensions did not replace the second byte with OPCODE.
  • Loading branch information
moaxcp committed Dec 20, 2023
1 parent 53f900f commit 03ebdf5
Show file tree
Hide file tree
Showing 18 changed files with 76 additions and 63 deletions.
2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions .idea/modules/x11-client.test.iml

This file was deleted.

9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ This library can be added to your project using maven or gradle.
<dependency>
<groupId>com.github.moaxcp.x11</groupId>
<artifactId>x11-client</artifactId>
<version>0.12.0</version>
<version>0.14.0</version>
<type>module</type>
</dependency>
```

## Gradle

```
implementation 'com.github.moaxcp.x11:x11-client:0.12.0'
implementation 'com.github.moaxcp.x11:x11-client:0.14.0'
```

The library has one dependency for using unix sockets.
Expand Down Expand Up @@ -452,6 +452,11 @@ https://jamey.thesharps.us/2021/03/25/xcb-protocol-specifications-data/

# versions

## 0.14.0

* Fixed issue where WILD XAuthority entries do not have an address
* Fixed major bug when writing requests. For xproto the header should start with the OPCODE then 1 pad. For extensions the header should start with the majorOpcode then OPCODE. BigRequest and all QueryVersion requests worked because their OPCODE is 0 but everything else in extensions failed. This should be fixed for all extensions and has been tested with the RECORD extension.

## 0.13.0

ProtocolPluginService now sets majorOpcode on XProtocolPlugins and uses it instead of majorVersion as the base opcode for requests. This fixes a bug in loading plugins and sending requests.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ plugins {
id 'jacoco'
}

version = '0.13.0'
version = '0.14.0'
group = 'com.github.moaxcp.x11'
description = 'An x11 client implemented in java'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class JavaRequest extends JavaClass {
javaRequest.protocol = request.toJavaProtocol(javaRequest)
JavaProperty c = javaRequest.getJavaProperty('OPCODE')
c.constantField = true
c.writeValueExpression = CodeBlock.of('(byte)($1T.toUnsignedInt(OPCODE) + $1T.toUnsignedInt(offset))', ClassName.get('java.lang', 'Byte'))
c.writeValueExpression = CodeBlock.of('(byte)($1T.toUnsignedInt(OPCODE))', ClassName.get('java.lang', 'Byte'))
JavaProperty l = javaRequest.getJavaProperty('length')
l.writeValueExpression = CodeBlock.of('(short) getLength()')
return javaRequest
Expand Down Expand Up @@ -112,7 +112,7 @@ class JavaRequest extends JavaClass {

@Override
void addWriteParameters(MethodSpec.Builder methodBuilder) {
methodBuilder.addParameter(byte.class, 'offset')
methodBuilder.addParameter(byte.class, 'majorOpcode')
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ class XTypeRequest extends XTypeObject {
}

static XTypeRequest xTypeRequest(XResult result, Node node) {
XTypeRequest request = new XTypeRequest(result: result, name: node.attributes().get('name'), opCode: Integer.valueOf((String) node.attributes().get('opcode')), basePackage: result.basePackage, javaPackage: result.javaPackage)
int opCode = Integer.valueOf((String) node.attributes().get('opcode')) ?: 0
XTypeRequest request = new XTypeRequest(result: result, name: node.attributes().get('name'), opCode: opCode, basePackage: result.basePackage, javaPackage: result.javaPackage)
request.addUnits(result, node)
request.protocol.add(0, new XUnitField(result: result, name: 'OPCODE', type:'CARD8', constantValue: request.opCode))
if(request.protocol.size() == 1) {
request.protocol.add(new XUnitPad(bytes: 1))
request.protocol.add(new XUnitField(result: result, name: 'length', type: 'CARD16', localOnly: true))
} else {
XUnit firstField = request.protocol[1]
if(firstField instanceof XUnitPad && firstField.bytes != 1) {
request.protocol.add(1, new XUnitPad(bytes: 1))
} else if(firstField instanceof XUnitField && !['BOOL', 'BYTE', 'INT8', 'CARD8', 'char', 'void'].contains(firstField.resolvedType.name)) {
request.protocol.add(1, new XUnitPad(bytes: 1))
if (result.header == 'xproto') {
request.protocol.add(0, new XUnitField(result: result, name: 'OPCODE', type:'CARD8', constantValue: request.opCode))
if(request.protocol.size() == 1) {
request.protocol.add(new XUnitPad(bytes: 1))
} else {
XUnit firstField = request.protocol[1]
if(firstField instanceof XUnitPad && firstField.bytes != 1) {
request.protocol.add(1, new XUnitPad(bytes: 1))
} else if(firstField instanceof XUnitField && !['BOOL', 'BYTE', 'INT8', 'CARD8', 'char', 'void'].contains(firstField.resolvedType.name)) {
request.protocol.add(1, new XUnitPad(bytes: 1))
}
}
request.protocol.add(2, new XUnitField(result: result, name: 'length', type: 'CARD16', localOnly: true))
} else {
request.protocol.add(0, new XUnitField(result: result, name: 'majorOpcode', type:'CARD8', localOnly: true))
request.protocol.add(1, new XUnitField(result: result, name: 'OPCODE', type:'CARD8', constantValue: request.opCode))
request.protocol.add(2, new XUnitField(result: result, name: 'length', type: 'CARD16', localOnly: true))
}

Node replyNode = node.childNodes().find { it.name() == 'reply' }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ abstract class XmlSpec extends Specification {
parser.parseXml()
}

void parseHeaderAndXml() {
XParser parser = new XParser(xml: getGPathResult(), result: result)
parser.parseHeader()
parser.parseXml()
}

Node getFirstChild() {
(Node) getGPathResult().childNodes().next()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.github.moaxcp.x11protocol.XmlSpec
class JavaReplySpec extends XmlSpec {
def queryTree() {
given:
xmlBuilder.xcb() {
xmlBuilder.xcb(header: 'xproto') {
xidtype(name:'WINDOW')
request(name:'QueryTree', opcode:'15') {
pad(bytes:'1')
Expand Down Expand Up @@ -58,9 +58,9 @@ class JavaReplySpec extends XmlSpec {
}
@java.lang.Override
public void write(byte offset, com.github.moaxcp.x11client.protocol.X11Output out) throws
public void write(byte majorOpcode, com.github.moaxcp.x11client.protocol.X11Output out) throws
java.io.IOException {
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE) + java.lang.Byte.toUnsignedInt(offset)));
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE)));
out.writePad(1);
out.writeCard16((short) getLength());
out.writeCard32(window);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ class JavaRequestSpec extends XmlSpec {
}
@java.lang.Override
public void write(byte offset, com.github.moaxcp.x11client.protocol.X11Output out) throws
public void write(byte majorOpcode, com.github.moaxcp.x11client.protocol.X11Output out) throws
java.io.IOException {
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE) + java.lang.Byte.toUnsignedInt(offset)));
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE)));
out.writePad(1);
out.writeCard16((short) getLength());
out.writeCard32(window);
Expand Down Expand Up @@ -141,9 +141,9 @@ class JavaRequestSpec extends XmlSpec {
}
@java.lang.Override
public void write(byte offset, com.github.moaxcp.x11client.protocol.X11Output out) throws
public void write(byte majorOpcode, com.github.moaxcp.x11client.protocol.X11Output out) throws
java.io.IOException {
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE) + java.lang.Byte.toUnsignedInt(offset)));
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE)));
out.writeByte(coordinateMode);
out.writeCard16((short) getLength());
out.writeCard32(drawable);
Expand Down Expand Up @@ -249,9 +249,9 @@ class JavaRequestSpec extends XmlSpec {
}
@java.lang.Override
public void write(byte offset, com.github.moaxcp.x11client.protocol.X11Output out) throws
public void write(byte majorOpcode, com.github.moaxcp.x11client.protocol.X11Output out) throws
java.io.IOException {
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE) + java.lang.Byte.toUnsignedInt(offset)));
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE)));
out.writeBool(getOddLength());
out.writeCard16((short) getLength());
out.writeCard32(font);
Expand Down Expand Up @@ -291,39 +291,39 @@ class JavaRequestSpec extends XmlSpec {
}

when:
addChildNodes()
parseHeaderAndXml()
XTypeRequest request = result.resolveXType('Enable')
JavaRequest javaRequest = request.javaType

then:
javaRequest.typeSpec.toString() == '''\
@lombok.Value
@lombok.Builder
public class Enable implements com.github.moaxcp.x11client.protocol.TwoWayRequest<com.github.moaxcp.x11client.protocol.xproto.EnableReply>, com.github.moaxcp.x11client.protocol.xproto.XprotoObject {
public class Enable implements com.github.moaxcp.x11client.protocol.TwoWayRequest<com.github.moaxcp.x11client.protocol.bigreq.EnableReply>, com.github.moaxcp.x11client.protocol.bigreq.BigreqObject {
public static final byte OPCODE = 0;
public com.github.moaxcp.x11client.protocol.XReplyFunction<com.github.moaxcp.x11client.protocol.xproto.EnableReply> getReplyFunction(
public com.github.moaxcp.x11client.protocol.XReplyFunction<com.github.moaxcp.x11client.protocol.bigreq.EnableReply> getReplyFunction(
) {
return (field, sequenceNumber, in) -> com.github.moaxcp.x11client.protocol.xproto.EnableReply.readEnableReply(field, sequenceNumber, in);
return (field, sequenceNumber, in) -> com.github.moaxcp.x11client.protocol.bigreq.EnableReply.readEnableReply(field, sequenceNumber, in);
}
public byte getOpCode() {
return OPCODE;
}
public static com.github.moaxcp.x11client.protocol.xproto.Enable readEnable(
public static com.github.moaxcp.x11client.protocol.bigreq.Enable readEnable(
com.github.moaxcp.x11client.protocol.X11Input in) throws java.io.IOException {
com.github.moaxcp.x11client.protocol.xproto.Enable.EnableBuilder javaBuilder = com.github.moaxcp.x11client.protocol.xproto.Enable.builder();
byte[] pad1 = in.readPad(1);
com.github.moaxcp.x11client.protocol.bigreq.Enable.EnableBuilder javaBuilder = com.github.moaxcp.x11client.protocol.bigreq.Enable.builder();
byte majorOpcode = in.readCard8();
short length = in.readCard16();
return javaBuilder.build();
}
@java.lang.Override
public void write(byte offset, com.github.moaxcp.x11client.protocol.X11Output out) throws
public void write(byte majorOpcode, com.github.moaxcp.x11client.protocol.X11Output out) throws
java.io.IOException {
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE) + java.lang.Byte.toUnsignedInt(offset)));
out.writePad(1);
out.writeCard8(majorOpcode);
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE)));
out.writeCard16((short) getLength());
}
Expand Down Expand Up @@ -675,9 +675,9 @@ class JavaRequestSpec extends XmlSpec {
}
@java.lang.Override
public void write(byte offset, com.github.moaxcp.x11client.protocol.X11Output out) throws
public void write(byte majorOpcode, com.github.moaxcp.x11client.protocol.X11Output out) throws
java.io.IOException {
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE) + java.lang.Byte.toUnsignedInt(offset)));
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE)));
out.writeCard8(depth);
out.writeCard16((short) getLength());
out.writeCard32(wid);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ class SetCompatMapSpec extends XmlSpec {
}
@java.lang.Override
public void write(byte offset, com.github.moaxcp.x11client.protocol.X11Output out) throws
public void write(byte majorOpcode, com.github.moaxcp.x11client.protocol.X11Output out) throws
java.io.IOException {
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE) + java.lang.Byte.toUnsignedInt(offset)));
out.writeCard8((byte)(java.lang.Byte.toUnsignedInt(OPCODE)));
out.writePad(1);
out.writeCard16((short) getLength());
out.writeCard16(deviceSpec);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.github.moaxcp.x11client;

import com.github.moaxcp.x11client.protocol.X11OutputStream;
import com.github.moaxcp.x11client.protocol.record.*;
import java.io.ByteArrayOutputStream;
import lombok.extern.java.Log;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -37,6 +39,11 @@ void test() {
.ranges(Collections.singletonList(range))
.build();

try (ByteArrayOutputStream baos = new ByteArrayOutputStream(); X11OutputStream out = new X11OutputStream(baos)) {
createContext.write((byte) -110, out);
log.info(String.format("createContext bytes size = %d", baos.toByteArray().length));
}

log.info(String.format("createContext: %s, size: %d, length: %d", createContext, createContext.getSize(), createContext.getLength()));

client.send(createContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.ArrayList;
import java.util.List;

public class X11InputStream implements X11Input {
public class X11InputStream implements X11Input, AutoCloseable {
private final DataInputStream in;

public X11InputStream(InputStream inputStream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.io.OutputStream;
import java.util.List;

public class X11OutputStream implements X11Output {
public class X11OutputStream implements X11Output, AutoCloseable {
private final DataOutputStream out;

public X11OutputStream(OutputStream outputStream) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,17 @@ public class XAuthority {
@ToString
public enum Family {
INTERNET(0),
DEC_NET(1),
CHAOS(2),
SERVER_INTERPRETED(5),
INTERNET6(6),
LOCAL(256),
WILD(65535),
KRB5PRINCIPAL(254),
NETNAME(254),
KRB5PRINCIPAL(253),
LOCALHOST(252);

private int code;
private final int code;

Family(int code) {
this.code = code;
Expand Down Expand Up @@ -79,7 +84,7 @@ public static Family getByCode(int code) {
*/
public XAuthority(@NonNull Family family, @NonNull List<Byte> address, int displayNumber, @NonNull List<Byte> protocolName, @NonNull List<Byte> protocolData) {
this.family = family;
this.address = requireNonEmpty("address", address);
this.address = address;
if(displayNumber < 0) {
throw new IllegalArgumentException("displayNumber was \"" + displayNumber + "\" expected >= 0.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void wildCode() {
}
@Test
void krb5principalCode() {
AssertionsForClassTypes.assertThat(Family.KRB5PRINCIPAL.getCode()).isEqualTo(254);
AssertionsForClassTypes.assertThat(Family.KRB5PRINCIPAL.getCode()).isEqualTo(253);
}
@Test
void localhostCode() {
Expand All @@ -46,7 +46,7 @@ void wild_getByCode() {

@Test
void krb_getByCode() {
assertThat(Family.getByCode(254)).isEqualTo(Family.KRB5PRINCIPAL);
assertThat(Family.getByCode(253)).isEqualTo(Family.KRB5PRINCIPAL);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@ void constructor_fails_on_null_address() {
assertThat(exception).hasMessage("address is marked non-null but is null");
}

@Test
void constructor_fails_on_empty_address() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new XAuthority(LOCAL, toList(new byte[]{}), 0, toList("magic".getBytes()), toList(new byte[] {1})));
assertThat(exception).hasMessage("address must not be empty");
}

@Test
void constructor_fails_on_negative_displayNumber() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new XAuthority(LOCAL, toList("host".getBytes()), -1, toList("magic".getBytes()), toList(new byte[] {1})));
Expand Down

0 comments on commit 03ebdf5

Please sign in to comment.