blob: ef49a6f657e6262156aef510ada7f0914a77bf70 [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];
28 const int len = snprintf(expect, sizeof(expect), "[ %02i:%02i:%02i ] %s\n",
29 localTime.tm_hour, localTime.tm_min,
30 localTime.tm_sec, msg.c_str());
31
32 gzFile fd = gzopen(path.c_str(), "r");
33 ASSERT_TRUE(fd);
34 char buf[64];
35 memset(buf, 0, sizeof(buf));
36 EXPECT_EQ(gzread(fd, buf, sizeof(buf)), len);
37 EXPECT_STREQ(buf, expect);
38 EXPECT_EQ(gzclose(fd), 0);
39
40 unlink(path.c_str());
41}