153b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal/* 253b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * Copyright (C) 2016, The Android Open Source Project 353b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * 453b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * Licensed under the Apache License, Version 2.0 (the "License"); 553b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * you may not use this file except in compliance with the License. 653b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * You may obtain a copy of the License at 753b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * 853b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * http://www.apache.org/licenses/LICENSE-2.0 953b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * 1053b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * Unless required by applicable law or agreed to in writing, software 1153b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * distributed under the License is distributed on an "AS IS" BASIS, 1253b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1353b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * See the License for the specific language governing permissions and 1453b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal * limitations under the License. 1553b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal */ 1653b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 1753b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal#include <cstdint> 1853b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal#include <limits> 1953b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 20fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal#include "android-base/stringprintf.h" 2153b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal#include "gtest/gtest.h" 2253b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 2353b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal#include "wifilogd/local_utils.h" 2453b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 2553b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawalnamespace android { 2653b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawalnamespace wifilogd { 2753b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 289cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawalusing local_utils::CastEnumToInteger; 290da05477c58df736e69ec421d14a1c3216011d81mukesh agrawalusing local_utils::CopyFromBufferOrDie; 3053b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawalusing local_utils::GetMaxVal; 31fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawalusing local_utils::IsAsciiPrintable; 3253b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 339cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawalTEST(LocalUtilsTest, CastEnumToIntegerWorksForValidEnumValues) { 349cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal enum class basic_enum { VALUE0, VALUE1 }; 359cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal EXPECT_EQ(0, CastEnumToInteger(basic_enum::VALUE0)); 369cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal EXPECT_EQ(1, CastEnumToInteger(basic_enum::VALUE1)); 379cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal} 389cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal 399cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawalTEST(LocalUtilsTest, CastEnumToIntegerWorksForInvalidEnumValues) { 409cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal enum class basic_enum : int { VALUE0, VALUE1 }; 419cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal constexpr int invalid_enum_value = 2; 429cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal EXPECT_EQ(0, CastEnumToInteger(basic_enum::VALUE0)); 439cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal EXPECT_EQ(1, CastEnumToInteger(basic_enum::VALUE1)); 449cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal EXPECT_EQ(2, CastEnumToInteger(CopyFromBufferOrDie<basic_enum>( 459cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal &invalid_enum_value, sizeof(invalid_enum_value)))); 469cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal} 479cfc0379b078bdbed45bf0a3c3f5d3afc3860bdfmukesh agrawal 480da05477c58df736e69ec421d14a1c3216011d81mukesh agrawalTEST(LocalUtilsTest, CopyFromBufferOrDieCopiesData) { 490da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal struct Message { 500da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal int a; 510da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal char b; 520da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal }; 530da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal const Message original{5, 'c'}; 540da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal const auto& duplicate = 550da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal CopyFromBufferOrDie<Message>(&original, sizeof(original)); 560da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal EXPECT_EQ(original.a, duplicate.a); 570da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal EXPECT_EQ(original.b, duplicate.b); 580da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal} 590da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal 6053b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawalTEST(LocalUtilsTest, GetMaxValFromTypeIsCorrectForUnsignedTypes) { 6153b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<uint8_t>::max(), GetMaxVal<uint8_t>()); 6253b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<uint16_t>::max(), GetMaxVal<uint16_t>()); 6353b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<uint32_t>::max(), GetMaxVal<uint32_t>()); 6453b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<uint64_t>::max(), GetMaxVal<uint64_t>()); 6553b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal} 6653b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 6753b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawalTEST(LocalUtilsTest, GetMaxValFromTypeIsCorrectForSignedTypes) { 6853b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<int8_t>::max(), GetMaxVal<int8_t>()); 6953b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<int16_t>::max(), GetMaxVal<int16_t>()); 7053b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<int32_t>::max(), GetMaxVal<int32_t>()); 7153b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<int64_t>::max(), GetMaxVal<int64_t>()); 7253b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal} 7353b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 7453b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawalTEST(LocalUtilsTest, GetMaxValFromInstanceIsCorrectForUnsignedTypes) { 7553b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<uint8_t>::max(), GetMaxVal(uint8_t{})); 7653b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<uint16_t>::max(), GetMaxVal(uint16_t{})); 7753b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<uint32_t>::max(), GetMaxVal(uint32_t{})); 7853b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<uint64_t>::max(), GetMaxVal(uint64_t{})); 7953b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal} 8053b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 8153b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawalTEST(LocalUtilsTest, GetMaxValFromInstanceIsCorrectForSignedTypes) { 8253b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<int8_t>::max(), GetMaxVal(int8_t{})); 8353b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<int16_t>::max(), GetMaxVal(int16_t{})); 8453b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<int32_t>::max(), GetMaxVal(int32_t{})); 8553b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(std::numeric_limits<int64_t>::max(), GetMaxVal(int64_t{})); 8653b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal} 8753b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 88fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawalTEST(LocalUtilsTest, IsAsciiPrintableReturnsTrueForAlphabeticCharacters) { 89fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal for (const char c : {'a', 'z', 'A', 'Z'}) { 90fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal EXPECT_TRUE(IsAsciiPrintable(c)) << "Failed with: c == '" << c << "'"; 91fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal } 92fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal} 93fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal 94fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawalTEST(LocalUtilsTest, IsAsciiPrintableReturnsTrueForNumericCharacters) { 95fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal for (const char c : {'0', '9'}) { 96fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal EXPECT_TRUE(IsAsciiPrintable(c)) << "Failed with: c == '" << c << "'"; 97fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal } 98fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal} 99fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal 100fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawalTEST(LocalUtilsTest, IsAsciiPrintableReturnsTrueForPrintableSpaces) { 101fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal for (const char c : {'\n', '\t', ' '}) { 102fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal EXPECT_TRUE(IsAsciiPrintable(c)) 103fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal << base::StringPrintf("Failed with: c == 0x%02d", c); 104fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal } 105fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal} 106fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal 107fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawalTEST(LocalUtilsTest, IsAsciiPrintableReturnsTrueForMaximalPrintable) { 108fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal EXPECT_TRUE(IsAsciiPrintable('~')); 109fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal} 110fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal 111fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawalTEST(LocalUtilsTest, IsAsciiPrintableReturnsFalseForUnprintableSpaces) { 112fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal for (const char c : {'\f', '\r', '\v'}) { 113fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal EXPECT_FALSE(IsAsciiPrintable(c)) 114fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal << base::StringPrintf("Failed with: c == 0x%02d", c); 115fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal } 116fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal} 117fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal 118fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawalTEST(LocalUtilsTest, IsAsciiPrintableReturnsFalseForNeighborsOfPrintables) { 119fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal for (const char c : {char{0x1f}, char{0x7f}}) { 120fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal EXPECT_FALSE(IsAsciiPrintable(c)) 121fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal << base::StringPrintf("Failed with: c == 0x%02d", c); 122fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal } 123fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal} 124fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal 125fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawalTEST(LocalUtilsTest, IsAsciiPrintableReturnsFalseForMinimalAndMaximalChars) { 126fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal using uchar = unsigned char; 127fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal for (const uchar c : {uchar{0x00}, uchar{0xff}}) { 128fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal EXPECT_FALSE(IsAsciiPrintable(c)) 129fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal << base::StringPrintf("Failed with: c == 0x%02d", c); 130fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal } 131fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal} 132fde5bda852c87fa190002b14fed511b7d4a65a50mukesh agrawal 13353b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawalTEST(LocalUtilsTest, SafelyClampWorksForSameTypeClamping) { 13453b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(int8_t{0}, (SAFELY_CLAMP(int8_t{-1}, int8_t, 0, 2))); 13553b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(int8_t{0}, (SAFELY_CLAMP(int8_t{0}, int8_t, 0, 2))); 13653b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(int8_t{1}, (SAFELY_CLAMP(int8_t{1}, int8_t, 0, 2))); 13753b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(int8_t{2}, (SAFELY_CLAMP(int8_t{2}, int8_t, 0, 2))); 13853b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(int8_t{2}, (SAFELY_CLAMP(int8_t{3}, int8_t, 0, 2))); 13953b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal} 14053b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 14153b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawalTEST(LocalUtilsTest, SafelyClampWorksForSignedToUnsigned) { 14253b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal static_assert(std::numeric_limits<int8_t>::max() == 127, 14353b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal "upper bound is set incorrectly"); 14453b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(uint8_t{0}, (SAFELY_CLAMP(int8_t{-1}, uint8_t, 0, 127))); 14553b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(uint8_t{0}, (SAFELY_CLAMP(int8_t{0}, uint8_t, 0, 127))); 14653b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(uint8_t{1}, (SAFELY_CLAMP(int8_t{1}, uint8_t, 0, 127))); 14753b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(uint8_t{127}, (SAFELY_CLAMP(int8_t{127}, uint8_t, 0, 127))); 14853b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal} 14953b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 15053b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawalTEST(LocalUtilsTest, SafelyClampWorksForUnsignedToSigned) { 15153b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal static_assert(std::numeric_limits<int8_t>::max() == 127, 15253b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal "upper bound is set incorrectly"); 15353b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(int8_t{0}, (SAFELY_CLAMP(uint8_t{0}, int8_t, 0, 127))); 15453b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(int8_t{1}, (SAFELY_CLAMP(uint8_t{1}, int8_t, 0, 127))); 15553b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(int8_t{127}, (SAFELY_CLAMP(uint8_t{127}, int8_t, 0, 127))); 15653b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal EXPECT_EQ(int8_t{127}, (SAFELY_CLAMP(uint8_t{128}, int8_t, 0, 127))); 15753b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal} 15853b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal 1590da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal// Per 1607e979c369670feea99b9aff42bc366a903cf7049mukesh agrawal// github.com/google/googletest/blob/master/googletest/docs/AdvancedGuide.md#death-tests, 1610da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal// death tests should be specially named. 1620da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal 1630da05477c58df736e69ec421d14a1c3216011d81mukesh agrawalTEST(LocalUtilsDeathTest, CopyFromBufferOrDieWithShortBufferCausesDeath) { 1640da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal struct Message { 1650da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal int a; 1660da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal char b; 1670da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal }; 1680da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal const Message original{5, 'c'}; 1690da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal EXPECT_DEATH((CopyFromBufferOrDie<Message>(&original, sizeof(original) - 1)), 1700da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal "Check failed"); 1710da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal} 1720da05477c58df736e69ec421d14a1c3216011d81mukesh agrawal 17353b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal} // namespace wifilogd 17453b465f2e5c0cba5212b18bf27d21b84378b0c74mukesh agrawal} // namespace android 175