Skip to content

Force Proxy to any Windows/MacOS/Linux Application TCP and UDP traffic

test(linux): update-check and log scenarios in the GUI test, non-root start, config test for log-every; UI test build hooks

anof-cybere3b1f7031862
diff --git a/Linux/gui/main.c b/Linux/gui/main.c
index 9ad0957..e7ac65a 100644
--- a/Linux/gui/main.c
+++ b/Linux/gui/main.c
@@ -123,11 +123,19 @@ static int https_url_ok(const char *url) {
return 1;
}
+static const char *update_feed_url() {
+#ifdef PB_TEST_HOOKS
+ const char *override = getenv("PB_UPDATE_FEED"); // only in the UI test build: the shipped program always uses the real feed
+ if (override && *override) return override;
+#endif
+ return UPD_FEED_URL;
+}
+
static void on_check_update(GtkWidget *widget, gpointer data) {
char *argv[] = {
(char *)"curl", (char *)"-sS",
(char *)"-H", (char *)"User-Agent: ProxyBridge-UpdateChecker",
- (char *)UPD_FEED_URL,
+ (char *)update_feed_url(),
NULL
};
char *json = NULL;
@@ -369,20 +377,26 @@ static GtkTextBuffer *add_text_tab(GtkWidget *notebook, const char *title, const
gtk_container_set_border_width(GTK_CONTAINER(tab), 5);
GtkWidget *toolbar = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 5);
GtkWidget *search = gtk_search_entry_new();
+ const char *prefix = strcmp(title, "Connections") == 0 ? "conn" : "log"; // widget names for the UI test
+ char wname[32];
+ snprintf(wname, sizeof wname, "%s_search", prefix); gtk_widget_set_name(search, wname);
gtk_entry_set_placeholder_text(GTK_ENTRY(search), placeholder);
g_signal_connect(search, "search-changed", on_search, NULL);
GtkWidget *clear_btn = gtk_button_new_with_label("Clear Logs");
+ snprintf(wname, sizeof wname, "%s_clear", prefix); gtk_widget_set_name(clear_btn, wname);
g_signal_connect(clear_btn, "clicked", on_clear, NULL);
gtk_box_pack_start(GTK_BOX(toolbar), search, TRUE, TRUE, 0);
gtk_box_pack_start(GTK_BOX(toolbar), clear_btn, FALSE, FALSE, 0);
if (on_pause) {
GtkWidget *pause_btn = gtk_toggle_button_new_with_label("Pause");
+ snprintf(wname, sizeof wname, "%s_pause", prefix); gtk_widget_set_name(pause_btn, wname);
g_signal_connect(pause_btn, "toggled", on_pause, NULL);
gtk_box_pack_start(GTK_BOX(toolbar), pause_btn, FALSE, FALSE, 0);
}
gtk_box_pack_start(GTK_BOX(tab), toolbar, FALSE, FALSE, 0);
GtkWidget *view = gtk_text_view_new();
+ snprintf(wname, sizeof wname, "%s_view", prefix); gtk_widget_set_name(view, wname);
gtk_text_view_set_editable(GTK_TEXT_VIEW(view), FALSE);
gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(view), FALSE);
GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(view));
diff --git a/Linux/gui/tests/config_test.c b/Linux/gui/tests/config_test.c
index 1973700..d27a4ee 100644
--- a/Linux/gui/tests/config_test.c
+++ b/Linux/gui/tests/config_test.c
@@ -10,6 +10,7 @@ char *g_default_proxy = NULL;
GList *g_rules_list = NULL;
bool g_chk_logging = true;
bool g_chk_dns = true;
+bool g_chk_log_every = false;
void free_proxy_data(ProxyData *p) {
g_free(p->name); g_free(p->host); g_free(p->username); g_free(p->password); g_free(p);
@@ -195,6 +196,7 @@ int main() {
CHECK(g_chk_logging && g_chk_dns); // a new profile starts with both switches on
g_chk_dns = false;
g_chk_logging = false;
+ g_chk_log_every = true;
save_config();
check_storage(dir);
@@ -207,14 +209,14 @@ int main() {
CHECK(strcmp(((RuleData *)g_rules_list->data)->target_hosts, "*.github.com") == 0);
CHECK(!((ProxyData *)g_proxies_list->data)->send_domain); // per-proxy "DNS via proxy" survives a restart
CHECK(strcmp(((RuleData *)g_rules_list->data)->name, "Burp \"intercept\"") == 0 && ((RuleData *)g_rules_list->data)->localhost);
- CHECK(!g_chk_dns && !g_chk_logging); // restored from the Work profile file itself
+ CHECK(!g_chk_dns && !g_chk_logging && g_chk_log_every); // restored from the Work profile file itself
CHECK(profile_find("Home") && !profile_find("Home")->logging && profile_find("Home")->dns); // migrated values of the old file, per profile
CHECK(profile_find("Home") && g_list_length(profile_find("Home")->rules) == 1);
CHECK(profile_activate("Home")); // switching profiles switches the values too
- CHECK(!g_chk_logging && g_chk_dns);
+ CHECK(!g_chk_logging && g_chk_dns && !g_chk_log_every);
CHECK(profile_activate("Work"));
- CHECK(!g_chk_logging && !g_chk_dns);
+ CHECK(!g_chk_logging && !g_chk_dns && g_chk_log_every);
CHECK(profile_activate("Home"));
CHECK(profile_activate("Work"));
CHECK(profile_delete(profile_find("Work")));
diff --git a/Linux/gui/tests/ui_test.c b/Linux/gui/tests/ui_test.c
index ee98085..cbef674 100644
--- a/Linux/gui/tests/ui_test.c
+++ b/Linux/gui/tests/ui_test.c
@@ -659,6 +659,155 @@ static void profiles_body(GtkWidget *dlg, gpointer arg) {
gtk_dialog_response(GTK_DIALOG(dlg), GTK_RESPONSE_CLOSE);
}
+
+// ================================================================================================================================
+// Update check (the feed is a local file; only the UI test build honours PB_UPDATE_FEED)
+// ================================================================================================================================
+static char *first_msg;
+static void msg_then_ok(GtkWidget *d, gpointer arg) {
+ remember_msg(d, arg);
+ g_free(first_msg);
+ first_msg = g_strdup(last_msg);
+ arm_msg(GTK_RESPONSE_OK);
+}
+
+static void feed_case(const char *name, const char *content, int first_response, const char *want, bool second_ok) {
+ char *path = tmp_path(name);
+ char *url = g_strdup_printf("file://%s", path);
+ if (content) g_file_set_contents(path, content, -1, NULL);
+ setenv("PB_UPDATE_FEED", url, 1);
+ g_free(last_msg); last_msg = NULL;
+ if (second_ok) arm("msg", msg_then_ok, NULL, first_response);
+ else arm_msg(first_response);
+ activate("Check for Updates");
+ CHECK_HAS(second_ok ? first_msg : last_msg, want);
+ g_free(path); g_free(url);
+}
+
+static void updates_scenario(void) {
+ STEP("update check");
+ feed_case("feed_missing.json", NULL, GTK_RESPONSE_OK, "Update check failed", false);
+ feed_case("feed_empty.json", "", GTK_RESPONSE_OK, "Update check failed", false);
+ feed_case("feed_nolinux.json", "{\"windows\": {\"version\": \"9.0.0\"}}", GTK_RESPONSE_OK, "Could not parse linux update info", false);
+ feed_case("feed_noversion.json", "{\"linux\": {\"download\": \"https://example.test/d\"}}", GTK_RESPONSE_OK, "Could not parse version info", false);
+ feed_case("feed_same.json", "{\"linux\": {\"version\": \"" PROXYBRIDGE_VERSION "\"}}", GTK_RESPONSE_OK, "latest version", false);
+ feed_case("feed_beta_suffix.json", "{\"linux\": {\"version\": \"4.0.0\"}}", GTK_RESPONSE_OK, "latest version", false);
+ feed_case("feed_older.json", "{\"linux\": {\"version\": \"3.2.0\"}}", GTK_RESPONSE_OK, "latest version", false);
+ feed_case("feed_newer.json", "{\"linux\": {\"version\": \"9.9.9\", \"download\": \"https://example.test/d\", \"release_notes\": \"https://example.test/n\"}}",
+ GTK_RESPONSE_CANCEL, "Version 9.9.9 is available", false);
+ feed_case("feed_patch.json", "{\"linux\": {\"version\": \"4.0.1\", \"download\": \"https://example.test/d\"}}", GTK_RESPONSE_CANCEL, "Version 4.0.1 is available", false);
+ // a feed must never make the program open anything but an https page
+ feed_case("feed_unsafe.json", "{\"linux\": {\"version\": \"9.9.9\", \"download\": \"http://insecure.test/x\", \"release_notes\": \"javascript:alert(1)\"}}",
+ GTK_RESPONSE_ACCEPT, "Version 9.9.9 is available", true);
+ CHECK_HAS(last_msg, "No safe https url");
+ unsetenv("PB_UPDATE_FEED");
+}
+
+// ================================================================================================================================
+// Connection and activity logs
+// ================================================================================================================================
+static char *buffer_of(const char *view_name) {
+ GtkWidget *v = find_named(view_name);
+ if (!v) return g_strdup("");
+ GtkTextBuffer *b = gtk_text_view_get_buffer(GTK_TEXT_VIEW(v));
+ GtkTextIter s, e;
+ gtk_text_buffer_get_bounds(b, &s, &e);
+ return gtk_text_buffer_get_text(b, &s, &e, FALSE);
+}
+
+static int visible_lines_with(const char *view_name, const char *needle) {
+ GtkWidget *v = find_named(view_name);
+ if (!v) return -1;
+ GtkTextBuffer *b = gtk_text_view_get_buffer(GTK_TEXT_VIEW(v));
+ GtkTextTag *hidden = gtk_text_tag_table_lookup(gtk_text_buffer_get_tag_table(b), "hidden");
+ int n = 0;
+ for (int line = 0; line < gtk_text_buffer_get_line_count(b); line++) {
+ GtkTextIter s, e;
+ gtk_text_buffer_get_iter_at_line(b, &s, line);
+ e = s;
+ if (!gtk_text_iter_ends_line(&e)) gtk_text_iter_forward_to_line_end(&e);
+ char *text = gtk_text_buffer_get_text(b, &s, &e, FALSE);
+ if (strstr(text, needle) && !gtk_text_iter_has_tag(&s, hidden)) n++;
+ g_free(text);
+ }
+ return n;
+}
+
+static void connect_once(const char *ip) {
+ char *script = tmp_path("conn.py");
+ g_file_set_contents(script, "import socket, sys\ns = socket.socket(); s.settimeout(3)\n"
+ "try:\n s.connect((sys.argv[1], 80)); s.sendall(b'GET / HTTP/1.0\\r\\n\\r\\n'); s.recv(100)\nexcept OSError:\n pass\n", -1, NULL);
+ char *argv[] = { (char *)"python3", script, (char *)ip, NULL };
+ g_spawn_sync(NULL, argv, NULL, G_SPAWN_SEARCH_PATH | G_SPAWN_STDOUT_TO_DEV_NULL | G_SPAWN_STDERR_TO_DEV_NULL, NULL, NULL, NULL, NULL, NULL, NULL);
+ g_free(script);
+}
+
+static void logs_scenario(void) {
+ STEP("connection and activity logs");
+ GtkWidget *log_item = menu_item("Enable Traffic Logging");
+ if (log_item) gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(log_item), TRUE); // the profile scenario left it off for Default
+ ProxyData *p = proxy_named("p1x");
+ CHECK(p != NULL);
+ char err[200] = "";
+ uint32_t rule = p ? ProxyBridge_AddRule("python3*", "192.0.2.40-192.0.2.49", "80", RULE_PROTOCOL_TCP, RULE_ACTION_PROXY, p->id, err, sizeof err) : 0;
+ CHECK(rule != 0);
+
+ connect_once("192.0.2.40");
+ connect_once("192.0.2.41");
+ pump(900);
+ char *conns = buffer_of("conn_view");
+ CHECK_HAS(conns, "192.0.2.40:80");
+ CHECK_HAS(conns, "192.0.2.41:80");
+ CHECK_HAS(conns, "via SOCKS5 127.0.0.1:19080");
+ g_free(conns);
+ char *logs = buffer_of("log_view");
+ CHECK_HAS(logs, "ProxyBridge started");
+ g_free(logs);
+
+ // search hides the lines that do not match, and shows them again
+ set_text(find_named("conn_search"), "conn_search", "192.0.2.41");
+ pump(500);
+ CHECK(visible_lines_with("conn_view", "192.0.2.41") == 1 && visible_lines_with("conn_view", "192.0.2.40") == 0);
+ set_text(find_named("conn_search"), "conn_search", "");
+ pump(500);
+ CHECK(visible_lines_with("conn_view", "192.0.2.40") == 1 && visible_lines_with("conn_view", "192.0.2.41") == 1);
+ set_text(find_named("log_search"), "log_search", "started");
+ pump(500);
+ CHECK(visible_lines_with("log_view", "ProxyBridge started") >= 1);
+ set_text(find_named("log_search"), "log_search", "");
+ pump(300);
+
+ // pause keeps new lines back until it is released
+ GtkWidget *pause = find_named("conn_pause");
+ CHECK(pause != NULL);
+ if (pause) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pause), TRUE);
+ connect_once("192.0.2.42");
+ pump(900);
+ conns = buffer_of("conn_view");
+ CHECK(strstr(conns, "192.0.2.42") == NULL);
+ g_free(conns);
+ if (pause) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(pause), FALSE);
+ pump(600);
+ conns = buffer_of("conn_view");
+ CHECK_HAS(conns, "192.0.2.42:80");
+ g_free(conns);
+
+ // the live connections tab is a view of the engine too
+ gui_refresh_flows();
+ GtkWidget *flows = find_named("flows_tree");
+ CHECK(flows != NULL);
+
+ click("conn_clear");
+ conns = buffer_of("conn_view");
+ CHECK(strlen(conns) == 0);
+ g_free(conns);
+ click("log_clear");
+ logs = buffer_of("log_view");
+ CHECK(strlen(logs) == 0);
+ g_free(logs);
+ ProxyBridge_DeleteRule(rule);
+}
+
// ================================================================================================================================
static void ui_main(void) {
pump(300);
@@ -682,6 +831,12 @@ static void ui_main(void) {
arm("dlg_profiles", profiles_body, NULL, 0);
activate("Profiles...");
+ printf("== update check\n");
+ updates_scenario();
+
+ printf("== logs\n");
+ logs_scenario();
+
printf("== menus and engine\n");
GtkWidget *start = menu_item("Start Engine"), *stop = menu_item("Stop Engine");
CHECK(start && stop);
diff --git a/Linux/tests/coverage.sh b/Linux/tests/coverage.sh
index 2b819c2..f5984db 100755
--- a/Linux/tests/coverage.sh
+++ b/Linux/tests/coverage.sh
@@ -26,7 +26,7 @@ unit_tests() {
gcc $cf tests/rules_test.c pb_rules.c -o "$UNIT/rules_test" && "$UNIT/rules_test" &&
gcc $cf tests/dns_test.c pb_dns.c -o "$UNIT/dns_test" && "$UNIT/dns_test" &&
gcc $cf tests/proc_test.c pb_proc.c -o "$UNIT/proc_test" && "$UNIT/proc_test" &&
- gcc $cf -I. tests/relay_test.c pb_relay.c -o "$UNIT/relay_test" -lpthread && "$UNIT/relay_test" ) 2>&1 | grep -E "FAIL|passed|RESULT" || true
+ gcc $cf -I. tests/relay_test.c pb_relay.c -o "$UNIT/relay_test" -lpthread && PB_CHURN_TCP=2000 PB_CHURN_UDP=500 PB_PERF_UDP=2000 PB_PERF_CONNS=100 "$UNIT/relay_test" ) 2>&1 | grep -E "FAIL|passed|RESULT" || true
( cd "$ROOT/profile/tests" && gcc $cf profile_test.c ../pb_profile.c -o "$UNIT/profile_test" && "$UNIT/profile_test" ) 2>&1 | tail -1
if [ -n "$gtk" ]; then
( cd "$ROOT/gui" &&
diff --git a/Linux/tests/e2e_lifecycle.sh b/Linux/tests/e2e_lifecycle.sh
index 453ee75..3cb5050 100755
--- a/Linux/tests/e2e_lifecycle.sh
+++ b/Linux/tests/e2e_lifecycle.sh
@@ -96,5 +96,34 @@ start "$WORK/pb.out"; kill -INT $PB; wait $PB 2>/dev/null; PB=""
sleep 5
grep -q "still usable" "$WORK/long.out" && ok "an existing connection survives ProxyBridge starting and stopping" || bad "existing connection: $(cat "$WORK/long.out")"
+echo "== starting when it cannot work"
+cat > "$WORK/probe.c" <<'C'
+#include <stdio.h>
+#include <string.h>
+#include "ProxyBridge.h"
+static void log_cb(const char *m) { printf("log: %s\n", m); }
+int main(int argc, char **argv)
+{
+ char err[200];
+ ProxyBridge_SetLogCallback(log_cb);
+ if (argc > 1 && !strcmp(argv[1], "withproxy")) ProxyBridge_AddProxy("a", PROXY_TYPE_SOCKS5, "127.0.0.1", 19080, "", "", err, sizeof err);
+ ProxyBridge_AddRule("curl", "*", "*", RULE_PROTOCOL_TCP, RULE_ACTION_PROXY, 0, err, sizeof err);
+ printf("start=%d\n", ProxyBridge_Start());
+ printf("stop=%d\n", ProxyBridge_Stop());
+ return 0;
+}
+C
+if gcc -O1 -I"$ROOT/src" -o "$WORK/probe" "$WORK/probe.c" -L"$PB_DIR" -lproxybridge; then
+ chmod 755 "$WORK" "$WORK/probe"
+ out=$("$WORK/probe" 2>&1)
+ [[ "$out" == *"start=0"* && "$out" == *"no proxy is configured"* ]] && ok "a PROXY rule without any proxy refuses to start and says why" || bad "no proxy: $out"
+ out=$(setpriv --reuid=65534 --regid=65534 --clear-groups env LD_LIBRARY_PATH="$PB_DIR" "$WORK/probe" withproxy 2>&1)
+ [[ "$out" == *"start=0"* && "$out" == *"needs root"* ]] && ok "without root it fails cleanly and explains" || bad "not root: $out"
+ [[ "$out" == *"stop=1"* ]] && ok "Stop after a failed Start is harmless" || bad "stop after failed start: $out"
+ [ "$(progs)" = 0 ] && ok "a failed start attaches nothing" || bad "programs attached by a failed start"
+else
+ bad "could not build the probe"
+fi
+
echo; echo "passed=$PASS failed=$FAIL"
[ "$FAIL" = 0 ]
diff --git a/Linux/tests/gui_ui.sh b/Linux/tests/gui_ui.sh
index 1746656..6f8eeb3 100755
--- a/Linux/tests/gui_ui.sh
+++ b/Linux/tests/gui_ui.sh
@@ -16,7 +16,7 @@ trap cleanup EXIT
G="$ROOT/gui"
SRC=$(ls "$G"/*.c | grep -v "/main.c$")
gcc -O1 -g -w -D_GNU_SOURCE ${EXTRA_CFLAGS:-} -I"$ROOT/src" -I"$ROOT/profile" -I"$G" $(pkg-config --cflags gtk+-3.0) \
- -Dmain=pb_gui_main -c "$G/main.c" -o "$BUILD/gui_main.o" || exit 1
+ -DPB_TEST_HOOKS -Dmain=pb_gui_main -c "$G/main.c" -o "$BUILD/gui_main.o" || exit 1
gcc -O1 -g -w -D_GNU_SOURCE ${EXTRA_CFLAGS:-} -I"$ROOT/src" -I"$ROOT/profile" -I"$G" $(pkg-config --cflags gtk+-3.0) \
-o "$BUILD/ui_test" "$G/tests/ui_test.c" "$BUILD/gui_main.o" $SRC "$ROOT/profile/pb_profile.c" \
-L"$LIBDIR" -Wl,-rpath,"$LIBDIR" -lproxybridge -lpthread $(pkg-config --libs gtk+-3.0) ${EXTRA_LDFLAGS:-} || exit 1