-
Notifications
You must be signed in to change notification settings - Fork 904
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eac7986
commit 8dee132
Showing
12 changed files
with
237 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* Copyright (c) 2021 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include <memory> | ||
#include <utility> | ||
|
||
#include "base/json/json_reader.h" | ||
#include "base/json/json_writer.h" | ||
|
||
#include "brave/components/services/ipfs/ipfs_service_utils.h" | ||
|
||
#include "brave/components/services/ipfs/public/mojom/ipfs_service.mojom.h" | ||
|
||
namespace ipfs { | ||
|
||
// Updates the ipfs node config to meet current preferences | ||
bool UpdateConfigJSON(const std::string& source, | ||
const ipfs::mojom::IpfsConfig* config, | ||
std::string* result) { | ||
base::JSONReader::ValueWithError value_with_error = | ||
base::JSONReader::ReadAndReturnValueWithError( | ||
source, base::JSONParserOptions::JSON_PARSE_RFC); | ||
base::Optional<base::Value>& records_v = value_with_error.value; | ||
if (!records_v) { | ||
VLOG(1) << "Could not parse JSON, JSON is: " << source; | ||
return false; | ||
} | ||
base::DictionaryValue* dict = nullptr; | ||
if (!records_v->GetAsDictionary(&dict)) { | ||
VLOG(1) << "Could not parse JSON, JSON is: " << source; | ||
return false; | ||
} | ||
dict->Set("Addresses.API", std::make_unique<base::Value>( | ||
"/ip4/127.0.0.1/tcp/" + config->api_port)); | ||
dict->Set("Addresses.Gateway", | ||
std::make_unique<base::Value>("/ip4/127.0.0.1/tcp/" + | ||
config->gateway_port)); | ||
dict->Set("Datastore.GCPeriod", std::make_unique<base::Value>("1h")); | ||
dict->Set("Swarm.ConnMgr.LowWater", std::make_unique<base::Value>(50)); | ||
dict->Set("Swarm.ConnMgr.HighWater", std::make_unique<base::Value>(300)); | ||
dict->Set("Datastore.StorageMax", | ||
std::make_unique<base::Value>(config->storage_max)); | ||
std::unique_ptr<base::ListValue> list = std::make_unique<base::ListValue>(); | ||
list->Append(base::Value("/ip4/0.0.0.0/tcp/" + config->swarm_port)); | ||
list->Append(base::Value("/ip6/::/tcp/" + config->swarm_port)); | ||
dict->Set("Addresses.Swarm", std::move(list)); | ||
std::string json_string; | ||
if (!base::JSONWriter::Write(records_v.value(), &json_string) || | ||
json_string.empty()) { | ||
return false; | ||
} | ||
*result = json_string; | ||
return true; | ||
} | ||
|
||
} // namespace ipfs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* Copyright (c) 2021 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#ifndef BRAVE_COMPONENTS_SERVICES_IPFS_IPFS_SERVICE_UTILS_H_ | ||
#define BRAVE_COMPONENTS_SERVICES_IPFS_IPFS_SERVICE_UTILS_H_ | ||
|
||
#include <string> | ||
|
||
namespace ipfs { | ||
|
||
namespace mojom { | ||
class IpfsConfig; | ||
} | ||
|
||
// Updates the ipfs node config to meet current preferences | ||
bool UpdateConfigJSON(const std::string& source, | ||
const ipfs::mojom::IpfsConfig* config, | ||
std::string* result); | ||
|
||
} // namespace ipfs | ||
|
||
#endif // BRAVE_COMPONENTS_SERVICES_IPFS_IPFS_SERVICE_UTILS_H_ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* Copyright (c) 2021 The Brave Authors. All rights reserved. | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
* You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
#include <memory> | ||
|
||
#include "base/json/json_reader.h" | ||
#include "base/json/json_writer.h" | ||
|
||
#include "brave/components/services/ipfs/ipfs_service_utils.h" | ||
|
||
#include "brave/components/services/ipfs/public/mojom/ipfs_service.mojom.h" | ||
#include "testing/gtest/include/gtest/gtest.h" | ||
|
||
namespace ipfs { | ||
|
||
typedef testing::Test IPFSServiceUtils; | ||
|
||
TEST_F(IPFSServiceUtils, UpdateConfigJSONTest) { | ||
std::string json = R"({})"; | ||
std::string updated; | ||
auto config = ipfs::mojom::IpfsConfig::New( | ||
base::FilePath(), base::FilePath(), base::FilePath(), "GatewayPort", | ||
"APIPort", "SwarmPort", "StorageSize"); | ||
|
||
std::string expect = | ||
"{\"Addresses\":{\"API\":\"/ip4/127.0.0.1/tcp/APIPort\"," | ||
"\"Gateway\":\"/ip4/127.0.0.1/tcp/GatewayPort\",\"Swarm\":" | ||
"[\"/ip4/0.0.0.0/tcp/SwarmPort\",\"/ip6/::/tcp/SwarmPort\"" | ||
"]},\"Datastore\":{\"GCPeriod\":\"1h\",\"StorageMax\":" | ||
"\"StorageSize\"},\"Swarm\":{\"ConnMgr\":{\"HighWater\"" | ||
":300,\"LowWater\":50}}}"; | ||
ASSERT_TRUE(UpdateConfigJSON(json, config.get(), &updated)); | ||
EXPECT_EQ(updated, expect); | ||
updated.clear(); | ||
json = R"({ | ||
"Addresses":{ | ||
"API":"/ip4/127.0.0.1/tcp/111", | ||
"Gateway":"/ip4/127.0.0.1/tcp/111", | ||
"Swarm":[ | ||
"/ip4/0.0.0.0/tcp/222", | ||
"/ip6/::/tcp/222", | ||
"/ip6/::/udp/666" | ||
] | ||
}, | ||
"Datastore":{ | ||
"GCPeriod":"6h", | ||
"StorageMax":"9GB" | ||
}, | ||
"Swarm":{ | ||
"ConnMgr":{ | ||
"HighWater":0, | ||
"LowWater":0 | ||
} | ||
} | ||
})"; | ||
ASSERT_TRUE(UpdateConfigJSON(json, config.get(), &updated)); | ||
EXPECT_EQ(updated, expect); | ||
updated.clear(); | ||
ASSERT_FALSE(UpdateConfigJSON("{", config.get(), &updated)); | ||
EXPECT_EQ(updated, ""); | ||
updated.clear(); | ||
ASSERT_FALSE(UpdateConfigJSON("[]", config.get(), &updated)); | ||
EXPECT_EQ(updated, ""); | ||
} | ||
|
||
} // namespace ipfs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Copyright (c) 2021 The Brave Authors. All rights reserved. | ||
# This Source Code Form is subject to the terms of the Mozilla Public | ||
# License, v. 2.0. If a copy of the MPL was not distributed with this file, | ||
# You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
import("//brave/build/config.gni") | ||
import("//brave/components/ipfs/buildflags/buildflags.gni") | ||
import("//testing/test.gni") | ||
|
||
source_set("ipfs_service_unit_tests") { | ||
testonly = true | ||
if (ipfs_enabled) { | ||
sources = | ||
[ "//brave/components/services/ipfs/ipfs_service_utils_unittest.cc" ] | ||
|
||
deps = [ | ||
"//base/test:test_support", | ||
"//brave/components/services/ipfs", | ||
"//brave/components/services/ipfs/public/mojom", | ||
"//testing/gtest", | ||
] | ||
} # if (ipfs_enabled) | ||
} # source_set("ipfs_service_unit_tests") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters