1 //
2 // Copyright (c) 2012 Artyom Beilis (Tonkikh)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See
5 // accompanying file LICENSE or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
7 //
8
9 #include <boost/nowide/cstdlib.hpp>
10 #include <cstring>
11
12 #if defined(BOOST_NOWIDE_TEST_INCLUDE_WINDOWS) && defined(BOOST_WINDOWS)
13 #include <windows.h>
14 #endif
15
16 #include "test.hpp"
17
18 // "Safe" strcpy version with NULL termination to make MSVC runtime happy
19 // which warns when using strncpy
20 template<size_t size>
strcpy_safe(char (& dest)[size],const char * src)21 void strcpy_safe(char (&dest)[size], const char* src)
22 {
23 size_t len = std::strlen(src);
24 if(len >= size)
25 len = size - 1u;
26 std::memcpy(dest, src, len);
27 dest[len] = 0;
28 }
29
test_main(int,char **,char **)30 void test_main(int, char**, char**)
31 {
32 std::string example = "\xd7\xa9-\xd0\xbc-\xce\xbd";
33 char penv[256] = {0};
34 strcpy_safe(penv, ("BOOST_TEST2=" + example + "x").c_str());
35
36 TEST(boost::nowide::setenv("BOOST_TEST1", example.c_str(), 1) == 0);
37 TEST(boost::nowide::getenv("BOOST_TEST1"));
38 TEST(boost::nowide::getenv("BOOST_TEST1") == example);
39 TEST(boost::nowide::setenv("BOOST_TEST1", "xx", 0) == 0);
40 TEST(boost::nowide::getenv("BOOST_TEST1") == example);
41 TEST(boost::nowide::putenv(penv) == 0);
42 TEST(boost::nowide::getenv("BOOST_TEST2"));
43 TEST(boost::nowide::getenv("BOOST_TEST_INVALID") == 0);
44 TEST(boost::nowide::getenv("BOOST_TEST2") == example + "x");
45 #ifdef BOOST_WINDOWS
46 // Passing a variable without an equals sign (before \0) is an error
47 // But GLIBC has an extension that unsets the env var instead
48 char penv2[256] = {0};
49 const char* sPenv2 = "BOOST_TEST1SOMEGARBAGE=";
50 strcpy_safe(penv2, sPenv2);
51 // End the string before the equals sign -> Expect fail
52 penv2[strlen("BOOST_TEST1")] = '\0';
53 TEST(boost::nowide::putenv(penv2) == -1);
54 TEST(boost::nowide::getenv("BOOST_TEST1"));
55 TEST(boost::nowide::getenv("BOOST_TEST1") == example);
56 #endif
57 }
58