1b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross/*
2b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross * Copyright (C) 2013 The Android Open Source Project
3b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross *
4b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross * Licensed under the Apache License, Version 2.0 (the "License");
5b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross * you may not use this file except in compliance with the License.
6b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross * You may obtain a copy of the License at
7b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross *
8b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross *      http://www.apache.org/licenses/LICENSE-2.0
9b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross *
10b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross * Unless required by applicable law or agreed to in writing, software
11b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross * distributed under the License is distributed on an "AS IS" BASIS,
12b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross * See the License for the specific language governing permissions and
14b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross * limitations under the License.
15b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross */
16b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
17b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross#include <gtest/gtest.h>
18b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross#include <sys/wait.h>
19e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes#include <errno.h>
20cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann#include <unistd.h>
21cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann#include <string>
22b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
23f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
24b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
25b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
26b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross#include <sys/_system_properties.h>
27b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
281540f601be32bdd4af8e8c13bdf2bc06bdaa76f1Greg Hackmannextern void *__system_property_area__;
29b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
30b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Crossstruct LocalPropertyTestState {
31cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    LocalPropertyTestState() : valid(false) {
32e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes        const char* ANDROID_DATA = getenv("ANDROID_DATA");
33e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes        char dir_template[PATH_MAX];
34e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes        snprintf(dir_template, sizeof(dir_template), "%s/local/tmp/prop-XXXXXX", ANDROID_DATA);
35e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes        char* dirname = mkdtemp(dir_template);
36cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        if (!dirname) {
37e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes            fprintf(stderr, "making temp file for test state failed (is %s writable?): %s",
38e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes                    dir_template, strerror(errno));
39cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann            return;
40cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        }
41cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann
421540f601be32bdd4af8e8c13bdf2bc06bdaa76f1Greg Hackmann        old_pa = __system_property_area__;
431540f601be32bdd4af8e8c13bdf2bc06bdaa76f1Greg Hackmann        __system_property_area__ = NULL;
44cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann
45cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        pa_dirname = dirname;
46cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        pa_filename = pa_dirname + "/__properties__";
47cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann
48cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        __system_property_set_filename(pa_filename.c_str());
49cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        __system_property_area_init();
50cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        valid = true;
51b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    }
52b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
53b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ~LocalPropertyTestState() {
54e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes        if (!valid) {
55cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann            return;
56e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes        }
57cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann
581540f601be32bdd4af8e8c13bdf2bc06bdaa76f1Greg Hackmann        __system_property_area__ = old_pa;
59cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann
60cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        __system_property_set_filename(PROP_FILENAME);
61cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        unlink(pa_filename.c_str());
62cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        rmdir(pa_dirname.c_str());
63b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    }
64cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmannpublic:
65cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    bool valid;
66b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Crossprivate:
67cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    std::string pa_dirname;
68cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    std::string pa_filename;
691540f601be32bdd4af8e8c13bdf2bc06bdaa76f1Greg Hackmann    void *old_pa;
70b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross};
71b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
72f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferrisstatic void foreach_test_callback(const prop_info *pi, void* cookie) {
73f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    size_t *count = static_cast<size_t *>(cookie);
74f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
75f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_NE((prop_info *)NULL, pi);
76f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    (*count)++;
77f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris}
78f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
79f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferrisstatic void hierarchical_test_callback(const prop_info *pi, void *cookie) {
80f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    bool (*ok)[8][8] = static_cast<bool (*)[8][8]>(cookie);
81f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
82f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    char name[PROP_NAME_MAX];
83f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    char value[PROP_VALUE_MAX];
84f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
85f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    __system_property_read(pi, name, value);
86f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
87f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    int name_i, name_j, name_k;
88f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    int value_i, value_j, value_k;
89f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_EQ(3, sscanf(name, "property_%d.%d.%d", &name_i, &name_j, &name_k));
90f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_EQ(3, sscanf(value, "value_%d.%d.%d", &value_i, &value_j, &value_k));
91f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_EQ(name_i, value_i);
92f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_GE(name_i, 0);
93f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_LT(name_i, 8);
94f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_EQ(name_j, value_j);
95f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_GE(name_j, 0);
96f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_LT(name_j, 8);
97f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_EQ(name_k, value_k);
98f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_GE(name_k, 0);
99f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ASSERT_LT(name_k, 8);
100f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
101f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    ok[name_i][name_j][name_k] = true;
102f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris}
103f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
104f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferrisstatic void *PropertyWaitHelperFn(void *arg) {
105f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    int *flag = (int *)arg;
106f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    prop_info *pi;
107f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    pi = (prop_info *)__system_property_find("property");
108f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    usleep(100000);
109f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
110f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    *flag = 1;
111f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    __system_property_update(pi, "value3", 6);
112f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
113f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    return NULL;
114f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris}
115f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
116f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
117f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris
118b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin CrossTEST(properties, add) {
119f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
120b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    LocalPropertyTestState pa;
121cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    ASSERT_TRUE(pa.valid);
122b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
123b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    char propvalue[PROP_VALUE_MAX];
124b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
125b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("property", 8, "value1", 6));
126b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("other_property", 14, "value2", 6));
127b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("property_other", 14, "value3", 6));
128b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
129b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(6, __system_property_get("property", propvalue));
130b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_STREQ(propvalue, "value1");
131b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
132b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(6, __system_property_get("other_property", propvalue));
133b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_STREQ(propvalue, "value2");
134b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
135b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(6, __system_property_get("property_other", propvalue));
136b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_STREQ(propvalue, "value3");
137f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
138f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    GTEST_LOG_(INFO) << "This test does nothing.\n";
139f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
140b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross}
141b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
142b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin CrossTEST(properties, update) {
143f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
144b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    LocalPropertyTestState pa;
145cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    ASSERT_TRUE(pa.valid);
146b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
147b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    char propvalue[PROP_VALUE_MAX];
148b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    prop_info *pi;
149b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
150b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("property", 8, "oldvalue1", 9));
151b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("other_property", 14, "value2", 6));
152b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("property_other", 14, "value3", 6));
153b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
154b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    pi = (prop_info *)__system_property_find("property");
155b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_NE((prop_info *)NULL, pi);
156b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    __system_property_update(pi, "value4", 6);
157b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
158b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    pi = (prop_info *)__system_property_find("other_property");
159b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_NE((prop_info *)NULL, pi);
160b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    __system_property_update(pi, "newvalue5", 9);
161b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
162b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    pi = (prop_info *)__system_property_find("property_other");
163b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_NE((prop_info *)NULL, pi);
164b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    __system_property_update(pi, "value6", 6);
165b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
166b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(6, __system_property_get("property", propvalue));
167b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_STREQ(propvalue, "value4");
168b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
169b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(9, __system_property_get("other_property", propvalue));
170b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_STREQ(propvalue, "newvalue5");
171b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
172b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(6, __system_property_get("property_other", propvalue));
173b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_STREQ(propvalue, "value6");
174f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
175f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    GTEST_LOG_(INFO) << "This test does nothing.\n";
176f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
177b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross}
178b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
179cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg HackmannTEST(properties, fill) {
180f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
181b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    LocalPropertyTestState pa;
182cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    ASSERT_TRUE(pa.valid);
183b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    char prop_name[PROP_NAME_MAX];
184b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    char prop_value[PROP_VALUE_MAX];
185b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    char prop_value_ret[PROP_VALUE_MAX];
186cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    int count = 0;
187b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    int ret;
188b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
189cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    while (true) {
190cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        ret = snprintf(prop_name, PROP_NAME_MAX - 1, "property_%d", count);
191b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        memset(prop_name + ret, 'a', PROP_NAME_MAX - 1 - ret);
192cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        ret = snprintf(prop_value, PROP_VALUE_MAX - 1, "value_%d", count);
193b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        memset(prop_value + ret, 'b', PROP_VALUE_MAX - 1 - ret);
194b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        prop_name[PROP_NAME_MAX - 1] = 0;
195b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        prop_value[PROP_VALUE_MAX - 1] = 0;
196b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
197cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        ret = __system_property_add(prop_name, PROP_NAME_MAX - 1, prop_value, PROP_VALUE_MAX - 1);
198cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        if (ret < 0)
199cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann            break;
200cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann
201cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann        count++;
202b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    }
203b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
204cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    // For historical reasons at least 247 properties must be supported
205cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    ASSERT_GE(count, 247);
206cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann
207cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    for (int i = 0; i < count; i++) {
208b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        ret = snprintf(prop_name, PROP_NAME_MAX - 1, "property_%d", i);
209b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        memset(prop_name + ret, 'a', PROP_NAME_MAX - 1 - ret);
210b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        ret = snprintf(prop_value, PROP_VALUE_MAX - 1, "value_%d", i);
211b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        memset(prop_value + ret, 'b', PROP_VALUE_MAX - 1 - ret);
212b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        prop_name[PROP_NAME_MAX - 1] = 0;
213b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        prop_value[PROP_VALUE_MAX - 1] = 0;
214b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        memset(prop_value_ret, '\0', PROP_VALUE_MAX);
215b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
216b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        ASSERT_EQ(PROP_VALUE_MAX - 1, __system_property_get(prop_name, prop_value_ret));
217b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        ASSERT_EQ(0, memcmp(prop_value, prop_value_ret, PROP_VALUE_MAX));
218b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    }
219f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
220f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    GTEST_LOG_(INFO) << "This test does nothing.\n";
221f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
222c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann}
223c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann
224c6ff844d75dddfb19fc804b8add2d6a79947b101Greg HackmannTEST(properties, foreach) {
225f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
226c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann    LocalPropertyTestState pa;
227cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    ASSERT_TRUE(pa.valid);
228c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann    size_t count = 0;
229c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann
230c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann    ASSERT_EQ(0, __system_property_add("property", 8, "value1", 6));
231c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann    ASSERT_EQ(0, __system_property_add("other_property", 14, "value2", 6));
232c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann    ASSERT_EQ(0, __system_property_add("property_other", 14, "value3", 6));
233c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann
234c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann    ASSERT_EQ(0, __system_property_foreach(foreach_test_callback, &count));
235c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann    ASSERT_EQ(3U, count);
236f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
237f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    GTEST_LOG_(INFO) << "This test does nothing.\n";
238f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
239c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann}
240c6ff844d75dddfb19fc804b8add2d6a79947b101Greg Hackmann
241b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin CrossTEST(properties, find_nth) {
242f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
243b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    LocalPropertyTestState pa;
244cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    ASSERT_TRUE(pa.valid);
245b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
246b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("property", 8, "value1", 6));
247b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("other_property", 14, "value2", 6));
248b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("property_other", 14, "value3", 6));
249b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
250b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_NE((const prop_info *)NULL, __system_property_find_nth(0));
251b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_NE((const prop_info *)NULL, __system_property_find_nth(1));
252b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_NE((const prop_info *)NULL, __system_property_find_nth(2));
253b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
254b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(3));
255b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(4));
256b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(5));
257b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(100));
258b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(200));
259b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ((const prop_info *)NULL, __system_property_find_nth(247));
260f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
261f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    GTEST_LOG_(INFO) << "This test does nothing.\n";
262f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
263d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann}
264d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann
265d5276422ca9f1f4d45e91c189a1655521e91962dGreg HackmannTEST(properties, fill_hierarchical) {
266f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
267d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    LocalPropertyTestState pa;
268d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    ASSERT_TRUE(pa.valid);
269d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    char prop_name[PROP_NAME_MAX];
270d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    char prop_value[PROP_VALUE_MAX];
271d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    char prop_value_ret[PROP_VALUE_MAX];
272d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    int ret;
273d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann
274d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    for (int i = 0; i < 8; i++) {
275d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann        for (int j = 0; j < 8; j++) {
276d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann            for (int k = 0; k < 8; k++) {
277d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                ret = snprintf(prop_name, PROP_NAME_MAX - 1, "property_%d.%d.%d", i, j, k);
278d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                memset(prop_name + ret, 'a', PROP_NAME_MAX - 1 - ret);
279d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                ret = snprintf(prop_value, PROP_VALUE_MAX - 1, "value_%d.%d.%d", i, j, k);
280d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                memset(prop_value + ret, 'b', PROP_VALUE_MAX - 1 - ret);
281d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                prop_name[PROP_NAME_MAX - 1] = 0;
282d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                prop_value[PROP_VALUE_MAX - 1] = 0;
283d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann
284d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                ASSERT_EQ(0, __system_property_add(prop_name, PROP_NAME_MAX - 1, prop_value, PROP_VALUE_MAX - 1));
285d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann            }
286d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann        }
287d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    }
288d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann
289d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    for (int i = 0; i < 8; i++) {
290d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann        for (int j = 0; j < 8; j++) {
291d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann            for (int k = 0; k < 8; k++) {
292d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                ret = snprintf(prop_name, PROP_NAME_MAX - 1, "property_%d.%d.%d", i, j, k);
293d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                memset(prop_name + ret, 'a', PROP_NAME_MAX - 1 - ret);
294d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                ret = snprintf(prop_value, PROP_VALUE_MAX - 1, "value_%d.%d.%d", i, j, k);
295d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                memset(prop_value + ret, 'b', PROP_VALUE_MAX - 1 - ret);
296d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                prop_name[PROP_NAME_MAX - 1] = 0;
297d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                prop_value[PROP_VALUE_MAX - 1] = 0;
298d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                memset(prop_value_ret, '\0', PROP_VALUE_MAX);
299d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann
300d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                ASSERT_EQ(PROP_VALUE_MAX - 1, __system_property_get(prop_name, prop_value_ret));
301d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                ASSERT_EQ(0, memcmp(prop_value, prop_value_ret, PROP_VALUE_MAX));
302d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann            }
303d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann        }
304d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    }
305d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann
306d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    bool ok[8][8][8];
307d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    memset(ok, 0, sizeof(ok));
308d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    __system_property_foreach(hierarchical_test_callback, ok);
309d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann
310d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    for (int i = 0; i < 8; i++) {
311d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann        for (int j = 0; j < 8; j++) {
312d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann            for (int k = 0; k < 8; k++) {
313d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann                ASSERT_TRUE(ok[i][j][k]);
314d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann            }
315d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann        }
316d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann    }
317f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
318f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    GTEST_LOG_(INFO) << "This test does nothing.\n";
319f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
320d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann}
321d5276422ca9f1f4d45e91c189a1655521e91962dGreg Hackmann
322b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin CrossTEST(properties, errors) {
323f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
324b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    LocalPropertyTestState pa;
325cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    ASSERT_TRUE(pa.valid);
326b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    char prop_value[PROP_NAME_MAX];
327b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
328b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("property", 8, "value1", 6));
329b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("other_property", 14, "value2", 6));
330b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("property_other", 14, "value3", 6));
331b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
332b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_find("property1"));
333b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_get("property1", prop_value));
334b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
335b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(-1, __system_property_add("name", PROP_NAME_MAX, "value", 5));
336b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(-1, __system_property_add("name", 4, "value", PROP_VALUE_MAX));
337b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(-1, __system_property_update(NULL, "value", PROP_VALUE_MAX));
338f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
339f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    GTEST_LOG_(INFO) << "This test does nothing.\n";
340f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
341b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross}
342b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
343b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin CrossTEST(properties, serial) {
344f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
345b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    LocalPropertyTestState pa;
346cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    ASSERT_TRUE(pa.valid);
347b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    const prop_info *pi;
348b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    unsigned int serial;
349b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
350b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("property", 8, "value1", 6));
351b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_NE((const prop_info *)NULL, pi = __system_property_find("property"));
352b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    serial = __system_property_serial(pi);
353b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_update((prop_info *)pi, "value2", 6));
354b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_NE(serial, __system_property_serial(pi));
355f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
356f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    GTEST_LOG_(INFO) << "This test does nothing.\n";
357f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
358b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross}
359b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
360b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin CrossTEST(properties, wait) {
361f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
362b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    LocalPropertyTestState pa;
363cb215a7e9ecec9feb5aae9d9a5b1c89f392208e7Greg Hackmann    ASSERT_TRUE(pa.valid);
364b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    unsigned int serial;
365b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    prop_info *pi;
366b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    pthread_t t;
367b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    int flag = 0;
368b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
369b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, __system_property_add("property", 8, "value1", 6));
370b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    serial = __system_property_wait_any(0);
371b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    pi = (prop_info *)__system_property_find("property");
372b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_NE((prop_info *)NULL, pi);
373b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    __system_property_update(pi, "value2", 6);
374b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    serial = __system_property_wait_any(serial);
375b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
376b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, pthread_create(&t, NULL, PropertyWaitHelperFn, &flag));
377b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(flag, 0);
378b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    serial = __system_property_wait_any(serial);
379b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(flag, 1);
380b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
381b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    void* result;
382b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    ASSERT_EQ(0, pthread_join(t, &result));
383f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
384f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris    GTEST_LOG_(INFO) << "This test does nothing.\n";
385f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
386b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross}
387b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
388b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Crossclass KilledByFault {
389b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    public:
390b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        explicit KilledByFault() {};
391b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        bool operator()(int exit_status) const;
392b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross};
393b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
394b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Crossbool KilledByFault::operator()(int exit_status) const {
395b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross    return WIFSIGNALED(exit_status) &&
396b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross        (WTERMSIG(exit_status) == SIGSEGV ||
397b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross         WTERMSIG(exit_status) == SIGBUS ||
398b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross         WTERMSIG(exit_status) == SIGABRT);
399b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross}
400b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross
401b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin CrossTEST(properties_DeathTest, read_only) {
402f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#if defined(__BIONIC__)
403e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes  ::testing::FLAGS_gtest_death_test_style = "threadsafe";
404e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes
405e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes  // This test only makes sense if we're talking to the real system property service.
406e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes  struct stat sb;
407e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes  if (stat(PROP_FILENAME, &sb) == -1 && errno == ENOENT) {
408e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes    return;
409e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes  }
410e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes
411e4375196d650f68ad486e2202699c98f9342d616Elliott Hughes  ASSERT_EXIT(__system_property_add("property", 8, "value", 5), KilledByFault(), "");
412f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#else // __BIONIC__
413f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
414f04935c85e0b466f0d30d2cd4c0fa2fff62e7d6dChristopher Ferris#endif // __BIONIC__
415b27e200ad6170ba3163f5ae6ba581bdaabb2e696Colin Cross}
416