github.com/dtroyer-salad/og2/v2@v2.0.0-20240412154159-c47231610877/content/descriptor_test.go (about) 1 /* 2 Copyright The ORAS Authors. 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 16 package content 17 18 import ( 19 "reflect" 20 "testing" 21 22 "github.com/opencontainers/go-digest" 23 ocispec "github.com/opencontainers/image-spec/specs-go/v1" 24 "oras.land/oras-go/v2/internal/descriptor" 25 ) 26 27 func TestGenerateDescriptor(t *testing.T) { 28 contentFoo := []byte("foo") 29 contentBar := []byte("bar") 30 31 type args struct { 32 content []byte 33 mediaType string 34 } 35 tests := []struct { 36 name string 37 args args 38 want ocispec.Descriptor 39 }{ 40 { 41 name: "foo descriptor", 42 args: args{contentFoo, "example media type"}, 43 want: ocispec.Descriptor{ 44 MediaType: "example media type", 45 Digest: digest.FromBytes(contentFoo), 46 Size: int64(len(contentFoo))}, 47 }, 48 { 49 name: "empty content", 50 args: args{[]byte(""), "example media type"}, 51 want: ocispec.Descriptor{ 52 MediaType: "example media type", 53 Digest: digest.FromBytes([]byte("")), 54 Size: int64(len([]byte("")))}, 55 }, 56 { 57 name: "missing media type", 58 args: args{contentBar, ""}, 59 want: ocispec.Descriptor{ 60 MediaType: descriptor.DefaultMediaType, 61 Digest: digest.FromBytes(contentBar), 62 Size: int64(len(contentBar))}, 63 }, 64 } 65 for _, tt := range tests { 66 t.Run(tt.name, func(t *testing.T) { 67 got := NewDescriptorFromBytes(tt.args.mediaType, tt.args.content) 68 if !reflect.DeepEqual(got, tt.want) { 69 t.Errorf("GenerateDescriptor() = %v, want %v", got, tt.want) 70 } 71 }) 72 } 73 } 74 75 func TestEqual(t *testing.T) { 76 contentFoo := []byte("foo") 77 contentBar := []byte("bar") 78 type args struct { 79 a ocispec.Descriptor 80 b ocispec.Descriptor 81 } 82 tests := []struct { 83 name string 84 args args 85 want bool 86 }{ 87 { 88 name: "same media type, digest and size", 89 args: args{ 90 ocispec.Descriptor{ 91 MediaType: "example media type", 92 Digest: digest.FromBytes(contentFoo), 93 Size: int64(len(contentFoo))}, 94 ocispec.Descriptor{ 95 MediaType: "example media type", 96 Digest: digest.FromBytes(contentFoo), 97 Size: int64(len(contentFoo))}}, 98 want: true, 99 }, 100 { 101 name: "different media type, same digest and size", 102 args: args{ 103 ocispec.Descriptor{ 104 MediaType: "example media type", 105 Digest: digest.FromBytes(contentFoo), 106 Size: int64(len(contentFoo))}, 107 ocispec.Descriptor{ 108 MediaType: "another media type", 109 Digest: digest.FromBytes(contentFoo), 110 Size: int64(len(contentFoo))}}, 111 want: false, 112 }, 113 { 114 name: "different digest, same media type and size", 115 args: args{ 116 ocispec.Descriptor{ 117 MediaType: "example media type", 118 Digest: digest.FromBytes(contentFoo), 119 Size: int64(len(contentFoo))}, 120 ocispec.Descriptor{ 121 MediaType: "example media type", 122 Digest: digest.FromBytes(contentBar), 123 Size: int64(len(contentBar))}}, 124 want: false, 125 }, 126 { 127 name: "only same media type", 128 args: args{ 129 ocispec.Descriptor{ 130 MediaType: "example media type", 131 Digest: digest.FromBytes([]byte("fooooo")), 132 Size: int64(len([]byte("foooo")))}, 133 ocispec.Descriptor{ 134 MediaType: "example media type", 135 Digest: digest.FromBytes(contentBar), 136 Size: int64(len(contentBar))}}, 137 want: false, 138 }, 139 { 140 name: "different size, same media type and digest", 141 args: args{ 142 ocispec.Descriptor{ 143 MediaType: "example media type", 144 Digest: digest.FromBytes(contentFoo), 145 Size: int64(len(contentFoo))}, 146 ocispec.Descriptor{ 147 MediaType: "example media type", 148 Digest: digest.FromBytes(contentFoo), 149 Size: int64(len(contentFoo)) + 1}}, 150 want: false, 151 }, 152 { 153 name: "two empty descriptors", 154 args: args{ 155 ocispec.Descriptor{}, 156 ocispec.Descriptor{}}, 157 want: true, 158 }, 159 } 160 for _, tt := range tests { 161 t.Run(tt.name, func(t *testing.T) { 162 if got := Equal(tt.args.a, tt.args.b); got != tt.want { 163 t.Errorf("Equal() = %v, want %v", got, tt.want) 164 } 165 }) 166 } 167 }