/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.coyote.http2;

import java.nio.ByteBuffer;

import org.junit.Assert;
import org.junit.Test;

/**
 * Unit tests for Section 5.§ of
 * <a href="https://tools.ietf.org/html/rfc7540">RFC 7540</a>.
 * <br>
 * The order of tests in this class is aligned with the order of the
 * requirements in the RFC.
 */
public class TestHttp2Section_5_1 extends Http2TestBase {

    @Test
    public void testIdleStateInvalidFrame01() throws Exception {
        http2Connect();

        sendWindowUpdate(3, 200);

        parser.readFrame(true);

        Assert.assertTrue(output.getTrace(),
                output.getTrace().startsWith("0-Goaway-[2147483647]-[" +
                        Error.PROTOCOL_ERROR.getCode() + "]-["));
    }


    @Test
    public void testIdleStateInvalidFrame02() throws Exception {
        http2Connect();

        sendData(3, new byte[] {});

        parser.readFrame(true);

        Assert.assertTrue(output.getTrace(),
                output.getTrace().startsWith("0-Goaway-[2147483647]-[" +
                        Error.PROTOCOL_ERROR.getCode() + "]-["));
    }


    // TODO: reserved local
    // TODO: reserved remote


    @Test
    public void halfClosedRemoteInvalidFrame() throws Exception {
        hpackEncoder = new HpackEncoder(ConnectionSettings.DEFAULT_HEADER_TABLE_SIZE);
        http2Connect();

        // This half-closes the stream since it includes the end of stream flag
        sendSimpleRequest(3);
        readSimpleResponse();
        Assert.assertEquals(getSimpleResponseTrace(3), output.getTrace());
        output.clearTrace();

        // This should trigger a stream error
        sendData(3, new byte[] {});
        parser.readFrame(true);

        Assert.assertTrue(output.getTrace(),
                output.getTrace().startsWith("0-Goaway-[2147483647]-[" +
                        Error.STREAM_CLOSED.getCode() + "]-["));
    }


    @Test
    public void testClosedInvalidFrame01() throws Exception {
        hpackEncoder = new HpackEncoder(ConnectionSettings.DEFAULT_HEADER_TABLE_SIZE);

        // HTTP2 upgrade
        http2Connect();

        // Build the simple request
        byte[] frameHeader = new byte[9];
        ByteBuffer headersPayload = ByteBuffer.allocate(128);
        buildSimpleRequest(frameHeader, headersPayload, 3);

        // Remove the end of stream and end of headers flags
        frameHeader[4] = 0;

        // Process the request
        writeFrame(frameHeader, headersPayload);

        // Send a rst
        sendRst(3, Error.INTERNAL_ERROR.getCode());

        // Then try sending some data (which should fail)
        sendData(3, new byte[] {});
        parser.readFrame(true);

        Assert.assertTrue(output.getTrace(),
                output.getTrace().startsWith("3-RST-[" + Error.STREAM_CLOSED.getCode() + "]"));
    }


    @Test
    public void testClosedInvalidFrame02() throws Exception {
        http2Connect();

        // Stream 1 is closed. This should trigger a stream error
        sendData(1, new byte[] {});
        parser.readFrame(true);

        Assert.assertTrue(output.getTrace(),
                output.getTrace().startsWith("0-Goaway-[2147483647]-[" +
                        Error.STREAM_CLOSED.getCode() + "]-["));
    }


    // TODO: Invalid frames for each of the remaining states

    // Section 5.1.1

    @Test
    public void testClientSendEvenStream() throws Exception {
        hpackEncoder = new HpackEncoder(ConnectionSettings.DEFAULT_HEADER_TABLE_SIZE);

        // HTTP2 upgrade
        http2Connect();

        // Part 1
        byte[] frameHeader = new byte[9];
        ByteBuffer headersPayload = ByteBuffer.allocate(128);
        buildSimpleRequestPart1(frameHeader, headersPayload, 4);
        writeFrame(frameHeader, headersPayload);

        // headers, body
        parser.readFrame(true);

        Assert.assertTrue(output.getTrace(),
                output.getTrace().startsWith("0-Goaway-[2147483647]-[" +
                        Error.PROTOCOL_ERROR.getCode() + "]-["));
    }

    // TODO Remaining 5.1.1 tests

    // TODO 5.1.2 tests
}

@@ -4616,14 +4616,871 @@
 ders
-, body
+%0A        parser.readFrame(true);%0A%0A        Assert.assertTrue(output.getTrace(),%0A                output.getTrace().startsWith(%220-Goaway-%5B2147483647%5D-%5B%22 +%0A                        Error.PROTOCOL_ERROR.getCode() + %22%5D-%5B%22));%0A    %7D%0A%0A%0A    @Test%0A    public void testClientSendOldStream() throws Exception %7B%0A        hpackEncoder = new HpackEncoder(ConnectionSettings.DEFAULT_HEADER_TABLE_SIZE);%0A%0A        http2Connect();%0A        sendSimpleRequest(5);%0A        readSimpleResponse();%0A        Assert.assertEquals(getSimpleResponseTrace(5), output.getTrace());%0A        output.clearTrace();%0A%0A%0A        // Build the simple request on an old stream%0A        byte%5B%5D frameHeader = new byte%5B9%5D;%0A        ByteBuffer headersPayload = ByteBuffer.allocate(128);%0A        buildSimpleRequest(frameHeader, headersPayload, 3);%0A%0A        os.write(frameHeader);%0A        os.flush();%0A%0A        // headers
 %0A   
@@ -5693,23 +5693,25 @@
 %5D-%5B%22));%0A
+%0A
     %7D%0A%0A
+%0A
     // T

Check stream IDs do not go backwards