• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

yitsushi / macpot / 4284291729

pending completion
4284291729

Pull #10

github

GitHub
Merge 8169ebedc into be03cc87c
Pull Request #10: Bump github.com/stretchr/testify from 1.8.1 to 1.8.2

180 of 192 relevant lines covered (93.75%)

10.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/mac.go
1
package macpot
2

3
import (
4
        "crypto/rand"
5
        "encoding/binary"
6
        "fmt"
7
        "strings"
8
)
9

10
// first 3 octets are the OUI (Organisationally Unique Identifier)
11
// last 3 octets are the NIC (Network interface controler specific)
12
//
13
// first octet:
14
//   [d][d][d][d][d][d][gl][um]
15
//
16
//   um = 0: unicast; 1: multicast
17
//   gl = 0: global; 1: local
18
//
19
// For IPv4 we can use a deterministic value:
20
//   OUI: host machine specific
21
//   NIC: the last 3 bytes of the IP address
22

23
// MAC address.
24
type MAC struct {
25
        bytes []byte
26
}
27

28
// New creates a new MAC address with Options.
29
func New(options ...Option) (MAC, error) {
21✔
30
        mac := MAC{bytes: make([]byte, macByteLength)}
21✔
31

21✔
32
        if err := mac.FillRandom(); err != nil {
21✔
33
                return mac, err
×
34
        }
×
35

36
        for _, option := range options {
50✔
37
                err := option(&mac)
29✔
38
                if err != nil {
41✔
39
                        return mac, err
12✔
40
                }
12✔
41
        }
42

43
        return mac, nil
9✔
44
}
45

46
// NewFromBytes creates a new MAC address from bytes.
47
func NewFromBytes(value []byte) MAC {
16✔
48
        bytes := make([]byte, macByteLength)
16✔
49

16✔
50
        copy(bytes, value)
16✔
51

16✔
52
        return MAC{bytes: bytes}
16✔
53
}
16✔
54

55
// NewFromUint64 creates a new MAC address from Uint64.
56
func NewFromUint64(value uint64) MAC {
5✔
57
        bytes := make([]byte, intValueByteSize)
5✔
58

5✔
59
        binary.BigEndian.PutUint64(bytes, value)
5✔
60

5✔
61
        return MAC{bytes: bytes[(intValueByteSize - macByteLength):]}
5✔
62
}
5✔
63

64
// ToString returns the well known string representation of the MAC address.
65
func (mac MAC) ToString() string {
31✔
66
        output := []string{}
31✔
67

31✔
68
        for _, b := range mac.bytes {
217✔
69
                output = append(output, fmt.Sprintf("%02x", b))
186✔
70
        }
186✔
71

72
        return strings.Join(output, ":")
31✔
73
}
74

75
// FillRandom popupates the MAC address with random values.
76
func (mac *MAC) FillRandom() error {
21✔
77
        if _, err := rand.Read(mac.bytes); err != nil {
21✔
78
                return fmt.Errorf("unable to generate random data: %w", err)
×
79
        }
×
80

81
        return nil
21✔
82
}
83

84
// Bytes of the MAC address.
85
func (mac MAC) Bytes() []byte {
3✔
86
        return mac.bytes
3✔
87
}
3✔
88

89
// ToUint64 returns with the Uint64 representation of the MAC address.
90
func (mac MAC) ToUint64() uint64 {
1✔
91
        bytes := make([]byte, intValueByteSize)
1✔
92

1✔
93
        copy(bytes[intValueByteSize-macByteLength:], mac.Bytes())
1✔
94

1✔
95
        return binary.BigEndian.Uint64(bytes)
1✔
96
}
1✔
97

98
// Next generates the next MAC address.
99
func (mac MAC) Next() MAC {
2✔
100
        bytes := make([]byte, intValueByteSize)
2✔
101

2✔
102
        copy(bytes[intValueByteSize-macByteLength:], mac.Bytes())
2✔
103

2✔
104
        intValue := binary.BigEndian.Uint64(bytes)
2✔
105

2✔
106
        return NewFromUint64(intValue + 1)
2✔
107
}
2✔
108

109
// SetLocal forces the MAC address to be a Locally Administered address.
110
func (mac *MAC) SetLocal() {
3✔
111
        mac.bytes[0] |= secondBitOn
3✔
112
}
3✔
113

114
// SetGlobal forces the MAC address to be a Globally Unique address.
115
func (mac *MAC) SetGlobal() {
3✔
116
        mac.bytes[0] &= secondBitOff
3✔
117
}
3✔
118

119
// SetMulticast forces the MAC address to be a Multicast address.
120
func (mac *MAC) SetMulticast() {
3✔
121
        mac.bytes[0] |= firstBitOn
3✔
122
}
3✔
123

124
// SetUnicast forces the MAC address to be a Unicast address.
125
func (mac *MAC) SetUnicast() {
3✔
126
        mac.bytes[0] &= firstBitOff
3✔
127
}
3✔
128

129
// IsLocal checks if the address is a Locally Administered address.
130
func (mac MAC) IsLocal() bool {
10✔
131
        return mac.bytes[0]&secondBitOn == secondBitOn
10✔
132
}
10✔
133

134
// IsGlobal checks if the address is a Globally Unique address.
135
func (mac MAC) IsGlobal() bool {
5✔
136
        return !mac.IsLocal()
5✔
137
}
5✔
138

139
// IsMulticast checks if the address is a Multicast address.
140
func (mac MAC) IsMulticast() bool {
10✔
141
        return mac.bytes[0]&firstBitOn == firstBitOn
10✔
142
}
10✔
143

144
// IsUnicast checks if the address is a Unicast address.
145
func (mac MAC) IsUnicast() bool {
5✔
146
        return !mac.IsMulticast()
5✔
147
}
5✔
148

149
// SetOctet sets a specific octet in the MAC address.
150
func (mac *MAC) SetOctet(index int, value byte) error {
49✔
151
        if index < 0 || index > macByteLength {
51✔
152
                return OutOfBoundError{TargetIndex: index}
2✔
153
        }
2✔
154

155
        mac.bytes[index] = value
47✔
156

47✔
157
        return nil
47✔
158
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc