2014-01-06 08:06:02 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2014-01-06 08:06:02 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "shrpx_config_test.h"
|
|
|
|
|
2015-05-13 15:30:35 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2015-01-07 17:26:30 +01:00
|
|
|
#include <unistd.h>
|
2015-05-13 15:30:35 +02:00
|
|
|
#endif // HAVE_UNISTD_H
|
2015-01-07 17:26:30 +01:00
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2014-01-06 08:06:02 +01:00
|
|
|
#include <CUnit/CUnit.h>
|
|
|
|
|
|
|
|
#include "shrpx_config.h"
|
2017-02-16 14:46:22 +01:00
|
|
|
#include "shrpx_log.h"
|
2014-01-06 08:06:02 +01:00
|
|
|
|
|
|
|
namespace shrpx {
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void test_shrpx_config_parse_header(void) {
|
2016-10-02 15:04:04 +02:00
|
|
|
BlockAllocator balloc(4096, 4096);
|
|
|
|
|
|
|
|
auto p = parse_header(balloc, StringRef::from_lit("a: b"));
|
2016-02-13 14:19:05 +01:00
|
|
|
CU_ASSERT("a" == p.name);
|
|
|
|
CU_ASSERT("b" == p.value);
|
2014-04-26 07:56:08 +02:00
|
|
|
|
2016-10-02 15:04:04 +02:00
|
|
|
p = parse_header(balloc, StringRef::from_lit("a: b"));
|
2016-02-13 14:19:05 +01:00
|
|
|
CU_ASSERT("a" == p.name);
|
|
|
|
CU_ASSERT("b" == p.value);
|
2014-04-26 07:56:08 +02:00
|
|
|
|
2016-10-02 15:04:04 +02:00
|
|
|
p = parse_header(balloc, StringRef::from_lit(":a: b"));
|
2016-02-13 14:19:05 +01:00
|
|
|
CU_ASSERT(p.name.empty());
|
2014-04-26 07:56:08 +02:00
|
|
|
|
2016-10-02 15:04:04 +02:00
|
|
|
p = parse_header(balloc, StringRef::from_lit("a: :b"));
|
2016-02-13 14:19:05 +01:00
|
|
|
CU_ASSERT("a" == p.name);
|
|
|
|
CU_ASSERT(":b" == p.value);
|
2014-04-26 07:56:08 +02:00
|
|
|
|
2016-10-02 15:04:04 +02:00
|
|
|
p = parse_header(balloc, StringRef::from_lit(": b"));
|
2016-02-13 14:19:05 +01:00
|
|
|
CU_ASSERT(p.name.empty());
|
2014-04-26 07:56:08 +02:00
|
|
|
|
2016-10-02 15:04:04 +02:00
|
|
|
p = parse_header(balloc, StringRef::from_lit("alpha: bravo charlie"));
|
2016-02-13 14:19:05 +01:00
|
|
|
CU_ASSERT("alpha" == p.name);
|
|
|
|
CU_ASSERT("bravo charlie" == p.value);
|
2015-12-25 12:57:24 +01:00
|
|
|
|
2016-10-02 15:04:04 +02:00
|
|
|
p = parse_header(balloc, StringRef::from_lit("a,: b"));
|
2016-02-13 14:19:05 +01:00
|
|
|
CU_ASSERT(p.name.empty());
|
2015-12-25 12:57:24 +01:00
|
|
|
|
2016-10-02 15:04:04 +02:00
|
|
|
p = parse_header(balloc, StringRef::from_lit("a: b\x0a"));
|
2016-02-13 14:19:05 +01:00
|
|
|
CU_ASSERT(p.name.empty());
|
2014-04-26 07:56:08 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void test_shrpx_config_parse_log_format(void) {
|
2016-10-02 14:22:02 +02:00
|
|
|
BlockAllocator balloc(4096, 4096);
|
|
|
|
|
|
|
|
auto res = parse_log_format(
|
|
|
|
balloc, StringRef::from_lit(
|
|
|
|
R"($remote_addr - $remote_user [$time_local] )"
|
|
|
|
R"("$request" $status $body_bytes_sent )"
|
|
|
|
R"("${http_referer}" $http_host "$http_user_agent")"));
|
2015-10-28 15:12:16 +01:00
|
|
|
CU_ASSERT(16 == res.size());
|
2014-11-18 16:56:44 +01:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_REMOTE_ADDR == res[0].type);
|
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[1].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT(" - $remote_user [" == res[1].value);
|
2014-11-18 16:56:44 +01:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_TIME_LOCAL == res[2].type);
|
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[3].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("] \"" == res[3].value);
|
2014-11-18 16:56:44 +01:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_REQUEST == res[4].type);
|
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[5].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("\" " == res[5].value);
|
2014-11-18 16:56:44 +01:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_STATUS == res[6].type);
|
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[7].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT(" " == res[7].value);
|
2014-11-18 16:56:44 +01:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_BODY_BYTES_SENT == res[8].type);
|
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[9].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT(" \"" == res[9].value);
|
2014-11-18 16:56:44 +01:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_HTTP == res[10].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("referer" == res[10].value);
|
2014-11-18 16:56:44 +01:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[11].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("\" " == res[11].value);
|
2014-11-18 16:56:44 +01:00
|
|
|
|
2015-10-28 15:12:16 +01:00
|
|
|
CU_ASSERT(SHRPX_LOGF_AUTHORITY == res[12].type);
|
2014-11-18 16:56:44 +01:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[13].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT(" \"" == res[13].value);
|
2015-10-28 15:12:16 +01:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_HTTP == res[14].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("user-agent" == res[14].value);
|
2015-10-28 15:12:16 +01:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[15].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("\"" == res[15].value);
|
2015-07-14 15:25:52 +02:00
|
|
|
|
2016-10-02 14:22:02 +02:00
|
|
|
res = parse_log_format(balloc, StringRef::from_lit("$"));
|
2015-07-14 15:25:52 +02:00
|
|
|
|
|
|
|
CU_ASSERT(1 == res.size());
|
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[0].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("$" == res[0].value);
|
2015-07-14 15:25:52 +02:00
|
|
|
|
2016-10-02 14:22:02 +02:00
|
|
|
res = parse_log_format(balloc, StringRef::from_lit("${"));
|
2015-07-14 15:25:52 +02:00
|
|
|
|
|
|
|
CU_ASSERT(1 == res.size());
|
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[0].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("${" == res[0].value);
|
2015-07-14 15:25:52 +02:00
|
|
|
|
2016-10-02 14:22:02 +02:00
|
|
|
res = parse_log_format(balloc, StringRef::from_lit("${a"));
|
2015-07-14 15:25:52 +02:00
|
|
|
|
|
|
|
CU_ASSERT(1 == res.size());
|
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[0].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("${a" == res[0].value);
|
2015-07-14 15:25:52 +02:00
|
|
|
|
2016-10-02 14:22:02 +02:00
|
|
|
res = parse_log_format(balloc, StringRef::from_lit("${a "));
|
2015-07-14 15:25:52 +02:00
|
|
|
|
|
|
|
CU_ASSERT(1 == res.size());
|
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[0].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("${a " == res[0].value);
|
2015-07-14 15:25:52 +02:00
|
|
|
|
2016-10-02 14:22:02 +02:00
|
|
|
res = parse_log_format(balloc, StringRef::from_lit("$$remote_addr"));
|
2015-07-14 15:25:52 +02:00
|
|
|
|
|
|
|
CU_ASSERT(2 == res.size());
|
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_LITERAL == res[0].type);
|
2016-01-17 08:33:23 +01:00
|
|
|
CU_ASSERT("$" == res[0].value);
|
2015-07-14 15:25:52 +02:00
|
|
|
|
|
|
|
CU_ASSERT(SHRPX_LOGF_REMOTE_ADDR == res[1].type);
|
2016-01-17 13:31:30 +01:00
|
|
|
CU_ASSERT("" == res[1].value);
|
2014-11-18 16:56:44 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 17:26:30 +01:00
|
|
|
void test_shrpx_config_read_tls_ticket_key_file(void) {
|
|
|
|
char file1[] = "/tmp/nghttpx-unittest.XXXXXX";
|
|
|
|
auto fd1 = mkstemp(file1);
|
|
|
|
assert(fd1 != -1);
|
|
|
|
assert(48 ==
|
|
|
|
write(fd1, "0..............12..............34..............5", 48));
|
|
|
|
char file2[] = "/tmp/nghttpx-unittest.XXXXXX";
|
|
|
|
auto fd2 = mkstemp(file2);
|
|
|
|
assert(fd2 != -1);
|
|
|
|
assert(48 ==
|
|
|
|
write(fd2, "6..............78..............9a..............b", 48));
|
|
|
|
|
|
|
|
close(fd1);
|
|
|
|
close(fd2);
|
2016-10-02 15:08:51 +02:00
|
|
|
auto ticket_keys = read_tls_ticket_key_file(
|
|
|
|
{StringRef{file1}, StringRef{file2}}, EVP_aes_128_cbc(), EVP_sha256());
|
2015-01-07 17:26:30 +01:00
|
|
|
unlink(file1);
|
|
|
|
unlink(file2);
|
|
|
|
CU_ASSERT(ticket_keys.get() != nullptr);
|
|
|
|
CU_ASSERT(2 == ticket_keys->keys.size());
|
|
|
|
auto key = &ticket_keys->keys[0];
|
2015-07-26 19:12:07 +02:00
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.name), std::end(key->data.name),
|
|
|
|
"0..............1"));
|
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.enc_key),
|
|
|
|
std::begin(key->data.enc_key) + 16, "2..............3"));
|
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.hmac_key),
|
|
|
|
std::begin(key->data.hmac_key) + 16,
|
|
|
|
"4..............5"));
|
|
|
|
CU_ASSERT(16 == key->hmac_keylen);
|
2015-07-17 18:49:20 +02:00
|
|
|
|
|
|
|
key = &ticket_keys->keys[1];
|
2015-07-26 19:12:07 +02:00
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.name), std::end(key->data.name),
|
|
|
|
"6..............7"));
|
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.enc_key),
|
|
|
|
std::begin(key->data.enc_key) + 16, "8..............9"));
|
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.hmac_key),
|
|
|
|
std::begin(key->data.hmac_key) + 16,
|
|
|
|
"a..............b"));
|
|
|
|
CU_ASSERT(16 == key->hmac_keylen);
|
2015-07-17 18:49:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_shrpx_config_read_tls_ticket_key_file_aes_256(void) {
|
|
|
|
char file1[] = "/tmp/nghttpx-unittest.XXXXXX";
|
|
|
|
auto fd1 = mkstemp(file1);
|
|
|
|
assert(fd1 != -1);
|
|
|
|
assert(80 == write(fd1, "0..............12..............................34..."
|
|
|
|
"...........................5",
|
|
|
|
80));
|
|
|
|
char file2[] = "/tmp/nghttpx-unittest.XXXXXX";
|
|
|
|
auto fd2 = mkstemp(file2);
|
|
|
|
assert(fd2 != -1);
|
|
|
|
assert(80 == write(fd2, "6..............78..............................9a..."
|
|
|
|
"...........................b",
|
|
|
|
80));
|
|
|
|
|
|
|
|
close(fd1);
|
|
|
|
close(fd2);
|
2016-10-02 15:08:51 +02:00
|
|
|
auto ticket_keys = read_tls_ticket_key_file(
|
|
|
|
{StringRef{file1}, StringRef{file2}}, EVP_aes_256_cbc(), EVP_sha256());
|
2015-07-17 18:49:20 +02:00
|
|
|
unlink(file1);
|
|
|
|
unlink(file2);
|
|
|
|
CU_ASSERT(ticket_keys.get() != nullptr);
|
|
|
|
CU_ASSERT(2 == ticket_keys->keys.size());
|
|
|
|
auto key = &ticket_keys->keys[0];
|
2015-07-26 19:12:07 +02:00
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.name), std::end(key->data.name),
|
|
|
|
"0..............1"));
|
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.enc_key),
|
|
|
|
std::end(key->data.enc_key),
|
|
|
|
"2..............................3"));
|
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.hmac_key),
|
|
|
|
std::end(key->data.hmac_key),
|
|
|
|
"4..............................5"));
|
2015-01-07 17:26:30 +01:00
|
|
|
|
|
|
|
key = &ticket_keys->keys[1];
|
2015-07-26 19:12:07 +02:00
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.name), std::end(key->data.name),
|
|
|
|
"6..............7"));
|
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.enc_key),
|
|
|
|
std::end(key->data.enc_key),
|
|
|
|
"8..............................9"));
|
|
|
|
CU_ASSERT(std::equal(std::begin(key->data.hmac_key),
|
|
|
|
std::end(key->data.hmac_key),
|
|
|
|
"a..............................b"));
|
2015-01-07 17:26:30 +01:00
|
|
|
}
|
|
|
|
|
2014-01-06 08:06:02 +01:00
|
|
|
} // namespace shrpx
|