| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // |
| 4 | // All rights reserved. |
| 5 | // |
| 6 | // Redistribution and use in source and binary forms, with or without modification, are |
| 7 | // permitted provided that the following conditions are met: |
| 8 | // |
| 9 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 10 | // conditions and the following disclaimer. |
| 11 | // |
| 12 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 13 | // of conditions and the following disclaimer in the documentation and/or other |
| 14 | // materials provided with the distribution. |
| 15 | // |
| 16 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 17 | // used to endorse or promote products derived from this software without specific |
| 18 | // prior written permission. |
| 19 | // |
| 20 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 21 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 22 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 23 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 25 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 26 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 27 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 28 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | |
| 30 | #ifdef __GLIBC__ |
| 31 | #include <sys/stat.h> |
| 32 | #endif |
| 33 | |
| 34 | #include "gtest/gtest.h" |
| 35 | |
| 36 | #include <boost/filesystem.hpp> |
| 37 | |
| 38 | #include "misc_language.h" |
| 39 | #include "string_tools.h" |
| 40 | #include "file_io_utils.h" |
| 41 | #include "common/notify.h" |
| 42 | |
| 43 | TEST(notify, works) |
| 44 | { |
| 45 | #ifdef __GLIBC__ |
| 46 | mode_t prevmode = umask(077); |
| 47 | #endif |
| 48 | const char *tmp = getenv("TEMP"); |
| 49 | if (!tmp) |
| 50 | tmp = "/tmp"; |
| 51 | static const char *filename = "monero-notify-unit-test-XXXXXX"; |
| 52 | const size_t len = strlen(tmp) + 1 + strlen(filename); |
| 53 | std::unique_ptr<char[]> name_template_(new char[len + 1]); |
| 54 | char *name_template = name_template_.get(); |
| 55 | ASSERT_TRUE(name_template != NULL); |
| 56 | snprintf(name_template, len + 1, "%s/%s", tmp, filename); |
| 57 | int fd = mkstemp(name_template); |
| 58 | #ifdef __GLIBC__ |
| 59 | umask(prevmode); |
| 60 | #endif |
| 61 | ASSERT_TRUE(fd >= 0); |
| 62 | close(fd); |
| 63 | |
| 64 | const std::string spec = epee::string_tools::get_current_module_folder() + "/test_notifier" |
| 65 | #ifdef _WIN32 |
| 66 | + ".exe" |
| 67 | #endif |
| 68 | + " " + name_template + " %s"; |
| 69 | |
| 70 | tools::Notify notify(spec.c_str()); |
| 71 | notify.notify("%s", "1111111111111111111111111111111111111111111111111111111111111111", NULL); |
| 72 | |
| 73 | bool ok = false; |
| 74 | for (int i = 0; i < 10; ++i) |
| 75 | { |
| 76 | epee::misc_utils::sleep_no_w(100); |
| 77 | |
| 78 | std::string s; |
| 79 | if (epee::file_io_utils::load_file_to_string(name_template, s)) |
| 80 | { |
| 81 | if (s == "1111111111111111111111111111111111111111111111111111111111111111") |
| 82 | { |
| 83 | ok = true; |
| 84 | break; |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | boost::filesystem::remove(name_template); |
| 89 | ASSERT_TRUE(ok); |
| 90 | } |