summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/slime/BinaryViewTest.java
blob: 568124369d4c0c960c12557d637f17857d39564e (plain) (blame)
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.slime;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.function.Consumer;

import org.junit.Test;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static com.yahoo.slime.BinaryFormat.encode_type_and_meta;

public class BinaryViewTest {
    static String makeString(int size) {
        var str = new StringBuilder();
        for (int i = 0; i < size; ++i) {
            str.append("A");
        }
        return str.toString();
    }
    static byte[] makeData(int size) {
        byte[] data = new byte[size];
        for (int i = 0; i < size; ++i) {
            data[i] = 65;
        }
        return data;
    }
    static final int numLeafs = 22;
    static Cursor insertLeaf(Inserter dst, int id) {
        return switch (id) {
        case  0 -> dst.insertNIX();
        case  1 -> dst.insertBOOL(false);
        case  2 -> dst.insertBOOL(true);
        case  3 -> dst.insertLONG(42L);
        case  4 -> dst.insertLONG(-42L);
        case  5 -> dst.insertLONG(0x1234_5678_8765_4321L);
        case  6 -> dst.insertLONG(-0x1234_5678_8765_4321L);
        case  7 -> dst.insertDOUBLE(3.5);
        case  8 -> dst.insertDOUBLE(1.0/3.0);
        case  9 -> dst.insertDOUBLE(-3.5);
        case 10 -> dst.insertDOUBLE(-(1.0/3.0));
        case 11 -> dst.insertSTRING(makeString(5));
        case 12 -> dst.insertSTRING(makeString(50));
        case 13 -> dst.insertSTRING(makeString(300));
        case 14 -> dst.insertSTRING(makeData(5));
        case 15 -> dst.insertSTRING(makeData(50));
        case 16 -> dst.insertSTRING(makeData(300));
        case 17 -> dst.insertDATA(makeData(5));
        case 18 -> dst.insertDATA(makeData(50));
        case 19 -> dst.insertDATA(makeData(300));
        case 20 -> dst.insertARRAY();
        case 21 -> dst.insertOBJECT();
        default -> NixValue.invalid();
        };
    }
    static Cursor insertInnerObject(Inserter dst) {
        var obj = dst.insertOBJECT();
        for (int i = 0; i < numLeafs; ++i) {
            assertTrue(insertLeaf(new ObjectInserter(obj, "leaf" + i), i).valid());
        }
        return obj;
    }
    static Cursor insertInnerArray(Inserter dst) {
        var arr = dst.insertARRAY();
        for (int i = 0; i < numLeafs; ++i) {
            assertTrue(insertLeaf(new ArrayInserter(arr), i).valid());
        }
        return arr;
    }
    static Cursor insertOuterObject(Inserter dst) {
        var obj = dst.insertOBJECT();
        assertTrue(insertInnerObject(new ObjectInserter(obj, "foo")).valid());
        assertTrue(insertInnerArray(new ObjectInserter(obj, "bar")).valid());
        return obj;
    }
    static Cursor insertOuterArray(Inserter dst) {
        var arr = dst.insertARRAY();
        assertTrue(insertInnerObject(new ArrayInserter(arr)).valid());
        assertTrue(insertInnerArray(new ArrayInserter(arr)).valid());
        return arr;
    }
    static Cursor insertManySymbols(Inserter dst) {
        var obj = dst.insertOBJECT();
        for (int i = 0; i < 300; ++i) {
            obj.setLong("val" + i, i);
        }
        assertEquals(300, obj.fields());
        return obj;
    }
    static Cursor insertLargeArray(Inserter dst) {
        var arr = dst.insertARRAY();
        for (int i = 0; i < 300; ++i) {
            arr.addLong(i);
        }
        assertEquals(300, arr.entries());
        return arr;
    }
    static final int numShapes = numLeafs + 6;
    static Cursor insertRoot(Slime dst, int shape) {
        var root = new SlimeInserter(dst);
        if (shape < numLeafs) {
            return insertLeaf(root, shape);
        }
        return switch (shape) {
        case (numLeafs) -> insertInnerObject(root);
        case (numLeafs + 1) -> insertInnerArray(root);
        case (numLeafs + 2) -> insertOuterObject(root);
        case (numLeafs + 3) -> insertOuterArray(root); 
        case (numLeafs + 4) -> insertManySymbols(root);
        case (numLeafs + 5) -> insertLargeArray(root);
        default -> NixValue.invalid();
        };
    }
    static Slime makeSlime(int shape) {
        var slime = new Slime();
        var root = insertRoot(slime, shape);
        assertTrue(root.valid());
        return slime;
    }

