summaryrefslogtreecommitdiff
path: root/pjlib-util/src/pjlib-util-test
diff options
context:
space:
mode:
authorBenny Prijono <bennylp@teluu.com>2007-02-26 22:31:06 +0000
committerBenny Prijono <bennylp@teluu.com>2007-02-26 22:31:06 +0000
commit79768e50b4c2e0d1c4fb871dc99a696fc5d8d2dd (patch)
tree290fc8ef0b0c5c0870278f9aad2cac38db649299 /pjlib-util/src/pjlib-util-test
parentbf839cd77520ad28437e55a73fd8167838023f42 (diff)
Finishing up STUN server side authentication
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@1003 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjlib-util/src/pjlib-util-test')
-rw-r--r--pjlib-util/src/pjlib-util-test/encryption.c135
-rw-r--r--pjlib-util/src/pjlib-util-test/stun.c118
-rw-r--r--pjlib-util/src/pjlib-util-test/test.c4
-rw-r--r--pjlib-util/src/pjlib-util-test/test.h3
4 files changed, 260 insertions, 0 deletions
diff --git a/pjlib-util/src/pjlib-util-test/encryption.c b/pjlib-util/src/pjlib-util-test/encryption.c
index 34e5ae59..47dc17cd 100644
--- a/pjlib-util/src/pjlib-util-test/encryption.c
+++ b/pjlib-util/src/pjlib-util-test/encryption.c
@@ -422,6 +422,7 @@ static int crc32_test(void)
PJ_LOG(3, (THIS_FILE, " crc32 test.."));
+ /* testing pj_crc32_calc */
for (i=0; i<PJ_ARRAY_SIZE(crc32_test_data); ++i) {
pj_uint32_t crc;
@@ -432,6 +433,34 @@ static int crc32_test(void)
return -80;
}
}
+
+ /* testing incremental CRC32 calculation */
+ for (i=0; i<PJ_ARRAY_SIZE(crc32_test_data); ++i) {
+ pj_crc32_context ctx;
+ pj_uint32_t crc0, crc1;
+ unsigned len;
+
+ len = pj_ansi_strlen(crc32_test_data[i].input);
+ crc0 = pj_crc32_calc((pj_uint8_t*)crc32_test_data[i].input, len);
+
+ pj_crc32_init(&ctx);
+ pj_crc32_update(&ctx, (pj_uint8_t*)crc32_test_data[i].input,
+ len / 2);
+
+ if (len/2 > 0) {
+ pj_crc32_update(&ctx, (pj_uint8_t*)crc32_test_data[i].input + len/2,
+ len - len/2);
+ }
+
+ crc1 = pj_crc32_final(&ctx);
+
+ if (crc0 != crc1) {
+ PJ_LOG(3,(THIS_FILE,
+ " error: crc algorithm error on test %d", i));
+ return -85;
+ }
+
+ }
return 0;
}
@@ -459,6 +488,112 @@ int encryption_test()
return 0;
}
+static void crc32_update(pj_crc32_context *c, const pj_uint8_t *data,
+ pj_size_t nbytes)
+{
+ pj_crc32_update(c, data, nbytes);
+}
+
+static void crc32_final(pj_crc32_context *ctx, pj_uint32_t *digest)
+{
+ *digest = pj_crc32_final(ctx);
+}
+
+int encryption_benchmark()
+{
+ pj_pool_t *pool;
+ pj_uint8_t *input;
+ union {
+ pj_md5_context md5_context;
+ pj_sha1_context sha1_context;
+ } context;
+ pj_uint8_t digest[32];
+ pj_size_t input_len;
+ struct algorithm
+ {
+ const char *name;
+ void (*init_context)(void*);
+ void (*update)(void*, const pj_uint8_t*, unsigned);
+ void (*final)(void*, void*);
+ pj_uint32_t t;
+ } algorithms[] =
+ {
+ {
+ "MD5 ",
+ &pj_md5_init,
+ &pj_md5_update,
+ &pj_md5_final
+ },
+ {
+ "SHA1 ",
+ &pj_sha1_init,
+ &pj_sha1_update,
+ &pj_sha1_final
+ },
+ {
+ "CRC32",
+ &pj_crc32_init,
+ &crc32_update,
+ &crc32_final
+ }
+ };
+#if defined(PJ_DEBUG) && PJ_DEBUG!=0
+ enum { LOOP = 1000 };
+#else
+ enum { LOOP = 10000 };
+#endif
+ unsigned i;
+ double total_len;
+
+ input_len = 2048;
+ total_len = input_len * LOOP;
+ pool = pj_pool_create(mem, "enc", input_len+256, 0, NULL);
+ if (!pool)
+ return PJ_ENOMEM;
+
+ input = pj_pool_alloc(pool, input_len);
+ pj_memset(input, '\xaa', input_len);
+
+ PJ_LOG(3, (THIS_FILE, " feeding %d Mbytes of data",
+ (unsigned)(total_len/1024/1024)));
+
+ /* Dry run */
+ for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) {
+ algorithms[i].init_context(&context);
+ algorithms[i].update(&context, input, input_len);
+ algorithms[i].final(&context, digest);
+ }
+
+ /* Run */
+ for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) {
+ int j;
+ pj_timestamp t1, t2;
+
+ pj_get_timestamp(&t1);
+ algorithms[i].init_context(&context);
+ for (j=0; j<LOOP; ++j) {
+ algorithms[i].update(&context, input, input_len);
+ }
+ algorithms[i].final(&context, digest);
+ pj_get_timestamp(&t2);
+
+ algorithms[i].t = pj_elapsed_usec(&t1, &t2);
+ }
+
+ /* Results */
+ for (i=0; i<PJ_ARRAY_SIZE(algorithms); ++i) {
+ double bytes;
+
+ bytes = (total_len * 1000000 / algorithms[i].t);
+ PJ_LOG(3, (THIS_FILE, " %s:%8d usec (%3d.%03d Mbytes/sec)",
+ algorithms[i].name, algorithms[i].t,
+ (unsigned)(bytes / 1024 / 1024),
+ ((unsigned)(bytes) % (1024 * 1024)) / 1024));
+ }
+
+ return 0;
+}
+
#endif /* INCLUDE_ENCRYPTION_TEST */
diff --git a/pjlib-util/src/pjlib-util-test/stun.c b/pjlib-util/src/pjlib-util-test/stun.c
new file mode 100644
index 00000000..230e51e7
--- /dev/null
+++ b/pjlib-util/src/pjlib-util-test/stun.c
@@ -0,0 +1,118 @@
+/* $Id$ */
+/*
+ * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+static int decode_test(void)
+{
+ /* Invalid message type */
+
+ /* Short message */
+
+ /* Long, random message */
+
+ /* Message length in header is shorter */
+
+ /* Message length in header is longer */
+
+ /* Invalid magic */
+
+ /* Attribute length is not valid */
+
+ /* Unknown mandatory attribute type should generate error */
+
+ /* Unknown but non-mandatory should be okay */
+
+ /* String/binary attribute length is larger than the message */
+
+ /* Valid message with MESSAGE-INTEGRITY */
+
+ /* Valid message with FINGERPRINT */
+
+ /* Valid message with MESSAGE-INTEGRITY and FINGERPRINT */
+
+ /* Another attribute not FINGERPRINT exists after MESSAGE-INTEGRITY */
+
+ /* Another attribute exists after FINGERPRINT */
+
+ return 0;
+}
+
+static int decode_verify(void)
+{
+ /* Decode all attribute types */
+ return 0;
+}
+
+static int auth_test(void)
+{
+ /* REALM and USERNAME is present, but MESSAGE-INTEGRITY is not present.
+ * For short term, must with reply 401 without REALM.
+ * For long term, must reply with 401 with REALM.
+ */
+
+ /* USERNAME is not present, server must respond with 432 (Missing
+ * Username).
+ */
+
+ /* If long term credential is wanted and REALM is not present, server
+ * must respond with 434 (Missing Realm)
+ */
+
+ /* If REALM doesn't match, server must respond with 434 (Missing Realm)
+ * too, containing REALM and NONCE attribute.
+ */
+
+ /* When long term authentication is wanted and NONCE is NOT present,
+ * server must respond with 435 (Missing Nonce), containing REALM and
+ * NONCE attribute.
+ */
+
+ /* Simulate 438 (Stale Nonce) */
+
+ /* Simulate 436 (Unknown Username) */
+
+ /* When server wants to use short term credential, but request has
+ * REALM, reject with .... ???
+ */
+
+ /* Invalid HMAC */
+
+ /* Valid static short term, without NONCE */
+
+ /* Valid static short term, WITH NONCE */
+
+ /* Valid static long term (with NONCE */
+
+ /* Valid dynamic short term (without NONCE) */
+
+ /* Valid dynamic short term (with NONCE) */
+
+ /* Valid dynamic long term (with NONCE) */
+
+ return 0;
+}
+
+
+int stun_test(void)
+{
+ decode_verify();
+ decode_test();
+ auth_test();
+ return 0;
+}
+
diff --git a/pjlib-util/src/pjlib-util-test/test.c b/pjlib-util/src/pjlib-util-test/test.c
index b6cc7290..599b72e3 100644
--- a/pjlib-util/src/pjlib-util-test/test.c
+++ b/pjlib-util/src/pjlib-util-test/test.c
@@ -68,8 +68,12 @@ static int test_inner(void)
#if INCLUDE_ENCRYPTION_TEST
DO_TEST(encryption_test());
+ DO_TEST(encryption_benchmark());
#endif
+#if INCLUDE_STUN_TEST
+ DO_TEST(stun_test());
+#endif
on_return:
return rc;
diff --git a/pjlib-util/src/pjlib-util-test/test.h b/pjlib-util/src/pjlib-util-test/test.h
index 252b3399..e5e1e24f 100644
--- a/pjlib-util/src/pjlib-util-test/test.h
+++ b/pjlib-util/src/pjlib-util-test/test.h
@@ -20,9 +20,12 @@
#define INCLUDE_XML_TEST 1
#define INCLUDE_ENCRYPTION_TEST 1
+#define INCLUDE_STUN_TEST 1
extern int xml_test(void);
extern int encryption_test();
+extern int encryption_benchmark();
+extern int stun_test();
extern int test_main(void);
extern void app_perror(const char *title, pj_status_t rc);