bolar_2 backup
This commit is contained in:
141
host_tests/ble_provisioning_codec_test.cc
Normal file
141
host_tests/ble_provisioning_codec_test.cc
Normal file
@@ -0,0 +1,141 @@
|
||||
#include "provisioning/ble_provisioning_codec.h"
|
||||
|
||||
#include <array>
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace borla;
|
||||
|
||||
class ChunkCollector final : public IBleChunkVisitor {
|
||||
public:
|
||||
bool OnBleChunk(const uint8_t* data, size_t size) override
|
||||
{
|
||||
chunks.emplace_back(data, data + size);
|
||||
return true;
|
||||
}
|
||||
|
||||
std::vector<std::vector<uint8_t>> chunks;
|
||||
};
|
||||
|
||||
void TestCredentialAssemblyAndValidation()
|
||||
{
|
||||
BleChunkAssembler assembler;
|
||||
const std::string json =
|
||||
R"({"ssid":"Home","password":"secret123"})";
|
||||
const std::array<uint8_t, 12> first{
|
||||
0, 0, '{', '"', 's', 's', 'i', 'd', '"', ':', '"', 'H'};
|
||||
std::vector<uint8_t> second{1, 1};
|
||||
second.insert(
|
||||
second.end(), json.begin() + 10, json.end());
|
||||
|
||||
assert(
|
||||
assembler.Push(first.data(), first.size()) ==
|
||||
BleChunkAssemblyResult::kAccepted);
|
||||
assert(
|
||||
assembler.Push(second.data(), second.size()) ==
|
||||
BleChunkAssemblyResult::kComplete);
|
||||
WifiCredential credential;
|
||||
assert(ParseProvisioningCredential(
|
||||
assembler.data(), assembler.size(), &credential));
|
||||
assert(std::strcmp(credential.ssid.data(), "Home") == 0);
|
||||
assert(std::strcmp(
|
||||
credential.password.data(), "secret123") == 0);
|
||||
|
||||
const std::array<uint8_t, 3> out_of_order{2, 1, 'x'};
|
||||
assert(
|
||||
assembler.Push(
|
||||
out_of_order.data(), out_of_order.size()) ==
|
||||
BleChunkAssemblyResult::kInvalid);
|
||||
}
|
||||
|
||||
void TestChunkRoundTripAtDefaultMtu()
|
||||
{
|
||||
const std::string json(97, 'x');
|
||||
ChunkCollector collector;
|
||||
assert(VisitBleNotificationChunks(
|
||||
json.data(), json.size(), 23, collector));
|
||||
assert(collector.chunks.size() == 6);
|
||||
|
||||
BleChunkAssembler assembler;
|
||||
for (size_t index = 0;
|
||||
index < collector.chunks.size();
|
||||
++index) {
|
||||
const auto& chunk = collector.chunks[index];
|
||||
const auto expected =
|
||||
index + 1 == collector.chunks.size()
|
||||
? BleChunkAssemblyResult::kComplete
|
||||
: BleChunkAssemblyResult::kAccepted;
|
||||
assert(assembler.Push(chunk.data(), chunk.size()) == expected);
|
||||
}
|
||||
assert(assembler.size() == json.size());
|
||||
assert(std::memcmp(
|
||||
assembler.data(), json.data(), json.size()) == 0);
|
||||
}
|
||||
|
||||
void TestJsonContractsHidePasswords()
|
||||
{
|
||||
DeviceIdentity identity;
|
||||
std::strcpy(identity.mac, "AA:BB:CC:DD:EE:FF");
|
||||
std::strcpy(identity.app_version, "2.0.0-dev");
|
||||
std::strcpy(identity.board, "borla_s3");
|
||||
std::array<char, 512> output{};
|
||||
size_t size = 0;
|
||||
assert(BuildProvisioningDeviceInfoJson(
|
||||
identity, output.data(), output.size(), &size));
|
||||
assert(std::strstr(output.data(), "AA:BB:CC:DD:EE:FF"));
|
||||
assert(std::strstr(output.data(), "borla_s3"));
|
||||
|
||||
std::array<WifiCredential, 2> saved{};
|
||||
assert(saved[0].Set("Home", "top-secret"));
|
||||
assert(saved[1].Set("Office", "also-secret"));
|
||||
assert(BuildProvisioningSavedWifiJson(
|
||||
saved.data(), saved.size(),
|
||||
output.data(), output.size(), &size));
|
||||
assert(std::strstr(output.data(), "\"ssid\":\"Home\""));
|
||||
assert(std::strstr(output.data(), "\"index\":1"));
|
||||
assert(std::strstr(output.data(), "secret") == nullptr);
|
||||
assert(std::strstr(output.data(), "password") == nullptr);
|
||||
}
|
||||
|
||||
void TestScanAndResultJson()
|
||||
{
|
||||
std::array<WifiNetwork, 2> networks{};
|
||||
std::strcpy(networks[0].ssid.data(), "Home");
|
||||
networks[0].rssi_dbm = -42;
|
||||
networks[0].auth_mode = 3;
|
||||
std::strcpy(networks[1].ssid.data(), "Guest");
|
||||
networks[1].rssi_dbm = -70;
|
||||
|
||||
std::array<char, 512> output{};
|
||||
size_t size = 0;
|
||||
assert(BuildProvisioningScanJson(
|
||||
networks.data(), networks.size(),
|
||||
output.data(), output.size(), &size));
|
||||
assert(std::strstr(output.data(), "\"rssi\":-42"));
|
||||
assert(std::strstr(output.data(), "\"auth\":3"));
|
||||
|
||||
assert(BuildProvisioningResultJson(
|
||||
false, "password_wrong",
|
||||
output.data(), output.size(), &size));
|
||||
assert(std::strcmp(
|
||||
output.data(),
|
||||
R"({"success":false,"error":"password_wrong"})") == 0);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
TestCredentialAssemblyAndValidation();
|
||||
TestChunkRoundTripAtDefaultMtu();
|
||||
TestJsonContractsHidePasswords();
|
||||
TestScanAndResultJson();
|
||||
std::cout << "BLE provisioning codec tests passed\n";
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user