fix(#1117): slot 0 is Public's, on every add path

The firmware shipping next (firmware-base, #1896) refuses any channel but
Public in slot 0, even while it is empty, refuses Public in any other slot,
and refuses clearing Public. Every refusal is ERR_CODE_BAD_STATE, the same
code as an occupied slot, so the client has to avoid them, not interpret
them. Owner rulings, 2026-10-28: slot 0 "should always remain public",
"make sure Public can't be deleted", "Of course add the real public".

- decideChannelSlot takes a required isPublic. Public goes to slot 0 when
  it is free; nothing else ever does. The fallback that lets Public take
  another free slot is reached only when slot 0 holds some other channel,
  which happens only on older firmware.
- The shared allocator takes the key being written and decides from it,
  so Join Public, a typed private key, a QR share and a hashtag join all
  follow the rule, and a second Public is refused on every one of them.
- planChannelImport follows the same rule for a stock config file.
- Deleting Public is refused at the attempt with "Public can't be removed
  from the radio. It always stays in slot 0." The control stays live.
- Edit Channel locks Public's key, and refuses typing Public's key into
  another channel while Public is on the radio.
- A hashtag join of "public" (leading # and spaces and trailing spaces
  dropped, any case: the device's namesPublic rule) adds the real Public,
  not sha256("#public").

The #1022 test that asserted Public has no claim on slot 0 is replaced by
tests for the rule above.

On older firmware, slot 0 is no longer handed to other channels, so one
slot of 40 goes unused there. That follows the owner's ruling.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
fix/1896-client-slot0-public
diffwireauto 3 weeks ago
parent 8e345d8b63
commit b16afb35ec

@ -45,16 +45,28 @@ class ChannelSlotDecision {
///
/// An absent index genuinely means a free slot, because the connector never
/// adds an empty channel to its list.
///
/// Slot 0 is Public's (firmware #1896, owner 2026-10-28: slot 0 "should always
/// remain public"). Firmware refuses any other channel there, even while it is
/// empty, so [isPublic] false never gets slot 0. Public goes to slot 0 when it
/// is free; firmware refuses Public anywhere else, so the fallback to another
/// free slot exists only for older firmware, where slot 0 can be held by some
/// other channel. [isPublic] is required so no caller can forget the rule.
ChannelSlotDecision decideChannelSlot({
required List<Channel> channels,
required int maxChannels,
required bool isSyncing,
required bool isPublic,
}) {
if (isSyncing) {
return const ChannelSlotDecision.refused(ChannelSlotRefusal.syncing);
}
final used = channels.map((c) => c.index).toSet();
if (isPublic && !used.contains(Channel.publicSlot)) {
return const ChannelSlotDecision.slot(Channel.publicSlot);
}
for (var i = 0; i < maxChannels; i++) {
if (i == Channel.publicSlot) continue;
if (!used.contains(i)) return ChannelSlotDecision.slot(i);
}
return const ChannelSlotDecision.refused(ChannelSlotRefusal.full);

@ -1,7 +1,10 @@
import 'dart:typed_data';
import 'package:flutter/material.dart';
import '../connector/meshcore_connector.dart';
import '../l10n/l10n.dart';
import '../models/channel.dart';
import 'channel_slot_decision.dart';
import 'snack_bar_builder.dart';
@ -22,10 +25,18 @@ import 'snack_bar_builder.dart';
/// The owner's ruling is that the unsafe write is blocked but the control stays
/// live: a greyed-out button prevents the damage and tells the user nothing. So
/// a refusal returns null *and* says what is wrong.
///
/// [psk] is the key about to be written. Public's key gets slot 0 and nothing
/// else does (#1896), and a second copy of Public is refused, whichever path
/// the key came in by: Join Public, a typed private key, a QR share, or a
/// hashtag join of "public".
int? allocateChannelSlotOrExplain(
BuildContext context,
MeshCoreConnector connector,
) {
MeshCoreConnector connector, {
required Uint8List psk,
}) {
final isPublic = Channel.isPublicKey(psk);
if (isPublic && refusePublicAlreadyOnRadio(context, connector)) return null;
final decision = decideChannelSlot(
channels: connector.channels,
maxChannels: connector.maxChannels,
@ -34,6 +45,7 @@ int? allocateChannelSlotOrExplain(
// impossible on a flaky link. Busy also covers a write the radio has not
// answered yet (#1026): until it does, the list does not show its slot.
isSyncing: connector.isChannelTableBusy,
isPublic: isPublic,
);
switch (decision.refusal) {
case ChannelSlotRefusal.syncing:
@ -81,8 +93,8 @@ bool explainUnconfirmedChannelWrite(BuildContext context, ChannelWrite result) {
/// Refuses a second Public channel, and says so. Returns true if it refused.
///
/// Public is identified by its PSK (`Channel.isPublicChannel`), not by slot 0,
/// so it can live in any slot and this is the only duplicate check it needs.
/// Public is identified by its PSK (`Channel.isPublicChannel`), not by name or
/// slot, so this finds it wherever older firmware left it.
bool refusePublicAlreadyOnRadio(
BuildContext context,
MeshCoreConnector connector,
@ -92,6 +104,19 @@ bool refusePublicAlreadyOnRadio(
return true;
}
/// For a delete: refuses to remove Public, and says so. Returns true if it
/// refused. Firmware #1896 refuses the clear anyway, with the same error code
/// as an occupied slot, so without this the user would read the wrong reason
/// (owner 2026-10-28: "make sure Public can't be deleted").
bool refuseDeletingPublic(BuildContext context, Channel channel) {
if (!channel.isPublicChannel) return false;
showChannelBlockedMessage(
context,
context.l10n.channels_publicCannotBeRemoved,
);
return true;
}
/// For an edit or delete: refuses while the channel list is being re-read or
/// a write is unanswered, and says so. Returns true if it refused.
///

@ -1042,6 +1042,10 @@
}
},
"channels_publicAlreadyAdded": "The Public channel is already on this radio.",
"channels_publicCannotBeRemoved": "Public can't be removed from the radio. It always stays in slot 0.",
"@channels_publicCannotBeRemoved": {
"description": "Refusal when the user tries to delete the Public channel. Firmware #1896 keeps Public permanently in slot 0. Persistent (#1117)."
},
"channels_writeOccupied": "The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.",
"@channels_writeOccupied": {
"description": "The radio refused a channel write because a different channel holds the slot (ERR_CODE_BAD_STATE, firmware #1864). Persistent (#1026)."

@ -3250,6 +3250,12 @@ abstract class AppLocalizations {
/// **'The Public channel is already on this radio.'**
String get channels_publicAlreadyAdded;
/// Refusal when the user tries to delete the Public channel. Firmware #1896 keeps Public permanently in slot 0. Persistent (#1117).
///
/// In en, this message translates to:
/// **'Public can\'t be removed from the radio. It always stays in slot 0.'**
String get channels_publicCannotBeRemoved;
/// The radio refused a channel write because a different channel holds the slot (ERR_CODE_BAD_STATE, firmware #1864). Persistent (#1026).
///
/// In en, this message translates to:

@ -1846,6 +1846,10 @@ class AppLocalizationsBg extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1841,6 +1841,10 @@ class AppLocalizationsDe extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1819,6 +1819,10 @@ class AppLocalizationsEn extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1842,6 +1842,10 @@ class AppLocalizationsEs extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1847,6 +1847,10 @@ class AppLocalizationsFr extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1847,6 +1847,10 @@ class AppLocalizationsHu extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1844,6 +1844,10 @@ class AppLocalizationsIt extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1785,6 +1785,10 @@ class AppLocalizationsJa extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1780,6 +1780,10 @@ class AppLocalizationsKo extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1832,6 +1832,10 @@ class AppLocalizationsNl extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1853,6 +1853,10 @@ class AppLocalizationsPl extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1841,6 +1841,10 @@ class AppLocalizationsPt extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1842,6 +1842,10 @@ class AppLocalizationsRu extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1833,6 +1833,10 @@ class AppLocalizationsSk extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1830,6 +1830,10 @@ class AppLocalizationsSl extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1822,6 +1822,10 @@ class AppLocalizationsSv extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1837,6 +1837,10 @@ class AppLocalizationsUk extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -1766,6 +1766,10 @@ class AppLocalizationsZh extends AppLocalizations {
String get channels_publicAlreadyAdded =>
'The Public channel is already on this radio.';
@override
String get channels_publicCannotBeRemoved =>
'Public can\'t be removed from the radio. It always stays in slot 0.';
@override
String get channels_writeOccupied =>
'The radio refused: another channel already uses that slot, and it was left unchanged. Try again now that the list has been re-read.';

@ -44,6 +44,27 @@ class Channel {
bool get isPublicChannel => pskHex == publicChannelPsk;
/// Whether [psk] is Public's fixed key. Public is known by its key, never by
/// its name or slot.
static bool isPublicKey(Uint8List psk) =>
_bytesToHex(psk) == publicChannelPsk;
/// The slot firmware reserves for Public (#1896): no other channel may be
/// written there, even while it is empty, and Public goes nowhere else.
static const int publicSlot = 0;
/// Whether a hashtag join of [hashtag] means Public itself, not the hashtag
/// channel `sha256("#public")` (owner 2026-10-28: "Of course add the real
/// public"). The same rule as the device's `namesPublic` (firmware #1896):
/// leading `#` and spaces and trailing spaces dropped, then "public" in any
/// case.
static bool hashtagMeansPublic(String hashtag) {
final bare = hashtag
.replaceFirst(RegExp(r'^[# ]+'), '')
.replaceFirst(RegExp(r' +$'), '');
return bare.toLowerCase() == 'public';
}
static Channel? fromFrame(Uint8List frame) {
// CHANNEL_INFO format:
// [0] = RESP_CODE_CHANNEL_INFO (18)

@ -921,18 +921,19 @@ class _ChannelsScreenState extends State<ChannelsScreen>
);
return;
}
final random = Random.secure();
final psk = Uint8List(16);
for (int i = 0; i < 16; i++) {
psk[i] = random.nextInt(256);
}
final slot = _allocateChannelSlot(
dialogContext,
connector,
psk,
);
// Refused: leave the dialog open so the user
// can retry once the sync finishes.
if (slot == null) return;
final random = Random.secure();
final psk = Uint8List(16);
for (int i = 0; i < 16; i++) {
psk[i] = random.nextInt(256);
}
Navigator.pop(dialogContext);
final result = await connector.setChannel(
slot,
@ -1036,6 +1037,7 @@ class _ChannelsScreenState extends State<ChannelsScreen>
final slot = _allocateChannelSlot(
dialogContext,
connector,
psk,
);
if (slot == null) return;
Navigator.pop(dialogContext);
@ -1078,14 +1080,15 @@ class _ChannelsScreenState extends State<ChannelsScreen>
Expanded(
child: FilledButton(
onPressed: () async {
final psk = Channel.parsePskHex(
Channel.publicChannelPsk,
);
final slot = _allocateChannelSlot(
dialogContext,
connector,
psk,
);
if (slot == null) return;
final psk = Channel.parsePskHex(
Channel.publicChannelPsk,
);
Navigator.pop(dialogContext);
final result = await connector.setChannel(
slot,
@ -1245,7 +1248,15 @@ class _ChannelsScreenState extends State<ChannelsScreen>
final String channelName;
final Uint8List psk;
if (isRegularHashtag) {
if (isRegularHashtag &&
Channel.hashtagMeansPublic(hashtag)) {
// "public" means the real Public, as on the
// device (#1896), not sha256("#public").
channelName = 'Public';
psk = Channel.parsePskHex(
Channel.publicChannelPsk,
);
} else if (isRegularHashtag) {
channelName = '#$hashtag';
// Regular hashtag - public derivation using SHA256
psk = Channel.derivePskFromHashtag(hashtag);
@ -1281,6 +1292,7 @@ class _ChannelsScreenState extends State<ChannelsScreen>
final slot = _allocateChannelSlot(
context,
connector,
psk,
);
if (slot == null) return;
if (dialogContext.mounted) {
@ -1426,6 +1438,7 @@ class _ChannelsScreenState extends State<ChannelsScreen>
slot = _allocateChannelSlot(
context,
connector,
community.deriveCommunityPublicPsk(),
);
channelRefused = slot == null;
}
@ -1791,6 +1804,14 @@ class _ChannelsScreenState extends State<ChannelsScreen>
return;
}
}
// Typing Public's key into another channel would make a
// second Public, which the radio refuses (#1896). Say so here
// instead of reporting a generic refusal.
if (Channel.isPublicKey(psk) &&
!channel.isPublicChannel &&
refusePublicAlreadyOnRadio(dialogContext, connector)) {
return;
}
Navigator.pop(dialogContext);
try {
@ -1895,6 +1916,7 @@ class _ChannelsScreenState extends State<ChannelsScreen>
ChannelMessageStore channelMessageStore,
Channel channel,
) {
if (refuseDeletingPublic(context, channel)) return;
showDialog(
context: context,
builder: (dialogContext) => AlertDialog(
@ -1960,13 +1982,12 @@ class _ChannelsScreenState extends State<ChannelsScreen>
MeshCoreConnector connector,
) async {
// This used to write slot 0 unconditionally, with no occupancy check, so
// it overwrote whatever was there (#1021). Public is identified by its PSK
// (`Channel.isPublicChannel`), not by its index, so it has no claim on
// slot 0 and can live anywhere.
if (refusePublicAlreadyOnRadio(context, connector)) return;
final slot = _allocateChannelSlot(context, connector);
if (slot == null) return;
// it overwrote whatever was there (#1021). It now goes through the shared
// allocator, which refuses a second Public and puts Public in slot 0, the
// slot firmware reserves for it (#1896), whenever slot 0 is free.
final psk = Channel.parsePskHex(Channel.publicChannelPsk);
final slot = _allocateChannelSlot(context, connector, psk);
if (slot == null) return;
final result = await connector.setChannel(slot, 'Public', psk);
if (!context.mounted) return;
if (explainUnconfirmedChannelWrite(context, result)) return;
@ -1983,7 +2004,8 @@ class _ChannelsScreenState extends State<ChannelsScreen>
int? _allocateChannelSlot(
BuildContext context,
MeshCoreConnector connector,
) => allocateChannelSlotOrExplain(context, connector);
Uint8List psk,
) => allocateChannelSlotOrExplain(context, connector, psk: psk);
void _showManageCommunitiesDialog(BuildContext context) {
showModalBottomSheet(
@ -2245,10 +2267,14 @@ class _ChannelsScreenState extends State<ChannelsScreen>
/// A hashtag channel's key IS its name (Channel.derivePskFromHashtag), so the
/// key is never hand-editable and a rename re-derives it. A community
/// hashtag's name and key both derive from the community secret, so both are
/// locked and managed through the community. The key stays visible and
/// copyable in every case: OpenHop routing rules need it.
/// locked and managed through the community. Public's key is fixed: a new key
/// would make it a different channel, and firmware #1896 refuses the clear
/// that a key change needs. The key stays visible and copyable in every case:
/// OpenHop routing rules need it.
bool channelPskEditable(ChannelType type) =>
type != ChannelType.hashtag && type != ChannelType.communityHashtag;
type != ChannelType.hashtag &&
type != ChannelType.communityHashtag &&
type != ChannelType.public;
bool channelNameEditable(ChannelType type) =>
type != ChannelType.communityHashtag;

@ -237,11 +237,11 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
var channelRefused = false;
if (addPublicChannel && context.mounted) {
final connector = context.read<MeshCoreConnector>();
final slot = allocateChannelSlotOrExplain(context, connector);
final psk = community.deriveCommunityPublicPsk();
final slot = allocateChannelSlotOrExplain(context, connector, psk: psk);
if (slot == null) {
channelRefused = true;
} else {
final psk = community.deriveCommunityPublicPsk();
final channelName = '${community.name} Public';
final result = await connector.setChannel(slot, channelName, psk);
if (context.mounted) {
@ -313,7 +313,11 @@ class _CommunityQrScannerScreenState extends State<CommunityQrScannerScreen> {
),
);
if (confirmed == true && context.mounted) {
final slot = allocateChannelSlotOrExplain(context, connector);
final slot = allocateChannelSlotOrExplain(
context,
connector,
psk: channel.psk,
);
// Refused: the persistent message explains why. Stay on this screen so
// the user can retry, rather than popping away from the explanation.
if (slot == null) return;

@ -386,9 +386,10 @@ class ChannelImportPlan {
/// is the non-destructive reading, and pinning down what stock actually does is
/// one of the T1 gate's questions.
///
/// New channels go into the lowest free slot. Stock's file has no index field,
/// so incoming order is the only ordering information there is, and slots
/// already occupied are left alone.
/// New channels go into the lowest free slot, never slot 0 unless the channel is
/// Public (#1896). Stock's file has no index field, so incoming order is the
/// only ordering information there is, and slots already occupied are left
/// alone.
///
/// While [isSyncing] is true nothing is planned. The connector empties its
/// channel list for the duration of a sync, so [existing] would look empty and
@ -424,11 +425,19 @@ ChannelImportPlan planChannelImport({
);
continue;
}
// Slot 0 is Public's (#1896): Public goes there when it is free, and no
// other channel goes there at all, the same rule as decideChannelSlot.
final isPublic = Channel.isPublicKey(channel.secret);
int? slot;
for (var i = 0; i < maxChannels; i++) {
if (!takenSlots.contains(i)) {
slot = i;
break;
if (isPublic && !takenSlots.contains(Channel.publicSlot)) {
slot = Channel.publicSlot;
} else {
for (var i = 0; i < maxChannels; i++) {
if (i == Channel.publicSlot) continue;
if (!takenSlots.contains(i)) {
slot = i;
break;
}
}
}
if (slot == null) {

@ -313,6 +313,7 @@ void main() {
channels: connector.channels,
maxChannels: 3,
isSyncing: connector.isChannelTableBusy,
isPublic: false,
);
expect(decision.slot, 2);
},

@ -5,6 +5,10 @@
// connector empties its channel list during a sync so any index looked free,
// and the chooser answered "no free slot" with slot 0, which is where Public
// normally lives. These pin the decision so none of them can come back.
//
// #1117: slot 0 is Public's. Firmware #1896 refuses any other channel there,
// even while it is empty, and refuses Public anywhere else (owner 2026-10-28:
// slot 0 "should always remain public").
import 'dart:typed_data';
@ -18,34 +22,39 @@ Channel _ch(int index, {String name = 'ch', String? pskHex}) => Channel(
psk: Channel.parsePskHex(pskHex ?? '00112233445566778899aabbccddeeff'),
);
Channel _public(int index) => Channel(
index: index,
name: 'Public',
psk: Channel.parsePskHex(Channel.publicChannelPsk),
);
ChannelSlotDecision _other(List<Channel> channels, {int max = 8}) =>
decideChannelSlot(
channels: channels,
maxChannels: max,
isSyncing: false,
isPublic: false,
);
ChannelSlotDecision _forPublic(List<Channel> channels, {int max = 8}) =>
decideChannelSlot(
channels: channels,
maxChannels: max,
isSyncing: false,
isPublic: true,
);
void main() {
group('decideChannelSlot', () {
test('takes the lowest free slot', () {
final d = decideChannelSlot(
channels: [_ch(0), _ch(1), _ch(3)],
maxChannels: 8,
isSyncing: false,
);
final d = _other([_public(0), _ch(1), _ch(3)]);
expect(d.isAllowed, isTrue);
expect(d.slot, 2);
expect(d.refusal, isNull);
});
test('takes slot 0 when the radio is empty', () {
final d = decideChannelSlot(
channels: const [],
maxChannels: 8,
isSyncing: false,
);
expect(d.slot, 0);
});
test('refuses with full, NEVER slot 0, when every slot is used', () {
final d = decideChannelSlot(
channels: [for (var i = 0; i < 8; i++) _ch(i)],
maxChannels: 8,
isSyncing: false,
);
final d = _other([for (var i = 0; i < 8; i++) _ch(i)]);
expect(d.isAllowed, isFalse);
expect(d.refusal, ChannelSlotRefusal.full);
expect(
@ -57,23 +66,27 @@ void main() {
test('refuses while syncing, even though every slot looks free', () {
// This is the reported bug. Mid-sync the connector has emptied its list,
// so an empty list plus a free-looking slot 0 is exactly the state that
// so an empty list plus a free-looking slot is exactly the state that
// overwrote Public.
final d = decideChannelSlot(
channels: const [],
maxChannels: 8,
isSyncing: true,
);
expect(d.isAllowed, isFalse);
expect(d.refusal, ChannelSlotRefusal.syncing);
expect(d.slot, isNull);
for (final isPublic in [false, true]) {
final d = decideChannelSlot(
channels: const [],
maxChannels: 8,
isSyncing: true,
isPublic: isPublic,
);
expect(d.isAllowed, isFalse);
expect(d.refusal, ChannelSlotRefusal.syncing);
expect(d.slot, isNull);
}
});
test('syncing is refused even when a slot genuinely is free', () {
final d = decideChannelSlot(
channels: [_ch(0)],
channels: [_public(0)],
maxChannels: 8,
isSyncing: true,
isPublic: false,
);
expect(d.refusal, ChannelSlotRefusal.syncing);
});
@ -83,6 +96,7 @@ void main() {
channels: [for (var i = 0; i < 8; i++) _ch(i)],
maxChannels: 8,
isSyncing: true,
isPublic: false,
);
expect(
d.refusal,
@ -97,11 +111,7 @@ void main() {
// so the decision takes isSyncing only, and a restored list allocates
// normally.
test('allocates normally after a failed sync, from the restored list', () {
final d = decideChannelSlot(
channels: [_ch(0), _ch(1)],
maxChannels: 8,
isSyncing: false,
);
final d = _other([_public(0), _ch(1)]);
expect(
d.isAllowed,
isTrue,
@ -110,68 +120,94 @@ void main() {
expect(d.slot, 2);
});
test('a single-slot radio can be filled and then refuses', () {
expect(
decideChannelSlot(
channels: const [],
maxChannels: 1,
isSyncing: false,
).slot,
0,
);
expect(
decideChannelSlot(
channels: [_ch(0)],
maxChannels: 1,
isSyncing: false,
).refusal,
ChannelSlotRefusal.full,
);
});
test('an out-of-range index in the list does not block a free slot', () {
// Defensive: a stale or odd index must not make the radio look full.
final d = decideChannelSlot(
channels: [_ch(99)],
maxChannels: 4,
isSyncing: false,
);
expect(d.slot, 0);
});
test('Public holds no claim on slot 0, since it is identified by PSK', () {
// `Channel.isPublicChannel` compares the PSK, not the index, so Public
// can live anywhere and nothing may assume slot 0 belongs to it.
final public = Channel(
index: 3,
name: 'Public',
psk: Channel.parsePskHex(Channel.publicChannelPsk),
);
expect(public.isPublicChannel, isTrue);
final d = decideChannelSlot(
channels: [public],
maxChannels: 8,
isSyncing: false,
);
expect(d.slot, 0, reason: 'slot 0 is free here and may be used');
expect(_other([_ch(99)], max: 4).slot, 1);
});
test('a decision is either a slot or a refusal, never both', () {
final allowed = decideChannelSlot(
channels: const [],
maxChannels: 4,
isSyncing: false,
);
final allowed = _other(const [], max: 4);
final refused = decideChannelSlot(
channels: const [],
maxChannels: 4,
isSyncing: true,
isPublic: false,
);
expect(allowed.slot != null && allowed.refusal == null, isTrue);
expect(refused.slot == null && refused.refusal != null, isTrue);
});
});
// #1117: firmware #1896 refuses these writes with the same error code as an
// occupied slot, so the client must never ask for them.
group('slot 0 is Public\'s', () {
test('no other channel gets slot 0, even on an empty radio', () {
final d = _other(const []);
expect(d.slot, 1, reason: 'firmware refuses a non-Public slot 0 write');
});
test('no other channel gets slot 0 when it is the only free slot', () {
final d = _other([for (var i = 1; i < 8; i++) _ch(i)]);
expect(d.refusal, ChannelSlotRefusal.full);
});
test('a one-slot radio has no room for anything but Public', () {
expect(_other(const [], max: 1).refusal, ChannelSlotRefusal.full);
expect(_forPublic(const [], max: 1).slot, 0);
});
test('Public goes to slot 0 when it is free', () {
expect(_forPublic([_ch(1), _ch(2)]).slot, 0);
});
test('Public goes to slot 0 even when lower-numbered gaps exist', () {
expect(_forPublic([_ch(3)]).slot, 0);
});
test('Public falls back to a free slot only when slot 0 is taken', () {
// Older firmware only: a channel other than Public can hold slot 0
// there, and it accepts Public anywhere.
expect(_forPublic([_ch(0), _ch(1)]).slot, 2);
});
test('a full radio refuses Public too, never slot 0 over a channel', () {
expect(
_forPublic([for (var i = 0; i < 8; i++) _ch(i)]).refusal,
ChannelSlotRefusal.full,
);
});
});
group('Channel helpers for the slot-0 rule', () {
test('Public is known by its key', () {
expect(
Channel.isPublicKey(Channel.parsePskHex(Channel.publicChannelPsk)),
isTrue,
);
expect(Channel.isPublicKey(_ch(1).psk), isFalse);
});
test('a hashtag of "public" means Public, as on the device', () {
for (final typed in [
'public',
'Public',
'PUBLIC',
'#public',
'##public',
' #Public ',
'# public',
]) {
expect(Channel.hashtagMeansPublic(typed), isTrue, reason: typed);
}
});
test('anything else stays a hashtag channel', () {
for (final typed in ['publics', 'public2', 'pub lic', '#pub', 'publ']) {
expect(Channel.hashtagMeansPublic(typed), isFalse, reason: typed);
}
});
});
group('Uint8List psk sanity', () {
test('the helper builds channels with a real 16 byte psk', () {
expect(_ch(0).psk, isA<Uint8List>());

@ -35,11 +35,15 @@ Channel _ch(int index, String name, int fill) => Channel(
final _l10n = lookupAppLocalizations(const Locale('en'));
final _publicPsk = Channel.parsePskHex(Channel.publicChannelPsk);
final _otherPsk = Channel.parsePskHex('00112233445566778899aabbccddeeff');
/// Every refusal this file can show, so a success can assert none of them.
final _allMessages = [
_l10n.channels_slotBusySyncing,
_l10n.channels_slotsAllFull(3),
_l10n.channels_publicAlreadyAdded,
_l10n.channels_publicCannotBeRemoved,
_l10n.channels_writeOccupied,
_l10n.channels_writeRejected,
_l10n.channels_writeNoReply,
@ -84,7 +88,11 @@ void main() {
group('allocateChannelSlotOrExplain', () {
testWidgets('busy: refused, with the busy message', (tester) async {
final context = await _host(tester);
final slot = allocateChannelSlotOrExplain(context, _FakeConn(busy: true));
final slot = allocateChannelSlotOrExplain(
context,
_FakeConn(busy: true),
psk: _otherPsk,
);
expect(slot, isNull);
await _expectPersistent(tester, _l10n.channels_slotBusySyncing);
@ -98,6 +106,7 @@ void main() {
list: [_ch(0, 'A', 1), _ch(1, 'B', 2), _ch(2, 'C', 3)],
max: 3,
),
psk: _otherPsk,
);
expect(slot, isNull);
@ -109,9 +118,83 @@ void main() {
final slot = allocateChannelSlotOrExplain(
context,
_FakeConn(list: [_ch(0, 'A', 1)], max: 3),
psk: _otherPsk,
);
expect(slot, 1);
await _expectNoRefusal(tester);
});
// #1117: the key decides. Public's key goes to slot 0; any other key never.
testWidgets('Public\'s key on an empty radio: slot 0', (tester) async {
final context = await _host(tester);
final slot = allocateChannelSlotOrExplain(
context,
_FakeConn(),
psk: _publicPsk,
);
expect(slot, 0);
await _expectNoRefusal(tester);
});
testWidgets('another key on an empty radio: not slot 0', (tester) async {
final context = await _host(tester);
final slot = allocateChannelSlotOrExplain(
context,
_FakeConn(),
psk: _otherPsk,
);
expect(slot, 1);
});
testWidgets('Public\'s key with Public present: refused, any path', (
tester,
) async {
// Join Public, a typed private key, a QR share and a hashtag "public"
// all come through here, so a second Public is refused once.
final context = await _host(tester);
final slot = allocateChannelSlotOrExplain(
context,
_FakeConn(
list: [Channel(index: 0, name: 'Public', psk: _publicPsk)],
),
psk: _publicPsk,
);
expect(slot, isNull);
await _expectPersistent(tester, _l10n.channels_publicAlreadyAdded);
});
});
group('refuseDeletingPublic', () {
testWidgets('Public: refused, persistently', (tester) async {
final context = await _host(tester);
final refused = refuseDeletingPublic(
context,
Channel(index: 0, name: 'Public', psk: _publicPsk),
);
expect(refused, isTrue);
await _expectPersistent(tester, _l10n.channels_publicCannotBeRemoved);
});
testWidgets('Public by key, whatever its name: refused', (tester) async {
final context = await _host(tester);
final refused = refuseDeletingPublic(
context,
Channel(index: 4, name: 'Renamed', psk: _publicPsk),
);
expect(refused, isTrue);
});
testWidgets('any other channel: allowed, and no message', (tester) async {
final context = await _host(tester);
final refused = refuseDeletingPublic(context, _ch(1, 'Public', 2));
expect(refused, isFalse, reason: 'named Public, but not its key');
await _expectNoRefusal(tester);
});
});

@ -14,9 +14,14 @@ void main() {
expect(channelPskEditable(ChannelType.communityHashtag), isFalse);
});
// #1117: Public's key is fixed, and firmware #1896 refuses clearing
// Public, which a key change needs.
test('locked for Public', () {
expect(channelPskEditable(ChannelType.public), isFalse);
});
test('editable for the rest', () {
expect(channelPskEditable(ChannelType.private), isTrue);
expect(channelPskEditable(ChannelType.public), isTrue);
expect(channelPskEditable(ChannelType.communityPublic), isTrue);
});
});

@ -141,6 +141,50 @@ void main() {
expect(plan.skipped, isEmpty);
});
// #1117: firmware #1896 refuses any channel but Public in slot 0, even
// while it is empty, and refuses Public anywhere else.
test('no channel but Public is planned into slot 0', () {
final plan = planChannelImport(
existing: const [],
incoming: [incoming('Weather', 0x22), incoming('Emergency', 0x33)],
maxChannels: 40,
isSyncing: false,
);
expect(plan.assignments.map((a) => a.slot), [1, 2]);
});
test('Public in the file goes to slot 0 when it is free', () {
final plan = planChannelImport(
existing: const [],
incoming: [
incoming('Weather', 0x22),
StockChannel(
name: 'Public',
secret: Channel.parsePskHex(Channel.publicChannelPsk),
),
],
maxChannels: 40,
isSyncing: false,
);
final bySlot = {for (final a in plan.assignments) a.channel.name: a.slot};
expect(bySlot['Public'], 0);
expect(bySlot['Weather'], 1);
});
test('a radio full but for slot 0 has no room for other channels', () {
final plan = planChannelImport(
existing: [for (var i = 1; i < 4; i++) live(i, 'c$i', 0x40 + i)],
incoming: [incoming('Weather', 0x22)],
maxChannels: 4,
isSyncing: false,
);
expect(plan.assignments, isEmpty);
expect(plan.skipped.single.reason, StockConfigImportIssue.noFreeSlot);
});
// #1021: the connector empties its channel list for the length of a sync,
// so mid-sync `existing` looks empty. Planning then would write every
// incoming channel over slots that are really occupied, starting at 0.

@ -157,6 +157,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -534,6 +535,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -911,6 +913,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -1288,6 +1291,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -1665,6 +1669,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -2042,6 +2047,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -2419,6 +2425,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -2796,6 +2803,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -3173,6 +3181,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -3550,6 +3559,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -3927,6 +3937,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -4304,6 +4315,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -4681,6 +4693,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -5058,6 +5071,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -5435,6 +5449,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -5812,6 +5827,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",
@ -6189,6 +6205,7 @@
"channels_slotBusySyncing",
"channels_slotsAllFull",
"channels_publicAlreadyAdded",
"channels_publicCannotBeRemoved",
"channels_writeOccupied",
"channels_writeRejected",
"channels_writeNoReply",

Loading…
Cancel
Save

Powered by TurnKey Linux.