github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/tools/go_marshal/test/test.go (about)

     1  // Copyright 2019 The gVisor Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Package test contains data structures for testing the go_marshal tool.
    16  package test
    17  
    18  import (
    19  	// We're intentionally using a package name alias here even though it's not
    20  	// necessary to test the code generator's ability to handle package aliases.
    21  	ex "github.com/SagerNet/gvisor/tools/go_marshal/test/external"
    22  )
    23  
    24  // Type1 is a test data type.
    25  //
    26  // +marshal slice:Type1Slice
    27  type Type1 struct {
    28  	a    Type2
    29  	x, y int64 // Multiple field names.
    30  	b    byte  `marshal:"unaligned"` // Short field.
    31  	c    uint64
    32  	_    uint32  // Unnamed scalar field.
    33  	_    [6]byte // Unnamed vector field, typical padding.
    34  	_    [2]byte
    35  	xs   [8]int32
    36  	as   [10]Type2 `marshal:"unaligned"` // Array of Marshallable objects.
    37  	ss   Type3
    38  }
    39  
    40  // Type2 is a test data type.
    41  //
    42  // +marshal
    43  type Type2 struct {
    44  	n int64
    45  	c byte
    46  	_ [7]byte
    47  	m int64
    48  	a int64
    49  }
    50  
    51  // Type3 is a test data type.
    52  //
    53  // +marshal
    54  type Type3 struct {
    55  	s int64
    56  	x ex.External // Type defined in another package.
    57  }
    58  
    59  // Type4 is a test data type.
    60  //
    61  // +marshal
    62  type Type4 struct {
    63  	c byte
    64  	x int64 `marshal:"unaligned"`
    65  	d byte
    66  	_ [7]byte
    67  }
    68  
    69  // Type5 is a test data type.
    70  //
    71  // +marshal
    72  type Type5 struct {
    73  	n int64
    74  	t Type4
    75  	m int64
    76  }
    77  
    78  // Type6 is a test data type ends mid-word.
    79  //
    80  // +marshal
    81  type Type6 struct {
    82  	a int64
    83  	b int64
    84  	// If c isn't marked unaligned, analysis fails (as it should, since
    85  	// the unsafe API corrupts Type7).
    86  	c byte `marshal:"unaligned"`
    87  }
    88  
    89  // Type7 is a test data type that contains a child struct that ends
    90  // mid-word.
    91  // +marshal
    92  type Type7 struct {
    93  	x Type6
    94  	y int64
    95  }
    96  
    97  // Type8 is a test data type which contains an external non-packed field.
    98  //
    99  // +marshal slice:Type8Slice
   100  type Type8 struct {
   101  	a  int64
   102  	np ex.NotPacked
   103  	b  int64
   104  }
   105  
   106  // Timespec represents struct timespec in <time.h>.
   107  //
   108  // +marshal
   109  type Timespec struct {
   110  	Sec  int64
   111  	Nsec int64
   112  }
   113  
   114  // Stat represents struct stat.
   115  //
   116  // +marshal slice:StatSlice
   117  type Stat struct {
   118  	Dev     uint64
   119  	Ino     uint64
   120  	Nlink   uint64
   121  	Mode    uint32
   122  	UID     uint32
   123  	GID     uint32
   124  	_       int32
   125  	Rdev    uint64
   126  	Size    int64
   127  	Blksize int64
   128  	Blocks  int64
   129  	ATime   Timespec
   130  	MTime   Timespec
   131  	CTime   Timespec
   132  	_       [3]int64
   133  }
   134  
   135  // InetAddr is an example marshallable newtype on an array.
   136  //
   137  // +marshal
   138  type InetAddr [4]byte
   139  
   140  // SignalSet is an example marshallable newtype on a primitive.
   141  //
   142  // +marshal slice:SignalSetSlice:inner
   143  type SignalSet uint64
   144  
   145  // SignalSetAlias is an example newtype on another marshallable type.
   146  //
   147  // +marshal slice:SignalSetAliasSlice
   148  type SignalSetAlias SignalSet
   149  
   150  const sizeA = 64
   151  const sizeB = 8
   152  
   153  // TestArray is a test data structure on an array with a constant length.
   154  //
   155  // +marshal
   156  type TestArray [sizeA]int32
   157  
   158  // TestArray2 is a newtype on an array with a simple arithmetic expression of
   159  // constants for the array length.
   160  //
   161  // +marshal
   162  type TestArray2 [sizeA * sizeB]int32
   163  
   164  // TestArray3 is a newtype on an array with a simple arithmetic expression of
   165  // mixed constants and literals for the array length.
   166  //
   167  // +marshal
   168  type TestArray3 [sizeA*sizeB + 12]int32
   169  
   170  // Type9 is a test data type containing an array with a non-literal length.
   171  //
   172  // +marshal
   173  type Type9 struct {
   174  	x int64
   175  	y [sizeA]int32
   176  }
   177  
   178  // Type10Embed is a test data type which is be embedded into another type.
   179  //
   180  // +marshal
   181  type Type10Embed struct {
   182  	x int64
   183  }
   184  
   185  // Type10 is a test data type which contains an embedded struct.
   186  //
   187  // +marshal
   188  type Type10 struct {
   189  	Type10Embed
   190  	y int64
   191  }
   192  
   193  // Type11 is a test data type which contains an embedded struct from an external
   194  // package.
   195  //
   196  // +marshal
   197  type Type11 struct {
   198  	ex.External
   199  	y int64
   200  }