forked from confluentinc/kibosh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_unit.c
More file actions
96 lines (83 loc) · 2.65 KB
/
log_unit.c
File metadata and controls
96 lines (83 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Copyright 2017 Confluent Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
#include "io.h"
#include "test.h"
#include "util.h"
#include <errno.h>
#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
static int test_log_prefix_lexicographically_increases(void)
{
int ret;
char *prefix1 = NULL;
const char *cur_prefix = NULL;
prefix1 = strdup(log_prefix());
EXPECT_NONNULL(prefix1);
do {
cur_prefix = log_prefix();
EXPECT_NONNULL(cur_prefix);
ret = strcmp(cur_prefix, prefix1);
if (ret <= 0) {
fprintf(stderr, "test_log_prefix_lexicographically_increases: log prefix "
"'%s' came before initial log prefix of '%s'\n", cur_prefix, prefix1);
}
} while (ret <= 0);
free(prefix1);
return 0;
}
static int test_signal_safe_uint32_to_string(void)
{
char buf[128];
memset(buf, 0, sizeof(buf));
EXPECT_INT_EQ(3, signal_safe_uint32_to_string(123, buf, sizeof(buf)));
EXPECT_STR_EQ("123", buf);
EXPECT_INT_EQ(1, signal_safe_uint32_to_string(0, buf, sizeof(buf)));
EXPECT_STR_EQ("0", buf);
EXPECT_INT_EQ(-ENAMETOOLONG, signal_safe_uint32_to_string(14, buf, 2));
EXPECT_INT_EQ(1, signal_safe_uint32_to_string(3, buf, 2));
EXPECT_STR_EQ("3", buf);
return 0;
}
static int test_emit_shutdown_message(void)
{
FILE *fp;
char path[PATH_MAX], buf[4096];
char const *tmp = getenv("TMPDIR");
if (!tmp)
tmp = "/dev/shm";
snprintf(path, sizeof(path), "%s/log_path.%lld.%d", tmp, (long long)getpid(), rand());
fp = fopen(path, "w");
EXPECT_NONNULL(fp);
kibosh_log_init(fp, 0);
emit_shutdown_message(11);
memset(buf, 0, sizeof(buf));
EXPECT_INT_EQ(0, read_string_from_file(path, buf, sizeof(buf)));
EXPECT_STR_EQ("kibosh was terminated by signal 11\n", buf);
fclose(fp);
return 0;
}
int main(void)
{
EXPECT_INT_ZERO(test_log_prefix_lexicographically_increases());
EXPECT_INT_ZERO(test_signal_safe_uint32_to_string());
EXPECT_INT_ZERO(test_emit_shutdown_message());
return EXIT_SUCCESS;
}
// vim: ts=4:sw=4:tw=99:et