OLD | NEW |
| (Empty) |
1 | |
2 import com.google.protobuf.conformance.Conformance; | |
3 import com.google.protobuf.InvalidProtocolBufferException; | |
4 | |
5 class ConformanceJava { | |
6 private int testCount = 0; | |
7 | |
8 private boolean readFromStdin(byte[] buf, int len) throws Exception { | |
9 int ofs = 0; | |
10 while (len > 0) { | |
11 int read = System.in.read(buf, ofs, len); | |
12 if (read == -1) { | |
13 return false; // EOF | |
14 } | |
15 ofs += read; | |
16 len -= read; | |
17 } | |
18 | |
19 return true; | |
20 } | |
21 | |
22 private void writeToStdout(byte[] buf) throws Exception { | |
23 System.out.write(buf); | |
24 } | |
25 | |
26 // Returns -1 on EOF (the actual values will always be positive). | |
27 private int readLittleEndianIntFromStdin() throws Exception { | |
28 byte[] buf = new byte[4]; | |
29 if (!readFromStdin(buf, 4)) { | |
30 return -1; | |
31 } | |
32 return buf[0] | (buf[1] << 1) | (buf[2] << 2) | (buf[3] << 3); | |
33 } | |
34 | |
35 private void writeLittleEndianIntToStdout(int val) throws Exception { | |
36 byte[] buf = new byte[4]; | |
37 buf[0] = (byte)val; | |
38 buf[1] = (byte)(val >> 8); | |
39 buf[2] = (byte)(val >> 16); | |
40 buf[3] = (byte)(val >> 24); | |
41 writeToStdout(buf); | |
42 } | |
43 | |
44 private Conformance.ConformanceResponse doTest(Conformance.ConformanceRequest
request) { | |
45 Conformance.TestAllTypes testMessage; | |
46 | |
47 switch (request.getPayloadCase()) { | |
48 case PROTOBUF_PAYLOAD: { | |
49 try { | |
50 testMessage = Conformance.TestAllTypes.parseFrom(request.getProtobufPa
yload()); | |
51 } catch (InvalidProtocolBufferException e) { | |
52 return Conformance.ConformanceResponse.newBuilder().setParseError(e.ge
tMessage()).build(); | |
53 } | |
54 break; | |
55 } | |
56 case JSON_PAYLOAD: { | |
57 return Conformance.ConformanceResponse.newBuilder().setSkipped("JSON not
yet supported.").build(); | |
58 } | |
59 case PAYLOAD_NOT_SET: { | |
60 throw new RuntimeException("Request didn't have payload."); | |
61 } | |
62 | |
63 default: { | |
64 throw new RuntimeException("Unexpected payload case."); | |
65 } | |
66 } | |
67 | |
68 switch (request.getRequestedOutputFormat()) { | |
69 case UNSPECIFIED: | |
70 throw new RuntimeException("Unspecified output format."); | |
71 | |
72 case PROTOBUF: | |
73 return Conformance.ConformanceResponse.newBuilder().setProtobufPayload(t
estMessage.toByteString()).build(); | |
74 | |
75 case JSON: | |
76 return Conformance.ConformanceResponse.newBuilder().setSkipped("JSON not
yet supported.").build(); | |
77 | |
78 default: { | |
79 throw new RuntimeException("Unexpected request output."); | |
80 } | |
81 } | |
82 } | |
83 | |
84 private boolean doTestIo() throws Exception { | |
85 int bytes = readLittleEndianIntFromStdin(); | |
86 | |
87 if (bytes == -1) { | |
88 return false; // EOF | |
89 } | |
90 | |
91 byte[] serializedInput = new byte[bytes]; | |
92 | |
93 if (!readFromStdin(serializedInput, bytes)) { | |
94 throw new RuntimeException("Unexpected EOF from test program."); | |
95 } | |
96 | |
97 Conformance.ConformanceRequest request = | |
98 Conformance.ConformanceRequest.parseFrom(serializedInput); | |
99 Conformance.ConformanceResponse response = doTest(request); | |
100 byte[] serializedOutput = response.toByteArray(); | |
101 | |
102 writeLittleEndianIntToStdout(serializedOutput.length); | |
103 writeToStdout(serializedOutput); | |
104 | |
105 return true; | |
106 } | |
107 | |
108 public void run() throws Exception { | |
109 while (doTestIo()) { | |
110 // Empty. | |
111 } | |
112 | |
113 System.err.println("ConformanceJava: received EOF from test runner after " + | |
114 this.testCount + " tests"); | |
115 } | |
116 | |
117 public static void main(String[] args) throws Exception { | |
118 new ConformanceJava().run(); | |
119 } | |
120 } | |
OLD | NEW |