summaryrefslogtreecommitdiffstats
path: root/nixos
diff options
context:
space:
mode:
authorrnhmjoj <rnhmjoj@inventati.org>2021-03-03 17:18:09 +0100
committerrnhmjoj <rnhmjoj@inventati.org>2021-03-09 11:03:00 +0100
commit2120f02960dfe854caeec5707951872effae92b3 (patch)
tree6f57c79d4c91b69d0f421d6926f75051cac3ca08 /nixos
parent8e21a1c51bbcdddd001c60cf9ea3975b2e317bbe (diff)
nixos/tests/privoxy: add test
Diffstat (limited to 'nixos')
-rw-r--r--nixos/tests/all-tests.nix1
-rw-r--r--nixos/tests/privoxy.nix113
2 files changed, 114 insertions, 0 deletions
diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix
index fe60b0b83f5a..00e84a9df82c 100644
--- a/nixos/tests/all-tests.nix
+++ b/nixos/tests/all-tests.nix
@@ -326,6 +326,7 @@ in
predictable-interface-names = handleTest ./predictable-interface-names.nix {};
printing = handleTest ./printing.nix {};
privacyidea = handleTest ./privacyidea.nix {};
+ privoxy = handleTest ./privoxy.nix {};
prometheus = handleTest ./prometheus.nix {};
prometheus-exporters = handleTest ./prometheus-exporters.nix {};
prosody = handleTest ./xmpp/prosody.nix {};
diff --git a/nixos/tests/privoxy.nix b/nixos/tests/privoxy.nix
new file mode 100644
index 000000000000..d16cc498691f
--- /dev/null
+++ b/nixos/tests/privoxy.nix
@@ -0,0 +1,113 @@
+import ./make-test-python.nix ({ lib, pkgs, ... }:
+
+let
+ # Note: For some reason Privoxy can't issue valid
+ # certificates if the CA is generated using gnutls :(
+ certs = pkgs.runCommand "example-certs"
+ { buildInputs = [ pkgs.openssl ]; }
+ ''
+ mkdir $out
+
+ # generate CA keypair
+ openssl req -new -nodes -x509 \
+ -extensions v3_ca -keyout $out/ca.key \
+ -out $out/ca.crt -days 365 \
+ -subj "/O=Privoxy CA/CN=Privoxy CA"
+
+ # generate server key/signing request
+ openssl genrsa -out $out/server.key 3072
+ openssl req -new -key $out/server.key \
+ -out server.csr -sha256 \
+ -subj "/O=An unhappy server./CN=example.com"
+
+ # sign the request/generate the certificate
+ openssl x509 -req -in server.csr -CA $out/ca.crt \
+ -CAkey $out/ca.key -CAcreateserial -out $out/server.crt \
+ -days 500 -sha256
+ '';
+in
+
+{
+ name = "privoxy";
+ meta = with lib.maintainers; {
+ maintainers = [ rnhmjoj ];
+ };
+
+ machine = { ... }: {
+ services.nginx.enable = true;
+ services.nginx.virtualHosts."example.com" = {
+ addSSL = true;
+ sslCertificate = "${certs}/server.crt";
+ sslCertificateKey = "${certs}/server.key";
+ locations."/".root = pkgs.writeTextFile
+ { name = "bad-day";
+ destination = "/how-are-you/index.html";
+ text = "I've had a bad day!\n";
+ };
+ locations."/ads".extraConfig = ''
+ return 200 "Hot Nixpkgs PRs in your area. Click here!\n";
+ '';
+ };
+
+ services.privoxy = {
+ enable = true;
+ inspectHttps = true;
+ settings = {
+ ca-cert-file = "${certs}/ca.crt";
+ ca-key-file = "${certs}/ca.key";
+ debug = 65536;
+ };
+ userActions = ''
+ {+filter{positive}}
+ example.com
+
+ {+block{Fake ads}}
+ example.com/ads
+ '';
+ userFilters = ''
+ FILTER: positive This is a filter example.
+ s/bad/great/ig
+ '';
+ };
+
+ security.pki.certificateFiles = [ "${certs}/ca.crt" ];
+
+ networking.hosts."::1" = [ "example.com" ];
+ networking.proxy.httpProxy = "http://localhost:8118";
+ networking.proxy.httpsProxy = "http://localhost:8118";
+ };
+
+ testScript =
+ ''
+ with subtest("Privoxy is running"):
+ machine.wait_for_unit("privoxy")
+ machine.wait_for_open_port("8118")
+ machine.succeed("curl -f http://config.privoxy.org")
+
+ with subtest("Privoxy can filter http requests"):
+ machine.wait_for_open_port("80")
+ assert "great day" in machine.succeed(
+ "curl -sfL http://example.com/how-are-you? | tee /dev/stderr"
+ )
+
+ with subtest("Privoxy can filter https requests"):
+ machine.wait_for_open_port("443")
+ assert "great day" in machine.succeed(
+ "curl -sfL https://example.com/how-are-you? | tee /dev/stderr"
+ )
+
+ with subtest("Blocks are working"):
+ machine.wait_for_open_port("443")
+ machine.fail("curl -f https://example.com/ads 1>&2")
+ machine.succeed("curl -f https://example.com/PRIVOXY-FORCE/ads 1>&2")
+
+ with subtest("Temporary certificates are cleaned"):
+ # Count current certificates
+ machine.succeed("test $(ls /run/privoxy/certs | wc -l) -gt 0")
+ # Forward in time 12 days, trigger the timer..
+ machine.succeed("date -s \"$(date --date '12 days')\"")
+ machine.systemctl("start systemd-tmpfiles-clean")
+ # ...and count again
+ machine.succeed("test $(ls /run/privoxy/certs | wc -l) -eq 0")
+ '';
+})