<!doctype html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Testing DTLS10 connectivity</title>
    <script src="../../resources/testharness.js"></script>
    <script src="../../resources/testharnessreport.js"></script>
  </head>
  <body>
    <script>
  'use strict';
var useDTLS10ForLocalConnection = false;
var useDTLS10ForRemoteConnection = false;

async function createDTLS10andDTLS12Connections(setupLocalConnection, setupRemoteConnection) {
    internals.setUseDTLS10(useDTLS10ForLocalConnection);
    const localConnection = new RTCPeerConnection();
    internals.setUseDTLS10(useDTLS10ForRemoteConnection);
    const remoteConnection = new RTCPeerConnection();
    internals.setUseDTLS10(false);

    remoteConnection.onicecandidate = (event) => { localConnection.addIceCandidate(event.candidate); };
    localConnection.onicecandidate = (event) => { remoteConnection.addIceCandidate(event.candidate); };

    setupLocalConnection(localConnection);
    setupRemoteConnection(localConnection);

    const offer = await localConnection.createOffer();
    await localConnection.setLocalDescription(offer);
    await remoteConnection.setRemoteDescription(offer);

    const answer = await remoteConnection.createAnswer();
    await remoteConnection.setLocalDescription(answer);
    await localConnection.setRemoteDescription(answer);

    return [localConnection, remoteConnection];
}

promise_test(async (test) => {
    if (!window.internals)
        return Promise.reject("test requires internals");

    useDTLS10ForLocalConnection = false;
    useDTLS10ForRemoteConnection = false;

    const [localConnection, remoteConnection] = await createDTLS10andDTLS12Connections((connection) => {
        connection.createDataChannel('test');
    }, (remoteConnection) => {
    });

    assert_not_equals(localConnection.sctp, null);

    let counter = 0;
    while (++counter < 20) {
        if (localConnection.sctp.transport.state === "connected")
           return;
        await new Promise(resolve => setTimeout(resolve, 10));
    }
}, "Verify regular clients can connect with each other");

promise_test(async (test) => {
    if (!window.internals)
        return Promise.reject("test requires internals");

    useDTLS10ForLocalConnection = false;
    useDTLS10ForRemoteConnection = true;

    const [localConnection, remoteConnection] = await createDTLS10andDTLS12Connections((connection) => {
        connection.createDataChannel('test');
    }, (remoteConnection) => {
    });

    await new Promise(resolve => setTimeout(resolve, 200));
    assert_not_equals(localConnection.sctp, null);
    assert_not_equals(localConnection.sctp.transport.state, "connected");
}, "Verify regular clients cannot connect with DTLS1-only clients");
    </script>
  </body>
</html>
