1+ /*
2+ * Copyright (C) 2020 Anthony Doud & Joel Baranick
3+ * All rights reserved
4+ *
5+ * SPDX-License-Identifier: GPL-2.0-only
6+ */
7+
8+ #include < unity.h>
9+ #include < Arduino.h>
10+ #include " test.h"
11+
12+ // Test helper function to create a mock address string and test the randomization detection
13+ // Since we can't easily mock NimBLEAdvertisedDevice, we'll test the logic indirectly
14+ // by testing the address pattern detection logic
15+
16+ void TestAdevName2UniqueName::test_traditional_device_keeps_address_suffix () {
17+ // This test verifies the behavior for traditional (non-randomized) addresses
18+ // We can't directly test without a real NimBLEAdvertisedDevice, so this is a placeholder
19+ // for manual testing or integration testing
20+
21+ // The logic should be:
22+ // - Addresses starting with 0x00, 0x01, 0x04, 0x05, etc. (even numbers and some odd)
23+ // are typically manufacturer-assigned (non-random)
24+ // - These should keep the address suffix behavior
25+
26+ TEST_ASSERT_TRUE_MESSAGE (true , " Traditional device suffix test placeholder - requires manual verification" );
27+ }
28+
29+ void TestAdevName2UniqueName::test_android_device_no_address_suffix () {
30+ // This test verifies the behavior for randomized addresses (Android devices)
31+ // Addresses with local administration bit set (0x02, 0x03, 0x06, 0x07, etc.)
32+ // should NOT have address suffix
33+
34+ TEST_ASSERT_TRUE_MESSAGE (true , " Android device no-suffix test placeholder - requires manual verification" );
35+ }
36+
37+ void TestAdevName2UniqueName::test_random_address_pattern_detection () {
38+ // Test the bit pattern detection logic
39+ // We can test this by examining the first byte of MAC addresses
40+
41+ // Test addresses that should be detected as randomized
42+ // Format: "XX:YY:ZZ:AA:BB:CC" where XX has bit 1 set
43+
44+ // Simulate the logic from isRandomizedAddress function
45+ struct {
46+ const char * address;
47+ bool shouldBeRandom;
48+ const char * description;
49+ } testCases[] = {
50+ {" 00:11:22:33:44:55" , false , " Manufacturer assigned - no local admin bit" },
51+ {" 01:11:22:33:44:55" , false , " Manufacturer assigned - no local admin bit" },
52+ {" 02:11:22:33:44:55" , true , " Random address - local admin bit set" },
53+ {" 03:11:22:33:44:55" , true , " Random address - local admin bit set" },
54+ {" 04:11:22:33:44:55" , false , " Manufacturer assigned - no local admin bit" },
55+ {" 06:11:22:33:44:55" , true , " Random address - local admin bit set" },
56+ {" 07:11:22:33:44:55" , true , " Random address - local admin bit set" },
57+ {" A4:C1:38:12:34:56" , false , " Real device example - manufacturer assigned" },
58+ {" FE:11:22:33:44:55" , true , " Random address - local admin bit set" },
59+ };
60+
61+ for (size_t i = 0 ; i < sizeof (testCases) / sizeof (testCases[0 ]); i++) {
62+ // Parse first byte
63+ char firstByteStr[3 ] = {testCases[i].address [0 ], testCases[i].address [1 ], ' \0 ' };
64+ int firstByte = strtol (firstByteStr, nullptr , 16 );
65+ bool isRandom = (firstByte & 0x02 ) != 0 ;
66+
67+ TEST_ASSERT_EQUAL_MESSAGE (testCases[i].shouldBeRandom , isRandom, testCases[i].description );
68+ }
69+ }
70+
71+ void TestAdevName2UniqueName::test_null_device_handling () {
72+ // Test that the function handles null input gracefully
73+ // Since we can't call the actual function without proper setup,
74+ // this is a validation of the expected behavior
75+
76+ TEST_ASSERT_TRUE_MESSAGE (true , " Null device handling test - function should return 'null' string" );
77+ }
78+
79+ void TestAdevName2UniqueName::test_device_without_name () {
80+ // Test behavior when device doesn't have a name
81+ // Should return the full address regardless of randomization
82+
83+ TEST_ASSERT_TRUE_MESSAGE (true , " No-name device test - should return full address" );
84+ }
85+
86+ void TestAdevName2UniqueName::test_backward_compatibility () {
87+ // Verify that existing device names remain unchanged for traditional devices
88+ // This is critical for not breaking existing user configurations
89+
90+ // Expected behavior:
91+ // - Non-randomized addresses: "DeviceName XX" (where XX is last 2 chars of MAC)
92+ // - Randomized addresses: "DeviceName" (no suffix)
93+
94+ TEST_ASSERT_TRUE_MESSAGE (true , " Backward compatibility test - non-random devices should keep suffix" );
95+ }
0 commit comments