xref: /aosp_15_r20/external/open-dice/src/cbor_reader_fuzzer.cc (revision 60b67249c2e226f42f35cc6cfe66c6048e0bae6b)
1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at
6 //
7 //     https://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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 
15 #include "dice/cbor_reader.h"
16 #include "fuzzer/FuzzedDataProvider.h"
17 
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)18 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
19   int64_t signed_int;
20   uint64_t unsigned_int;
21   size_t sz;
22   const uint8_t* ptr;
23   const char* str;
24   CborIn in;
25   CborIn peeker;
26 
27   CborInInit(data, size, &in);
28 
29   do {
30     peeker = in;
31     CborReadInt(&peeker, &signed_int);
32 
33     peeker = in;
34     CborReadUint(&peeker, &unsigned_int);
35 
36     peeker = in;
37     CborReadBstr(&peeker, &sz, &ptr);
38 
39     peeker = in;
40     CborReadTstr(&peeker, &sz, &str);
41 
42     peeker = in;
43     CborReadArray(&peeker, &sz);
44 
45     peeker = in;
46     CborReadMap(&peeker, &sz);
47 
48     peeker = in;
49     CborReadTag(&peeker, &unsigned_int);
50 
51     peeker = in;
52     CborReadFalse(&peeker);
53 
54     peeker = in;
55     CborReadTrue(&peeker);
56 
57     peeker = in;
58     CborReadNull(&peeker);
59 
60     if (CborReadSkip(&in) != CBOR_READ_RESULT_OK) {
61       // Cannot progress futher with this buffer.
62       break;
63     }
64   } while (!CborInAtEnd(&in));
65 
66   return 0;
67 }
68