test: improve coverage

This commit is contained in:
Gregor Jasny
2026-06-20 16:59:03 +02:00
parent 72fd485196
commit a5c04f47e1
5 changed files with 54 additions and 0 deletions
+5
View File
@@ -57,6 +57,11 @@ TEST(HistogramTest, reject_unsorted_bucket_bounds) {
EXPECT_ANY_THROW(Histogram({2, 1}));
}
TEST(HistogramTest, reject_unsorted_bucket_bounds_lvalue) {
const Histogram::BucketBoundaries unsorted = {2, 1};
EXPECT_THROW(Histogram{unsorted}, std::invalid_argument);
}
TEST(HistogramTest, reject_non_incrementing_bucket_bounds) {
EXPECT_ANY_THROW(Histogram({1, 2, 2, 3}));
}
+21
View File
@@ -102,5 +102,26 @@ TEST(SummaryTest, construction_with_dynamic_quantile_vector) {
summary.Observe(8.0);
}
TEST(SummaryTest, quantile_with_out_of_order_batches) {
// Flush large values into the sample first, then insert smaller values so
// that insertBatch() hits the --idx path (value < sample_[item].value).
Summary summary{Summary::Quantiles{{0.5, 0.05}}, std::chrono::hours{1}};
for (int i = 0; i < 10; ++i) summary.Observe(100.0 + i);
summary.Collect(); // flushes buffer → sample_ now contains large values
for (int i = 0; i < 10; ++i) summary.Observe(1.0 + i);
auto metric = summary.Collect(); // flushes buffer with small values < existing sample → --idx
auto s = metric.summary;
// 20 observations total, sum = (1..10) + (100..109) = 55 + 1045 = 1100
EXPECT_EQ(s.sample_count, 20U);
EXPECT_DOUBLE_EQ(s.sample_sum, 1100.0);
// p50 with error 0.05 on 20 samples: rank error ≤ 1, true median is 10 or 100
ASSERT_EQ(s.quantile.size(), 1U);
EXPECT_GE(s.quantile.at(0).value, 1.0);
EXPECT_LE(s.quantile.at(0).value, 109.0);
}
} // namespace
} // namespace prometheus
@@ -274,6 +274,19 @@ TEST_F(BasicAuthIntegrationTest, shouldRejectWrongAuthorizationMethod) {
}
#endif
TEST_F(BasicAuthIntegrationTest, shouldRejectNonBasicAuthScheme) {
std::unique_ptr<curl_slist, decltype(&curl_slist_free_all)> header(
curl_slist_append(nullptr, "Authorization: Bearer sometoken"),
curl_slist_free_all);
fetchPrePerform_ = [&header](CURL* curl) {
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header.get());
};
const auto metrics = FetchMetrics(default_metrics_path_);
ASSERT_EQ(metrics.code, 401);
}
TEST_F(BasicAuthIntegrationTest, shouldRejectMalformedBase64) {
std::unique_ptr<curl_slist, decltype(&curl_slist_free_all)> header(
curl_slist_append(nullptr, "Authorization: Basic $"),
+12
View File
@@ -23,6 +23,18 @@ TEST_F(LabelEncoderTest, regular) {
EXPECT_EQ("/foo/bar", Encode(Label{"foo", "bar"}));
}
TEST_F(LabelEncoderTest, uppercase) {
EXPECT_EQ("/foo/Bar", Encode(Label{"foo", "Bar"}));
}
TEST_F(LabelEncoderTest, digits) {
EXPECT_EQ("/foo/bar123", Encode(Label{"foo", "bar123"}));
}
TEST_F(LabelEncoderTest, unreserved_chars) {
EXPECT_EQ("/foo/a-b.c_d~e", Encode(Label{"foo", "a-b.c_d~e"}));
}
TEST_F(LabelEncoderTest, empty) {
EXPECT_EQ("/first_label@base64/=", Encode(Label{"first_label", ""}));
}
+3
View File
@@ -36,6 +36,9 @@ const TestVector testVector[] = {
{"easure.", "ZWFzdXJlLg=="},
{"asure.", "YXN1cmUu"},
{"sure.", "c3VyZS4="},
// '/' character in encoded output (covers temp |= 0x3F branch in decode)
{"\xff\xff\xff", "////"},
};
using namespace testing;