-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathendian_test.go
117 lines (100 loc) · 2.88 KB
/
endian_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2023 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package arch_test
import (
"encoding/binary"
"fmt"
. "gopkg.in/check.v1"
"github.com/snapcore/snapd/arch"
)
type endianTestSuite struct{}
var _ = Suite(&endianTestSuite{})
// back in 14.04/16.04 32bit powerpc was supported via gccgo
var knownGccGoArch = map[string]bool{
"ppc": true,
}
// copied from:
// https://github.com/golang/go/blob/release-branch.go1.20/src/go/build/syslist.go#L53
// alternatively this could be done via "go tool dist list" but seems not
// worth the extra parsing
var knownArch = map[string]bool{
"386": true,
"amd64": true,
"amd64p32": true,
"arm": true,
"armbe": true,
"arm64": true,
"arm64be": true,
"loong64": true,
"mips": true,
"mipsle": true,
"mips64": true,
"mips64le": true,
"mips64p32": true,
"mips64p32le": true,
"ppc": true,
"ppc64": true,
"ppc64le": true,
"riscv": true,
"riscv64": true,
"s390": true,
"s390x": true,
"sparc": true,
"sparc64": true,
"wasm": true,
}
func knownGoArch(arch string) error {
// this knownGccGoArch map can be removed after 16.04 goes EOL
// in 2026
if knownGccGoArch[arch] {
return nil
}
if knownArch[arch] {
return nil
}
return fmt.Errorf("cannot find %s in supported go arches", arch)
}
func (s *endianTestSuite) TestKnownGoArch(c *C) {
c.Check(knownGoArch("not-supported-arch"), ErrorMatches, "cannot find not-supported-arch in supported go arches")
}
func (s *endianTestSuite) TestEndian(c *C) {
for _, t := range []struct {
arch string
endian binary.ByteOrder
}{
{"ppc", binary.BigEndian},
{"ppc64", binary.BigEndian},
{"s390x", binary.BigEndian},
{"386", binary.LittleEndian},
{"amd64", binary.LittleEndian},
{"arm", binary.LittleEndian},
{"arm64", binary.LittleEndian},
{"ppc64le", binary.LittleEndian},
{"riscv64", binary.LittleEndian},
} {
restore := arch.MockRuntimeGOARCH(t.arch)
defer restore()
c.Check(arch.Endian(), Equals, t.endian)
c.Check(knownGoArch(t.arch), IsNil)
}
}
func (s *endianTestSuite) TestEndianErrors(c *C) {
restore := arch.MockRuntimeGOARCH("unknown-arch")
defer restore()
c.Check(func() { arch.Endian() }, Panics, "unknown architecture unknown-arch")
}