August update

A rewrite of the BinaryLayout and BitField implementation to be able to handle very complex binary structures, even more so than just c-structures.

The junit source test and others now pass successfully. Here is the output and source code for a mock-up Ip4 header.

Note: that both BinaryLayout and BitField references should normally be declared in a static context and only defined once. For testing purposes here, each test case instantiates a new BitField which is discarded afterwards.

(Beautified with: hilite.me)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/*
 * MIT License
 * 
 * Copyright (c) 2020 Sly Technologies Inc.
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package org.jnet.binary.layout.tests;

import static org.jnet.binary.layout.BinaryLayout.sequenceLayout;
import static org.jnet.binary.layout.BinaryLayout.structLayout;
import static org.jnet.binary.layout.BinaryLayout.unionLayout;
import static org.jnet.util.ArrayUtils.parseHexString;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.jnet.binary.BitField;
import org.jnet.binary.layout.BinaryLayout;
import org.jnet.binary.layout.PredefinedLayout.Int8;
import org.jnet.binary.layout.PredefinedLayout.IntB16;
import org.jnet.binary.layout.PredefinedLayout.IntB32;
import org.jnet.binary.layout.PredefinedLayout.Padding;
import org.junit.jupiter.api.Test;

/**
 * JUnit test of early BinaryLayout and BinField API and implementation
 * 
 * @author Mark Bednarczyk
 */
class TestBinaryLayoutOnIp4ExtHeader {

	/**
	 * (Source wireshark capture and copy/paste)
	 * 
	 * <pre>
		Internet Protocol Version 4, Src: 127.0.0.1, Dst: 127.0.0.1
		    0100 .... = Version: 4
		    .... 0101 = Header Length: 20 bytes (5)
		    Differentiated Services Field: 0x00 (DSCP: CS0, ECN: Not-ECT)
		        0000 00.. = Differentiated Services Codepoint: Default (0)
		        .... ..00 = Explicit Congestion Notification: Not ECN-Capable Transport (0)
		    Total Length: 60
		    Identification: 0x496c (18796)
		    Flags: 0x40, Don't fragment
		        0... .... = Reserved bit: Not set
		        .1.. .... = Don't fragment: Set
		        ..0. .... = More fragments: Not set
		    ...0 0000 0000 0000 = Fragment Offset: 0
		    Time to Live: 64
		    Protocol: TCP (6)
		    Header Checksum: 0xf34d [correct]
		    [Header checksum status: Good]
		    [Calculated Checksum: 0xf34d]
		    Source Address: 127.0.0.1
		    Destination Address: 127.0.0.1
	 * </pre>
	 * 
	 * Source:
	 * https://github.com/freebsd/freebsd-src/blob/master/sys/netinet/ip.h#L51-L59
	 * 
	 * <pre>
	 * <code>
		struct ip {
		#if BYTE_ORDER == LITTLE_ENDIAN
			u_char	ip_hl:4,				/* header length 
				ip_v:4;						/* version 
		#endif
		#if BYTE_ORDER == BIG_ENDIAN
			u_char	ip_v:4,					/* version 
				ip_hl:4;					/* header length 
		#endif
			u_char	ip_tos;					/* type of service 
			u_short	ip_len;					/* total length 
			u_short	ip_id;					/* identification 
			u_short	ip_off;					/* fragment offset field 
		#define	IP_RF 0x8000				/* reserved fragment flag 
		#define	IP_DF 0x4000				/* dont fragment flag 
		#define	IP_MF 0x2000				/* more fragments flag 
		#define	IP_OFFMASK 0x1fff			/* mask for fragmenting bits 
			u_char	ip_ttl;					/* time to live 
			u_char	ip_p;					/* protocol 
			u_short	ip_sum;					/* checksum 
			struct	in_addr ip_src,ip_dst;	/* source and dest address 
		} __packed __aligned(2);
	 * </code>
	 * </pre>
	 * 
	 * Binary representation of the above header.
	 */
	private final static byte[] IP4_DATA = parseHexString("4500003c496c40004006f34d7f0000017f000001");