    class MyConsumer implements Consumer<Inspector> {
        Inspector value = null;
        @Override public void accept(Inspector value) {
            assertNull(ctx, this.value);
            this.value = value;
        }
    };

    void checkConsumer(Inspector view) {
        var consumer = new MyConsumer();
        view.ifValid(consumer);
        assertEquals(ctx, view.valid(), consumer.value != null);
        if (view.valid()) {
            assertSame(ctx, view, consumer.value);
        }
    }

    class MyVisitor implements Visitor {
        enum Called { NONE, INVALID, NIX, BOOL, LONG, DOUBLE, UTF8, DATA, ARRAY, OBJECT }
        Called called = Called.NONE;
        boolean boolValue;
        long longValue;
        double doubleValue;
        byte[] bytes;
        Inspector stuff;
        @Override public void visitInvalid() {
            assertEquals(ctx, Called.NONE, called);
            called = Called.INVALID;
        }
        @Override public void visitNix() {
            assertEquals(ctx, Called.NONE, called);
            called = Called.NIX;
        }
        @Override public void visitBool(boolean bit) {
            assertEquals(ctx, Called.NONE, called);
            called = Called.BOOL;
            boolValue = bit;
        }
        @Override public void visitLong(long l) {
            assertEquals(ctx, Called.NONE, called);
            called = Called.LONG;
            longValue = l;
        }
        @Override public void visitDouble(double d) {
            assertEquals(ctx, Called.NONE, called);
            called = Called.DOUBLE;
            doubleValue = d;
        }
        @Override public void visitString(String str) {
            fail(ctx + ", strings are never utf-16 in binary view");
        }
        @Override public void visitString(byte[] utf8) {
            assertEquals(ctx, Called.NONE, called);
            called = Called.UTF8;
            bytes = utf8;
        }
        @Override public void visitData(byte[] data) {
            assertEquals(ctx, Called.NONE, called);
            called = Called.DATA;
            bytes = data;
        }
        @Override public void visitArray(Inspector arr) {
            assertEquals(ctx, Called.NONE, called);
            called = Called.ARRAY;
            stuff = arr;
        }
        @Override public void visitObject(Inspector obj) {
            assertEquals(ctx, Called.NONE, called);
            called = Called.OBJECT;
            stuff = obj;
        }
    };

    void checkVisitor(Inspector view) {
        var visitor = new MyVisitor();
        view.accept(visitor);
        if (!view.valid()) {
            assertEquals(ctx, MyVisitor.Called.INVALID, visitor.called);
            return;
        }
        switch (view.type()) {
            case NIX:
                assertEquals(ctx, MyVisitor.Called.NIX, visitor.called);
                break;
            case BOOL:
                assertEquals(ctx, MyVisitor.Called.BOOL, visitor.called);
                assertEquals(ctx, view.asBool(), visitor.boolValue);
                break;
            case LONG:
                assertEquals(ctx, MyVisitor.Called.LONG, visitor.called);
                assertEquals(ctx, view.asLong(), visitor.longValue);
                break;
            case DOUBLE:
                assertEquals(ctx, MyVisitor.Called.DOUBLE, visitor.called);
                assertEquals(ctx, view.asDouble(), visitor.doubleValue, 0.0);
                break;
            case STRING:
                assertEquals(ctx, MyVisitor.Called.UTF8, visitor.called);
                assertArrayEquals(ctx, view.asUtf8(), visitor.bytes);
                break;
            case DATA:
                assertEquals(ctx, MyVisitor.Called.DATA, visitor.called);
                assertArrayEquals(ctx, view.asData(), visitor.bytes);
                break;
            case ARRAY:
                assertEquals(ctx, MyVisitor.Called.ARRAY, visitor.called);
                assertSame(ctx, view, visitor.stuff);
                break;
            case OBJECT:
                assertEquals(ctx, MyVisitor.Called.OBJECT, visitor.called);
                assertSame(ctx, view, visitor.stuff);
                break;
            default:
                fail(ctx + ", should not be reached");
                break;
        }
    }

