def test_xx_handshake(self):
     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     s.connect(self.ENDPOINT)
     # send WA header indicating protocol version
     s.send(self.HEADER)
     # use WASegmentedStream for sending/receiving in frames
     stream = WASegmentedStream(SocketArbitraryStream(s))
     # initialize WANoiseProtocol
     wa_noiseprotocol = WANoiseProtocol(*PROTOCOL_VERSION)
     # start the protocol, this should a XX handshake since
     # we are not passing the remote static public key
     wa_noiseprotocol.start(stream, self.CONFIG, self.KEYPAIR)
     # we are now in transport phase, first incoming data
     # will indicate whether we are authenticated
     first_transport_data = wa_noiseprotocol.receive()
     # fourth + fifth byte are status, [237, 38] is failure
     self.assertEqual(b'\xed\x26', first_transport_data[3:5])
ENDPOINT = ("e1.whatsapp.net", 443)
HEADER = b"WA" + bytes(PROTOCOL_VERSION)

if __name__ == "__main__":
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(ENDPOINT)
    # send WA header indicating protocol version
    s.send(HEADER)
    # use WASegmentedStream for sending/receiving in frames
    stream = WASegmentedStream(SocketArbitraryStream(s))
    # initialize WANoiseProtocol
    wa_noiseprotocol = WANoiseProtocol(*PROTOCOL_VERSION)
    # start the protocol, this should a XX handshake since
    # we are not passing the remote static public key
    try:
        wa_noiseprotocol.start(stream, CONFIG, KEYPAIR)
        print("Handshake completed, checking authentication...")
        # we are now in transport phase, first incoming data
        # will indicate whether we are authenticated
        first_transport_data = wa_noiseprotocol.receive()
        # fourth + fifth byte are status, [237, 38] is failure
        if first_transport_data[3] == 51:
            print("Authentication succeeded")
        elif list(first_transport_data[3:5]) == [237, 38]:
            print("Authentication failed")
            sys.exit(1)
        else:
            print("Unrecognized authentication response: %s" %
                  (first_transport_data[3]))
            sys.exit(1)
    except:
if __name__ == "__main__":
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(ENDPOINT)
    # send WA header indicating protocol version
    s.send(HEADER)
    # use WASegmentedStream for sending/receiving in frames
    stream = WASegmentedStream(SocketArbitraryStream(s))
    # initialize WANoiseProtocol
    wa_noiseprotocol = WANoiseProtocol(*PROTOCOL_VERSION)
    # start the protocol, this should attempt a IK handshake since we
    # specifying the remote static public key. The handshake should
    # fail because WA_PUBLIC is invalid. This results in reverting to
    # a xxfallack handshake. Note that eventually authentication
    # should fail anyways due to invalid credentials of this examples.
    try:
        wa_noiseprotocol.start(stream, CONFIG, KEYPAIR, rs=WA_PUBLIC)
        print("Handshake completed, checking authentication...")
        # we are now in transport phase, first incoming data
        # will indicate whether we are authenticated
        first_transport_data = wa_noiseprotocol.receive()
        # fourth + fifth byte are status, [237, 38] is failure
        if first_transport_data[3] == 51:
            print("Authentication succeeded")
        elif list(first_transport_data[3:5]) == [237, 38]:
            print("Authentication failed")
            sys.exit(1)
        else:
            print("Unrecognized authentication response: %s" %
                  (list(first_transport_data[3:5])))
            sys.exit(1)
    except: