github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/twinj/uuid/state_test.go (about) 1 package uuid 2 3 /**************** 4 * Date: 14/02/14 5 * Time: 9:08 PM 6 ***************/ 7 8 import ( 9 "bytes" 10 "fmt" 11 "net" 12 "testing" 13 ) 14 15 var state_bytes = []byte{ 16 0xAA, 0xCF, 0xEE, 0x12, 17 0xD4, 0x00, 18 0x27, 0x23, 19 0x00, 20 0xD3, 21 0x23, 0x12, 0x4A, 0x11, 0x89, 0xFF, 22 } 23 24 25 func TestUUID_getHardwareAddress(t *testing.T) { 26 intfcs, err := net.Interfaces() 27 if err != nil { 28 return 29 } 30 addr := getHardwareAddress(intfcs) 31 if addr == nil { 32 return 33 } 34 fmt.Println(addr) 35 } 36 37 func TestUUID_StateSeed(t *testing.T) { 38 if state.past < Timestamp((1391463463*10000000)+(100*10)+gregorianToUNIXOffset) { 39 t.Errorf("Expected a value greater than 02/03/2014 @ 9:37pm in UTC but got %d", state.past) 40 } 41 if state.node == nil { 42 t.Errorf("Expected a non nil node") 43 } 44 if state.sequence <= 0 { 45 t.Errorf("Expected a value greater than but got %d", state.sequence) 46 } 47 } 48 49 func TestUUID_State_read(t *testing.T) { 50 s := new(State) 51 s.past = Timestamp((1391463463 * 10000000) + (100 * 10) + gregorianToUNIXOffset) 52 s.node = state_bytes 53 54 now := Timestamp((1391463463 * 10000000) + (100 * 10)) 55 s.read(now+(100*10), net.HardwareAddr(make([]byte, length))) 56 if s.sequence != 1 { 57 t.Error("The sequence should increment when the time is"+ 58 "older than the state past time and the node"+ 59 "id are not the same.", s.sequence) 60 } 61 s.read(now, net.HardwareAddr(state_bytes)) 62 63 if s.sequence == 1 { 64 t.Error("The sequence should be randomly generated when"+ 65 " the nodes are equal.", s.sequence) 66 } 67 68 s = new(State) 69 s.past = Timestamp((1391463463 * 10000000) + (100 * 10) + gregorianToUNIXOffset) 70 s.node = state_bytes 71 s.randomSequence = true 72 s.read(now, net.HardwareAddr(make([]byte, length))) 73 74 if s.sequence == 0 { 75 t.Error("The sequence should be randomly generated when"+ 76 " the randomSequence flag is set.", s.sequence) 77 } 78 79 if s.past != now { 80 t.Error("The past time should equal the time passed in" + 81 " the method.") 82 } 83 84 if !bytes.Equal(s.node, make([]byte, length)) { 85 t.Error("The node id should equal the node passed in" + 86 " the method.") 87 } 88 } 89 90 func TestUUID_State_init(t *testing.T) { 91 92 }