1 // Copyright (C) 2013 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include "validating_util.h" 16 17 #include <string> 18 19 #include <gtest/gtest.h> 20 21 #define ITOA_HELPER(i) #i 22 #define ITOA(i) ITOA_HELPER(i) 23 24 #define DATA "{'foo': 'bar'}" 25 #define TIMESTAMP 1388001600 26 #define TIMESTAMP_HALF_MONTH_AGO 1386705600 27 #define TIMESTAMP_TWO_MONTHS_AGO 1382817600 28 #define CHECKSUM "dd63dafcbd4d5b28badfcaf86fb6fcdb" 29 30 namespace { 31 32 using i18n::addressinput::ValidatingUtil; 33 34 // The data being wrapped. 35 const char kUnwrappedData[] = DATA; 36 37 // The timestamp for the wrapped data. 38 const time_t kTimestamp = TIMESTAMP; 39 40 // The checksum and data together. 41 const char kChecksummedData[] = "checksum=" CHECKSUM "\n" 42 DATA; 43 44 // "Randomly" corrupted checksummed data. The "m" in "checksum" is capitalized. 45 const char kCorruptedChecksummedData[] = "checksuM=" CHECKSUM "\n" 46 DATA; 47 48 // The checksum in the middle of data. 49 const char kChecksumInMiddle[] = DATA "\n" 50 "checksum=" CHECKSUM "\n" 51 DATA; 52 53 // The file as it is stored on disk. 54 const char kWrappedData[] = "timestamp=" ITOA(TIMESTAMP) "\n" 55 "checksum=" CHECKSUM "\n" 56 DATA; 57 58 // "Randomly" corrupted file. The "p" in "timestamp" is capitalized. 59 const char kCorruptedWrappedData[] = "timestamP=" ITOA(TIMESTAMP) "\n" 60 "checksum=" CHECKSUM "\n" 61 DATA; 62 63 // The timestamp in the middle of data. 64 const char kTimestampInMiddle[] = DATA "\n" 65 "timestamp=" ITOA(TIMESTAMP) "\n" 66 DATA; 67 68 // A recent timestamp and data together. 69 const char kTimestampHalfMonthAgo[] = 70 "timestamp=" ITOA(TIMESTAMP_HALF_MONTH_AGO) "\n" 71 DATA; 72 73 // A stale timestamp and data together. 74 const char kTimestampTwoMonthsAgo[] = 75 "timestamp=" ITOA(TIMESTAMP_TWO_MONTHS_AGO) "\n" 76 DATA; 77 78 TEST(ValidatingUtilTest, UnwrapChecksum_CorruptedData) { 79 std::string data(kCorruptedChecksummedData); 80 EXPECT_FALSE(ValidatingUtil::UnwrapChecksum(&data)); 81 } 82 83 TEST(ValidatingUtilTest, UnwrapChecksum_EmptyString) { 84 std::string data; 85 EXPECT_FALSE(ValidatingUtil::UnwrapChecksum(&data)); 86 } 87 88 TEST(ValidatingUtilTest, UnwrapChecksum_GarbageData) { 89 std::string data("garbage"); 90 EXPECT_FALSE(ValidatingUtil::UnwrapChecksum(&data)); 91 } 92 93 TEST(ValidatingUtilTest, UnwrapChecksum_InMiddle) { 94 std::string data(kChecksumInMiddle); 95 EXPECT_FALSE(ValidatingUtil::UnwrapChecksum(&data)); 96 } 97 98 TEST(ValidatingUtilTest, UnwrapChecksum) { 99 std::string data(kChecksummedData); 100 EXPECT_TRUE(ValidatingUtil::UnwrapChecksum(&data)); 101 EXPECT_EQ(kUnwrappedData, data); 102 } 103 104 TEST(ValidatingUtilTest, UnwrapTimestamp_CorruptedData) { 105 std::string data(kCorruptedWrappedData); 106 EXPECT_FALSE(ValidatingUtil::UnwrapTimestamp(&data, kTimestamp)); 107 } 108 109 TEST(ValidatingUtilTest, UnwrapTimestamp_EmptyString) { 110 std::string data; 111 EXPECT_FALSE(ValidatingUtil::UnwrapTimestamp(&data, kTimestamp)); 112 } 113 114 TEST(ValidatingUtilTest, UnwrapTimestamp_GarbageData) { 115 std::string data("garbage"); 116 EXPECT_FALSE(ValidatingUtil::UnwrapTimestamp(&data, kTimestamp)); 117 } 118 119 TEST(ValidatingUtilTest, UnwrapTimestamp_InMiddle) { 120 std::string data(kTimestampInMiddle); 121 EXPECT_FALSE(ValidatingUtil::UnwrapTimestamp(&data, kTimestamp)); 122 } 123 124 TEST(ValidatingUtilTest, UnwrapTimestamp_Recent) { 125 std::string data(kTimestampHalfMonthAgo); 126 EXPECT_TRUE(ValidatingUtil::UnwrapTimestamp(&data, kTimestamp)); 127 EXPECT_EQ(kUnwrappedData, data); 128 } 129 130 TEST(ValidatingUtilTest, UnwrapTimestamp_Stale) { 131 std::string data(kTimestampTwoMonthsAgo); 132 EXPECT_FALSE(ValidatingUtil::UnwrapTimestamp(&data, kTimestamp)); 133 } 134 135 TEST(ValidatingUtilTest, UnwrapTimestamp) { 136 std::string data(kWrappedData); 137 EXPECT_TRUE(ValidatingUtil::UnwrapTimestamp(&data, kTimestamp)); 138 EXPECT_EQ(kChecksummedData, data); 139 } 140 141 TEST(ValidatingUtilTest, Wrap) { 142 std::string data = kUnwrappedData; 143 ValidatingUtil::Wrap(kTimestamp, &data); 144 EXPECT_EQ(kWrappedData, data); 145 } 146 147 TEST(ValidatingUtilTest, WrapUnwrapIt) { 148 std::string data = kUnwrappedData; 149 ValidatingUtil::Wrap(kTimestamp, &data); 150 EXPECT_TRUE(ValidatingUtil::UnwrapTimestamp(&data, kTimestamp)); 151 EXPECT_EQ(kChecksummedData, data); 152 EXPECT_TRUE(ValidatingUtil::UnwrapChecksum(&data)); 153 EXPECT_EQ(kUnwrappedData, data); 154 } 155 156 } // namespace 157