    class MyArrayTraverser implements ArrayTraverser {
        ArrayList<Inspector> list = new ArrayList<>();
        @Override public void entry(int idx, Inspector value) {
            list.add(value);
        }
    }

    void checkTraverseArray(Inspector value, Inspector view) {
        var a = new MyArrayTraverser();
        var b = new MyArrayTraverser();
        value.traverse(a);
        view.traverse(b);
        assertEquals(ctx, a.list.size(), b.list.size());
        for (int i = 0; i < a.list.size(); ++i) {
            checkParity(a.list.get(i), b.list.get(i));
        }
    }

    class MyObjectSymbolTraverser implements ObjectSymbolTraverser {
        HashMap<Integer,Inspector> map = new HashMap<>();
        @Override public void field(int sym, Inspector value) {
            map.put(sym, value);
        }
    }

    void checkTraverseObjectSymbol(Inspector value, Inspector view) {
        var a = new MyObjectSymbolTraverser();
        var b = new MyObjectSymbolTraverser();
        value.traverse(a);
        view.traverse(b);
        assertEquals(ctx, a.map.size(), b.map.size());
        for (Integer key: a.map.keySet()) {
            assertTrue(ctx, b.map.containsKey(key));
            checkParity(a.map.get(key), b.map.get(key));
        }
    }

    class MyObjectTraverser implements ObjectTraverser {
        HashMap<String,Inspector> map = new HashMap<>();
        @Override public void field(String name, Inspector value) {
            map.put(name, value);
        }
    }

    void checkTraverseObject(Inspector value, Inspector view) {
        var a = new MyObjectTraverser();
        var b = new MyObjectTraverser();
        value.traverse(a);
        view.traverse(b);
        assertEquals(ctx, a.map.size(), b.map.size());
        for (String key: a.map.keySet()) {
            assertTrue(ctx, b.map.containsKey(key));
            checkParity(a.map.get(key), b.map.get(key));
        }
    }
    void checkParity(Inspector value, Inspector view) {
        checkConsumer(view);
        checkVisitor(view);
        if (value == view) {
            // avoid infinite invalid nix recursion
            assertSame(ctx, value, view);
            return;
        }
        assertEquals(ctx, value.valid(), view.valid());
        assertEquals(ctx, value.type(), view.type());
        assertEquals(ctx, value.children(), view.children());
        assertEquals(ctx, value.entries(), view.entries());
        assertEquals(ctx, value.fields(), view.fields());
        assertEquals(ctx, value.asBool(), view.asBool());
        assertEquals(ctx, value.asLong(), view.asLong());
        assertEquals(ctx, value.asDouble(), view.asDouble(), 0.0);
        assertEquals(ctx, value.asString(), view.asString());
        assertArrayEquals(ctx, value.asUtf8(), view.asUtf8());
        assertArrayEquals(ctx, value.asData(), view.asData());
        checkTraverseArray(value, view);
        checkTraverseObjectSymbol(value, view);
        checkTraverseObject(value, view);
        checkParity(value.entry(0), view.entry(0));
        checkParity(value.entry(1), view.entry(1));
        checkParity(value.entry(2), view.entry(2));
        checkParity(value.entry(3), view.entry(3));
        checkParity(value.entry(200), view.entry(200));
        checkParity(value.entry(500), view.entry(500));
        checkParity(value.entry(-1), view.entry(-1));
        checkParity(value.field(0), view.field(0));
        checkParity(value.field(1), view.field(1));
        checkParity(value.field(2), view.field(2));
        checkParity(value.field(3), view.field(3));
        checkParity(value.field(SymbolTable.INVALID), view.field(SymbolTable.INVALID));
        checkParity(value.field(-1), view.field(-1));
        checkParity(value.field("foo"), view.field("foo"));
        checkParity(value.field("bar"), view.field("bar"));
        checkParity(value.field("val256"), view.field("val256"));
        checkParity(value.field("bogus"), view.field("bogus"));
        assertTrue(ctx, value.equalTo(view));
        assertTrue(ctx, view.equalTo(value));
    }

