package be.iminds.ilabt.jfed.ssh_terminal_tool.unix;

import be.iminds.ilabt.jfed.util.IOUtils;
import org.testng.annotations.Test;

import java.io.File;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;

public class OpenSshKnownHostsFileTest {

    @Test
    public void testAddKnownHost() throws Exception {
        File tmpFile = File.createTempFile("test_known_hosts", ".tmp");
        tmpFile.deleteOnExit();
        OpenSshKnownHostsFile openSshKnownHostsFile = new OpenSshKnownHostsFile(tmpFile);
        openSshKnownHostsFile.addKnownHost("example.com", "example.com ssh-rsa AAAAB3test/Ua0=");
        openSshKnownHostsFile.addKnownHost("example.net", "example.net ssh-rsa AAAAB3test2FFF= comment");
        String knowHostContent = IOUtils.fileToString(tmpFile);
        assertThat(knowHostContent, containsString("example.com ssh-rsa AAAAB3test/Ua0="));
        assertThat(knowHostContent, containsString("example.net ssh-rsa AAAAB3test2FFF="));
    }
    @Test
    public void testAddKnownHost2() throws Exception {
        File tmpFile = File.createTempFile("test_known_hosts", ".tmp");
        tmpFile.deleteOnExit();
        OpenSshKnownHostsFile openSshKnownHostsFile = new OpenSshKnownHostsFile(tmpFile);
        openSshKnownHostsFile.addKnownHost("example.com", "AAAAB3test/Ua0=");
        openSshKnownHostsFile.addKnownHost("example.net", "AAAAB3test2FFF=");
        String knowHostContent = IOUtils.fileToString(tmpFile);
        assertThat(knowHostContent, containsString("example.com ssh-rsa AAAAB3test/Ua0="));
        assertThat(knowHostContent, containsString("example.net ssh-rsa AAAAB3test2FFF="));
    }
    @Test
    public void testAddKnownHost3() throws Exception {
        File tmpFile = File.createTempFile("test_known_hosts", ".tmp");
        tmpFile.deleteOnExit();
        OpenSshKnownHostsFile openSshKnownHostsFile = new OpenSshKnownHostsFile(tmpFile);
        //must handle extra spaces, because testbeds.xml might have those
        openSshKnownHostsFile.addKnownHost("example.com", "\n      example.com ssh-rsa\n      AAAAB3test/Ua0=\n");
        openSshKnownHostsFile.addKnownHost("example.net", "\n   \n  example.net  \n  ssh-rsa\n   \nAAAAB3test2FFF=\ncomment\n\n\n");
        String knowHostContent = IOUtils.fileToString(tmpFile);
        assertThat(knowHostContent, containsString("example.com ssh-rsa AAAAB3test/Ua0="));
        assertThat(knowHostContent, containsString("example.net ssh-rsa AAAAB3test2FFF="));

        assertThat(openSshKnownHostsFile.knowsHost("example.com", "AAAAB3test/Ua0="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "AAAAB3test2FFF="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "AAAAB3test3FFF="), is(false));
        assertThat(openSshKnownHostsFile.knowsHost("example.com", "AAAAB3test5/Ua0="), is(false));
    }



    @Test
    public void testKnowsHost() throws Exception {
        File tmpFile = File.createTempFile("test_known_hosts", ".tmp");
        tmpFile.deleteOnExit();
        OpenSshKnownHostsFile openSshKnownHostsFile = new OpenSshKnownHostsFile(tmpFile);
        openSshKnownHostsFile.addKnownHost("example.com", "example.com ssh-rsa AAAAB3test/Ua0=");
        openSshKnownHostsFile.addKnownHost("example.net", "example.net ssh-rsa AAAAB3test2FFF= comment");


        assertThat(openSshKnownHostsFile.knowsHost("example.com", "example.com ssh-rsa AAAAB3test/Ua0="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "example.net ssh-rsa AAAAB3test2FFF= comment"), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "example.org ssh-rsa AAAAB3test2FFF="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "example.org ssh-rsa AAAAB3test3FFF="), is(false));
        assertThat(openSshKnownHostsFile.knowsHost("example.com", "example.com ssh-rsa AAAAB3test5/Ua0="), is(false));

