You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.5 KiB
42 lines
1.5 KiB
package devcode
|
|
|
|
import "testing"
|
|
|
|
func TestProductID(t *testing.T) {
|
|
// MAC 形态:PID 编在前两字节,带不带冒号都要认
|
|
if pid, err := ProductID("B0:23:00:00:00:01"); err != nil || pid != 0xB023 {
|
|
t.Fatalf("带冒号 MAC: pid=%X err=%v", pid, err)
|
|
}
|
|
if pid, err := ProductID("B023000000AB"); err != nil || pid != 0xB023 {
|
|
t.Fatalf("不带冒号 MAC: pid=%X err=%v", pid, err)
|
|
}
|
|
// 已废弃的 license 串形态:必须报错而不是解出个 PID 来。
|
|
// 这两种串长度都是 20,若哪天有人把 license 逻辑加回来,这条会先炸。
|
|
if _, err := ProductID("71E5E5C3A797C043B5A7"); err == nil {
|
|
t.Fatal("license 串应当解析失败(授权码已移除)")
|
|
}
|
|
if _, err := ProductID("nonsense"); err == nil {
|
|
t.Fatal("垃圾输入应该报错")
|
|
}
|
|
}
|
|
|
|
// 客户端送上来的各种写法都要能规范化成落库形态(大写+冒号),
|
|
// 这是「后台看到的 MAC == 客户端校验用的 MAC」的前提。
|
|
func TestNormalizeMatchesStoredForm(t *testing.T) {
|
|
const stored = "B0:23:00:00:00:AB"
|
|
for _, in := range []string{
|
|
"B0:23:00:00:00:AB", // Android BluetoothDevice.getAddress() 的原样
|
|
"b0:23:00:00:00:ab", // 小写
|
|
"B023000000AB", // 无冒号
|
|
"b023000000ab",
|
|
" B0:23:00:00:00:AB ",
|
|
} {
|
|
got, ok := NormalizeMac(in)
|
|
if !ok || got != stored {
|
|
t.Fatalf("NormalizeMac(%q) = %q,%v;期望 %q", in, got, ok, stored)
|
|
}
|
|
}
|
|
if _, ok := NormalizeMac("ZZ:ZZ"); ok {
|
|
t.Fatal("非法 MAC 应该 ok=false")
|
|
}
|
|
}
|
|
|