    String ctx;
    @Test public void testBinaryViewShapesParity() {
        for (int i = 0; i < numShapes; ++i) {
            var slime = makeSlime(i);
            ctx = "case " + i + ": '" + slime.toString() + "'";
            byte[] data = BinaryFormat.encode(slime);
            try {
                checkParity(slime.get(), BinaryView.inspect(data));
            } catch (Exception e) {
                fail(ctx + ", got exception: " + e);
            }
        }
    }

    @Test public void testTrivialView() {
        byte[] data = {0, 0};
        var input = new BufferedInput(data);
        var view = BinaryView.inspectImpl(input);
        assertTrue(view.valid());
        assertEquals(Type.NIX, view.type());
        assertFalse(input.failed());
    }

    @Test public void testUnderflow() {
        byte[] data = {};
        var input = new BufferedInput(data);
        var view = BinaryView.inspectImpl(input);
        assertFalse(view.valid());
        assertTrue(input.failed());
        assertEquals("underflow", input.getErrorMessage());
    }

    @Test public void testMultiByteUnderflow() {
        byte[] data = { 0, encode_type_and_meta(Type.STRING.ID, 3), 65 };
        var input = new BufferedInput(data);
        var view = BinaryView.inspectImpl(input);
        assertFalse(view.valid());
        assertTrue(input.failed());
        assertEquals("underflow", input.getErrorMessage());
    }

    @Test public void testCompressedIntOverflow() {
        byte[] data = { -1, -1, -1, -1, 8 };
        var input = new BufferedInput(data);
        var view = BinaryView.inspectImpl(input);
        assertFalse(view.valid());
        assertTrue(input.failed());
        assertEquals("compressed int overflow", input.getErrorMessage());
    }

    @Test public void testExtBitsOverflow() {
        byte[] data = { 0, encode_type_and_meta(Type.OBJECT.ID, 2), -1, -1, -1, -1, 1 };
        var input = new BufferedInput(data);
        var view = BinaryView.inspectImpl(input);
        assertFalse(view.valid());
        assertTrue(input.failed());
        assertEquals("symbol id too big", input.getErrorMessage());
    }

    @Test public void testDecodeIndexOverflowArray() {
        byte[] data = { 0, encode_type_and_meta(Type.ARRAY.ID, 4) };
        var input = new BufferedInput(data);
        var view = BinaryView.inspectImpl(input);
        assertFalse(view.valid());
        assertTrue(input.failed());
        assertEquals("decode index too big", input.getErrorMessage());
    }

    @Test public void testDecodeIndexOverflowObject() {
        byte[] data = { 0, encode_type_and_meta(Type.OBJECT.ID, 4) };
        var input = new BufferedInput(data);
        var view = BinaryView.inspectImpl(input);
        assertFalse(view.valid());
        assertTrue(input.failed());
        assertEquals("decode index too big", input.getErrorMessage());
    }
}