// Path: src/main/java/com/github/ffalcinelli/jdivert/exceptions/WinDivertException.java
// public class WinDivertException extends Exception {
//     protected int code;
//     protected String message;
//     protected LastErrorException lee;
// 
//     public WinDivertException(int code) {
//         this(code, Kernel32Util.formatMessage(code));
//     }
// 
//     public WinDivertException(int code, String message) {
//         this(code, message, null);
//     }
// 
//     public WinDivertException(int code, String message, Throwable cause) {
//         super(message, cause);
//         this.code = code;
//     }
// 
//     /**
//      * Throw a WinDivertException whenever GetLastError returned a code different from
//      * <ul>
//      * <li>0 (Success)</li>
//      * <li>997 (Overlapped I/O is in progress)</li>
//      *</ul>
//      * @return The GetLastError code.
//      * @throws WinDivertException When {@code GetLastError} returns a code different from 0 or 997, {@link WinDivertException} is thrown.
//      */
//     public static int throwExceptionOnGetLastError() throws WinDivertException {
//         int lastError = Native.getLastError();
//         if (lastError != 0 && lastError != 997) {
//             throw new WinDivertException(lastError);
//         }
//         return lastError;
//     }
// 
//     public int getCode() {
//         return code;
//     }
// 
//     @Override
//     public String toString() {
//         return "WinDivertException{" +
//                 "code=" + code +
//                 ", message='" + message + '\'' +
//                 '}';
//     }
// }


import com.github.ffalcinelli.jdivert.exceptions.WinDivertException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import static com.github.ffalcinelli.jdivert.headers.Tcp.Flag.FIN;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
/*
 * Copyright (c) Fabio Falcinelli 2016.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.github.ffalcinelli.jdivert;




/**
 * Created by fabio on 06/11/2016.
 */
public class LiveCaptureTestCase {
    WinDivert wd;
    EchoServer srv;
    EchoClient clt;

    @Before
    public void setUp() throws IOException {
        srv = new EchoServer();
        clt = new EchoClient(srv.getAddress(), srv.getPort(), "Test message.");

    }

    public void startupWithFilter(String filter) {
        wd = new WinDivert(filter);
        clt.setWinDivert(wd);
        srv.start();
        clt.start();
    }

    @After
    public void tearDown() throws InterruptedException {
        endThreads();
    }

    public void endThreads() throws InterruptedException {
        if (srv != null) {
            srv.close();
            srv.join();
        }
        if (clt != null) clt.join();
    }

    @Test
// Path: src/main/java/com/github/ffalcinelli/jdivert/exceptions/WinDivertException.java
// public class WinDivertException extends Exception {
//     protected int code;
//     protected String message;
//     protected LastErrorException lee;
// 
//     public WinDivertException(int code) {
//         this(code, Kernel32Util.formatMessage(code));
//     }
// 
//     public WinDivertException(int code, String message) {
//         this(code, message, null);
//     }
// 
//     public WinDivertException(int code, String message, Throwable cause) {
//         super(message, cause);
//         this.code = code;
//     }
// 
//     /**
//      * Throw a WinDivertException whenever GetLastError returned a code different from
//      * <ul>
//      * <li>0 (Success)</li>
//      * <li>997 (Overlapped I/O is in progress)</li>
//      *</ul>
//      * @return The GetLastError code.
//      * @throws WinDivertException When {@code GetLastError} returns a code different from 0 or 997, {@link WinDivertException} is thrown.
//      */
//     public static int throwExceptionOnGetLastError() throws WinDivertException {
//         int lastError = Native.getLastError();
//         if (lastError != 0 && lastError != 997) {
//             throw new WinDivertException(lastError);
//         }
//         return lastError;
//     }
// 
//     public int getCode() {
//         return code;
//     }
// 
//     @Override
//     public String toString() {
//         return "WinDivertException{" +
//                 "code=" + code +
//                 ", message='" + message + '\'' +
//                 '}';
//     }
// }


// Path: src/test/java/com/github/ffalcinelli/jdivert/LiveCaptureTestCase.java
import com.github.ffalcinelli.jdivert.exceptions.WinDivertException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import static com.github.ffalcinelli.jdivert.headers.Tcp.Flag.FIN;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

/*
 * Copyright (c) Fabio Falcinelli 2016.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.github.ffalcinelli.jdivert;




/**
 * Created by fabio on 06/11/2016.
 */
public class LiveCaptureTestCase {
    WinDivert wd;
    EchoServer srv;
    EchoClient clt;

    @Before
    public void setUp() throws IOException {
        srv = new EchoServer();
        clt = new EchoClient(srv.getAddress(), srv.getPort(), "Test message.");

    }

    public void startupWithFilter(String filter) {
        wd = new WinDivert(filter);
        clt.setWinDivert(wd);
        srv.start();
        clt.start();
    }

    @After
    public void tearDown() throws InterruptedException {
        endThreads();
    }

    public void endThreads() throws InterruptedException {
        if (srv != null) {
            srv.close();
            srv.join();
        }
        if (clt != null) clt.join();
    }

    @Test
    public void passThrough() throws WinDivertException, InterruptedException {