1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <cutils/str_parms.h>
18#include <gtest/gtest.h>
19
20static void test_str_parms_str(const char* str, const char* expected) {
21    str_parms* str_parms = str_parms_create_str(str);
22    str_parms_add_str(str_parms, "dude", "woah");
23    str_parms_add_str(str_parms, "dude", "woah");
24    str_parms_del(str_parms, "dude");
25    str_parms_dump(str_parms);
26    char* out_str = str_parms_to_str(str_parms);
27    str_parms_destroy(str_parms);
28    ASSERT_STREQ(expected, out_str) << str;
29    free(out_str);
30}
31
32TEST(str_parms, smoke) {
33    test_str_parms_str("", "");
34    test_str_parms_str(";", "");
35    test_str_parms_str("=", "");
36    test_str_parms_str("=;", "");
37    test_str_parms_str("=bar", "");
38    test_str_parms_str("=bar;", "");
39    test_str_parms_str("foo=", "foo=");
40    test_str_parms_str("foo=;", "foo=");
41    test_str_parms_str("foo=bar", "foo=bar");
42    test_str_parms_str("foo=bar;", "foo=bar");
43    test_str_parms_str("foo=bar;baz", "foo=bar;baz=");
44    test_str_parms_str("foo=bar;baz=", "foo=bar;baz=");
45    test_str_parms_str("foo=bar;baz=bat", "foo=bar;baz=bat");
46    test_str_parms_str("foo=bar;baz=bat;", "foo=bar;baz=bat");
47    test_str_parms_str("foo=bar1;baz=bat;foo=bar2", "foo=bar2;baz=bat");
48}
49
50TEST(str_parms, put_ENOMEM) {
51    // hashmapPut reports errors by setting errno to ENOMEM.
52    // Test that we're not confused by running in an environment where this is already true.
53    errno = ENOMEM;
54    test_str_parms_str("foo=bar;baz=", "foo=bar;baz=");
55    ASSERT_EQ(ENOMEM, errno);
56    test_str_parms_str("foo=bar;baz=", "foo=bar;baz=");
57}
58