| OLD | NEW |
| (Empty) |
| 1 // Copyright 2014 The Crashpad Authors. All rights reserved. | |
| 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 "util/mac/launchd.h" | |
| 16 | |
| 17 #import <Foundation/Foundation.h> | |
| 18 #include <launch.h> | |
| 19 #include <string.h> | |
| 20 | |
| 21 #include <cmath> | |
| 22 #include <limits> | |
| 23 | |
| 24 #include "base/basictypes.h" | |
| 25 #include "base/mac/scoped_launch_data.h" | |
| 26 #include "gtest/gtest.h" | |
| 27 #include "util/stdlib/objc.h" | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 using namespace crashpad; | |
| 32 | |
| 33 TEST(Launchd, CFPropertyToLaunchData_Integer) { | |
| 34 base::mac::ScopedLaunchData launch_data; | |
| 35 | |
| 36 NSNumber* integer_nses[] = { | |
| 37 @0, | |
| 38 @1, | |
| 39 @-1, | |
| 40 @0x7f, | |
| 41 @0x80, | |
| 42 @0xff, | |
| 43 @0x0100, | |
| 44 @0x7fff, | |
| 45 @0x8000, | |
| 46 @0xffff, | |
| 47 @0x00010000, | |
| 48 @0x7fffffff, | |
| 49 @0x80000000, | |
| 50 @0xffffffff, | |
| 51 @0x1000000000000000, | |
| 52 @0x7fffffffffffffff, | |
| 53 @0x8000000000000000, | |
| 54 @0xffffffffffffffff, | |
| 55 @0x0123456789abcdef, | |
| 56 @0xfedcba9876543210, | |
| 57 }; | |
| 58 | |
| 59 for (size_t index = 0; index < arraysize(integer_nses); ++index) { | |
| 60 NSNumber* integer_ns = integer_nses[index]; | |
| 61 launch_data.reset(CFPropertyToLaunchData(integer_ns)); | |
| 62 ASSERT_TRUE(launch_data.get()); | |
| 63 ASSERT_EQ(LAUNCH_DATA_INTEGER, launch_data_get_type(launch_data)); | |
| 64 EXPECT_EQ([integer_ns longLongValue], launch_data_get_integer(launch_data)) | |
| 65 << "index " << index; | |
| 66 } | |
| 67 } | |
| 68 | |
| 69 TEST(Launchd, CFPropertyToLaunchData_FloatingPoint) { | |
| 70 base::mac::ScopedLaunchData launch_data; | |
| 71 | |
| 72 @autoreleasepool { | |
| 73 NSNumber* double_nses[] = { | |
| 74 @0.0, | |
| 75 @1.0, | |
| 76 @-1.0, | |
| 77 [NSNumber numberWithFloat:std::numeric_limits<float>::min()], | |
| 78 [NSNumber numberWithFloat:std::numeric_limits<float>::max()], | |
| 79 [NSNumber numberWithFloat:std::numeric_limits<double>::min()], | |
| 80 [NSNumber numberWithFloat:std::numeric_limits<double>::max()], | |
| 81 @3.1415926535897932, | |
| 82 [NSNumber numberWithFloat:std::numeric_limits<double>::infinity()], | |
| 83 [NSNumber numberWithFloat:std::numeric_limits<double>::quiet_NaN()], | |
| 84 [NSNumber numberWithFloat:std::numeric_limits<double>::signaling_NaN()], | |
| 85 }; | |
| 86 | |
| 87 for (size_t index = 0; index < arraysize(double_nses); ++index) { | |
| 88 NSNumber* double_ns = double_nses[index]; | |
| 89 launch_data.reset(CFPropertyToLaunchData(double_ns)); | |
| 90 ASSERT_TRUE(launch_data.get()); | |
| 91 ASSERT_EQ(LAUNCH_DATA_REAL, launch_data_get_type(launch_data)); | |
| 92 double expected_double_value = [double_ns doubleValue]; | |
| 93 double observed_double_value = launch_data_get_real(launch_data); | |
| 94 bool expected_is_nan = std::isnan(expected_double_value); | |
| 95 EXPECT_EQ(expected_is_nan, std::isnan(observed_double_value)); | |
| 96 if (!expected_is_nan) { | |
| 97 EXPECT_DOUBLE_EQ(expected_double_value, observed_double_value) | |
| 98 << "index " << index; | |
| 99 } | |
| 100 } | |
| 101 } | |
| 102 } | |
| 103 | |
| 104 TEST(Launchd, CFPropertyToLaunchData_Boolean) { | |
| 105 base::mac::ScopedLaunchData launch_data; | |
| 106 | |
| 107 NSNumber* bool_nses[] = { | |
| 108 @NO, | |
| 109 @YES, | |
| 110 }; | |
| 111 | |
| 112 for (size_t index = 0; index < arraysize(bool_nses); ++index) { | |
| 113 NSNumber* bool_ns = bool_nses[index]; | |
| 114 launch_data.reset(CFPropertyToLaunchData(bool_ns)); | |
| 115 ASSERT_TRUE(launch_data.get()); | |
| 116 ASSERT_EQ(LAUNCH_DATA_BOOL, launch_data_get_type(launch_data)); | |
| 117 if ([bool_ns boolValue]) { | |
| 118 EXPECT_TRUE(launch_data_get_bool(launch_data)); | |
| 119 } else { | |
| 120 EXPECT_FALSE(launch_data_get_bool(launch_data)); | |
| 121 } | |
| 122 } | |
| 123 } | |
| 124 | |
| 125 TEST(Launchd, CFPropertyToLaunchData_String) { | |
| 126 base::mac::ScopedLaunchData launch_data; | |
| 127 | |
| 128 @autoreleasepool { | |
| 129 NSString* string_nses[] = { | |
| 130 @"", | |
| 131 @"string", | |
| 132 @"Üñîçø∂é", | |
| 133 }; | |
| 134 | |
| 135 for (size_t index = 0; index < arraysize(string_nses); ++index) { | |
| 136 NSString* string_ns = string_nses[index]; | |
| 137 launch_data.reset(CFPropertyToLaunchData(string_ns)); | |
| 138 ASSERT_TRUE(launch_data.get()); | |
| 139 ASSERT_EQ(LAUNCH_DATA_STRING, launch_data_get_type(launch_data)); | |
| 140 EXPECT_STREQ([string_ns UTF8String], launch_data_get_string(launch_data)); | |
| 141 } | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 TEST(Launchd, CFPropertyToLaunchData_Data) { | |
| 146 base::mac::ScopedLaunchData launch_data; | |
| 147 | |
| 148 @autoreleasepool { | |
| 149 const uint8_t data_c[] = { | |
| 150 1, 2, 3, 4, 5, 6, 7, 8, 0, 0, 0, 0, 0, 0, 0, 0, 9, 8, 7, 6, 5, 4, 3, 2}; | |
| 151 NSData* data_ns = [NSData dataWithBytes:data_c length:sizeof(data_c)]; | |
| 152 launch_data.reset(CFPropertyToLaunchData(data_ns)); | |
| 153 ASSERT_TRUE(launch_data.get()); | |
| 154 ASSERT_EQ(LAUNCH_DATA_OPAQUE, launch_data_get_type(launch_data)); | |
| 155 EXPECT_EQ(sizeof(data_c), launch_data_get_opaque_size(launch_data)); | |
| 156 EXPECT_EQ( | |
| 157 0, memcmp(launch_data_get_opaque(launch_data), data_c, sizeof(data_c))); | |
| 158 } | |
| 159 } | |
| 160 | |
| 161 TEST(Launchd, CFPropertyToLaunchData_Dictionary) { | |
| 162 base::mac::ScopedLaunchData launch_data; | |
| 163 | |
| 164 NSDictionary* dictionary_ns = @{ | |
| 165 @"key" : @"value", | |
| 166 }; | |
| 167 | |
| 168 launch_data.reset(CFPropertyToLaunchData(dictionary_ns)); | |
| 169 ASSERT_TRUE(launch_data.get()); | |
| 170 ASSERT_EQ(LAUNCH_DATA_DICTIONARY, launch_data_get_type(launch_data)); | |
| 171 EXPECT_EQ([dictionary_ns count], launch_data_dict_get_count(launch_data)); | |
| 172 | |
| 173 launch_data_t launch_lookup_data = | |
| 174 launch_data_dict_lookup(launch_data, "key"); | |
| 175 ASSERT_TRUE(launch_lookup_data); | |
| 176 ASSERT_EQ(LAUNCH_DATA_STRING, launch_data_get_type(launch_lookup_data)); | |
| 177 EXPECT_STREQ("value", launch_data_get_string(launch_lookup_data)); | |
| 178 } | |
| 179 | |
| 180 TEST(Launchd, CFPropertyToLaunchData_Array) { | |
| 181 base::mac::ScopedLaunchData launch_data; | |
| 182 | |
| 183 NSArray* array_ns = @[ @"element_1", @"element_2", ]; | |
| 184 | |
| 185 launch_data.reset(CFPropertyToLaunchData(array_ns)); | |
| 186 ASSERT_TRUE(launch_data.get()); | |
| 187 ASSERT_EQ(LAUNCH_DATA_ARRAY, launch_data_get_type(launch_data)); | |
| 188 EXPECT_EQ([array_ns count], launch_data_array_get_count(launch_data)); | |
| 189 | |
| 190 launch_data_t launch_lookup_data = | |
| 191 launch_data_array_get_index(launch_data, 0); | |
| 192 ASSERT_TRUE(launch_lookup_data); | |
| 193 ASSERT_EQ(LAUNCH_DATA_STRING, launch_data_get_type(launch_lookup_data)); | |
| 194 EXPECT_STREQ("element_1", launch_data_get_string(launch_lookup_data)); | |
| 195 | |
| 196 launch_lookup_data = launch_data_array_get_index(launch_data, 1); | |
| 197 ASSERT_TRUE(launch_lookup_data); | |
| 198 ASSERT_EQ(LAUNCH_DATA_STRING, launch_data_get_type(launch_lookup_data)); | |
| 199 EXPECT_STREQ("element_2", launch_data_get_string(launch_lookup_data)); | |
| 200 } | |
| 201 | |
| 202 TEST(Launchd, CFPropertyToLaunchData_NSDate) { | |
| 203 // Test that NSDate conversions fail as advertised. There’s no type for | |
| 204 // storing date values in a launch_data_t. | |
| 205 | |
| 206 base::mac::ScopedLaunchData launch_data; | |
| 207 | |
| 208 @autoreleasepool { | |
| 209 NSDate* date = [NSDate date]; | |
| 210 launch_data.reset(CFPropertyToLaunchData(date)); | |
| 211 EXPECT_FALSE(launch_data); | |
| 212 | |
| 213 NSDictionary* date_dictionary = @{ | |
| 214 @"key" : @"value", | |
| 215 @"date" : date, | |
| 216 }; | |
| 217 launch_data.reset(CFPropertyToLaunchData(date_dictionary)); | |
| 218 EXPECT_FALSE(launch_data); | |
| 219 | |
| 220 NSArray* date_array = @[ @"string_1", date, @"string_2", ]; | |
| 221 launch_data.reset(CFPropertyToLaunchData(date_array)); | |
| 222 EXPECT_FALSE(launch_data); | |
| 223 } | |
| 224 } | |
| 225 | |
| 226 TEST(Launchd, CFPropertyToLaunchData_RealWorldJobDictionary) { | |
| 227 base::mac::ScopedLaunchData launch_data; | |
| 228 | |
| 229 NSDictionary* job_dictionary = @{ | |
| 230 @LAUNCH_JOBKEY_LABEL : @"com.example.job.rebooter", | |
| 231 @LAUNCH_JOBKEY_ONDEMAND : @YES, | |
| 232 @LAUNCH_JOBKEY_PROGRAMARGUMENTS : | |
| 233 @[ @"/bin/bash", @"-c", @"/sbin/reboot", ], | |
| 234 @LAUNCH_JOBKEY_MACHSERVICES : @{ | |
| 235 @"com.example.service.rebooter" : @YES, | |
| 236 }, | |
| 237 }; | |
| 238 | |
| 239 launch_data.reset(CFPropertyToLaunchData(job_dictionary)); | |
| 240 ASSERT_TRUE(launch_data.get()); | |
| 241 ASSERT_EQ(LAUNCH_DATA_DICTIONARY, launch_data_get_type(launch_data)); | |
| 242 EXPECT_EQ(4u, launch_data_dict_get_count(launch_data)); | |
| 243 | |
| 244 launch_data_t launch_lookup_data = | |
| 245 launch_data_dict_lookup(launch_data, LAUNCH_JOBKEY_LABEL); | |
| 246 ASSERT_TRUE(launch_lookup_data); | |
| 247 ASSERT_EQ(LAUNCH_DATA_STRING, launch_data_get_type(launch_lookup_data)); | |
| 248 EXPECT_STREQ("com.example.job.rebooter", | |
| 249 launch_data_get_string(launch_lookup_data)); | |
| 250 | |
| 251 launch_lookup_data = | |
| 252 launch_data_dict_lookup(launch_data, LAUNCH_JOBKEY_ONDEMAND); | |
| 253 ASSERT_TRUE(launch_lookup_data); | |
| 254 ASSERT_EQ(LAUNCH_DATA_BOOL, launch_data_get_type(launch_lookup_data)); | |
| 255 EXPECT_TRUE(launch_data_get_bool(launch_lookup_data)); | |
| 256 | |
| 257 launch_lookup_data = | |
| 258 launch_data_dict_lookup(launch_data, LAUNCH_JOBKEY_PROGRAMARGUMENTS); | |
| 259 ASSERT_TRUE(launch_lookup_data); | |
| 260 ASSERT_EQ(LAUNCH_DATA_ARRAY, launch_data_get_type(launch_lookup_data)); | |
| 261 EXPECT_EQ(3u, launch_data_array_get_count(launch_lookup_data)); | |
| 262 | |
| 263 launch_data_t launch_sublookup_data = | |
| 264 launch_data_array_get_index(launch_lookup_data, 0); | |
| 265 ASSERT_TRUE(launch_sublookup_data); | |
| 266 ASSERT_EQ(LAUNCH_DATA_STRING, launch_data_get_type(launch_sublookup_data)); | |
| 267 EXPECT_STREQ("/bin/bash", launch_data_get_string(launch_sublookup_data)); | |
| 268 | |
| 269 launch_sublookup_data = launch_data_array_get_index(launch_lookup_data, 1); | |
| 270 ASSERT_TRUE(launch_sublookup_data); | |
| 271 ASSERT_EQ(LAUNCH_DATA_STRING, launch_data_get_type(launch_sublookup_data)); | |
| 272 EXPECT_STREQ("-c", launch_data_get_string(launch_sublookup_data)); | |
| 273 | |
| 274 launch_sublookup_data = launch_data_array_get_index(launch_lookup_data, 2); | |
| 275 ASSERT_TRUE(launch_sublookup_data); | |
| 276 ASSERT_EQ(LAUNCH_DATA_STRING, launch_data_get_type(launch_sublookup_data)); | |
| 277 EXPECT_STREQ("/sbin/reboot", launch_data_get_string(launch_sublookup_data)); | |
| 278 | |
| 279 launch_lookup_data = | |
| 280 launch_data_dict_lookup(launch_data, LAUNCH_JOBKEY_MACHSERVICES); | |
| 281 ASSERT_TRUE(launch_lookup_data); | |
| 282 ASSERT_EQ(LAUNCH_DATA_DICTIONARY, launch_data_get_type(launch_lookup_data)); | |
| 283 EXPECT_EQ(1u, launch_data_dict_get_count(launch_lookup_data)); | |
| 284 | |
| 285 launch_sublookup_data = launch_data_dict_lookup( | |
| 286 launch_lookup_data, "com.example.service.rebooter"); | |
| 287 ASSERT_TRUE(launch_sublookup_data); | |
| 288 ASSERT_EQ(LAUNCH_DATA_BOOL, launch_data_get_type(launch_sublookup_data)); | |
| 289 EXPECT_TRUE(launch_data_get_bool(launch_sublookup_data)); | |
| 290 } | |
| 291 | |
| 292 } // namespace | |
| OLD | NEW |