1 // Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <syslog.h>
6 #include <gtest/gtest.h>
7
8 #include "dumper.h"
9
10 namespace {
11
TEST(DumperTest,SyslogDumper)12 TEST(DumperTest, SyslogDumper) {
13 struct dumper *dumper = syslog_dumper_create(LOG_ERR);
14 dumpf(dumper, "hello %d", 1);
15 dumpf(dumper, "world %d\n123", 2);
16 dumpf(dumper, "456\n");
17 // The following should appear in syslog:
18 // dumper_unittest: hello 1world 2
19 // dumper_unittest: 123456
20 syslog_dumper_free(dumper);
21 }
22
TEST(DumperTest,MemDumper)23 TEST(DumperTest, MemDumper) {
24 struct dumper *dumper = mem_dumper_create();
25 char *buf;
26 int size, i;
27
28 mem_dumper_get(dumper, &buf, &size);
29 EXPECT_STREQ("", buf);
30
31 dumpf(dumper, "hello %d\n", 1);
32 mem_dumper_get(dumper, &buf, &size);
33 EXPECT_STREQ("hello 1\n", buf);
34 EXPECT_EQ(8, size);
35
36 dumpf(dumper, "world %d", 2);
37 mem_dumper_get(dumper, &buf, &size);
38 EXPECT_STREQ("hello 1\nworld 2", buf);
39 EXPECT_EQ(15, size);
40
41 mem_dumper_clear(dumper);
42 mem_dumper_get(dumper, &buf, &size);
43 EXPECT_STREQ("", buf);
44 EXPECT_EQ(0, size);
45
46 for (i = 0; i < 1000; i++) {
47 dumpf(dumper, "a");
48 }
49 mem_dumper_get(dumper, &buf, &size);
50 EXPECT_EQ(1000, strlen(buf));
51 EXPECT_EQ(1000, 1000);
52
53 mem_dumper_free(dumper);
54 }
55
56 } // namespace
57
main(int argc,char ** argv)58 int main(int argc, char **argv) {
59 ::testing::InitGoogleTest(&argc, argv);
60 return RUN_ALL_TESTS();
61 }
62