<commit_before>open Hostnet
open Lwt.Infix

let src =
  let src = Logs.Src.create "test" ~doc:"Test the slirp stack" in
  Logs.Src.set_level src (Some Logs.Debug);
  src

module Log = (val Logs.src_log src : Logs.LOG)

module Make(Host: Sig.HOST) = struct

module Slirp_stack = Slirp_stack.Make(Host)
open Slirp_stack

let test_dhcp_query () =
  let t =
    with_stack
      (fun stack ->
        let ips = List.map Ipaddr.V4.to_string (Client.IPV4.get_ip (Client.ipv4 stack)) in
        Log.info (fun f -> f "Got an IP: %s" (String.concat ", " ips));
        Lwt.return ()
      ) in
  Host.Main.run t

let test_dns_query server () =
  let t =
    with_stack
      (fun stack ->
        let resolver = DNS.create stack in
        DNS.gethostbyname ~server resolver "www.google.com"
        >>= fun ips ->
        Log.info (fun f -> f "www.google.com has IPs: %s" (String.concat ", " (List.map Ipaddr.to_string ips)));
        Lwt.return ()
      ) in
  Host.Main.run t

let test_etc_hosts_query server () =
  let test_name = "vpnkit.is.cool.yes.really" in
  let t =
    with_stack
      (fun stack ->
        let resolver = DNS.create stack in
        DNS.gethostbyname ~server resolver test_name
        >>= function
        | (_ :: _) as ips ->
          Log.err (fun f -> f "This test relies on the name %s not existing but it really has IPs: %s" test_name (String.concat ", " (List.map Ipaddr.to_string ips)));
          failwith (Printf.sprintf "Test name %s really does exist" test_name)
        | _ -> begin
          Hostnet.Hosts.etc_hosts := [
            test_name, Ipaddr.V4 (Ipaddr.V4.localhost);
          ];
          DNS.gethostbyname ~server resolver test_name
          >>= function
          | (_ :: _) as ips ->
            Log.info (fun f -> f "Name %s has IPs: %s" test_name (String.concat ", " (List.map Ipaddr.to_string ips)));<commit_msg>test: if we fail to resolve www.google.com, make the test fail<commit_after>open Hostnet
open Lwt.Infix

let src =
  let src = Logs.Src.create "test" ~doc:"Test the slirp stack" in
  Logs.Src.set_level src (Some Logs.Debug);
  src

module Log = (val Logs.src_log src : Logs.LOG)

module Make(Host: Sig.HOST) = struct

module Slirp_stack = Slirp_stack.Make(Host)
open Slirp_stack

let test_dhcp_query () =
  let t =
    with_stack
      (fun stack ->
        let ips = List.map Ipaddr.V4.to_string (Client.IPV4.get_ip (Client.ipv4 stack)) in
        Log.info (fun f -> f "Got an IP: %s" (String.concat ", " ips));
        Lwt.return ()
      ) in
  Host.Main.run t

let test_dns_query server () =
  let t =
    with_stack
      (fun stack ->
        let resolver = DNS.create stack in
        DNS.gethostbyname ~server resolver "www.google.com"
        >>= function
        | (_ :: _) as ips ->
          Log.info (fun f -> f "www.google.com has IPs: %s" (String.concat ", " (List.map Ipaddr.to_string ips)));
          Lwt.return ()
        | _ ->
          Log.err (fun f -> f "Failed to lookup www.google.com");
          failwith "Failed to lookup www.google.com"
      ) in
  Host.Main.run t

let test_etc_hosts_query server () =
  let test_name = "vpnkit.is.cool.yes.really" in
  let t =
    with_stack
      (fun stack ->
        let resolver = DNS.create stack in
        DNS.gethostbyname ~server resolver test_name
        >>= function
        | (_ :: _) as ips ->
          Log.err (fun f -> f "This test relies on the name %s not existing but it really has IPs: %s" test_name (String.concat ", " (List.map Ipaddr.to_string ips)));
          failwith (Printf.sprintf "Test name %s really does exist" test_name)
        | _ -> begin
          Hostnet.Hosts.etc_hosts := [
            test_name, Ipaddr.V4 (Ipaddr.V4.localhost);
          ];
          DNS.gethostbyname ~server resolver test_name
          >>= function
          | (_ :: _) as ips ->
            Log.info (fun f -> f "Name %s has IPs: %s" test_name (String.concat ", " (List.map Ipaddr.to_string ips)));
{'fraction_non_alphanumeric': 0.12227295788939624, 'fraction_numerical': 0.0025367833587011668, 'mean_word_length': 2.2266775777414076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 39, 'https://': 0, 'lorem ipsum': 0, 'www.': 7, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3923796', 'n_tokens_mistral': 1388, 'n_tokens_neox': 1371, 'n_words': 387}