Artem Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 1 | // 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 | |
| 9 | TEST(ZlibFileTest, Exception) |
| 10 | { |
| 11 | ASSERT_THROW(ZlibFile("invalid/path"), ZlibException); |
| 12 | } |
| 13 | |
| 14 | TEST(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]; |
SpencerKu | e9af83c | 2020-10-16 10:55:42 +0800 | [diff] [blame] | 28 | 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 Senichev | e8837d5 | 2020-06-07 11:59:04 +0300 | [diff] [blame] | 35 | |
| 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 | } |