aboutsummaryrefslogtreecommitdiff
path: root/tools/aconfig/aconfig_storage_write_api/tests/storage_write_api_test.cpp
blob: bd39e9e1dedb189581a453fa56ee53a04e3dcaf3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
 * Copyright (C) 2024 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <string>
#include <vector>
#include <cstdio>

#include <sys/stat.h>
#include "aconfig_storage/aconfig_storage_read_api.hpp"
#include "aconfig_storage/aconfig_storage_write_api.hpp"
#include <gtest/gtest.h>
#include <protos/aconfig_storage_metadata.pb.h>
#include <android-base/file.h>
#include <android-base/result.h>

using android::aconfig_storage_metadata::storage_files;
using namespace android::base;

namespace api = aconfig_storage;
namespace private_api = aconfig_storage::private_internal_api;

class AconfigStorageTest : public ::testing::Test {
 protected:
  Result<std::string> copy_to_rw_temp_file(std::string const& source_file) {
    auto temp_file = std::string(std::tmpnam(nullptr));
    auto content = std::string();
    if (!ReadFileToString(source_file, &content)) {
      return Error() << "failed to read file: " << source_file;
    }
    if (!WriteStringToFile(content, temp_file)) {
      return Error() << "failed to copy file: " << source_file;
    }
    if (chmod(temp_file.c_str(),
              S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH) == -1) {
      return Error() << "failed to chmod";
    }
    return temp_file;
  }

  void SetUp() override {
    auto const test_dir = android::base::GetExecutableDirectory();
    flag_val = *copy_to_rw_temp_file(test_dir + "/flag.val");
    flag_info = *copy_to_rw_temp_file(test_dir + "/flag.info");
  }

  void TearDown() override {
    std::remove(flag_val.c_str());
    std::remove(flag_info.c_str());
  }

  std::string flag_val;
  std::string flag_info;
};

/// Negative test to lock down the error when mapping a non writeable storage file
TEST_F(AconfigStorageTest, test_non_writable_storage_file_mapping) {
  ASSERT_TRUE(chmod(flag_val.c_str(), S_IRUSR | S_IRGRP | S_IROTH) != -1);
  auto mapped_file_result = api::map_mutable_storage_file(flag_val);
  ASSERT_FALSE(mapped_file_result.ok());
  auto it = mapped_file_result.error().message().find("cannot map nonwriteable file");
  ASSERT_TRUE(it != std::string::npos) << mapped_file_result.error().message();
}

/// Test to lock down storage flag value update api
TEST_F(AconfigStorageTest, test_boolean_flag_value_update) {
  auto mapped_file_result = api::map_mutable_storage_file(flag_val);
  ASSERT_TRUE(mapped_file_result.ok());

  auto mapped_file = *mapped_file_result;
  for (int offset = 0; offset < 8; ++offset) {
    auto update_result = api::set_boolean_flag_value(mapped_file, offset, true);
    ASSERT_TRUE(update_result.ok());
    auto ro_mapped_file = api::MappedStorageFile();
    ro_mapped_file.file_ptr = mapped_file.file_ptr;
    ro_mapped_file.file_size = mapped_file.file_size;
    auto value = api::get_boolean_flag_value(ro_mapped_file, offset);
    ASSERT_TRUE(value.ok());
    ASSERT_TRUE(*value);
  }
}

/// Negative test to lock down the error when querying flag value out of range
TEST_F(AconfigStorageTest, test_invalid_boolean_flag_value_update) {
  auto mapped_file_result = api::map_mutable_storage_file(flag_val);
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = *mapped_file_result;

  auto update_result = api::set_boolean_flag_value(mapped_file, 8, true);
  ASSERT_FALSE(update_result.ok());
  ASSERT_EQ(update_result.error().message(),
            std::string("InvalidStorageFileOffset(Flag value offset goes beyond the end of the file.)"));
}

/// Test to lock down storage flag has server override update api
TEST_F(AconfigStorageTest, test_flag_has_server_override_update) {
  auto mapped_file_result = api::map_mutable_storage_file(flag_info);
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = *mapped_file_result;

  for (int offset = 0; offset < 8; ++offset) {
    auto update_result = api::set_flag_has_server_override(
        mapped_file, api::FlagValueType::Boolean, offset, true);
    ASSERT_TRUE(update_result.ok()) << update_result.error();
    auto ro_mapped_file = api::MappedStorageFile();
    ro_mapped_file.file_ptr = mapped_file.file_ptr;
    ro_mapped_file.file_size = mapped_file.file_size;
    auto attribute = api::get_flag_attribute(
        ro_mapped_file, api::FlagValueType::Boolean, offset);
    ASSERT_TRUE(attribute.ok());
    ASSERT_TRUE(*attribute & api::FlagInfoBit::HasServerOverride);

    update_result = api::set_flag_has_server_override(
        mapped_file, api::FlagValueType::Boolean, offset, false);
    ASSERT_TRUE(update_result.ok());
    ro_mapped_file.file_ptr = mapped_file.file_ptr;
    ro_mapped_file.file_size = mapped_file.file_size;
    attribute = api::get_flag_attribute(
        ro_mapped_file, api::FlagValueType::Boolean, offset);
    ASSERT_TRUE(attribute.ok());
    ASSERT_FALSE(*attribute & api::FlagInfoBit::HasServerOverride);
  }
}

/// Test to lock down storage flag has local override update api
TEST_F(AconfigStorageTest, test_flag_has_local_override_update) {
  auto mapped_file_result = api::map_mutable_storage_file(flag_info);
  ASSERT_TRUE(mapped_file_result.ok());
  auto mapped_file = *mapped_file_result;

  for (int offset = 0; offset < 8; ++offset) {
    auto update_result = api::set_flag_has_local_override(
        mapped_file, api::FlagValueType::Boolean, offset, true);
    ASSERT_TRUE(update_result.ok());
    auto ro_mapped_file = api::MappedStorageFile();
    ro_mapped_file.file_ptr = mapped_file.file_ptr;
    ro_mapped_file.file_size = mapped_file.file_size;
    auto attribute = api::get_flag_attribute(
        ro_mapped_file, api::FlagValueType::Boolean, offset);
    ASSERT_TRUE(attribute.ok());
    ASSERT_TRUE(*attribute & api::FlagInfoBit::HasLocalOverride);

    update_result = api::set_flag_has_local_override(
        mapped_file, api::FlagValueType::Boolean, offset, false);
    ASSERT_TRUE(update_result.ok());
    ro_mapped_file.file_ptr = mapped_file.file_ptr;
    ro_mapped_file.file_size = mapped_file.file_size;
    attribute = api::get_flag_attribute(
        ro_mapped_file, api::FlagValueType::Boolean, offset);
    ASSERT_TRUE(attribute.ok());
    ASSERT_FALSE(*attribute & api::FlagInfoBit::HasLocalOverride);
  }
}