int main(int argc, char* argv[])
{
  InfStandaloneIo* io;
  InfdTcpServer* server;
  GError* error;

  g_type_init();

  io = inf_standalone_io_new();
  error = NULL;

  server = g_object_new(
    INFD_TYPE_TCP_SERVER,
    "io", io,
    "local-port", 5223,
    NULL
  );

  g_signal_connect(
    G_OBJECT(server),
    "error",
    G_CALLBACK(error_cb),
    io
  );

  g_signal_connect(
    G_OBJECT(server),
    "new-connection",
    G_CALLBACK(new_connection_cb),
    io
  );

  g_signal_connect(
    G_OBJECT(server),
    "notify::status",
    G_CALLBACK(notify_status_cb),
    io
  );

  if(infd_tcp_server_open(server, &error) == FALSE)
  {
    fprintf(stderr, "Could not open server: %s\n", error->message);
    g_error_free(error);
  }
  else
  {
    inf_standalone_io_loop(io);
  }

  g_object_unref(G_OBJECT(io));
  g_object_unref(G_OBJECT(server));

  return 0;
}
int
main(int argc,
     char* argv[])
{
    InfTestMassJoin massjoin;
    GError* error;
    int i;
    gchar* name;

    error = NULL;
    if(!inf_init(&error))
    {
        fprintf(stderr, "%s\n", error->message);
        return -1;
    }

    massjoin.io = INF_IO(inf_standalone_io_new());
    massjoin.joiners = NULL;

    for(i = 0; i < 128; ++i)
    {
        name = g_strdup_printf("MassJoin%03d", i);

        inf_test_mass_join_connect(
            &massjoin,
            "127.0.0.1",
            inf_protocol_get_default_port(),
            "Test",
            name
        );

        g_free(name);
        //g_usleep(100000);
    }

    inf_standalone_io_loop(INF_STANDALONE_IO(massjoin.io));
    return 0;
}
int main(int argc, char* argv[])
{
  InfStandaloneIo* io;
  InfNameResolver* resolver;
  InfTcpConnection* connection;
  GError* error;

  error = NULL;
  if(inf_init(&error) == FALSE)
  {
    fprintf(stderr, "%s", error->message);
    g_error_free(error);
    return 1;
  }

  io = inf_standalone_io_new();

  resolver =
    inf_name_resolver_new(INF_IO(io), "0x539.de", "5223", "_jabber._tcp");

  g_signal_connect(
    G_OBJECT(resolver),
    "resolved",
    G_CALLBACK(resolved_cb),
    io
  );

  connection = inf_tcp_connection_new_resolve(INF_IO(io), resolver);
  g_object_unref(resolver);

  g_signal_connect(
    G_OBJECT(connection),
    "received",
    G_CALLBACK(received_cb),
    io
  );

  g_signal_connect(
    G_OBJECT(connection),
    "sent",
    G_CALLBACK(sent_cb),
    io
  );

  g_signal_connect(
    G_OBJECT(connection),
    "error",
    G_CALLBACK(error_cb),
    io
  );

  g_signal_connect(
    G_OBJECT(connection),
    "notify::status",
    G_CALLBACK(notify_status_cb),
    io
  );

  if(inf_tcp_connection_open(connection, &error) == FALSE)
  {
    fprintf(stderr, "Could not open connection: %s\n", error->message);
    g_error_free(error);
  }
  else
  {
    inf_standalone_io_loop(io);
  }

  g_object_unref(G_OBJECT(io));
  g_object_unref(G_OBJECT(connection));

  return 0;
}
static gboolean
inf_test_certificate_validate_run(const InfTestCertificateValidateDesc* desc,
                                  GError** error)
{
  InfIo* io;
  InfdXmppServer* server;

  InfXmppManager* xmpp_manager;
  InfCertificateVerify* verify;
  InfXmppConnection* client;
  gchar* pinned_file;

  InfXmlConnectionStatus status;
  InfTestCertificateValidateCheckCertificateData check_certificate_data;
  gboolean result;

  GError* conn_error;
  GHashTable* pinned;
  gnutls_x509_crt_t pinned_cert;
  InfCertificateChain* current_cert;
  gboolean cert_equal;

  /* Setup server */
  io = INF_IO(inf_standalone_io_new());

  server = inf_test_certificate_setup_server(
    io,
    desc->key_file,
    desc->cert_file,
    error
  );

  if(server == NULL)
  {
    g_object_unref(io);
    return FALSE;
  }

  /* Create client */
  pinned_file = inf_test_validate_setup_pin(
    desc->hostname,
    desc->pinned_certificate,
    error
  );

  if(pinned_file == NULL)
  {
    g_object_unref(server);
    g_object_unref(io);
    return FALSE;
  }

  xmpp_manager = inf_xmpp_manager_new();
  verify = inf_certificate_verify_new(xmpp_manager, pinned_file);

  check_certificate_data.did_query = FALSE;
  check_certificate_data.accept_query = desc->accept_query;
  g_signal_connect(
    G_OBJECT(verify),
    "check-certificate",
    G_CALLBACK(inf_test_certificate_validate_check_certificate),
    &check_certificate_data
  );

  client = inf_test_certificate_validate_setup_client(
    io,
    desc->ca_file,
    desc->hostname,
    error
  );

  if(client == NULL)
  {
    g_unlink(pinned_file);
    g_free(pinned_file);
    g_object_unref(io);
    g_object_unref(xmpp_manager);
    g_object_unref(verify);
    g_object_unref(server);
    return FALSE;
  }

  inf_xmpp_manager_add_connection(xmpp_manager, client);

  /* Okay, now watch for status changes on the client or whether a dialog
   * appears. */
  g_signal_connect(
    G_OBJECT(client),
    "notify::status",
    G_CALLBACK(inf_test_validate_certificate_notify_status_cb),
    io
  );

  conn_error = NULL;
  g_signal_connect(
    G_OBJECT(client),
    "error",
    G_CALLBACK(inf_test_validate_certificate_error_cb),
    &conn_error
  );

  inf_standalone_io_loop(INF_STANDALONE_IO(io));
  g_object_unref(io);

  /* Evaluate result */
  result = TRUE;
  g_object_get(G_OBJECT(client), "status", &status, NULL);
  if(status == INF_XML_CONNECTION_OPEN)
  {
    g_assert(conn_error == NULL);

    if(check_certificate_data.did_query == TRUE &&
       desc->expectation != INF_TEST_CERTIFICATE_VALIDATE_EXPECT_QUERY_ACCEPT)
    {
      g_set_error(
        error,
        inf_test_certificate_validate_error(),
        3,
        "Certificate queried and accepted but not expected to"
      );

      result = FALSE;
    }
    else if(check_certificate_data.did_query == FALSE &&
            desc->expectation != INF_TEST_CERTIFICATE_VALIDATE_EXPECT_ACCEPT)
    {
      g_set_error(
        error,
        inf_test_certificate_validate_error(),
        0,
        "Certificate accepted but not expected to"
      );

      result = FALSE;
    }
  }
  else
  {
    g_assert(check_certificate_data.did_query || conn_error != NULL);

    /* TODO: The certificate verification result is not preserved at
     * the moment. We could change this in
     * inf_xmpp_connection_certificate_verify_cancel such that the existing
     * error is used if any, or otherwise our own is created. */
    if(conn_error != NULL &&
       conn_error->domain != inf_xmpp_connection_error_quark() &&
       conn_error->code != INF_XMPP_CONNECTION_ERROR_CERTIFICATE_NOT_TRUSTED)
    {
      g_propagate_error(error, conn_error);
      conn_error = NULL;
      result = FALSE;
    }
    else if(check_certificate_data.did_query == TRUE &&
            desc->expectation != INF_TEST_CERTIFICATE_VALIDATE_EXPECT_QUERY_REJECT)
    {
      g_set_error(
        error,
        inf_test_certificate_validate_error(),
        2,
        "Certificate queried and rejected but not expected to"
      );

      result = FALSE;
    }
    else if(check_certificate_data.did_query == FALSE &&
            desc->expectation != INF_TEST_CERTIFICATE_VALIDATE_EXPECT_REJECT)
    {
      g_set_error(
        error,
        inf_test_certificate_validate_error(),
        1,
        "Certificate rejected but not expected to"
      );

      result = FALSE;
    }

    if(conn_error != NULL)
    {
      g_error_free(conn_error);
      conn_error = NULL;
    }
  }

  /* If we got the expected result, check whether the host was correctly
   * pinned or not. */
  if(result == TRUE)
  {
    pinned = inf_cert_util_read_certificate_map(pinned_file, error);
    if(pinned == NULL)
    {
      result = FALSE;
    }
    else
    {
      pinned_cert = g_hash_table_lookup(pinned, desc->hostname);

      cert_equal = FALSE;
      if(pinned_cert != NULL)
      {
        g_object_get(
          G_OBJECT(client),
          "remote-certificate", &current_cert,
          NULL
        );

        cert_equal = inf_cert_util_compare_fingerprint(
          pinned_cert,
          inf_certificate_chain_get_own_certificate(current_cert),
          &conn_error
        );

        inf_certificate_chain_unref(current_cert);
      }

      if(conn_error != NULL)
      {
        g_propagate_error(error, conn_error);
        conn_error = NULL;
      }
      else if(cert_equal == TRUE && desc->expect_pinned == FALSE)
      {
        g_set_error(
          error,
          inf_test_certificate_validate_error(),
          4,
          "Certificate was pinned but not expected to"
        );

        result = FALSE;
      }
      else if(pinned_cert == NULL && desc->expect_pinned == TRUE)
      {
        g_set_error(
          error,
          inf_test_certificate_validate_error(),
          5,
          "Certificate was not pinned but expected to"
        );

        result = FALSE;
      }

      g_hash_table_destroy(pinned);
    }
  }

  g_unlink(pinned_file);
  g_free(pinned_file);
  g_object_unref(xmpp_manager);
  g_object_unref(verify);
  g_object_unref(server);
  g_object_unref(client);
  return result;
}
int
main(int argc, char* argv[])
{
  InfTestBrowser test;
  InfIpAddress* address;
  InfCommunicationManager* manager;
  InfTcpConnection* tcp_conn;
  GError* error;

  gnutls_global_init();
  g_type_init();

  test.io = inf_standalone_io_new();
#ifndef G_OS_WIN32
  test.input_fd = STDIN_FILENO;
#endif
  address = inf_ip_address_new_loopback4();

  error = NULL;
  tcp_conn =
    inf_tcp_connection_new_and_open(INF_IO(test.io), address, inf_protocol_get_default_port(), &error);

  inf_ip_address_free(address);

  if(tcp_conn == NULL)
  {
    fprintf(stderr, "Could not open TCP connection: %s\n", error->message);
    g_error_free(error);
  }
  else
  {
    test.conn = inf_xmpp_connection_new(
      tcp_conn,
      INF_XMPP_CONNECTION_CLIENT,
      NULL,
      "localhost",
      INF_XMPP_CONNECTION_SECURITY_BOTH_PREFER_TLS,
      NULL,
      NULL,
      NULL
    );

    g_object_unref(G_OBJECT(tcp_conn));

    manager = inf_communication_manager_new();
    test.browser = INF_BROWSER(
      infc_browser_new(
        INF_IO(test.io),
        manager,
        INF_XML_CONNECTION(test.conn)
      )
    );

    g_signal_connect_after(
      G_OBJECT(test.browser),
      "notify::status",
      G_CALLBACK(inf_test_browser_notify_status_cb),
      &test
    );

    g_signal_connect(
      G_OBJECT(test.browser),
      "error",
      G_CALLBACK(inf_test_browser_error_cb),
      &test
    );

    inf_standalone_io_loop(test.io);
    g_object_unref(G_OBJECT(manager));
    g_object_unref(G_OBJECT(test.browser));

    /* TODO: Wait until the XMPP connection is in status closed */
    g_object_unref(G_OBJECT(test.conn));
  }

  g_object_unref(G_OBJECT(test.io));
  return 0;
}
int main(int argc, char* argv[])
{
  InfIpAddress* addr;
  InfStandaloneIo* io;
  InfTcpConnection* connection;
  GError* error;

  g_type_init();

  addr = inf_ip_address_new_loopback4();
  io = inf_standalone_io_new();
  error = NULL;

  connection = g_object_new(
    INF_TYPE_TCP_CONNECTION,
    "io", io,
    "remote-address", addr,
    "remote-port", 5223,
    NULL
  );

  inf_ip_address_free(addr);

  g_signal_connect(
    G_OBJECT(connection),
    "received",
    G_CALLBACK(received_cb),
    io
  );

  g_signal_connect(
    G_OBJECT(connection),
    "sent",
    G_CALLBACK(sent_cb),
    io
  );

  g_signal_connect(
    G_OBJECT(connection),
    "error",
    G_CALLBACK(error_cb),
    io
  );

  g_signal_connect(
    G_OBJECT(connection),
    "notify::status",
    G_CALLBACK(notify_status_cb),
    io
  );

  if(inf_tcp_connection_open(connection, &error) == FALSE)
  {
    fprintf(stderr, "Could not open connection: %s\n", error->message);
    g_error_free(error);
  }
  else
  {
    inf_standalone_io_loop(io);
  }

  g_object_unref(G_OBJECT(io));
  g_object_unref(G_OBJECT(connection));

  return 0;
}
int main(int argc, char* argv[])
{
  InfStandaloneIo* io;
  InfdTcpServer* server;
  InfdXmppServer* xmpp;
  GError* error;

  error = NULL;
  if(!inf_init(&error))
  {
    fprintf(stderr, "%s\n", error->message);
    return 1;
  }

  io = inf_standalone_io_new();

  server = g_object_new(
    INFD_TYPE_TCP_SERVER,
    "io", io,
    "local-port", 5223,
    NULL
  );

  if(infd_tcp_server_open(server, &error) == FALSE)
  {
    fprintf(stderr, "Could not open server: %s\n", error->message);
    g_error_free(error);
  }
  else
  {
    xmpp = infd_xmpp_server_new(
      server,
      INF_XMPP_CONNECTION_SECURITY_ONLY_UNSECURED,
      NULL,
      NULL,
      NULL
    );

    g_signal_connect(
      G_OBJECT(xmpp),
      "error",
      G_CALLBACK(error_cb),
      io
    );

    g_signal_connect(
      G_OBJECT(xmpp),
      "new-connection",
      G_CALLBACK(new_connection_cb),
      io
    );

    g_signal_connect(
      G_OBJECT(xmpp),
      "notify::status",
      G_CALLBACK(notify_status_cb),
      io
    );

    inf_standalone_io_loop(io);
  }

  g_object_unref(G_OBJECT(io));
  g_object_unref(G_OBJECT(server));

  return 0;
}