blob: 65f220645baf1b7cfaa4dff79816e7afb718de20 [file] [log] [blame]
Artem Senicheve8837d52020-06-07 11:59:04 +03001// SPDX-License-Identifier: Apache-2.0
2// Copyright (C) 2020 YADRO
3
4#include "zlib_exception.hpp"
5#include "zlib_file.hpp"
6
7#include <gtest/gtest.h>
8
9TEST(ZlibFileTest, Exception)
10{
11 ASSERT_THROW(ZlibFile("invalid/path"), ZlibException);
12}
13
14TEST(ZlibFileTest, Write)
15{
16 const std::string msg = "Test message";
17 time_t currTime;
18 time(&currTime);
19 tm localTime;
20 localtime_r(&currTime, &localTime);
21
22 const std::string path = "/tmp/zlib_file_test.out";
23 ZlibFile file(path);
24 file.write(localTime, msg);
25 file.close();
26
27 char expect[64];
SpencerKue9af83c2020-10-16 10:55:42 +080028 const int len =
29 snprintf(expect, sizeof(expect),
30 "[ %i-%02i-%02iT%02i:%02i:%02i%+03ld:%02ld ] %s\n",
31 localTime.tm_year + 1900, localTime.tm_mon + 1,
32 localTime.tm_mday, localTime.tm_hour, localTime.tm_min,
33 localTime.tm_sec, localTime.tm_gmtoff / (60 * 60),
34 abs(localTime.tm_gmtoff % (60 * 60)) / 60, msg.c_str());
Artem Senicheve8837d52020-06-07 11:59:04 +030035
36 gzFile fd = gzopen(path.c_str(), "r");
37 ASSERT_TRUE(fd);
38 char buf[64];
39 memset(buf, 0, sizeof(buf));
40 EXPECT_EQ(gzread(fd, buf, sizeof(buf)), len);
41 EXPECT_STREQ(buf, expect);
42 EXPECT_EQ(gzclose(fd), 0);
43
44 unlink(path.c_str());
45}