github.com/letsencrypt/boulder@v0.20251208.0/grpc/pb-marshalling_test.go (about) 1 package grpc 2 3 import ( 4 "encoding/json" 5 "net/netip" 6 "testing" 7 "time" 8 9 "github.com/go-jose/go-jose/v4" 10 "google.golang.org/protobuf/types/known/timestamppb" 11 12 "github.com/letsencrypt/boulder/core" 13 corepb "github.com/letsencrypt/boulder/core/proto" 14 "github.com/letsencrypt/boulder/identifier" 15 "github.com/letsencrypt/boulder/probs" 16 "github.com/letsencrypt/boulder/test" 17 ) 18 19 const JWK1JSON = `{"kty":"RSA","n":"vuc785P8lBj3fUxyZchF_uZw6WtbxcorqgTyq-qapF5lrO1U82Tp93rpXlmctj6fyFHBVVB5aXnUHJ7LZeVPod7Wnfl8p5OyhlHQHC8BnzdzCqCMKmWZNX5DtETDId0qzU7dPzh0LP0idt5buU7L9QNaabChw3nnaL47iu_1Di5Wp264p2TwACeedv2hfRDjDlJmaQXuS8Rtv9GnRWyC9JBu7XmGvGDziumnJH7Hyzh3VNu-kSPQD3vuAFgMZS6uUzOztCkT0fpOalZI6hqxtWLvXUMj-crXrn-Maavz8qRhpAyp5kcYk3jiHGgQIi7QSK2JIdRJ8APyX9HlmTN5AQ","e":"AQAB"}` 20 21 func TestProblemDetails(t *testing.T) { 22 pb, err := ProblemDetailsToPB(nil) 23 test.AssertNotEquals(t, err, "problemDetailToPB failed") 24 test.Assert(t, pb == nil, "Returned corepb.ProblemDetails is not nil") 25 26 prob := &probs.ProblemDetails{Type: probs.TLSProblem, Detail: "asd", HTTPStatus: 200} 27 pb, err = ProblemDetailsToPB(prob) 28 test.AssertNotError(t, err, "problemDetailToPB failed") 29 test.Assert(t, pb != nil, "return corepb.ProblemDetails is nill") 30 test.AssertDeepEquals(t, pb.ProblemType, string(prob.Type)) 31 test.AssertEquals(t, pb.Detail, prob.Detail) 32 test.AssertEquals(t, int(pb.HttpStatus), prob.HTTPStatus) 33 34 recon, err := PBToProblemDetails(pb) 35 test.AssertNotError(t, err, "PBToProblemDetails failed") 36 test.AssertDeepEquals(t, recon, prob) 37 38 recon, err = PBToProblemDetails(nil) 39 test.AssertNotError(t, err, "PBToProblemDetails failed") 40 test.Assert(t, recon == nil, "Returned core.PRoblemDetails is not nil") 41 _, err = PBToProblemDetails(&corepb.ProblemDetails{}) 42 test.AssertError(t, err, "PBToProblemDetails did not fail") 43 test.AssertEquals(t, err, ErrMissingParameters) 44 _, err = PBToProblemDetails(&corepb.ProblemDetails{ProblemType: ""}) 45 test.AssertError(t, err, "PBToProblemDetails did not fail") 46 test.AssertEquals(t, err, ErrMissingParameters) 47 _, err = PBToProblemDetails(&corepb.ProblemDetails{Detail: ""}) 48 test.AssertError(t, err, "PBToProblemDetails did not fail") 49 test.AssertEquals(t, err, ErrMissingParameters) 50 } 51 52 func TestChallenge(t *testing.T) { 53 var jwk jose.JSONWebKey 54 err := json.Unmarshal([]byte(JWK1JSON), &jwk) 55 test.AssertNotError(t, err, "Failed to unmarshal test key") 56 validated := time.Now().Round(0).UTC() 57 chall := core.Challenge{ 58 Type: core.ChallengeTypeDNS01, 59 Status: core.StatusValid, 60 Token: "asd", 61 Validated: &validated, 62 } 63 64 pb, err := ChallengeToPB(chall) 65 test.AssertNotError(t, err, "ChallengeToPB failed") 66 test.Assert(t, pb != nil, "Returned corepb.Challenge is nil") 67 68 recon, err := PBToChallenge(pb) 69 test.AssertNotError(t, err, "PBToChallenge failed") 70 test.AssertDeepEquals(t, recon, chall) 71 72 ip := netip.MustParseAddr("1.1.1.1") 73 chall.ValidationRecord = []core.ValidationRecord{ 74 { 75 Hostname: "example.com", 76 Port: "2020", 77 AddressesResolved: []netip.Addr{ip}, 78 AddressUsed: ip, 79 URL: "https://example.com:2020", 80 AddressesTried: []netip.Addr{ip}, 81 }, 82 } 83 chall.Error = &probs.ProblemDetails{Type: probs.TLSProblem, Detail: "asd", HTTPStatus: 200} 84 pb, err = ChallengeToPB(chall) 85 test.AssertNotError(t, err, "ChallengeToPB failed") 86 test.Assert(t, pb != nil, "Returned corepb.Challenge is nil") 87 88 recon, err = PBToChallenge(pb) 89 test.AssertNotError(t, err, "PBToChallenge failed") 90 test.AssertDeepEquals(t, recon, chall) 91 92 _, err = PBToChallenge(nil) 93 test.AssertError(t, err, "PBToChallenge did not fail") 94 test.AssertEquals(t, err, ErrMissingParameters) 95 _, err = PBToChallenge(&corepb.Challenge{}) 96 test.AssertError(t, err, "PBToChallenge did not fail") 97 test.AssertEquals(t, err, ErrMissingParameters) 98 99 challNilValidation := core.Challenge{ 100 Type: core.ChallengeTypeDNS01, 101 Status: core.StatusValid, 102 Token: "asd", 103 Validated: nil, 104 } 105 pb, err = ChallengeToPB(challNilValidation) 106 test.AssertNotError(t, err, "ChallengeToPB failed") 107 test.Assert(t, pb != nil, "Returned corepb.Challenge is nil") 108 recon, err = PBToChallenge(pb) 109 test.AssertNotError(t, err, "PBToChallenge failed") 110 test.AssertDeepEquals(t, recon, challNilValidation) 111 } 112 113 func TestValidationRecord(t *testing.T) { 114 ip := netip.MustParseAddr("1.1.1.1") 115 vr := core.ValidationRecord{ 116 Hostname: "exampleA.com", 117 Port: "80", 118 AddressesResolved: []netip.Addr{ip}, 119 AddressUsed: ip, 120 URL: "http://exampleA.com", 121 AddressesTried: []netip.Addr{ip}, 122 ResolverAddrs: []string{"resolver:5353"}, 123 } 124 125 pb, err := ValidationRecordToPB(vr) 126 test.AssertNotError(t, err, "ValidationRecordToPB failed") 127 test.Assert(t, pb != nil, "Return core.ValidationRecord is nil") 128 129 recon, err := PBToValidationRecord(pb) 130 test.AssertNotError(t, err, "PBToValidationRecord failed") 131 test.AssertDeepEquals(t, recon, vr) 132 } 133 134 func TestValidationResult(t *testing.T) { 135 ip := netip.MustParseAddr("1.1.1.1") 136 vrA := core.ValidationRecord{ 137 Hostname: "exampleA.com", 138 Port: "443", 139 AddressesResolved: []netip.Addr{ip}, 140 AddressUsed: ip, 141 URL: "https://exampleA.com", 142 AddressesTried: []netip.Addr{ip}, 143 ResolverAddrs: []string{"resolver:5353"}, 144 } 145 vrB := core.ValidationRecord{ 146 Hostname: "exampleB.com", 147 Port: "443", 148 AddressesResolved: []netip.Addr{ip}, 149 AddressUsed: ip, 150 URL: "https://exampleB.com", 151 AddressesTried: []netip.Addr{ip}, 152 ResolverAddrs: []string{"resolver:5353"}, 153 } 154 result := []core.ValidationRecord{vrA, vrB} 155 prob := &probs.ProblemDetails{Type: probs.TLSProblem, Detail: "asd", HTTPStatus: 200} 156 157 pb, err := ValidationResultToPB(result, prob, "surreal", "ARIN") 158 test.AssertNotError(t, err, "ValidationResultToPB failed") 159 test.Assert(t, pb != nil, "Returned vapb.ValidationResult is nil") 160 test.AssertEquals(t, pb.Perspective, "surreal") 161 test.AssertEquals(t, pb.Rir, "ARIN") 162 163 reconResult, reconProb, err := pbToValidationResult(pb) 164 test.AssertNotError(t, err, "pbToValidationResult failed") 165 test.AssertDeepEquals(t, reconResult, result) 166 test.AssertDeepEquals(t, reconProb, prob) 167 } 168 169 func TestRegistration(t *testing.T) { 170 var key jose.JSONWebKey 171 err := json.Unmarshal([]byte(` 172 { 173 "e": "AQAB", 174 "kty": "RSA", 175 "n": "tSwgy3ORGvc7YJI9B2qqkelZRUC6F1S5NwXFvM4w5-M0TsxbFsH5UH6adigV0jzsDJ5imAechcSoOhAh9POceCbPN1sTNwLpNbOLiQQ7RD5mY_pSUHWXNmS9R4NZ3t2fQAzPeW7jOfF0LKuJRGkekx6tXP1uSnNibgpJULNc4208dgBaCHo3mvaE2HV2GmVl1yxwWX5QZZkGQGjNDZYnjFfa2DKVvFs0QbAk21ROm594kAxlRlMMrvqlf24Eq4ERO0ptzpZgm_3j_e4hGRD39gJS7kAzK-j2cacFQ5Qi2Y6wZI2p-FCq_wiYsfEAIkATPBiLKl_6d_Jfcvs_impcXQ" 176 } 177 `), &key) 178 test.AssertNotError(t, err, "Could not unmarshal testing key") 179 createdAt := time.Now().Round(0).UTC() 180 inReg := core.Registration{ 181 ID: 1, 182 Key: &key, 183 Agreement: "yup", 184 CreatedAt: &createdAt, 185 Status: core.StatusValid, 186 } 187 pbReg, err := RegistrationToPB(inReg) 188 test.AssertNotError(t, err, "registrationToPB failed") 189 outReg, err := PbToRegistration(pbReg) 190 test.AssertNotError(t, err, "PbToRegistration failed") 191 test.AssertDeepEquals(t, inReg, outReg) 192 193 inRegNilCreatedAt := core.Registration{ 194 ID: 1, 195 Key: &key, 196 Agreement: "yup", 197 CreatedAt: nil, 198 Status: core.StatusValid, 199 } 200 pbReg, err = RegistrationToPB(inRegNilCreatedAt) 201 test.AssertNotError(t, err, "registrationToPB failed") 202 outReg, err = PbToRegistration(pbReg) 203 test.AssertNotError(t, err, "PbToRegistration failed") 204 test.AssertDeepEquals(t, inRegNilCreatedAt, outReg) 205 } 206 207 func TestAuthz(t *testing.T) { 208 exp := time.Now().AddDate(0, 0, 1).UTC() 209 ident := identifier.NewDNS("example.com") 210 challA := core.Challenge{ 211 Type: core.ChallengeTypeDNS01, 212 Status: core.StatusPending, 213 Token: "asd", 214 } 215 challB := core.Challenge{ 216 Type: core.ChallengeTypeDNS01, 217 Status: core.StatusPending, 218 Token: "asd2", 219 } 220 inAuthz := core.Authorization{ 221 ID: "1", 222 Identifier: ident, 223 RegistrationID: 5, 224 Status: core.StatusPending, 225 Expires: &exp, 226 Challenges: []core.Challenge{challA, challB}, 227 } 228 pbAuthz, err := AuthzToPB(inAuthz) 229 test.AssertNotError(t, err, "AuthzToPB failed") 230 outAuthz, err := PBToAuthz(pbAuthz) 231 test.AssertNotError(t, err, "PBToAuthz failed") 232 test.AssertDeepEquals(t, inAuthz, outAuthz) 233 234 inAuthzNilExpires := core.Authorization{ 235 ID: "1", 236 Identifier: ident, 237 RegistrationID: 5, 238 Status: core.StatusPending, 239 Expires: nil, 240 Challenges: []core.Challenge{challA, challB}, 241 } 242 pbAuthz2, err := AuthzToPB(inAuthzNilExpires) 243 test.AssertNotError(t, err, "AuthzToPB failed") 244 outAuthz2, err := PBToAuthz(pbAuthz2) 245 test.AssertNotError(t, err, "PBToAuthz failed") 246 test.AssertDeepEquals(t, inAuthzNilExpires, outAuthz2) 247 } 248 249 func TestOrderValid(t *testing.T) { 250 created := time.Now() 251 expires := created.Add(1 * time.Hour) 252 testCases := []struct { 253 Name string 254 Order *corepb.Order 255 ExpectedValid bool 256 }{ 257 { 258 Name: "All valid", 259 Order: &corepb.Order{ 260 Id: 1, 261 RegistrationID: 1, 262 Expires: timestamppb.New(expires), 263 CertificateSerial: "", 264 V2Authorizations: []int64{}, 265 Identifiers: []*corepb.Identifier{identifier.NewDNS("example.com").ToProto()}, 266 BeganProcessing: false, 267 Created: timestamppb.New(created), 268 }, 269 ExpectedValid: true, 270 }, 271 { 272 Name: "Serial empty", 273 Order: &corepb.Order{ 274 Id: 1, 275 RegistrationID: 1, 276 Expires: timestamppb.New(expires), 277 V2Authorizations: []int64{}, 278 Identifiers: []*corepb.Identifier{identifier.NewDNS("example.com").ToProto()}, 279 BeganProcessing: false, 280 Created: timestamppb.New(created), 281 }, 282 ExpectedValid: true, 283 }, 284 { 285 Name: "All zero", 286 Order: &corepb.Order{}, 287 }, 288 { 289 Name: "ID 0", 290 Order: &corepb.Order{ 291 Id: 0, 292 RegistrationID: 1, 293 Expires: timestamppb.New(expires), 294 CertificateSerial: "", 295 V2Authorizations: []int64{}, 296 Identifiers: []*corepb.Identifier{identifier.NewDNS("example.com").ToProto()}, 297 BeganProcessing: false, 298 }, 299 }, 300 { 301 Name: "Reg ID zero", 302 Order: &corepb.Order{ 303 Id: 1, 304 RegistrationID: 0, 305 Expires: timestamppb.New(expires), 306 CertificateSerial: "", 307 V2Authorizations: []int64{}, 308 Identifiers: []*corepb.Identifier{identifier.NewDNS("example.com").ToProto()}, 309 BeganProcessing: false, 310 }, 311 }, 312 { 313 Name: "Expires 0", 314 Order: &corepb.Order{ 315 Id: 1, 316 RegistrationID: 1, 317 Expires: nil, 318 CertificateSerial: "", 319 V2Authorizations: []int64{}, 320 Identifiers: []*corepb.Identifier{identifier.NewDNS("example.com").ToProto()}, 321 BeganProcessing: false, 322 }, 323 }, 324 { 325 Name: "Names empty", 326 Order: &corepb.Order{ 327 Id: 1, 328 RegistrationID: 1, 329 Expires: timestamppb.New(expires), 330 CertificateSerial: "", 331 V2Authorizations: []int64{}, 332 Identifiers: []*corepb.Identifier{}, 333 BeganProcessing: false, 334 }, 335 }, 336 } 337 338 for _, tc := range testCases { 339 t.Run(tc.Name, func(t *testing.T) { 340 result := orderValid(tc.Order) 341 test.AssertEquals(t, result, tc.ExpectedValid) 342 }) 343 } 344 }