summaryrefslogtreecommitdiffstats
path: root/net/test/cstruct_test.py
blob: af1fc4291e018ded984b4100fc07535a0101d7b6 (plain)
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/python
#
# Copyright 2016 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest

import cstruct


# These aren't constants, they're classes. So, pylint: disable=invalid-name
TestStructA = cstruct.Struct("TestStructA", "=BI", "byte1 int2")
TestStructB = cstruct.Struct("TestStructB", "=BI", "byte1 int2")


class CstructTest(unittest.TestCase):

  def CheckEquals(self, a, b):
    self.assertEqual(a, b)
    self.assertEqual(b, a)
    assert a == b
    assert b == a
    assert not (a != b)  # pylint: disable=g-comparison-negation,superfluous-parens
    assert not (b != a)  # pylint: disable=g-comparison-negation,superfluous-parens

  def CheckNotEquals(self, a, b):
    self.assertNotEqual(a, b)
    self.assertNotEqual(b, a)
    assert a != b
    assert b != a
    assert not (a == b)  # pylint: disable=g-comparison-negation,superfluous-parens
    assert not (b == a)  # pylint: disable=g-comparison-negation,superfluous-parens

  def testEqAndNe(self):
    a1 = TestStructA((1, 2))
    a2 = TestStructA((2, 3))
    a3 = TestStructA((1, 2))
    b = TestStructB((1, 2))
    self.CheckNotEquals(a1, b)
    self.CheckNotEquals(a2, b)
    self.CheckNotEquals(a1, a2)
    self.CheckNotEquals(a2, a3)
    for i in [a1, a2, a3, b]:
      self.CheckEquals(i, i)
    self.CheckEquals(a1, a3)

  def testNestedStructs(self):
    Nested = cstruct.Struct("Nested", "!HSSi",
                            "word1 nest2 nest3 int4",
                            [TestStructA, TestStructB])
    DoubleNested = cstruct.Struct("DoubleNested", "SSB",
                                  "nest1 nest2 byte3",
                                  [TestStructA, Nested])
    d = DoubleNested((TestStructA((1, 2)),
                      Nested((5, TestStructA((3, 4)), TestStructB((7, 8)), 9)),
                      6))

    expectedlen = (len(TestStructA) +
                   2 + len(TestStructA) + len(TestStructB) + 4 +
                   1)
    self.assertEqual(expectedlen, len(DoubleNested))

    self.assertEqual(7, d.nest2.nest3.byte1)

    d.byte3 = 252
    d.nest2.word1 = 33214
    n = d.nest2
    n.int4 = -55
    t = n.nest3
    t.int2 = 33627591

    self.assertEqual(33627591, d.nest2.nest3.int2)

    expected = (
        "DoubleNested(nest1=TestStructA(byte1=1, int2=2),"
        " nest2=Nested(word1=33214, nest2=TestStructA(byte1=3, int2=4),"
        " nest3=TestStructB(byte1=7, int2=33627591), int4=-55), byte3=252)")
    self.assertEqual(expected, str(d))
    expected = ("01" "02000000"
                "81be" "03" "04000000"
                "07" "c71d0102" "ffffffc9" "fc").decode("hex")
    self.assertEqual(expected, d.Pack())
    unpacked = DoubleNested(expected)
    self.CheckEquals(unpacked, d)

  def testNullTerminatedStrings(self):
    TestStruct = cstruct.Struct("TestStruct", "B16si16AH",
                                "byte1 string2 int3 ascii4 word5")
    nullstr = "hello" + (16 - len("hello")) * "\x00"

    t = TestStruct((2, nullstr, 12345, nullstr, 33210))
    expected = ("TestStruct(byte1=2, string2=68656c6c6f0000000000000000000000,"
                " int3=12345, ascii4=hello, word5=33210)")
    self.assertEqual(expected, str(t))

    embeddednull = "hello\x00visible123"
    t = TestStruct((2, embeddednull, 12345, embeddednull, 33210))
    expected = ("TestStruct(byte1=2, string2=68656c6c6f0076697369626c65313233,"
                " int3=12345, ascii4=hello\x00visible123, word5=33210)")
    self.assertEqual(expected, str(t))

  def testZeroInitialization(self):
    TestStruct = cstruct.Struct("TestStruct", "B16si16AH",
                                "byte1 string2 int3 ascii4 word5")
    t = TestStruct()
    self.assertEqual(0, t.byte1)
    self.assertEqual("\x00" * 16, t.string2)
    self.assertEqual(0, t.int3)
    self.assertEqual("\x00" * 16, t.ascii4)
    self.assertEqual(0, t.word5)
    self.assertEqual("\x00" * len(TestStruct), t.Pack())

  def testKeywordInitialization(self):
    TestStruct = cstruct.Struct("TestStruct", "=B16sIH",
                                "byte1 string2 int3 word4")
    text = "hello world! ^_^"
    text_bytes = text.encode("hex")

    # Populate all fields
    t1 = TestStruct(byte1=1, string2=text, int3=0xFEDCBA98, word4=0x1234)
    expected = ("01" + text_bytes + "98BADCFE" "3412").decode("hex")
    self.assertEqual(expected, t1.Pack())

    # Partially populated
    t1 = TestStruct(string2=text, word4=0x1234)
    expected = ("00" + text_bytes + "00000000" "3412").decode("hex")
    self.assertEqual(expected, t1.Pack())

  def testCstructOffset(self):
    TestStruct = cstruct.Struct("TestStruct", "B16si16AH",
                                "byte1 string2 int3 ascii4 word5")
    nullstr = "hello" + (16 - len("hello")) * "\x00"
    t = TestStruct((2, nullstr, 12345, nullstr, 33210))
    self.assertEqual(0, t.offset("byte1"))
    self.assertEqual(1, t.offset("string2"))  # sizeof(byte)
    self.assertEqual(17, t.offset("int3"))    # sizeof(byte) + 16*sizeof(char)
    # The integer is automatically padded by the struct module
    # to match native alignment.
    # offset = sizeof(byte) + 16*sizeof(char) + padding + sizeof(int)
    self.assertEqual(24, t.offset("ascii4"))
    self.assertEqual(40, t.offset("word5"))
    self.assertRaises(KeyError, t.offset, "random")

    # TODO: Add support for nested struct offset
    Nested = cstruct.Struct("Nested", "!HSSi", "word1 nest2 nest3 int4",
                            [TestStructA, TestStructB])
    DoubleNested = cstruct.Struct("DoubleNested", "SSB", "nest1 nest2 byte3",
                                  [TestStructA, Nested])
    d = DoubleNested((TestStructA((1, 2)), Nested((5, TestStructA((3, 4)),
                                                   TestStructB((7, 8)), 9)), 6))
    self.assertEqual(0, d.offset("nest1"))
    self.assertEqual(len(TestStructA), d.offset("nest2"))
    self.assertEqual(len(TestStructA) + len(Nested), d.offset("byte3"))
    self.assertRaises(KeyError, t.offset, "word1")

  def testDefinitionFieldMismatch(self):
    cstruct.Struct("TestA", "=BI", "byte1 int2")
    cstruct.Struct("TestA", "=BxxxxxIx", "byte1 int2")
    with self.assertRaises(ValueError):
      cstruct.Struct("TestA", "=B", "byte1 int2")
    with self.assertRaises(ValueError):
      cstruct.Struct("TestA", "=BI", "byte1")

    Nested = cstruct.Struct("Nested", "!II", "int1 int2")
    cstruct.Struct("TestB", "=BSI", "byte1 nest2 int3", [Nested])
    cstruct.Struct("TestB", "=BxSxIx", "byte1 nest2 int3", [Nested])
    with self.assertRaises(ValueError):
      cstruct.Struct("TestB", "=BSI", "byte1 int3", [Nested])
    with self.assertRaises(ValueError):
      cstruct.Struct("TestB", "=BSI", "byte1 nest2", [Nested])

    cstruct.Struct("TestC", "=BSSI", "byte1 nest2 nest3 int4", [Nested, Nested])
    with self.assertRaises(ValueError):
      cstruct.Struct("TestC", "=BSSI", "byte1 nest2 int4", [Nested, Nested])


if __name__ == "__main__":
  unittest.main()