1// +build darwin 2 3package machineid 4 5import ( 6 "strings" 7 "testing" 8) 9 10const sampleOutput = `+-o MacBookPro12,1 <class IOPlatformExpertDevice, id 0x100000112, registered, matched, active, busy 0 (580075 ms), retain 42> 11{ 12 "IOPlatformSystemSleepPolicy" = <534c505402001300841e120004000000001400000004000006000000000000000f2500000000000000004000000040000000100000001000070000$ 13 "compatible" = <"MacBookPro12,1"> 14 "version" = <"1.0"> 15 "board-id" = <"Mac-EEECCCDDD8888AAA"> 16 "IOInterruptSpecifiers" = (<0900000005000000>) 17 "platform-feature" = <0200000000000000> 18 "serial-number" = <1111111100000000000000000022222222222d3333333000000000000000000000000000000000000> 19 "IOInterruptControllers" = ("io-apic-0") 20 "IOPlatformUUID" = "A3344D1DD-1234-22A1-B123-11AB1C11D111" 21 "target-type" = <"Mac"> 22 "clock-frequency" = <00e1f505> 23 "manufacturer" = <"Apple Inc."> 24 "IOPolledInterface" = "SMCPolledInterface is not serializable" 25 "IOPlatformSerialNumber" = "CCCCCCCCC" 26 "system-type" = <02> 27 "product-name" = <"MacBookPro12,1"> 28 "model" = <"MacBookPro12,1"> 29 "name" = <"/"> 30 "IOBusyInterest" = "IOCommand is not serializable" 31} 32` 33 34func Test_extractID(t *testing.T) { 35 want := "A3344D1DD-1234-22A1-B123-11AB1C11D111" 36 got, err := extractID(sampleOutput) 37 if err != nil { 38 t.Error(err) 39 } 40 if got != want { 41 t.Errorf("extractID() = %v, want %v", got, want) 42 } 43} 44 45func Test_extractID_invalidInput(t *testing.T) { 46 got, err := extractID("invalid input") 47 if err == nil { 48 t.Error("expected error, got none") 49 } 50 if got != "" { 51 t.Errorf("expected empty string, got some value %s", got) 52 } 53 if strings.Contains(err.Error(), "Failed to extract 'IOPlatformUUID'") == false { 54 t.Errorf("Got unexpected error: %v", err) 55 } 56} 57 58func Test_machineID(t *testing.T) { 59 got, err := machineID() 60 if err != nil { 61 t.Error(err) 62 } 63 if got == "" { 64 t.Error("Got empty machine id") 65 } 66} 67