Skip to content

Commit 8f18304

Browse files
committed
Adds proxy argument handling unit tests
1 parent 2ee4081 commit 8f18304

File tree

4 files changed

+159
-1
lines changed

4 files changed

+159
-1
lines changed

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ pub mod ffi {
149149
pub fn publishitem_test(out_ex: *mut TestException) -> libc::c_int;
150150
pub fn handlerengine_test(out_ex: *mut TestException) -> libc::c_int;
151151
pub fn handlerargs_test(out_ex: *mut TestException) -> libc::c_int;
152+
pub fn proxyargs_test(out_ex: *mut TestException) -> libc::c_int;
152153
pub fn template_test(out_ex: *mut TestException) -> libc::c_int;
153154
}
154155
}

src/proxy/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ mod tests {
3434
unsafe { ffi::proxyengine_test(out_ex) == 0 }
3535
}
3636

37+
fn proxyargs_test(out_ex: &mut TestException) -> bool {
38+
// SAFETY: safe to call
39+
unsafe { ffi::proxyargs_test(out_ex) == 0 }
40+
}
41+
3742
#[test]
3843
fn websocketoverhttp() {
3944
run_serial(websocketoverhttp_test);
@@ -48,4 +53,9 @@ mod tests {
4853
fn proxyengine() {
4954
run_serial(proxyengine_test);
5055
}
56+
57+
#[test]
58+
fn proxyargs() {
59+
run_serial(proxyargs_test);
60+
}
5161
}

src/proxy/proxyargstest.cpp

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
/*
2+
* Copyright (C) 2025 Fastly, Inc.
3+
*
4+
* This file is part of Pushpin.
5+
*
6+
* $FANOUT_BEGIN_LICENSE:APACHE2$
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*
20+
* $FANOUT_END_LICENSE$
21+
*/
22+
23+
24+
#include "config.h"
25+
#include "app.h"
26+
#include "settings.h"
27+
#include "test.h"
28+
#include "argsdata.h"
29+
30+
void proxyargstest()
31+
{
32+
// Get file for example config
33+
std::string configFile = "examples/config/pushpin.conf";
34+
35+
// Set up valid command line arguments
36+
int argc = 7;
37+
char * argv[] = {
38+
(char *) configFile.c_str(), // configFile
39+
(char *) "log.txt", // logFile
40+
(char *) "2", // logLevel
41+
(char *) "ipc:prefix", // ipcPrefix
42+
(char *) "80", // portOffset
43+
(char *) "route1,route2", // routeLines
44+
(char *) "true", // quietCheck
45+
};
46+
47+
// Set up QCoreApplication
48+
QCoreApplication qapp(argc, argv);
49+
App app;
50+
QStringList extArgs = qapp.arguments();
51+
52+
// Verify the arguments
53+
TEST_ASSERT_EQ(extArgs[0], QString("examples/config/pushpin.conf"));
54+
TEST_ASSERT_EQ(extArgs[1], QString("log.txt"));
55+
TEST_ASSERT_EQ(extArgs[2], QString("2"));
56+
TEST_ASSERT_EQ(extArgs[3], QString("ipc:prefix"));
57+
TEST_ASSERT_EQ(extArgs[4], QString("80"));
58+
TEST_ASSERT_EQ(extArgs[5], QString("route1,route2"));
59+
TEST_ASSERT_EQ(extArgs[6], QString("true"));
60+
61+
// Verify ArgsData parsing
62+
ArgsData args(extArgs);
63+
TEST_ASSERT_EQ(args.configFile, QString("examples/config/pushpin.conf"));
64+
TEST_ASSERT_EQ(args.logFile, QString("log.txt"));
65+
TEST_ASSERT_EQ(args.logLevel, 2);
66+
TEST_ASSERT_EQ(args.ipcPrefix, QString("ipc:prefix"));
67+
TEST_ASSERT_EQ(args.portOffset, 80);
68+
TEST_ASSERT_EQ(args.routeLines, QStringList({"route1", "route2"}));
69+
TEST_ASSERT_EQ(args.quietCheck, true);
70+
71+
// Set up mock settings
72+
Settings mock_settings(args.configFile);
73+
if (!args.ipcPrefix.isEmpty())
74+
mock_settings.setIpcPrefix(args.ipcPrefix);;
75+
if (args.portOffset != -1)
76+
mock_settings.setPortOffset(args.portOffset);
77+
78+
// Load settings from command line arguments
79+
Settings settings = args.loadIntoSettings();
80+
81+
// Verify mock settings match the loaded settings
82+
TEST_ASSERT_EQ(mock_settings, settings);
83+
84+
// Change the port offset to a different value
85+
mock_settings.setPortOffset(60);
86+
87+
// Verify that the mock settings no longer match the loaded settings
88+
TEST_ASSERT(!(mock_settings == settings));
89+
90+
// Set up valid empty command line arguments
91+
int argc_empty = 7;
92+
char * argv_empty[] = {
93+
(char *) configFile.c_str(), // configFile
94+
(char *) "", // logFile
95+
(char *) "2", // logLevel
96+
(char *) "", // ipcPrefix
97+
(char *) "", // portOffset
98+
(char *) "", // routeLines
99+
(char *) "false", // quietCheck
100+
};
101+
102+
// Set up QCoreApplication with empty arguments
103+
QCoreApplication qapp_empty(argc_empty, argv_empty);
104+
App app_empty;
105+
QStringList extArgs_empty = qapp_empty.arguments();
106+
107+
// Verify the arguments
108+
TEST_ASSERT_EQ(extArgs_empty[0], QString("examples/config/pushpin.conf"));
109+
TEST_ASSERT_EQ(extArgs_empty[1], QString(""));
110+
TEST_ASSERT_EQ(extArgs_empty[2], QString("2"));
111+
TEST_ASSERT_EQ(extArgs_empty[3], QString(""));
112+
TEST_ASSERT_EQ(extArgs_empty[4], QString(""));
113+
TEST_ASSERT_EQ(extArgs_empty[5], QString(""));
114+
TEST_ASSERT_EQ(extArgs_empty[6], QString("false"));
115+
116+
// Verify ArgsData parsing with empty arguments
117+
ArgsData args_empty(extArgs_empty);
118+
TEST_ASSERT_EQ(args_empty.configFile, QString("examples/config/pushpin.conf"));
119+
TEST_ASSERT_EQ(args_empty.logFile, QString(""));
120+
TEST_ASSERT_EQ(args_empty.logLevel, 2);
121+
TEST_ASSERT_EQ(args_empty.ipcPrefix, QString(""));
122+
TEST_ASSERT_EQ(args_empty.portOffset, -1);
123+
TEST_ASSERT_EQ(args_empty.routeLines, QStringList());
124+
TEST_ASSERT_EQ(args_empty.quietCheck, false);
125+
126+
// Load settings from empty command line arguments
127+
Settings settings_empty = args_empty.loadIntoSettings();
128+
129+
// Set up mock settings
130+
Settings mock_settings_empty(args_empty.configFile);
131+
if (!args_empty.ipcPrefix.isEmpty())
132+
mock_settings_empty.setIpcPrefix(args_empty.ipcPrefix);
133+
if (args_empty.portOffset != -1)
134+
mock_settings_empty.setPortOffset(args_empty.portOffset);
135+
136+
// Verify mock settings match the loaded settings
137+
TEST_ASSERT_EQ(mock_settings_empty, settings_empty);
138+
}
139+
140+
extern "C" int proxyargs_test(ffi::TestException *out_ex)
141+
{
142+
TEST_CATCH(proxyargstest());
143+
144+
return 0;
145+
}

src/proxy/tests.pri

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
SOURCES += \
22
$$PWD/websocketoverhttptest.cpp \
33
$$PWD/routesfiletest.cpp \
4-
$$PWD/proxyenginetest.cpp
4+
$$PWD/proxyenginetest.cpp \
5+
$$PWD/proxyargstest.cpp
6+

0 commit comments

Comments
 (0)