* 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. */
-
+// C
#include <assert.h>
+#include <stdint.h>
+
+// Dlog
#include <metrics.h>
#include <queued_entry.h>
+static bool fail_dlogutil_get_tag = false;
+int __real_dlogutil_entry_get_tag(const dlogutil_entry_s *entry, const char **tag);
+int __wrap_dlogutil_entry_get_tag(const dlogutil_entry_s *entry, const char **tag)
+{
+ if (!fail_dlogutil_get_tag)
+ return __real_dlogutil_entry_get_tag(entry, tag);
+
+ return 1;
+}
+
+static bool fail_dlogutil_get_pid = false;
+int __real_dlogutil_entry_get_pid(const dlogutil_entry_s *e, pid_t *pid);
+int __wrap_dlogutil_entry_get_pid(const dlogutil_entry_s *e, pid_t *pid)
+{
+ if (!fail_dlogutil_get_pid)
+ return __real_dlogutil_entry_get_pid(e, pid);
+
+ return 1;
+}
+
+static bool fail_dlogutil_get_priority = false;
+int __real_dlogutil_entry_get_priority(const dlogutil_entry_s *e, log_priority *prio);
+int __wrap_dlogutil_entry_get_priority(const dlogutil_entry_s *e, log_priority *prio)
+{
+ if (!fail_dlogutil_get_priority)
+ return __real_dlogutil_entry_get_priority(e, prio);
+
+ return 1;
+}
+
+uintptr_t sum_alloc;
+static bool count_pointers = false;
+
+void __real_free(void *ptr);
+void __wrap_free(void *ptr)
+{
+ if (count_pointers)
+ sum_alloc -= (uintptr_t) ptr;
+
+ __real_free(ptr);
+}
+
+void *__real_calloc(size_t nmemb, size_t size);
+void *__wrap_calloc(size_t nmemb, size_t size)
+{
+ void *tmp = __real_calloc(nmemb, size);
+
+ if (count_pointers)
+ sum_alloc += (uintptr_t) tmp;
+
+ return tmp;
+}
+
+char *__real_strdup(const char *s);
+char *__wrap_strdup(const char *s)
+{
+ void *tmp = __real_strdup(s);
+
+ if (count_pointers)
+ sum_alloc += (uintptr_t) tmp;
+
+ return tmp;
+}
+
int main()
{
struct metrics *m = metrics_create();
assert(m);
+ assert(0 == metrics_get_total(m));
int count;
struct metrics_info *info;
assert(info[1].count[DLOG_DEBUG] == 0);
assert(info[1].count[DLOG_VERBOSE] == 0);
free(info);
+ assert(6 == metrics_get_total(m));
const int PIDS = 1200;
assert(count == PIDS + 3);
free(info);
+ assert(1206 == metrics_get_total(m));
+
+ assert(metrics_add_log(m, &e0.header));
+ assert(1207 == metrics_get_total(m));
+
+ assert(metrics_add_log(m, &e2.header));
+ assert(1208 == metrics_get_total(m));
+
+ metrics_clear(m);
+ assert(0 == metrics_get_total(m));
+ info = metrics_get_info(m, &count);
+ assert(info == NULL);
+
+ fail_dlogutil_get_tag = true;
+ assert(!metrics_add_log(m, &e0.header));
+ fail_dlogutil_get_tag = false;
+
+ fail_dlogutil_get_pid = true;
+ assert(!metrics_add_log(m, &e0.header));
+ fail_dlogutil_get_pid = false;
+
+ fail_dlogutil_get_priority = true;
+ assert(!metrics_add_log(m, &e0.header));
+ fail_dlogutil_get_priority = false;
+
metrics_destroy(m);
+
+ sum_alloc = 0;
+ count_pointers = true;
+
+ struct metrics *m2 = metrics_create();
+ assert(metrics_add_log(m2, &e0.header));
+ assert(metrics_add_log(m2, &e3.header));
+ metrics_destroy(m2);
+
+ count_pointers = false;
+ assert(0 == sum_alloc);
+
+ // it shouldn't crash.
+ metrics_destroy(NULL);
}