|
| 1 | +/* |
| 2 | + * JBoss, Home of Professional Open Source. |
| 3 | + * Copyright 2026 Red Hat, Inc., and individual contributors |
| 4 | + * as indicated by the @author tags. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package io.undertow.protocols.http2; |
| 20 | + |
| 21 | +import io.undertow.protocols.http2.HpackDecoder.HeaderEmitter; |
| 22 | +import io.undertow.testutils.category.UnitTest; |
| 23 | +import io.undertow.util.HeaderMap; |
| 24 | +import io.undertow.util.HttpString; |
| 25 | + |
| 26 | +import org.junit.Assert; |
| 27 | +import org.junit.Test; |
| 28 | +import org.junit.experimental.categories.Category; |
| 29 | + |
| 30 | +import java.nio.ByteBuffer; |
| 31 | +import java.util.Arrays; |
| 32 | +import java.util.concurrent.atomic.AtomicInteger; |
| 33 | + |
| 34 | +/** |
| 35 | + * @author Kanatoko |
| 36 | + */ |
| 37 | +@Category(UnitTest.class) |
| 38 | +public class HpackOverflowUnitTestCase { |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testStringLiteralContainingEOS() throws HpackException { |
| 42 | + final int countOfSameNames = testImpl("X-Header-1", "X-Header-1"); |
| 43 | + final int countOfDifferentNames = testImpl("X-Header-1", "X-Header-2"); |
| 44 | + Assert.assertEquals(2, countOfSameNames); |
| 45 | + Assert.assertEquals(2, countOfDifferentNames); |
| 46 | + } |
| 47 | + |
| 48 | + private static int testImpl(final String name1, final String name2) { |
| 49 | + final HeaderMap headerMap = new HeaderMap(); |
| 50 | + headerMap.add(new HttpString(name1), getStr(300, (byte) 0x41));// overflow |
| 51 | + headerMap.add(new HttpString(name2), getStr(150, (byte) 0x42));// skipped if name1 equals name2 |
| 52 | + final HpackEncoder encoder = new HpackEncoder(4096); |
| 53 | + final ByteBuffer buffer1 = ByteBuffer.allocate(256); |
| 54 | + |
| 55 | + // overflow |
| 56 | + HpackEncoder.State result = encoder.encode(headerMap, buffer1); |
| 57 | + |
| 58 | + Assert.assertEquals(HpackEncoder.State.OVERFLOW, result); |
| 59 | + final ByteBuffer buffer2 = ByteBuffer.allocate(512); |
| 60 | + |
| 61 | + // complete |
| 62 | + result = encoder.encode(headerMap, buffer2); |
| 63 | + Assert.assertEquals(HpackEncoder.State.COMPLETE, result); |
| 64 | + |
| 65 | + final HpackDecoder decoder = new HpackDecoder(4096); |
| 66 | + |
| 67 | + final AtomicInteger count = new AtomicInteger(); |
| 68 | + try { |
| 69 | + buffer2.flip(); |
| 70 | + decoder.setHeaderEmitter(new HeaderEmitter() { |
| 71 | + |
| 72 | + @Override |
| 73 | + public void emitHeader(HttpString name, String value, boolean neverIndex) throws HpackException { |
| 74 | + count.incrementAndGet(); |
| 75 | + |
| 76 | + } |
| 77 | + }); |
| 78 | + decoder.decode(buffer2, false); |
| 79 | + } catch (HpackException e) { |
| 80 | + e.printStackTrace(); |
| 81 | + } |
| 82 | + return count.get(); |
| 83 | + } |
| 84 | + |
| 85 | + private static String getStr(final int length, byte content) { |
| 86 | + final byte[] buf = new byte[length]; |
| 87 | + Arrays.fill(buf, content); |
| 88 | + return new String(buf); |
| 89 | + } |
| 90 | + |
| 91 | +} |
0 commit comments