...

Source file src/github.com/containerd/typeurl/v2/types_test.go

Documentation: github.com/containerd/typeurl/v2

     1  /*
     2     Copyright The containerd Authors.
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package typeurl
    18  
    19  import (
    20  	"bytes"
    21  	"reflect"
    22  	"testing"
    23  	"time"
    24  
    25  	"github.com/gogo/protobuf/proto"
    26  	gogotypes "github.com/gogo/protobuf/types"
    27  	"google.golang.org/protobuf/types/known/anypb"
    28  	"google.golang.org/protobuf/types/known/timestamppb"
    29  )
    30  
    31  type test struct {
    32  	Name string
    33  	Age  int
    34  }
    35  
    36  func clear() {
    37  	registry = make(map[reflect.Type]string)
    38  }
    39  
    40  var _ Any = &gogotypes.Any{}
    41  var _ Any = &anypb.Any{}
    42  
    43  func TestRegisterPointerGetPointer(t *testing.T) {
    44  	clear()
    45  	expected := "test"
    46  	Register(&test{}, "test")
    47  
    48  	url, err := TypeURL(&test{})
    49  	if err != nil {
    50  		t.Fatal(err)
    51  	}
    52  	if url != expected {
    53  		t.Fatalf("expected %q but received %q", expected, url)
    54  	}
    55  }
    56  
    57  func TestMarshal(t *testing.T) {
    58  	clear()
    59  	expected := "test"
    60  	Register(&test{}, "test")
    61  
    62  	v := &test{
    63  		Name: "koye",
    64  		Age:  6,
    65  	}
    66  	any, err := MarshalAny(v)
    67  	if err != nil {
    68  		t.Fatal(err)
    69  	}
    70  	if any.GetTypeUrl() != expected {
    71  		t.Fatalf("expected %q but received %q", expected, any.GetTypeUrl())
    72  	}
    73  
    74  	// marshal it again and make sure we get the same thing back.
    75  	newany, err := MarshalAny(any)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  
    80  	val := any.GetValue()
    81  	newval := newany.GetValue()
    82  
    83  	// Ensure pointer to same exact slice
    84  	newval[0] = val[0] ^ 0xff
    85  
    86  	if !bytes.Equal(newval, val) {
    87  		t.Fatalf("expected to get back same object: %v != %v", newany, any)
    88  	}
    89  
    90  }
    91  
    92  func TestMarshalUnmarshal(t *testing.T) {
    93  	clear()
    94  	Register(&test{}, "test")
    95  
    96  	v := &test{
    97  		Name: "koye",
    98  		Age:  6,
    99  	}
   100  	any, err := MarshalAny(v)
   101  	if err != nil {
   102  		t.Fatal(err)
   103  	}
   104  	nv, err := UnmarshalAny(any)
   105  	if err != nil {
   106  		t.Fatal(err)
   107  	}
   108  	td, ok := nv.(*test)
   109  	if !ok {
   110  		t.Fatal("expected value to cast to *test")
   111  	}
   112  	if td.Name != "koye" {
   113  		t.Fatal("invalid name")
   114  	}
   115  	if td.Age != 6 {
   116  		t.Fatal("invalid age")
   117  	}
   118  }
   119  
   120  func TestMarshalUnmarshalTo(t *testing.T) {
   121  	clear()
   122  	Register(&test{}, "test")
   123  
   124  	in := &test{
   125  		Name: "koye",
   126  		Age:  6,
   127  	}
   128  	any, err := MarshalAny(in)
   129  	if err != nil {
   130  		t.Fatal(err)
   131  	}
   132  	out := &test{}
   133  	err = UnmarshalTo(any, out)
   134  	if err != nil {
   135  		t.Fatal(err)
   136  	}
   137  	if out.Name != "koye" {
   138  		t.Fatal("invalid name")
   139  	}
   140  	if out.Age != 6 {
   141  		t.Fatal("invalid age")
   142  	}
   143  }
   144  
   145  type test2 struct {
   146  	Name string
   147  }
   148  
   149  func TestUnmarshalToInvalid(t *testing.T) {
   150  	clear()
   151  	Register(&test{}, "test1")
   152  	Register(&test2{}, "test2")
   153  
   154  	in := &test{
   155  		Name: "koye",
   156  		Age:  6,
   157  	}
   158  	any, err := MarshalAny(in)
   159  	if err != nil {
   160  		t.Fatal(err)
   161  	}
   162  
   163  	out := &test2{}
   164  	err = UnmarshalTo(any, out)
   165  	if err == nil || err.Error() != `can't unmarshal type "test1" to output "test2"` {
   166  		t.Fatalf("unexpected result: %+v", err)
   167  	}
   168  }
   169  
   170  func TestIs(t *testing.T) {
   171  	clear()
   172  	Register(&test{}, "test")
   173  
   174  	v := &test{
   175  		Name: "koye",
   176  		Age:  6,
   177  	}
   178  	any, err := MarshalAny(v)
   179  	if err != nil {
   180  		t.Fatal(err)
   181  	}
   182  	if !Is(any, &test{}) {
   183  		t.Fatal("Is(any, test{}) should be true")
   184  	}
   185  }
   186  
   187  func TestRegisterDiffUrls(t *testing.T) {
   188  	clear()
   189  	defer func() {
   190  		if err := recover(); err == nil {
   191  			t.Error("registering the same type with different urls should panic")
   192  		}
   193  	}()
   194  	Register(&test{}, "test")
   195  	Register(&test{}, "test", "two")
   196  }
   197  
   198  func TestCheckNil(t *testing.T) {
   199  	var a *anyType
   200  
   201  	actual := a.GetValue()
   202  	if actual != nil {
   203  		t.Fatalf("expected nil, got %v", actual)
   204  	}
   205  }
   206  
   207  func TestProtoFallback(t *testing.T) {
   208  	expected := time.Now()
   209  	b, err := proto.Marshal(timestamppb.New(expected))
   210  	if err != nil {
   211  		t.Fatal(err)
   212  	}
   213  	x, err := UnmarshalByTypeURL("type.googleapis.com/google.protobuf.Timestamp", b)
   214  	if err != nil {
   215  		t.Fatal(err)
   216  	}
   217  	ts, ok := x.(*timestamppb.Timestamp)
   218  	if !ok {
   219  		t.Fatalf("failed to convert %+v to Timestamp", x)
   220  	}
   221  	if expected.Sub(ts.AsTime()) != 0 {
   222  		t.Fatalf("expected %+v but got %+v", expected, ts.AsTime())
   223  	}
   224  }
   225  

View as plain text