	private static final BinaryLayout IP4_LAYOUT = unionLayout(
			structLayout(

					/* Word0 */
					Int8.BITS_04.withName("ip.version"),
					Int8.BITS_04.withName("ip.hdr_len"),

					/* Detailed DS field */
					unionLayout(
							unionLayout(
									Int8.BITS_08.withName("ip.dsfield"),
									sequenceLayout(8, Int8.BITS_01).withName("ip.dsfield.bits"),
									structLayout(
											Int8.BITS_00,
											unionLayout(
													Int8.BITS_06.withName("ip.dsfield.dscp"),
													structLayout(
															Int8.BITS_03.withName("ip.dsfield.dscp.select"),
															Int8.BITS_03.withName("ip.dsfield.dscp.code"))),
											Int8.BITS_02.withName("ip.dsfield.ecn"))),
							unionLayout(
									Int8.BITS_08.withName("ip.tos"),
									sequenceLayout(8, Int8.BITS_01).withName("ip.tos.bits"),
									structLayout(
											Int8.BITS_03.withName("ip.tos.precedence"),
											Int8.BITS_01.withName("ip.tos.delay"),
											Int8.BITS_01.withName("ip.tos.throughput"),
											Int8.BITS_01.withName("ip.tos.reliability"),
											Int8.BITS_01.withName("ip.tos.cost"),
											Padding.BITS_01))),
					IntB16.BITS_16.withName("ip.len"),

					/* Word1 */
					IntB16.BITS_16.withName("ip.id"),
					IntB16.BITS_00, // Reset carrier
					unionLayout(
							IntB16.BITS_03.withName("ip.flags"),
							structLayout(
									IntB16.BITS_01.withName("ip.flags.rb"),
									IntB16.BITS_01.withName("ip.flags.df"),
									IntB16.BITS_01.withName("ip.flags.mf"))),
					IntB16.BITS_13.withName("ip.frag_offset"),

					/* Word2 */
					IntB32.BITS_08.withName("ip.ttl"),
					IntB32.BITS_08.withName("ip.proto"),
					IntB32.BITS_16.withName("ip.checksum"),

					/* Word3 */
					unionLayout(
							IntB32.BITS_32.withName("ip.src"),
							sequenceLayout(4, Int8.BITS_08).withName("ip.src.bytes"),
							sequenceLayout(32, Int8.BITS_01).withName("ip.src.bits")),

					/* Word4 */
					unionLayout(
							IntB32.BITS_32.withName("ip.dst"),
							sequenceLayout(4, Int8.BITS_08).withName("ip.dst.bytes"),
							sequenceLayout(32, Int8.BITS_01).withName("ip.dst.bits"))),

			structLayout(
					IntB32.BITS_32.withName("ip.word0"),
					IntB32.BITS_32.withName("ip.word1"),
					IntB32.BITS_32.withName("ip.word2"),
					IntB32.BITS_32.withName("ip.word3"),
					IntB32.BITS_32.withName("ip.word4")),

			sequenceLayout(5, IntB32.BITS_32).withName("ip.array.ints"),

			// 2 shortWord * 16 bits/shortWord = 32 bits
			sequenceLayout(2 * 5, IntB16.BITS_16).withName("ip.array.shorts"),

			// 4 bytes * 8 bits/byte = 32 bits
			sequenceLayout(4 * 5, Int8.BITS_08).withName("ip.array.bytes"),

			// 8 nibbles * 4 bits/nibble = 32 bits
			sequenceLayout(8 * 5, Int8.BITS_04).withName("ip.array.nibbles"),

			// 32 bits * 1 = 32 bits
			sequenceLayout(32 * 5, Int8.BITS_01).withName("ip.array.bits"));
	@Test
	void ip4_version() {
		BitField field = IP4_LAYOUT.bitField("ip.version");
		assertEquals(0x4, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_hdr_len() {
		BitField field = IP4_LAYOUT.bitField("ip.hdr_len");
		assertEquals(0x5, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_dsfield() {
		BitField field = IP4_LAYOUT.bitField("ip.dsfield");
		assertEquals(0x00, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_dsfield_dscp() {
		BitField field = IP4_LAYOUT.bitField("ip.dsfield.dscp");
		assertEquals(0x0, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_dsfield_dscp_select() {
		BitField field = IP4_LAYOUT.bitField("ip.dsfield.dscp.select");
		assertEquals(0, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_dsfield_dscp_code() {
		BitField field = IP4_LAYOUT.bitField("ip.dsfield.dscp.code");
		assertEquals(0, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_dsfield_ecn() {
		BitField field = IP4_LAYOUT.bitField("ip.dsfield.ecn");
		assertEquals(0, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_tos() {
		BitField field = IP4_LAYOUT.bitField("ip.tos");
		assertEquals(0x00, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_tos_precedence() {
		BitField field = IP4_LAYOUT.bitField("ip.tos.precedence");
		assertEquals(0x00, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_tos_delay() {
		BitField field = IP4_LAYOUT.bitField("ip.tos.delay");
		assertEquals(0x00, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_tos_throughput() {
		BitField field = IP4_LAYOUT.bitField("ip.tos.throughput");
		assertEquals(0x00, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_tos_reliability() {
		BitField field = IP4_LAYOUT.bitField("ip.tos.reliability");
		assertEquals(0x00, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_tos_cost() {
		BitField field = IP4_LAYOUT.bitField("ip.tos.cost");
		assertEquals(0x00, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_len() {
		BitField field = IP4_LAYOUT.bitField("ip.len");
		assertEquals(60, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_id() {
		BitField field = IP4_LAYOUT.bitField("ip.id");
		assertEquals(0x496c, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_flags() {
		BitField field = IP4_LAYOUT.bitField("ip.flags");
		assertEquals(2, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_flags_rb() {
		BitField field = IP4_LAYOUT.bitField("ip.flags.rb");
		assertEquals(0, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_flags_df() {
		BitField field = IP4_LAYOUT.bitField("ip.flags.df");
		assertEquals(1, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_flags_mf() {
		BitField field = IP4_LAYOUT.bitField("ip.flags.mf");
		assertEquals(0, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_frag_offset() {
		BitField field = IP4_LAYOUT.bitField("ip.frag_offset");
		assertEquals(0x00, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_ttl() {
		BitField field = IP4_LAYOUT.bitField("ip.ttl");
		assertEquals(64, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_proto() {
		BitField field = IP4_LAYOUT.bitField("ip.proto");
		assertEquals(6, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_src() {
		BitField field = IP4_LAYOUT.bitField("ip.src");
		assertEquals(0x7f000001, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_dst() {
		BitField field = IP4_LAYOUT.bitField("ip.dst");
		assertEquals(0x7f000001, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_word0() {
		BitField field = IP4_LAYOUT.bitField("ip.word0");
		assertEquals(0x4500003c, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_word1() {
		BitField field = IP4_LAYOUT.bitField("ip.word1");
		assertEquals(0x496c4000, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_word2() {
		BitField field = IP4_LAYOUT.bitField("ip.word2");
		assertEquals(0x4006f34d, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_word3() {
		BitField field = IP4_LAYOUT.bitField("ip.word3");
		assertEquals(0x7f000001, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_word4() {
		BitField field = IP4_LAYOUT.bitField("ip.word4");
		assertEquals(0x7f000001, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}


	@Test
	void ip4_array_ints() {
		BitField field = IP4_LAYOUT.bitField("ip.array.ints");
		assertEquals(0x4500003c, field.getInt(IP4_DATA), field.fieldName().orElse(""));
	}

	@Test
	void ip4_array_ints_index0() {
		BitField field = IP4_LAYOUT.bitField("ip.array.ints");
		assertEquals(0x4500003c, field.getInt(IP4_DATA, 0), field.fieldName().orElse(""));
	}

	@Test
	void ip4_array_ints_index1() {
		BitField field = IP4_LAYOUT.bitField("ip.array.ints");
		assertEquals(0x496c4000, field.getInt(IP4_DATA, 1), field.fieldName().orElse(""));
	}

	@Test
	void ip4_array_ints_index2() {
		BitField field = IP4_LAYOUT.bitField("ip.array.ints");
		assertEquals(0x4006f34d, field.getInt(IP4_DATA, 2), field.fieldName().orElse(""));
	}

	@Test
	void ip4_array_ints_index3() {
		BitField field = IP4_LAYOUT.bitField("ip.array.ints");
		assertEquals(0x7f000001, field.getInt(IP4_DATA, 3), field.fieldName().orElse(""));
	}

	@Test
	void ip4_array_ints_index4() {
		BitField field = IP4_LAYOUT.bitField("ip.array.ints");
		assertEquals(0x7f000001, field.getInt(IP4_DATA, 4), field.fieldName().orElse(""));
	}


}

Comments

Popular posts from this blog

jnetpcap-2.0.0-preview.1 imported to github

October updates