src/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
11 / 11
100.0%
Branches:
19 / 20
95.0%
Functions:
3 / 3
100.0%

Secrets.h
Line Branch Exec Source
1 #ifndef secrets_h
2 #define secrets_h
3 #include <string.h>
4
5 // Stored secrets (WiFi passphrase, MQTT password, login password/PIN) are
6 // write-only: responses carry hasSecret() flags and logs carry maskSecret().
7
8
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
7 inline bool hasSecret(const char *s) { return s && s[0] != '\0'; }
9
10
2/2
✓ Branch 1 taken 1 time.
✓ Branch 2 taken 2 times.
3 inline const char *maskSecret(const char *s) { return hasSecret(s) ? "<set>" : "<empty>"; }
11
12 // Updates dest from an incoming value, truncating to size. Pass
13 // obj["key"].as<const char *>(), which is null when the key is absent.
14 // absent -> keep the stored secret.
15 // "" -> keep it too, unless identityChanged (a new SSID or username in
16 // the same request), then clear it. UIs older than write-only
17 // secrets send "" for a field the device no longer fills in.
18 // value -> replace it.
19 // Returns true when dest changed.
20 11 inline bool applySecret(const char *incoming, char *dest, size_t size, bool identityChanged = false) {
21
3/4
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
11 if(!incoming || size == 0) return false;
22
4/4
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 time.
✓ Branch 3 taken 2 times.
9 if(incoming[0] == '\0' && !identityChanged) return false;
23 8 size_t len = strlen(incoming);
24
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 if(len >= size) len = size - 1;
25
4/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 2 times.
8 if(strncmp(dest, incoming, len) == 0 && dest[len] == '\0') return false;
26 5 memcpy(dest, incoming, len);
27 5 dest[len] = '\0';
28 5 return true;
29 }
30 #endif
31