        assertThat(openSshKnownHostsFile.knowsHost("example.com", "AAAAB3test/Ua0="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "AAAAB3test2FFF="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "AAAAB3test3FFF="), is(false));
        assertThat(openSshKnownHostsFile.knowsHost("example.com", "AAAAB3test5/Ua0="), is(false));
    }

    @Test
    public void testKnowsHost2() throws Exception {
        File tmpFile = File.createTempFile("test_known_hosts", ".tmp");
        tmpFile.deleteOnExit();
        OpenSshKnownHostsFile openSshKnownHostsFile = new OpenSshKnownHostsFile(tmpFile);
        openSshKnownHostsFile.addKnownHost("example.com", "AAAAB3test/Ua0=");
        openSshKnownHostsFile.addKnownHost("example.net", "AAAAB3test2FFF=");


        assertThat(openSshKnownHostsFile.knowsHost("example.com", "example.com ssh-rsa AAAAB3test/Ua0="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "example.net ssh-rsa AAAAB3test2FFF= comment"), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "example.org ssh-rsa AAAAB3test2FFF="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "example.org ssh-rsa AAAAB3test3FFF="), is(false));
        assertThat(openSshKnownHostsFile.knowsHost("example.com", "example.com ssh-rsa AAAAB3test5/Ua0="), is(false));

        assertThat(openSshKnownHostsFile.knowsHost("example.com", "AAAAB3test/Ua0="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "AAAAB3test2FFF="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "AAAAB3test3FFF="), is(false));
        assertThat(openSshKnownHostsFile.knowsHost("example.com", "AAAAB3test5/Ua0="), is(false));
    }

    @Test
    public void testKnowsHost3() throws Exception {
        File tmpFile = File.createTempFile("test_known_hosts", ".tmp");
        tmpFile.deleteOnExit();
        String content = "example.com,127.0.0.1 ssh-rsa AAAAB3test/Ua0= testkey\n" +
                "127.0.0.2,example.net ssh-rsa AAAAB3test2FFF=";
        IOUtils.stringToFile(tmpFile, content);
        OpenSshKnownHostsFile openSshKnownHostsFile = new OpenSshKnownHostsFile(tmpFile);

        assertThat(openSshKnownHostsFile.knowsHost("example.com", "example.com ssh-rsa AAAAB3test/Ua0="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("127.0.0.1", "example.com ssh-rsa AAAAB3test/Ua0="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "example.net ssh-rsa AAAAB3test2FFF= comment"), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("127.0.0.2", "example.net ssh-rsa AAAAB3test2FFF= comment"), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "example.org ssh-rsa AAAAB3test2FFF="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "example.org ssh-rsa AAAAB3test3FFF="), is(false));
        assertThat(openSshKnownHostsFile.knowsHost("example.com", "example.com ssh-rsa AAAAB3test5/Ua0="), is(false));
        assertThat(openSshKnownHostsFile.knowsHost("127.0.0.1", "example.com ssh-rsa AAAAB3test5/Ua0="), is(false));

        assertThat(openSshKnownHostsFile.knowsHost("example.com", "AAAAB3test/Ua0="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("127.0.0.1", "AAAAB3test/Ua0="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "AAAAB3test2FFF="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("127.0.0.2", "AAAAB3test2FFF="), is(true));
        assertThat(openSshKnownHostsFile.knowsHost("example.net", "AAAAB3test3FFF="), is(false));
        assertThat(openSshKnownHostsFile.knowsHost("example.com", "AAAAB3test5/Ua0="), is(false));
        assertThat(openSshKnownHostsFile.knowsHost("127.0.0.1", "AAAAB3test5/Ua0="), is(false));
    }
}