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