初始化项目
This commit is contained in:
81
trunk/goutil/deviceUtil/deviceUtil.go
Normal file
81
trunk/goutil/deviceUtil/deviceUtil.go
Normal file
@@ -0,0 +1,81 @@
|
||||
package deviceUtil
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// 将MAC地址转化为标准格式
|
||||
func ConvertMacToStandardFormat(mac string) string {
|
||||
if mac == "" || mac == "00:00:00:00:00:00" || mac == "02:00:00:00:00:00" {
|
||||
return ""
|
||||
}
|
||||
|
||||
//如果mac的长度不为12或17,则是不正确的格式
|
||||
if len(mac) != 12 && len(mac) != 17 {
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
//转化为大写
|
||||
mac = strings.ToUpper(mac)
|
||||
|
||||
//如果mac地址的长度为17(已经有:),则直接返回
|
||||
if len(mac) == 17 {
|
||||
return mac
|
||||
}
|
||||
|
||||
//如果没有分隔符,则添加分隔符
|
||||
newMac := make([]rune, 0, 17)
|
||||
for i, v := range []rune(mac) {
|
||||
newMac = append(newMac, v)
|
||||
if i < len(mac) - 1 && i % 2 == 1 {
|
||||
newMac = append(newMac, ':')
|
||||
}
|
||||
}
|
||||
|
||||
return string(newMac)
|
||||
}
|
||||
|
||||
func ConvertIdfaToStandardFormat(idfa string) string {
|
||||
//如果是空或默认值,则返回String.Empty
|
||||
if idfa == "" || idfa == "00000000-0000-0000-0000-000000000000" {
|
||||
return ""
|
||||
}
|
||||
|
||||
//如果idfa的长度不为32或36,则代表是Android的数据,则可以直接返回
|
||||
if len(idfa) != 32 && len(idfa) != 36 {
|
||||
return idfa
|
||||
}
|
||||
|
||||
//转化为大写
|
||||
idfa = strings.ToUpper(idfa);
|
||||
|
||||
//如果idfa地址的长度为36(已经有:),则直接返回
|
||||
if len(idfa) == 36 {
|
||||
return idfa
|
||||
}
|
||||
|
||||
//如果没有分隔符,则添加分隔符
|
||||
newIdfa := make([]rune, 0, 36)
|
||||
for i, v := range []rune(idfa) {
|
||||
newIdfa = append(newIdfa, v)
|
||||
if i == 7 || i == 11 || i == 15 || i == 19 {
|
||||
newIdfa = append(newIdfa, '-')
|
||||
}
|
||||
}
|
||||
|
||||
return string(newIdfa)
|
||||
}
|
||||
|
||||
// 根据MAC和IDFA获取唯一标识
|
||||
func GetIdentifier(mac, idfa string) string {
|
||||
mac = ConvertMacToStandardFormat(mac)
|
||||
idfa = ConvertIdfaToStandardFormat(idfa);
|
||||
|
||||
//如果idfa不为空,则使用idfa,否则使用mac
|
||||
if idfa != "" {
|
||||
return idfa
|
||||
} else {
|
||||
return mac
|
||||
}
|
||||
}
|
||||
131
trunk/goutil/deviceUtil/deviceUtil_test.go
Normal file
131
trunk/goutil/deviceUtil/deviceUtil_test.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package deviceUtil
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestConvertMacToStarndardFormat(t *testing.T) {
|
||||
mac := ""
|
||||
expected := ""
|
||||
got := ConvertMacToStandardFormat(mac)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
mac = "00:00:00:00:00:00"
|
||||
expected = ""
|
||||
got = ConvertMacToStandardFormat(mac)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
mac = "02:00:00:00:00:00"
|
||||
expected = ""
|
||||
got = ConvertMacToStandardFormat(mac)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
mac = "02:00:00:00:00"
|
||||
expected = ""
|
||||
got = ConvertMacToStandardFormat(mac)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
mac = "020000000020"
|
||||
expected = "02:00:00:00:00:20"
|
||||
got = ConvertMacToStandardFormat(mac)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
mac = "02:00:00:00:00:20"
|
||||
expected = "02:00:00:00:00:20"
|
||||
got = ConvertMacToStandardFormat(mac)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConvertIdfaToStandardFormat(t *testing.T) {
|
||||
idfa := ""
|
||||
expected := ""
|
||||
got := ConvertIdfaToStandardFormat(idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
idfa = "00000000-0000-0000-0000-000000000000"
|
||||
expected = ""
|
||||
got = ConvertIdfaToStandardFormat(idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
idfa = "00000000-0000-0000-0000-000000000000-123"
|
||||
expected = "00000000-0000-0000-0000-000000000000-123"
|
||||
got = ConvertIdfaToStandardFormat(idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
idfa = "00000000-1234-5678-0000-000000000000"
|
||||
expected = "00000000-1234-5678-0000-000000000000"
|
||||
got = ConvertIdfaToStandardFormat(idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
idfa = "00000000123456780000000000000000"
|
||||
expected = "00000000-1234-5678-0000-000000000000"
|
||||
got = ConvertIdfaToStandardFormat(idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetIdentifier(t *testing.T) {
|
||||
mac := ""
|
||||
idfa := ""
|
||||
expected := ""
|
||||
got := GetIdentifier(mac, idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
mac = "00:00:00:00:00:00"
|
||||
expected = ""
|
||||
got = GetIdentifier(mac, idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
mac = "02:00:00:00:00:00"
|
||||
expected = ""
|
||||
got = GetIdentifier(mac, idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
mac = "020000000020"
|
||||
expected = "02:00:00:00:00:20"
|
||||
got = GetIdentifier(mac, idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
mac = "02:00:00:00:00:20"
|
||||
expected = "02:00:00:00:00:20"
|
||||
got = GetIdentifier(mac, idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
|
||||
idfa = "00000000123456780000000000000000"
|
||||
expected = "00000000-1234-5678-0000-000000000000"
|
||||
got = GetIdentifier(mac, idfa)
|
||||
if got != expected {
|
||||
t.Errorf("Expected: %s, but got:%s", expected, got)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user