From 566108fc1a382b14355da34645346cfc6fa3d6fd Mon Sep 17 00:00:00 2001 From: Sebastian Reimers Date: Mon, 22 Apr 2024 09:16:47 +0200 Subject: [PATCH] extend sipauth tests --- test/sipauth.c | 102 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 77 insertions(+), 25 deletions(-) diff --git a/test/sipauth.c b/test/sipauth.c index 4c8d86b76..7ae8729a1 100644 --- a/test/sipauth.c +++ b/test/sipauth.c @@ -10,6 +10,57 @@ #include +static const char *testv[] = { + /* without algorithm - default MD5 */ + "SIP/2.0 401 Unauthorized\r\n" + "Via: SIP/2.0/TLS " + "10.0.0.1:37589;branch=z9hG4bK5625ce6f310a0fc8;rport=13718;" + "received=10.0.0.2\r\n" + "WWW-Authenticate: Digest realm=\"example.net\", " + "nonce=\"YZlVk2GZVGegVBZVKaMHpnxmUA+QyoSl\"\r\n" + "Content-Length: 0\r\n\r\n", + + /* explicit MD5 */ + "SIP/2.0 401 Unauthorized\r\n" + "Via: SIP/2.0/TLS " + "10.0.0.1:37589;branch=z9hG4bK5625ce6f310a0fc8;rport=13718;" + "received=10.0.0.2\r\n" + "WWW-Authenticate: Digest realm=\"example.net\", " + "algorithm=\"MD5\", " + "nonce=\"YZlVk2GZVGegVBZVKaMHpnxmUA+QyoSl\"\r\n" + "Content-Length: 0\r\n\r\n", + + /* explicit SHA-256 */ + "SIP/2.0 401 Unauthorized\r\n" + "Via: SIP/2.0/TLS " + "10.0.0.1:37589;branch=z9hG4bK5625ce6f310a0fc8;rport=13718;" + "received=10.0.0.2\r\n" + "WWW-Authenticate: Digest realm=\"example.net\", " + "algorithm=\"SHA-256\", " + "nonce=\"YZlVk2GZVGegVBZVKaMHpnxmUA+QyoSl\"\r\n" + "Content-Length: 0\r\n\r\n", + + /* explicit SHA-256 qop */ + "SIP/2.0 401 Unauthorized\r\n" + "Via: SIP/2.0/TLS " + "10.0.0.1:37589;branch=z9hG4bK5625ce6f310a0fc8;rport=13718;" + "received=10.0.0.2\r\n" + "WWW-Authenticate: Digest realm=\"example.net\", " + "algorithm=\"SHA-256\", " + "qop=\"auth\", " + "nonce=\"YZlVk2GZVGegVBZVKaMHpnxmUA+QyoS\"\r\n" + "Content-Length: 0\r\n\r\n" +}; + + +static const char *testr[] = { + "algorithm=MD5", + "algorithm=MD5", + "algorithm=SHA-256", + "algorithm=SHA-256" +}; + + static int auth_handler(char **user, char **pass, const char *rlm, void *arg) { (void)user; @@ -30,14 +81,6 @@ static int test_sip_auth_encode(void) struct sip_msg *msg = NULL; const char met[] = "REGISTER"; const char uri[] = ""; - const char str_raw[] = - "SIP/2.0 401 Unauthorized\r\n" - "Via: SIP/2.0/TLS " - "10.0.0.1:37589;branch=z9hG4bK5625ce6f310a0fc8;rport=13718;" - "received=10.0.0.2\r\n" - "WWW-Authenticate: Digest realm=\"example.net\", " - "nonce=\"YZlVk2GZVGegVBZVKaMHpnxmUA+QyoSl\"\r\n" - "Content-Length: 0\r\n\r\n"; mb = mbuf_alloc(2048); if (!mb) @@ -49,36 +92,45 @@ static int test_sip_auth_encode(void) return ENOMEM; } - err = sip_auth_alloc(&auth, auth_handler, NULL, false); - TEST_ERR(err); + for (size_t i = 0; i < RE_ARRAY_SIZE(testv); i++) { + mbuf_rewind(mb); + mbuf_rewind(mb_enc); - err = mbuf_write_str(mb, str_raw); - TEST_ERR(err); + err = sip_auth_alloc(&auth, auth_handler, NULL, false); + TEST_ERR(err); - mbuf_set_pos(mb, 0); + err = mbuf_write_str(mb, testv[i]); + TEST_ERR(err); - err = sip_msg_decode(&msg, mb); - TEST_ERR(err); + mbuf_set_pos(mb, 0); - err = sip_auth_authenticate(auth, msg); - TEST_ERR(err); + err = sip_msg_decode(&msg, mb); + TEST_ERR(err); - err = sip_auth_encode(mb_enc, auth, met, uri); - TEST_ERR(err); + err = sip_auth_authenticate(auth, msg); + TEST_ERR(err); - mbuf_set_pos(mb_enc, 0); - mbuf_read_str(mb_enc, buf, mbuf_get_left(mb_enc)); + err = sip_auth_encode(mb_enc, auth, met, uri); + TEST_ERR(err); - err = re_regex(buf, str_len(buf), "algorithm=MD5"); - TEST_ERR(err); + mbuf_set_pos(mb_enc, 0); + mbuf_read_str(mb_enc, buf, mbuf_get_left(mb_enc)); + + err = re_regex(buf, str_len(buf), testr[i]); + TEST_ERR(err); + + mem_deref(msg); + mem_deref(auth); + } out: mem_deref(mb); mem_deref(mb_enc); - if (msg) + if (err) { mem_deref(msg); - if (auth) mem_deref(auth); + } + return err; }