Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 1 | #!/usr/bin/python2 |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 2 | # |
Amin Hassani | f94b643 | 2018-01-26 17:39:47 -0800 | [diff] [blame] | 3 | # Copyright (C) 2013 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 17 | |
| 18 | """Unit testing checker.py.""" |
| 19 | |
Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 20 | from __future__ import print_function |
| 21 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 22 | import array |
| 23 | import collections |
| 24 | import cStringIO |
| 25 | import hashlib |
| 26 | import itertools |
| 27 | import os |
| 28 | import unittest |
| 29 | |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 30 | # pylint cannot find mox. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 31 | # pylint: disable=F0401 |
| 32 | import mox |
| 33 | |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 34 | from update_payload import checker |
| 35 | from update_payload import common |
| 36 | from update_payload import test_utils |
| 37 | from update_payload import update_metadata_pb2 |
| 38 | from update_payload.error import PayloadError |
| 39 | from update_payload.payload import Payload # Avoid name conflicts later. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 40 | |
| 41 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 42 | def _OpTypeByName(op_name): |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 43 | """Returns the type of an operation from itsname.""" |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 44 | op_name_to_type = { |
| 45 | 'REPLACE': common.OpType.REPLACE, |
| 46 | 'REPLACE_BZ': common.OpType.REPLACE_BZ, |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 47 | 'SOURCE_COPY': common.OpType.SOURCE_COPY, |
| 48 | 'SOURCE_BSDIFF': common.OpType.SOURCE_BSDIFF, |
Alex Deymo | 2846677 | 2015-09-11 17:16:44 -0700 | [diff] [blame] | 49 | 'ZERO': common.OpType.ZERO, |
| 50 | 'DISCARD': common.OpType.DISCARD, |
| 51 | 'REPLACE_XZ': common.OpType.REPLACE_XZ, |
Amin Hassani | 5ef5d45 | 2017-08-04 13:10:59 -0700 | [diff] [blame] | 52 | 'PUFFDIFF': common.OpType.PUFFDIFF, |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 53 | 'BROTLI_BSDIFF': common.OpType.BROTLI_BSDIFF, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 54 | } |
| 55 | return op_name_to_type[op_name] |
| 56 | |
| 57 | |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 58 | def _GetPayloadChecker(payload_gen_write_to_file_func, payload_gen_dargs=None, |
| 59 | checker_init_dargs=None): |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 60 | """Returns a payload checker from a given payload generator.""" |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 61 | if payload_gen_dargs is None: |
| 62 | payload_gen_dargs = {} |
| 63 | if checker_init_dargs is None: |
| 64 | checker_init_dargs = {} |
| 65 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 66 | payload_file = cStringIO.StringIO() |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 67 | payload_gen_write_to_file_func(payload_file, **payload_gen_dargs) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 68 | payload_file.seek(0) |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 69 | payload = Payload(payload_file) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 70 | payload.Init() |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 71 | return checker.PayloadChecker(payload, **checker_init_dargs) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 72 | |
| 73 | |
| 74 | def _GetPayloadCheckerWithData(payload_gen): |
| 75 | """Returns a payload checker from a given payload generator.""" |
| 76 | payload_file = cStringIO.StringIO() |
| 77 | payload_gen.WriteToFile(payload_file) |
| 78 | payload_file.seek(0) |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 79 | payload = Payload(payload_file) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 80 | payload.Init() |
| 81 | return checker.PayloadChecker(payload) |
| 82 | |
| 83 | |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 84 | # This class doesn't need an __init__(). |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 85 | # pylint: disable=W0232 |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 86 | # Unit testing is all about running protected methods. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 87 | # pylint: disable=W0212 |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 88 | # Don't bark about missing members of classes you cannot import. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 89 | # pylint: disable=E1101 |
| 90 | class PayloadCheckerTest(mox.MoxTestBase): |
| 91 | """Tests the PayloadChecker class. |
| 92 | |
| 93 | In addition to ordinary testFoo() methods, which are automatically invoked by |
| 94 | the unittest framework, in this class we make use of DoBarTest() calls that |
| 95 | implement parametric tests of certain features. In order to invoke each test, |
| 96 | which embodies a unique combination of parameter values, as a complete unit |
| 97 | test, we perform explicit enumeration of the parameter space and create |
| 98 | individual invocation contexts for each, which are then bound as |
| 99 | testBar__param1=val1__param2=val2(). The enumeration of parameter spaces for |
| 100 | all such tests is done in AddAllParametricTests(). |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 101 | """ |
| 102 | |
| 103 | def MockPayload(self): |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 104 | """Create a mock payload object, complete with a mock manifest.""" |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 105 | payload = self.mox.CreateMock(Payload) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 106 | payload.is_init = True |
| 107 | payload.manifest = self.mox.CreateMock( |
| 108 | update_metadata_pb2.DeltaArchiveManifest) |
| 109 | return payload |
| 110 | |
| 111 | @staticmethod |
| 112 | def NewExtent(start_block, num_blocks): |
| 113 | """Returns an Extent message. |
| 114 | |
| 115 | Each of the provided fields is set iff it is >= 0; otherwise, it's left at |
| 116 | its default state. |
| 117 | |
| 118 | Args: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 119 | start_block: The starting block of the extent. |
| 120 | num_blocks: The number of blocks in the extent. |
Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 121 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 122 | Returns: |
| 123 | An Extent message. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 124 | """ |
| 125 | ex = update_metadata_pb2.Extent() |
| 126 | if start_block >= 0: |
| 127 | ex.start_block = start_block |
| 128 | if num_blocks >= 0: |
| 129 | ex.num_blocks = num_blocks |
| 130 | return ex |
| 131 | |
| 132 | @staticmethod |
| 133 | def NewExtentList(*args): |
| 134 | """Returns an list of extents. |
| 135 | |
| 136 | Args: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 137 | *args: (start_block, num_blocks) pairs defining the extents. |
Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 138 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 139 | Returns: |
| 140 | A list of Extent objects. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 141 | """ |
| 142 | ex_list = [] |
| 143 | for start_block, num_blocks in args: |
| 144 | ex_list.append(PayloadCheckerTest.NewExtent(start_block, num_blocks)) |
| 145 | return ex_list |
| 146 | |
| 147 | @staticmethod |
| 148 | def AddToMessage(repeated_field, field_vals): |
| 149 | for field_val in field_vals: |
| 150 | new_field = repeated_field.add() |
| 151 | new_field.CopyFrom(field_val) |
| 152 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 153 | def SetupAddElemTest(self, is_present, is_submsg, convert=str, |
| 154 | linebreak=False, indent=0): |
| 155 | """Setup for testing of _CheckElem() and its derivatives. |
| 156 | |
| 157 | Args: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 158 | is_present: Whether or not the element is found in the message. |
| 159 | is_submsg: Whether the element is a sub-message itself. |
| 160 | convert: A representation conversion function. |
| 161 | linebreak: Whether or not a linebreak is to be used in the report. |
| 162 | indent: Indentation used for the report. |
Gilad Arnold | f583a7d | 2015-02-05 13:23:55 -0800 | [diff] [blame] | 163 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 164 | Returns: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 165 | msg: A mock message object. |
| 166 | report: A mock report object. |
| 167 | subreport: A mock sub-report object. |
| 168 | name: An element name to check. |
| 169 | val: Expected element value. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 170 | """ |
| 171 | name = 'foo' |
| 172 | val = 'fake submsg' if is_submsg else 'fake field' |
| 173 | subreport = 'fake subreport' |
| 174 | |
| 175 | # Create a mock message. |
Alex Deymo | 2846677 | 2015-09-11 17:16:44 -0700 | [diff] [blame] | 176 | msg = self.mox.CreateMock(update_metadata_pb2._message.Message) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 177 | msg.HasField(name).AndReturn(is_present) |
| 178 | setattr(msg, name, val) |
| 179 | |
| 180 | # Create a mock report. |
| 181 | report = self.mox.CreateMock(checker._PayloadReport) |
| 182 | if is_present: |
| 183 | if is_submsg: |
| 184 | report.AddSubReport(name).AndReturn(subreport) |
| 185 | else: |
| 186 | report.AddField(name, convert(val), linebreak=linebreak, indent=indent) |
| 187 | |
| 188 | self.mox.ReplayAll() |
| 189 | return (msg, report, subreport, name, val) |
| 190 | |
| 191 | def DoAddElemTest(self, is_present, is_mandatory, is_submsg, convert, |
| 192 | linebreak, indent): |
| 193 | """Parametric testing of _CheckElem(). |
| 194 | |
| 195 | Args: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 196 | is_present: Whether or not the element is found in the message. |
| 197 | is_mandatory: Whether or not it's a mandatory element. |
| 198 | is_submsg: Whether the element is a sub-message itself. |
| 199 | convert: A representation conversion function. |
| 200 | linebreak: Whether or not a linebreak is to be used in the report. |
| 201 | indent: Indentation used for the report. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 202 | """ |
| 203 | msg, report, subreport, name, val = self.SetupAddElemTest( |
| 204 | is_present, is_submsg, convert, linebreak, indent) |
| 205 | |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 206 | args = (msg, name, report, is_mandatory, is_submsg) |
| 207 | kwargs = {'convert': convert, 'linebreak': linebreak, 'indent': indent} |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 208 | if is_mandatory and not is_present: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 209 | self.assertRaises(PayloadError, |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 210 | checker.PayloadChecker._CheckElem, *args, **kwargs) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 211 | else: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 212 | ret_val, ret_subreport = checker.PayloadChecker._CheckElem(*args, |
| 213 | **kwargs) |
| 214 | self.assertEquals(val if is_present else None, ret_val) |
| 215 | self.assertEquals(subreport if is_present and is_submsg else None, |
| 216 | ret_subreport) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 217 | |
| 218 | def DoAddFieldTest(self, is_mandatory, is_present, convert, linebreak, |
| 219 | indent): |
| 220 | """Parametric testing of _Check{Mandatory,Optional}Field(). |
| 221 | |
| 222 | Args: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 223 | is_mandatory: Whether we're testing a mandatory call. |
| 224 | is_present: Whether or not the element is found in the message. |
| 225 | convert: A representation conversion function. |
| 226 | linebreak: Whether or not a linebreak is to be used in the report. |
| 227 | indent: Indentation used for the report. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 228 | """ |
| 229 | msg, report, _, name, val = self.SetupAddElemTest( |
| 230 | is_present, False, convert, linebreak, indent) |
| 231 | |
| 232 | # Prepare for invocation of the tested method. |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 233 | args = [msg, name, report] |
| 234 | kwargs = {'convert': convert, 'linebreak': linebreak, 'indent': indent} |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 235 | if is_mandatory: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 236 | args.append('bar') |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 237 | tested_func = checker.PayloadChecker._CheckMandatoryField |
| 238 | else: |
| 239 | tested_func = checker.PayloadChecker._CheckOptionalField |
| 240 | |
| 241 | # Test the method call. |
| 242 | if is_mandatory and not is_present: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 243 | self.assertRaises(PayloadError, tested_func, *args, **kwargs) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 244 | else: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 245 | ret_val = tested_func(*args, **kwargs) |
| 246 | self.assertEquals(val if is_present else None, ret_val) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 247 | |
| 248 | def DoAddSubMsgTest(self, is_mandatory, is_present): |
| 249 | """Parametrized testing of _Check{Mandatory,Optional}SubMsg(). |
| 250 | |
| 251 | Args: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 252 | is_mandatory: Whether we're testing a mandatory call. |
| 253 | is_present: Whether or not the element is found in the message. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 254 | """ |
| 255 | msg, report, subreport, name, val = self.SetupAddElemTest(is_present, True) |
| 256 | |
| 257 | # Prepare for invocation of the tested method. |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 258 | args = [msg, name, report] |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 259 | if is_mandatory: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 260 | args.append('bar') |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 261 | tested_func = checker.PayloadChecker._CheckMandatorySubMsg |
| 262 | else: |
| 263 | tested_func = checker.PayloadChecker._CheckOptionalSubMsg |
| 264 | |
| 265 | # Test the method call. |
| 266 | if is_mandatory and not is_present: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 267 | self.assertRaises(PayloadError, tested_func, *args) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 268 | else: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 269 | ret_val, ret_subreport = tested_func(*args) |
| 270 | self.assertEquals(val if is_present else None, ret_val) |
| 271 | self.assertEquals(subreport if is_present else None, ret_subreport) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 272 | |
| 273 | def testCheckPresentIff(self): |
| 274 | """Tests _CheckPresentIff().""" |
| 275 | self.assertIsNone(checker.PayloadChecker._CheckPresentIff( |
| 276 | None, None, 'foo', 'bar', 'baz')) |
| 277 | self.assertIsNone(checker.PayloadChecker._CheckPresentIff( |
| 278 | 'a', 'b', 'foo', 'bar', 'baz')) |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 279 | self.assertRaises(PayloadError, checker.PayloadChecker._CheckPresentIff, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 280 | 'a', None, 'foo', 'bar', 'baz') |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 281 | self.assertRaises(PayloadError, checker.PayloadChecker._CheckPresentIff, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 282 | None, 'b', 'foo', 'bar', 'baz') |
| 283 | |
| 284 | def DoCheckSha256SignatureTest(self, expect_pass, expect_subprocess_call, |
| 285 | sig_data, sig_asn1_header, |
| 286 | returned_signed_hash, expected_signed_hash): |
| 287 | """Parametric testing of _CheckSha256SignatureTest(). |
| 288 | |
| 289 | Args: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 290 | expect_pass: Whether or not it should pass. |
| 291 | expect_subprocess_call: Whether to expect the openssl call to happen. |
| 292 | sig_data: The signature raw data. |
| 293 | sig_asn1_header: The ASN1 header. |
| 294 | returned_signed_hash: The signed hash data retuned by openssl. |
| 295 | expected_signed_hash: The signed hash data to compare against. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 296 | """ |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 297 | try: |
| 298 | # Stub out the subprocess invocation. |
| 299 | self.mox.StubOutWithMock(checker.PayloadChecker, '_Run') |
| 300 | if expect_subprocess_call: |
| 301 | checker.PayloadChecker._Run( |
| 302 | mox.IsA(list), send_data=sig_data).AndReturn( |
| 303 | (sig_asn1_header + returned_signed_hash, None)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 304 | |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 305 | self.mox.ReplayAll() |
| 306 | if expect_pass: |
| 307 | self.assertIsNone(checker.PayloadChecker._CheckSha256Signature( |
| 308 | sig_data, 'foo', expected_signed_hash, 'bar')) |
| 309 | else: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 310 | self.assertRaises(PayloadError, |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 311 | checker.PayloadChecker._CheckSha256Signature, |
| 312 | sig_data, 'foo', expected_signed_hash, 'bar') |
| 313 | finally: |
| 314 | self.mox.UnsetStubs() |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 315 | |
| 316 | def testCheckSha256Signature_Pass(self): |
| 317 | """Tests _CheckSha256Signature(); pass case.""" |
| 318 | sig_data = 'fake-signature'.ljust(256) |
| 319 | signed_hash = hashlib.sha256('fake-data').digest() |
| 320 | self.DoCheckSha256SignatureTest(True, True, sig_data, |
| 321 | common.SIG_ASN1_HEADER, signed_hash, |
| 322 | signed_hash) |
| 323 | |
| 324 | def testCheckSha256Signature_FailBadSignature(self): |
| 325 | """Tests _CheckSha256Signature(); fails due to malformed signature.""" |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 326 | sig_data = 'fake-signature' # Malformed (not 256 bytes in length). |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 327 | signed_hash = hashlib.sha256('fake-data').digest() |
| 328 | self.DoCheckSha256SignatureTest(False, False, sig_data, |
| 329 | common.SIG_ASN1_HEADER, signed_hash, |
| 330 | signed_hash) |
| 331 | |
| 332 | def testCheckSha256Signature_FailBadOutputLength(self): |
| 333 | """Tests _CheckSha256Signature(); fails due to unexpected output length.""" |
| 334 | sig_data = 'fake-signature'.ljust(256) |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 335 | signed_hash = 'fake-hash' # Malformed (not 32 bytes in length). |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 336 | self.DoCheckSha256SignatureTest(False, True, sig_data, |
| 337 | common.SIG_ASN1_HEADER, signed_hash, |
| 338 | signed_hash) |
| 339 | |
| 340 | def testCheckSha256Signature_FailBadAsnHeader(self): |
| 341 | """Tests _CheckSha256Signature(); fails due to bad ASN1 header.""" |
| 342 | sig_data = 'fake-signature'.ljust(256) |
| 343 | signed_hash = hashlib.sha256('fake-data').digest() |
| 344 | bad_asn1_header = 'bad-asn-header'.ljust(len(common.SIG_ASN1_HEADER)) |
| 345 | self.DoCheckSha256SignatureTest(False, True, sig_data, bad_asn1_header, |
| 346 | signed_hash, signed_hash) |
| 347 | |
| 348 | def testCheckSha256Signature_FailBadHash(self): |
| 349 | """Tests _CheckSha256Signature(); fails due to bad hash returned.""" |
| 350 | sig_data = 'fake-signature'.ljust(256) |
| 351 | expected_signed_hash = hashlib.sha256('fake-data').digest() |
| 352 | returned_signed_hash = hashlib.sha256('bad-fake-data').digest() |
| 353 | self.DoCheckSha256SignatureTest(False, True, sig_data, |
| 354 | common.SIG_ASN1_HEADER, |
| 355 | expected_signed_hash, returned_signed_hash) |
| 356 | |
| 357 | def testCheckBlocksFitLength_Pass(self): |
| 358 | """Tests _CheckBlocksFitLength(); pass case.""" |
| 359 | self.assertIsNone(checker.PayloadChecker._CheckBlocksFitLength( |
| 360 | 64, 4, 16, 'foo')) |
| 361 | self.assertIsNone(checker.PayloadChecker._CheckBlocksFitLength( |
| 362 | 60, 4, 16, 'foo')) |
| 363 | self.assertIsNone(checker.PayloadChecker._CheckBlocksFitLength( |
| 364 | 49, 4, 16, 'foo')) |
| 365 | self.assertIsNone(checker.PayloadChecker._CheckBlocksFitLength( |
| 366 | 48, 3, 16, 'foo')) |
| 367 | |
| 368 | def testCheckBlocksFitLength_TooManyBlocks(self): |
| 369 | """Tests _CheckBlocksFitLength(); fails due to excess blocks.""" |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 370 | self.assertRaises(PayloadError, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 371 | checker.PayloadChecker._CheckBlocksFitLength, |
| 372 | 64, 5, 16, 'foo') |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 373 | self.assertRaises(PayloadError, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 374 | checker.PayloadChecker._CheckBlocksFitLength, |
| 375 | 60, 5, 16, 'foo') |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 376 | self.assertRaises(PayloadError, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 377 | checker.PayloadChecker._CheckBlocksFitLength, |
| 378 | 49, 5, 16, 'foo') |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 379 | self.assertRaises(PayloadError, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 380 | checker.PayloadChecker._CheckBlocksFitLength, |
| 381 | 48, 4, 16, 'foo') |
| 382 | |
| 383 | def testCheckBlocksFitLength_TooFewBlocks(self): |
| 384 | """Tests _CheckBlocksFitLength(); fails due to insufficient blocks.""" |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 385 | self.assertRaises(PayloadError, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 386 | checker.PayloadChecker._CheckBlocksFitLength, |
| 387 | 64, 3, 16, 'foo') |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 388 | self.assertRaises(PayloadError, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 389 | checker.PayloadChecker._CheckBlocksFitLength, |
| 390 | 60, 3, 16, 'foo') |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 391 | self.assertRaises(PayloadError, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 392 | checker.PayloadChecker._CheckBlocksFitLength, |
| 393 | 49, 3, 16, 'foo') |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 394 | self.assertRaises(PayloadError, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 395 | checker.PayloadChecker._CheckBlocksFitLength, |
| 396 | 48, 2, 16, 'foo') |
| 397 | |
| 398 | def DoCheckManifestTest(self, fail_mismatched_block_size, fail_bad_sigs, |
| 399 | fail_mismatched_oki_ori, fail_bad_oki, fail_bad_ori, |
Gilad Arnold | 5bc7fbe | 2015-02-05 13:01:09 -0800 | [diff] [blame] | 400 | fail_bad_nki, fail_bad_nri, fail_old_kernel_fs_size, |
| 401 | fail_old_rootfs_fs_size, fail_new_kernel_fs_size, |
| 402 | fail_new_rootfs_fs_size): |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 403 | """Parametric testing of _CheckManifest(). |
| 404 | |
| 405 | Args: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 406 | fail_mismatched_block_size: Simulate a missing block_size field. |
| 407 | fail_bad_sigs: Make signatures descriptor inconsistent. |
| 408 | fail_mismatched_oki_ori: Make old rootfs/kernel info partially present. |
| 409 | fail_bad_oki: Tamper with old kernel info. |
| 410 | fail_bad_ori: Tamper with old rootfs info. |
| 411 | fail_bad_nki: Tamper with new kernel info. |
| 412 | fail_bad_nri: Tamper with new rootfs info. |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 413 | fail_old_kernel_fs_size: Make old kernel fs size too big. |
| 414 | fail_old_rootfs_fs_size: Make old rootfs fs size too big. |
| 415 | fail_new_kernel_fs_size: Make new kernel fs size too big. |
| 416 | fail_new_rootfs_fs_size: Make new rootfs fs size too big. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 417 | """ |
| 418 | # Generate a test payload. For this test, we only care about the manifest |
| 419 | # and don't need any data blobs, hence we can use a plain paylaod generator |
| 420 | # (which also gives us more control on things that can be screwed up). |
| 421 | payload_gen = test_utils.PayloadGenerator() |
| 422 | |
| 423 | # Tamper with block size, if required. |
| 424 | if fail_mismatched_block_size: |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 425 | payload_gen.SetBlockSize(test_utils.KiB(1)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 426 | else: |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 427 | payload_gen.SetBlockSize(test_utils.KiB(4)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 428 | |
| 429 | # Add some operations. |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 430 | payload_gen.AddOperation(False, common.OpType.SOURCE_COPY, |
Gilad Arnold | 5bc7fbe | 2015-02-05 13:01:09 -0800 | [diff] [blame] | 431 | src_extents=[(0, 16), (16, 497)], |
| 432 | dst_extents=[(16, 496), (0, 16)]) |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 433 | payload_gen.AddOperation(True, common.OpType.SOURCE_COPY, |
Gilad Arnold | 5bc7fbe | 2015-02-05 13:01:09 -0800 | [diff] [blame] | 434 | src_extents=[(0, 8), (8, 8)], |
| 435 | dst_extents=[(8, 8), (0, 8)]) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 436 | |
| 437 | # Set an invalid signatures block (offset but no size), if required. |
| 438 | if fail_bad_sigs: |
| 439 | payload_gen.SetSignatures(32, None) |
| 440 | |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 441 | # Set partition / filesystem sizes. |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 442 | rootfs_part_size = test_utils.MiB(8) |
| 443 | kernel_part_size = test_utils.KiB(512) |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 444 | old_rootfs_fs_size = new_rootfs_fs_size = rootfs_part_size |
| 445 | old_kernel_fs_size = new_kernel_fs_size = kernel_part_size |
| 446 | if fail_old_kernel_fs_size: |
| 447 | old_kernel_fs_size += 100 |
| 448 | if fail_old_rootfs_fs_size: |
| 449 | old_rootfs_fs_size += 100 |
| 450 | if fail_new_kernel_fs_size: |
| 451 | new_kernel_fs_size += 100 |
| 452 | if fail_new_rootfs_fs_size: |
| 453 | new_rootfs_fs_size += 100 |
| 454 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 455 | # Add old kernel/rootfs partition info, as required. |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 456 | if fail_mismatched_oki_ori or fail_old_kernel_fs_size or fail_bad_oki: |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 457 | oki_hash = (None if fail_bad_oki |
| 458 | else hashlib.sha256('fake-oki-content').digest()) |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 459 | payload_gen.SetPartInfo(True, False, old_kernel_fs_size, oki_hash) |
| 460 | if not fail_mismatched_oki_ori and (fail_old_rootfs_fs_size or |
| 461 | fail_bad_ori): |
| 462 | ori_hash = (None if fail_bad_ori |
| 463 | else hashlib.sha256('fake-ori-content').digest()) |
| 464 | payload_gen.SetPartInfo(False, False, old_rootfs_fs_size, ori_hash) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 465 | |
| 466 | # Add new kernel/rootfs partition info. |
| 467 | payload_gen.SetPartInfo( |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 468 | True, True, new_kernel_fs_size, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 469 | None if fail_bad_nki else hashlib.sha256('fake-nki-content').digest()) |
| 470 | payload_gen.SetPartInfo( |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 471 | False, True, new_rootfs_fs_size, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 472 | None if fail_bad_nri else hashlib.sha256('fake-nri-content').digest()) |
| 473 | |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 474 | # Set the minor version. |
| 475 | payload_gen.SetMinorVersion(0) |
| 476 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 477 | # Create the test object. |
| 478 | payload_checker = _GetPayloadChecker(payload_gen.WriteToFile) |
| 479 | report = checker._PayloadReport() |
| 480 | |
| 481 | should_fail = (fail_mismatched_block_size or fail_bad_sigs or |
| 482 | fail_mismatched_oki_ori or fail_bad_oki or fail_bad_ori or |
Gilad Arnold | 5bc7fbe | 2015-02-05 13:01:09 -0800 | [diff] [blame] | 483 | fail_bad_nki or fail_bad_nri or fail_old_kernel_fs_size or |
| 484 | fail_old_rootfs_fs_size or fail_new_kernel_fs_size or |
| 485 | fail_new_rootfs_fs_size) |
Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 486 | part_sizes = { |
| 487 | common.ROOTFS: rootfs_part_size, |
| 488 | common.KERNEL: kernel_part_size |
| 489 | } |
| 490 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 491 | if should_fail: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 492 | self.assertRaises(PayloadError, payload_checker._CheckManifest, report, |
Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 493 | part_sizes) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 494 | else: |
Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 495 | self.assertIsNone(payload_checker._CheckManifest(report, part_sizes)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 496 | |
| 497 | def testCheckLength(self): |
| 498 | """Tests _CheckLength().""" |
| 499 | payload_checker = checker.PayloadChecker(self.MockPayload()) |
| 500 | block_size = payload_checker.block_size |
| 501 | |
| 502 | # Passes. |
| 503 | self.assertIsNone(payload_checker._CheckLength( |
| 504 | int(3.5 * block_size), 4, 'foo', 'bar')) |
| 505 | # Fails, too few blocks. |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 506 | self.assertRaises(PayloadError, payload_checker._CheckLength, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 507 | int(3.5 * block_size), 3, 'foo', 'bar') |
| 508 | # Fails, too many blocks. |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 509 | self.assertRaises(PayloadError, payload_checker._CheckLength, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 510 | int(3.5 * block_size), 5, 'foo', 'bar') |
| 511 | |
| 512 | def testCheckExtents(self): |
| 513 | """Tests _CheckExtents().""" |
| 514 | payload_checker = checker.PayloadChecker(self.MockPayload()) |
| 515 | block_size = payload_checker.block_size |
| 516 | |
| 517 | # Passes w/ all real extents. |
| 518 | extents = self.NewExtentList((0, 4), (8, 3), (1024, 16)) |
| 519 | self.assertEquals( |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 520 | 23, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 521 | payload_checker._CheckExtents(extents, (1024 + 16) * block_size, |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 522 | collections.defaultdict(int), 'foo')) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 523 | |
| 524 | # Passes w/ pseudo-extents (aka sparse holes). |
| 525 | extents = self.NewExtentList((0, 4), (common.PSEUDO_EXTENT_MARKER, 5), |
| 526 | (8, 3)) |
| 527 | self.assertEquals( |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 528 | 12, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 529 | payload_checker._CheckExtents(extents, (1024 + 16) * block_size, |
| 530 | collections.defaultdict(int), 'foo', |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 531 | allow_pseudo=True)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 532 | |
| 533 | # Passes w/ pseudo-extent due to a signature. |
| 534 | extents = self.NewExtentList((common.PSEUDO_EXTENT_MARKER, 2)) |
| 535 | self.assertEquals( |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 536 | 2, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 537 | payload_checker._CheckExtents(extents, (1024 + 16) * block_size, |
| 538 | collections.defaultdict(int), 'foo', |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 539 | allow_signature=True)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 540 | |
| 541 | # Fails, extent missing a start block. |
| 542 | extents = self.NewExtentList((-1, 4), (8, 3), (1024, 16)) |
| 543 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 544 | PayloadError, payload_checker._CheckExtents, extents, |
| 545 | (1024 + 16) * block_size, collections.defaultdict(int), 'foo') |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 546 | |
| 547 | # Fails, extent missing block count. |
| 548 | extents = self.NewExtentList((0, -1), (8, 3), (1024, 16)) |
| 549 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 550 | PayloadError, payload_checker._CheckExtents, extents, |
| 551 | (1024 + 16) * block_size, collections.defaultdict(int), 'foo') |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 552 | |
| 553 | # Fails, extent has zero blocks. |
| 554 | extents = self.NewExtentList((0, 4), (8, 3), (1024, 0)) |
| 555 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 556 | PayloadError, payload_checker._CheckExtents, extents, |
| 557 | (1024 + 16) * block_size, collections.defaultdict(int), 'foo') |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 558 | |
| 559 | # Fails, extent exceeds partition boundaries. |
| 560 | extents = self.NewExtentList((0, 4), (8, 3), (1024, 16)) |
| 561 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 562 | PayloadError, payload_checker._CheckExtents, extents, |
| 563 | (1024 + 15) * block_size, collections.defaultdict(int), 'foo') |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 564 | |
| 565 | def testCheckReplaceOperation(self): |
| 566 | """Tests _CheckReplaceOperation() where op.type == REPLACE.""" |
| 567 | payload_checker = checker.PayloadChecker(self.MockPayload()) |
| 568 | block_size = payload_checker.block_size |
| 569 | data_length = 10000 |
| 570 | |
| 571 | op = self.mox.CreateMock( |
Alex Deymo | 2846677 | 2015-09-11 17:16:44 -0700 | [diff] [blame] | 572 | update_metadata_pb2.InstallOperation) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 573 | op.type = common.OpType.REPLACE |
| 574 | |
| 575 | # Pass. |
| 576 | op.src_extents = [] |
| 577 | self.assertIsNone( |
| 578 | payload_checker._CheckReplaceOperation( |
| 579 | op, data_length, (data_length + block_size - 1) / block_size, |
| 580 | 'foo')) |
| 581 | |
| 582 | # Fail, src extents founds. |
| 583 | op.src_extents = ['bar'] |
| 584 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 585 | PayloadError, payload_checker._CheckReplaceOperation, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 586 | op, data_length, (data_length + block_size - 1) / block_size, 'foo') |
| 587 | |
| 588 | # Fail, missing data. |
| 589 | op.src_extents = [] |
| 590 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 591 | PayloadError, payload_checker._CheckReplaceOperation, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 592 | op, None, (data_length + block_size - 1) / block_size, 'foo') |
| 593 | |
| 594 | # Fail, length / block number mismatch. |
| 595 | op.src_extents = ['bar'] |
| 596 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 597 | PayloadError, payload_checker._CheckReplaceOperation, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 598 | op, data_length, (data_length + block_size - 1) / block_size + 1, 'foo') |
| 599 | |
| 600 | def testCheckReplaceBzOperation(self): |
| 601 | """Tests _CheckReplaceOperation() where op.type == REPLACE_BZ.""" |
| 602 | payload_checker = checker.PayloadChecker(self.MockPayload()) |
| 603 | block_size = payload_checker.block_size |
| 604 | data_length = block_size * 3 |
| 605 | |
| 606 | op = self.mox.CreateMock( |
Alex Deymo | 2846677 | 2015-09-11 17:16:44 -0700 | [diff] [blame] | 607 | update_metadata_pb2.InstallOperation) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 608 | op.type = common.OpType.REPLACE_BZ |
| 609 | |
| 610 | # Pass. |
| 611 | op.src_extents = [] |
| 612 | self.assertIsNone( |
| 613 | payload_checker._CheckReplaceOperation( |
| 614 | op, data_length, (data_length + block_size - 1) / block_size + 5, |
| 615 | 'foo')) |
| 616 | |
| 617 | # Fail, src extents founds. |
| 618 | op.src_extents = ['bar'] |
| 619 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 620 | PayloadError, payload_checker._CheckReplaceOperation, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 621 | op, data_length, (data_length + block_size - 1) / block_size + 5, 'foo') |
| 622 | |
| 623 | # Fail, missing data. |
| 624 | op.src_extents = [] |
| 625 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 626 | PayloadError, payload_checker._CheckReplaceOperation, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 627 | op, None, (data_length + block_size - 1) / block_size, 'foo') |
| 628 | |
| 629 | # Fail, too few blocks to justify BZ. |
| 630 | op.src_extents = [] |
| 631 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 632 | PayloadError, payload_checker._CheckReplaceOperation, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 633 | op, data_length, (data_length + block_size - 1) / block_size, 'foo') |
| 634 | |
Amin Hassani | 0de7f78 | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 635 | def testCheckReplaceXzOperation(self): |
| 636 | """Tests _CheckReplaceOperation() where op.type == REPLACE_XZ.""" |
| 637 | payload_checker = checker.PayloadChecker(self.MockPayload()) |
| 638 | block_size = payload_checker.block_size |
| 639 | data_length = block_size * 3 |
| 640 | |
| 641 | op = self.mox.CreateMock( |
| 642 | update_metadata_pb2.InstallOperation) |
| 643 | op.type = common.OpType.REPLACE_XZ |
| 644 | |
| 645 | # Pass. |
| 646 | op.src_extents = [] |
| 647 | self.assertIsNone( |
| 648 | payload_checker._CheckReplaceOperation( |
| 649 | op, data_length, (data_length + block_size - 1) / block_size + 5, |
| 650 | 'foo')) |
| 651 | |
| 652 | # Fail, src extents founds. |
| 653 | op.src_extents = ['bar'] |
| 654 | self.assertRaises( |
| 655 | PayloadError, payload_checker._CheckReplaceOperation, |
| 656 | op, data_length, (data_length + block_size - 1) / block_size + 5, 'foo') |
| 657 | |
| 658 | # Fail, missing data. |
| 659 | op.src_extents = [] |
| 660 | self.assertRaises( |
| 661 | PayloadError, payload_checker._CheckReplaceOperation, |
| 662 | op, None, (data_length + block_size - 1) / block_size, 'foo') |
| 663 | |
| 664 | # Fail, too few blocks to justify XZ. |
| 665 | op.src_extents = [] |
| 666 | self.assertRaises( |
| 667 | PayloadError, payload_checker._CheckReplaceOperation, |
| 668 | op, data_length, (data_length + block_size - 1) / block_size, 'foo') |
| 669 | |
Sen Jiang | 92161a7 | 2016-06-28 16:09:38 -0700 | [diff] [blame] | 670 | def testCheckAnyDiff(self): |
| 671 | """Tests _CheckAnyDiffOperation().""" |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 672 | payload_checker = checker.PayloadChecker(self.MockPayload()) |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 673 | op = update_metadata_pb2.InstallOperation() |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 674 | |
| 675 | # Pass. |
| 676 | self.assertIsNone( |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 677 | payload_checker._CheckAnyDiffOperation(op, 10000, 3, 'foo')) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 678 | |
| 679 | # Fail, missing data blob. |
| 680 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 681 | PayloadError, payload_checker._CheckAnyDiffOperation, |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 682 | op, None, 3, 'foo') |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 683 | |
| 684 | # Fail, too big of a diff blob (unjustified). |
| 685 | self.assertRaises( |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 686 | PayloadError, payload_checker._CheckAnyDiffOperation, |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 687 | op, 10000, 2, 'foo') |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 688 | |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 689 | def testCheckSourceCopyOperation_Pass(self): |
| 690 | """Tests _CheckSourceCopyOperation(); pass case.""" |
| 691 | payload_checker = checker.PayloadChecker(self.MockPayload()) |
| 692 | self.assertIsNone( |
| 693 | payload_checker._CheckSourceCopyOperation(None, 134, 134, 'foo')) |
| 694 | |
| 695 | def testCheckSourceCopyOperation_FailContainsData(self): |
| 696 | """Tests _CheckSourceCopyOperation(); message contains data.""" |
| 697 | payload_checker = checker.PayloadChecker(self.MockPayload()) |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 698 | self.assertRaises(PayloadError, payload_checker._CheckSourceCopyOperation, |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 699 | 134, 0, 0, 'foo') |
| 700 | |
| 701 | def testCheckSourceCopyOperation_FailBlockCountsMismatch(self): |
| 702 | """Tests _CheckSourceCopyOperation(); src and dst block totals not equal.""" |
| 703 | payload_checker = checker.PayloadChecker(self.MockPayload()) |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 704 | self.assertRaises(PayloadError, payload_checker._CheckSourceCopyOperation, |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 705 | None, 0, 1, 'foo') |
| 706 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 707 | def DoCheckOperationTest(self, op_type_name, is_last, allow_signature, |
| 708 | allow_unhashed, fail_src_extents, fail_dst_extents, |
| 709 | fail_mismatched_data_offset_length, |
| 710 | fail_missing_dst_extents, fail_src_length, |
| 711 | fail_dst_length, fail_data_hash, |
Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 712 | fail_prev_data_offset, fail_bad_minor_version): |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 713 | """Parametric testing of _CheckOperation(). |
| 714 | |
| 715 | Args: |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 716 | op_type_name: 'REPLACE', 'REPLACE_BZ', 'REPLACE_XZ', |
Amin Hassani | 0de7f78 | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 717 | 'SOURCE_COPY', 'SOURCE_BSDIFF', BROTLI_BSDIFF or 'PUFFDIFF'. |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 718 | is_last: Whether we're testing the last operation in a sequence. |
| 719 | allow_signature: Whether we're testing a signature-capable operation. |
| 720 | allow_unhashed: Whether we're allowing to not hash the data. |
| 721 | fail_src_extents: Tamper with src extents. |
| 722 | fail_dst_extents: Tamper with dst extents. |
| 723 | fail_mismatched_data_offset_length: Make data_{offset,length} |
| 724 | inconsistent. |
| 725 | fail_missing_dst_extents: Do not include dst extents. |
| 726 | fail_src_length: Make src length inconsistent. |
| 727 | fail_dst_length: Make dst length inconsistent. |
| 728 | fail_data_hash: Tamper with the data blob hash. |
| 729 | fail_prev_data_offset: Make data space uses incontiguous. |
Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 730 | fail_bad_minor_version: Make minor version incompatible with op. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 731 | """ |
| 732 | op_type = _OpTypeByName(op_type_name) |
| 733 | |
| 734 | # Create the test object. |
| 735 | payload = self.MockPayload() |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 736 | payload_checker = checker.PayloadChecker(payload, |
| 737 | allow_unhashed=allow_unhashed) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 738 | block_size = payload_checker.block_size |
| 739 | |
| 740 | # Create auxiliary arguments. |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 741 | old_part_size = test_utils.MiB(4) |
| 742 | new_part_size = test_utils.MiB(8) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 743 | old_block_counters = array.array( |
| 744 | 'B', [0] * ((old_part_size + block_size - 1) / block_size)) |
| 745 | new_block_counters = array.array( |
| 746 | 'B', [0] * ((new_part_size + block_size - 1) / block_size)) |
| 747 | prev_data_offset = 1876 |
| 748 | blob_hash_counts = collections.defaultdict(int) |
| 749 | |
| 750 | # Create the operation object for the test. |
Alex Deymo | 2846677 | 2015-09-11 17:16:44 -0700 | [diff] [blame] | 751 | op = update_metadata_pb2.InstallOperation() |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 752 | op.type = op_type |
| 753 | |
| 754 | total_src_blocks = 0 |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 755 | if op_type in (common.OpType.SOURCE_COPY, common.OpType.SOURCE_BSDIFF, |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 756 | common.OpType.PUFFDIFF, common.OpType.BROTLI_BSDIFF): |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 757 | if fail_src_extents: |
| 758 | self.AddToMessage(op.src_extents, |
Allie Wood | b065e13 | 2015-04-24 10:20:27 -0700 | [diff] [blame] | 759 | self.NewExtentList((1, 0))) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 760 | else: |
| 761 | self.AddToMessage(op.src_extents, |
Allie Wood | b065e13 | 2015-04-24 10:20:27 -0700 | [diff] [blame] | 762 | self.NewExtentList((1, 16))) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 763 | total_src_blocks = 16 |
| 764 | |
Tudor Brindus | 40506cd | 2018-06-18 20:18:17 -0700 | [diff] [blame] | 765 | # TODO(tbrindus): add major version 2 tests. |
Amin Hassani | 8ea1957 | 2018-12-05 12:06:21 -0800 | [diff] [blame] | 766 | payload_checker.major_version = common.CHROMEOS_MAJOR_PAYLOAD_VERSION |
Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 767 | if op_type in (common.OpType.REPLACE, common.OpType.REPLACE_BZ): |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 768 | payload_checker.minor_version = 0 |
Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 769 | elif op_type in (common.OpType.SOURCE_COPY, common.OpType.SOURCE_BSDIFF): |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 770 | payload_checker.minor_version = 1 if fail_bad_minor_version else 2 |
Amin Hassani | 0de7f78 | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 771 | if op_type == common.OpType.REPLACE_XZ: |
| 772 | payload_checker.minor_version = 2 if fail_bad_minor_version else 3 |
Amin Hassani | cdeb6e6 | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 773 | elif op_type in (common.OpType.ZERO, common.OpType.DISCARD, |
Amin Hassani | 77d7cbc | 2018-02-07 16:21:33 -0800 | [diff] [blame] | 774 | common.OpType.BROTLI_BSDIFF): |
Amin Hassani | 8ad22ba | 2017-10-11 10:15:11 -0700 | [diff] [blame] | 775 | payload_checker.minor_version = 3 if fail_bad_minor_version else 4 |
Amin Hassani | 77d7cbc | 2018-02-07 16:21:33 -0800 | [diff] [blame] | 776 | elif op_type == common.OpType.PUFFDIFF: |
| 777 | payload_checker.minor_version = 4 if fail_bad_minor_version else 5 |
Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 778 | |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 779 | if op_type != common.OpType.SOURCE_COPY: |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 780 | if not fail_mismatched_data_offset_length: |
| 781 | op.data_length = 16 * block_size - 8 |
| 782 | if fail_prev_data_offset: |
| 783 | op.data_offset = prev_data_offset + 16 |
| 784 | else: |
| 785 | op.data_offset = prev_data_offset |
| 786 | |
| 787 | fake_data = 'fake-data'.ljust(op.data_length) |
| 788 | if not (allow_unhashed or (is_last and allow_signature and |
| 789 | op_type == common.OpType.REPLACE)): |
| 790 | if not fail_data_hash: |
| 791 | # Create a valid data blob hash. |
| 792 | op.data_sha256_hash = hashlib.sha256(fake_data).digest() |
| 793 | payload.ReadDataBlob(op.data_offset, op.data_length).AndReturn( |
| 794 | fake_data) |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 795 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 796 | elif fail_data_hash: |
| 797 | # Create an invalid data blob hash. |
| 798 | op.data_sha256_hash = hashlib.sha256( |
| 799 | fake_data.replace(' ', '-')).digest() |
| 800 | payload.ReadDataBlob(op.data_offset, op.data_length).AndReturn( |
| 801 | fake_data) |
| 802 | |
| 803 | total_dst_blocks = 0 |
| 804 | if not fail_missing_dst_extents: |
| 805 | total_dst_blocks = 16 |
| 806 | if fail_dst_extents: |
| 807 | self.AddToMessage(op.dst_extents, |
| 808 | self.NewExtentList((4, 16), (32, 0))) |
| 809 | else: |
| 810 | self.AddToMessage(op.dst_extents, |
| 811 | self.NewExtentList((4, 8), (64, 8))) |
| 812 | |
| 813 | if total_src_blocks: |
| 814 | if fail_src_length: |
| 815 | op.src_length = total_src_blocks * block_size + 8 |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 816 | elif (op_type == common.OpType.SOURCE_BSDIFF and |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 817 | payload_checker.minor_version <= 3): |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 818 | op.src_length = total_src_blocks * block_size |
| 819 | elif fail_src_length: |
| 820 | # Add an orphaned src_length. |
| 821 | op.src_length = 16 |
| 822 | |
| 823 | if total_dst_blocks: |
| 824 | if fail_dst_length: |
| 825 | op.dst_length = total_dst_blocks * block_size + 8 |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 826 | elif (op_type == common.OpType.SOURCE_BSDIFF and |
Amin Hassani | efa62d9 | 2017-11-09 13:46:56 -0800 | [diff] [blame] | 827 | payload_checker.minor_version <= 3): |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 828 | op.dst_length = total_dst_blocks * block_size |
| 829 | |
| 830 | self.mox.ReplayAll() |
| 831 | should_fail = (fail_src_extents or fail_dst_extents or |
| 832 | fail_mismatched_data_offset_length or |
| 833 | fail_missing_dst_extents or fail_src_length or |
Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 834 | fail_dst_length or fail_data_hash or fail_prev_data_offset or |
| 835 | fail_bad_minor_version) |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 836 | args = (op, 'foo', is_last, old_block_counters, new_block_counters, |
| 837 | old_part_size, new_part_size, prev_data_offset, allow_signature, |
| 838 | blob_hash_counts) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 839 | if should_fail: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 840 | self.assertRaises(PayloadError, payload_checker._CheckOperation, *args) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 841 | else: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 842 | self.assertEqual(op.data_length if op.HasField('data_length') else 0, |
| 843 | payload_checker._CheckOperation(*args)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 844 | |
| 845 | def testAllocBlockCounters(self): |
| 846 | """Tests _CheckMoveOperation().""" |
| 847 | payload_checker = checker.PayloadChecker(self.MockPayload()) |
| 848 | block_size = payload_checker.block_size |
| 849 | |
| 850 | # Check allocation for block-aligned partition size, ensure it's integers. |
| 851 | result = payload_checker._AllocBlockCounters(16 * block_size) |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 852 | self.assertEqual(16, len(result)) |
| 853 | self.assertEqual(int, type(result[0])) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 854 | |
| 855 | # Check allocation of unaligned partition sizes. |
| 856 | result = payload_checker._AllocBlockCounters(16 * block_size - 1) |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 857 | self.assertEqual(16, len(result)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 858 | result = payload_checker._AllocBlockCounters(16 * block_size + 1) |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 859 | self.assertEqual(17, len(result)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 860 | |
Allie Wood | fb04d30 | 2015-04-03 14:25:48 -0700 | [diff] [blame] | 861 | def DoCheckOperationsTest(self, fail_nonexhaustive_full_update): |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 862 | """Tests _CheckOperations().""" |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 863 | # Generate a test payload. For this test, we only care about one |
| 864 | # (arbitrary) set of operations, so we'll only be generating kernel and |
| 865 | # test with them. |
| 866 | payload_gen = test_utils.PayloadGenerator() |
| 867 | |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 868 | block_size = test_utils.KiB(4) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 869 | payload_gen.SetBlockSize(block_size) |
| 870 | |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 871 | rootfs_part_size = test_utils.MiB(8) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 872 | |
| 873 | # Fake rootfs operations in a full update, tampered with as required. |
| 874 | rootfs_op_type = common.OpType.REPLACE |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 875 | rootfs_data_length = rootfs_part_size |
| 876 | if fail_nonexhaustive_full_update: |
| 877 | rootfs_data_length -= block_size |
| 878 | |
| 879 | payload_gen.AddOperation(False, rootfs_op_type, |
| 880 | dst_extents=[(0, rootfs_data_length / block_size)], |
| 881 | data_offset=0, |
| 882 | data_length=rootfs_data_length) |
| 883 | |
| 884 | # Create the test object. |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 885 | payload_checker = _GetPayloadChecker(payload_gen.WriteToFile, |
| 886 | checker_init_dargs={ |
| 887 | 'allow_unhashed': True}) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 888 | payload_checker.payload_type = checker._TYPE_FULL |
| 889 | report = checker._PayloadReport() |
| 890 | |
Amin Hassani | ae85374 | 2017-10-11 10:27:27 -0700 | [diff] [blame] | 891 | args = (payload_checker.payload.manifest.install_operations, report, 'foo', |
| 892 | 0, rootfs_part_size, rootfs_part_size, rootfs_part_size, 0, False) |
Allie Wood | fb04d30 | 2015-04-03 14:25:48 -0700 | [diff] [blame] | 893 | if fail_nonexhaustive_full_update: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 894 | self.assertRaises(PayloadError, payload_checker._CheckOperations, *args) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 895 | else: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 896 | self.assertEqual(rootfs_data_length, |
| 897 | payload_checker._CheckOperations(*args)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 898 | |
| 899 | def DoCheckSignaturesTest(self, fail_empty_sigs_blob, fail_missing_pseudo_op, |
| 900 | fail_mismatched_pseudo_op, fail_sig_missing_fields, |
| 901 | fail_unknown_sig_version, fail_incorrect_sig): |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 902 | """Tests _CheckSignatures().""" |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 903 | # Generate a test payload. For this test, we only care about the signature |
| 904 | # block and how it relates to the payload hash. Therefore, we're generating |
| 905 | # a random (otherwise useless) payload for this purpose. |
| 906 | payload_gen = test_utils.EnhancedPayloadGenerator() |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 907 | block_size = test_utils.KiB(4) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 908 | payload_gen.SetBlockSize(block_size) |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 909 | rootfs_part_size = test_utils.MiB(2) |
| 910 | kernel_part_size = test_utils.KiB(16) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 911 | payload_gen.SetPartInfo(False, True, rootfs_part_size, |
| 912 | hashlib.sha256('fake-new-rootfs-content').digest()) |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 913 | payload_gen.SetPartInfo(True, True, kernel_part_size, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 914 | hashlib.sha256('fake-new-kernel-content').digest()) |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 915 | payload_gen.SetMinorVersion(0) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 916 | payload_gen.AddOperationWithData( |
| 917 | False, common.OpType.REPLACE, |
| 918 | dst_extents=[(0, rootfs_part_size / block_size)], |
| 919 | data_blob=os.urandom(rootfs_part_size)) |
| 920 | |
| 921 | do_forge_pseudo_op = (fail_missing_pseudo_op or fail_mismatched_pseudo_op) |
| 922 | do_forge_sigs_data = (do_forge_pseudo_op or fail_empty_sigs_blob or |
| 923 | fail_sig_missing_fields or fail_unknown_sig_version |
| 924 | or fail_incorrect_sig) |
| 925 | |
| 926 | sigs_data = None |
| 927 | if do_forge_sigs_data: |
| 928 | sigs_gen = test_utils.SignaturesGenerator() |
| 929 | if not fail_empty_sigs_blob: |
| 930 | if fail_sig_missing_fields: |
| 931 | sig_data = None |
| 932 | else: |
| 933 | sig_data = test_utils.SignSha256('fake-payload-content', |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 934 | test_utils._PRIVKEY_FILE_NAME) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 935 | sigs_gen.AddSig(5 if fail_unknown_sig_version else 1, sig_data) |
| 936 | |
| 937 | sigs_data = sigs_gen.ToBinary() |
| 938 | payload_gen.SetSignatures(payload_gen.curr_offset, len(sigs_data)) |
| 939 | |
| 940 | if do_forge_pseudo_op: |
| 941 | assert sigs_data is not None, 'should have forged signatures blob by now' |
| 942 | sigs_len = len(sigs_data) |
| 943 | payload_gen.AddOperation( |
| 944 | False, common.OpType.REPLACE, |
| 945 | data_offset=payload_gen.curr_offset / 2, |
| 946 | data_length=sigs_len / 2, |
| 947 | dst_extents=[(0, (sigs_len / 2 + block_size - 1) / block_size)]) |
| 948 | |
| 949 | # Generate payload (complete w/ signature) and create the test object. |
| 950 | payload_checker = _GetPayloadChecker( |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 951 | payload_gen.WriteToFileWithData, |
| 952 | payload_gen_dargs={ |
| 953 | 'sigs_data': sigs_data, |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 954 | 'privkey_file_name': test_utils._PRIVKEY_FILE_NAME, |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 955 | 'do_add_pseudo_operation': not do_forge_pseudo_op}) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 956 | payload_checker.payload_type = checker._TYPE_FULL |
| 957 | report = checker._PayloadReport() |
| 958 | |
| 959 | # We have to check the manifest first in order to set signature attributes. |
Tudor Brindus | 8d05a7e | 2018-06-14 11:18:18 -0700 | [diff] [blame] | 960 | payload_checker._CheckManifest(report, { |
| 961 | common.ROOTFS: rootfs_part_size, |
| 962 | common.KERNEL: kernel_part_size |
| 963 | }) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 964 | |
| 965 | should_fail = (fail_empty_sigs_blob or fail_missing_pseudo_op or |
| 966 | fail_mismatched_pseudo_op or fail_sig_missing_fields or |
| 967 | fail_unknown_sig_version or fail_incorrect_sig) |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 968 | args = (report, test_utils._PUBKEY_FILE_NAME) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 969 | if should_fail: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 970 | self.assertRaises(PayloadError, payload_checker._CheckSignatures, *args) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 971 | else: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 972 | self.assertIsNone(payload_checker._CheckSignatures(*args)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 973 | |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 974 | def DoCheckManifestMinorVersionTest(self, minor_version, payload_type): |
| 975 | """Parametric testing for CheckManifestMinorVersion(). |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 976 | |
| 977 | Args: |
| 978 | minor_version: The payload minor version to test with. |
| 979 | payload_type: The type of the payload we're testing, delta or full. |
| 980 | """ |
| 981 | # Create the test object. |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 982 | payload = self.MockPayload() |
| 983 | payload.manifest.minor_version = minor_version |
| 984 | payload_checker = checker.PayloadChecker(payload) |
| 985 | payload_checker.payload_type = payload_type |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 986 | report = checker._PayloadReport() |
| 987 | |
| 988 | should_succeed = ( |
| 989 | (minor_version == 0 and payload_type == checker._TYPE_FULL) or |
Sen Jiang | 912c4df | 2015-12-10 12:17:13 -0800 | [diff] [blame] | 990 | (minor_version == 2 and payload_type == checker._TYPE_DELTA) or |
Sen Jiang | 92161a7 | 2016-06-28 16:09:38 -0700 | [diff] [blame] | 991 | (minor_version == 3 and payload_type == checker._TYPE_DELTA) or |
Amin Hassani | 77d7cbc | 2018-02-07 16:21:33 -0800 | [diff] [blame] | 992 | (minor_version == 4 and payload_type == checker._TYPE_DELTA) or |
| 993 | (minor_version == 5 and payload_type == checker._TYPE_DELTA)) |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 994 | args = (report,) |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 995 | |
| 996 | if should_succeed: |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 997 | self.assertIsNone(payload_checker._CheckManifestMinorVersion(*args)) |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 998 | else: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 999 | self.assertRaises(PayloadError, |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 1000 | payload_checker._CheckManifestMinorVersion, *args) |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 1001 | |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1002 | def DoRunTest(self, rootfs_part_size_provided, kernel_part_size_provided, |
| 1003 | fail_wrong_payload_type, fail_invalid_block_size, |
Amin Hassani | a86b108 | 2018-03-08 15:48:59 -0800 | [diff] [blame] | 1004 | fail_mismatched_metadata_size, fail_mismatched_block_size, |
| 1005 | fail_excess_data, fail_rootfs_part_size_exceeded, |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1006 | fail_kernel_part_size_exceeded): |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 1007 | """Tests Run().""" |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1008 | # Generate a test payload. For this test, we generate a full update that |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 1009 | # has sample kernel and rootfs operations. Since most testing is done with |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1010 | # internal PayloadChecker methods that are tested elsewhere, here we only |
| 1011 | # tamper with what's actually being manipulated and/or tested in the Run() |
| 1012 | # method itself. Note that the checker doesn't verify partition hashes, so |
| 1013 | # they're safe to fake. |
| 1014 | payload_gen = test_utils.EnhancedPayloadGenerator() |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 1015 | block_size = test_utils.KiB(4) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1016 | payload_gen.SetBlockSize(block_size) |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1017 | kernel_filesystem_size = test_utils.KiB(16) |
| 1018 | rootfs_filesystem_size = test_utils.MiB(2) |
| 1019 | payload_gen.SetPartInfo(False, True, rootfs_filesystem_size, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1020 | hashlib.sha256('fake-new-rootfs-content').digest()) |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1021 | payload_gen.SetPartInfo(True, True, kernel_filesystem_size, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1022 | hashlib.sha256('fake-new-kernel-content').digest()) |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 1023 | payload_gen.SetMinorVersion(0) |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1024 | |
| 1025 | rootfs_part_size = 0 |
| 1026 | if rootfs_part_size_provided: |
| 1027 | rootfs_part_size = rootfs_filesystem_size + block_size |
| 1028 | rootfs_op_size = rootfs_part_size or rootfs_filesystem_size |
| 1029 | if fail_rootfs_part_size_exceeded: |
| 1030 | rootfs_op_size += block_size |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1031 | payload_gen.AddOperationWithData( |
| 1032 | False, common.OpType.REPLACE, |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1033 | dst_extents=[(0, rootfs_op_size / block_size)], |
| 1034 | data_blob=os.urandom(rootfs_op_size)) |
| 1035 | |
| 1036 | kernel_part_size = 0 |
| 1037 | if kernel_part_size_provided: |
| 1038 | kernel_part_size = kernel_filesystem_size + block_size |
| 1039 | kernel_op_size = kernel_part_size or kernel_filesystem_size |
| 1040 | if fail_kernel_part_size_exceeded: |
| 1041 | kernel_op_size += block_size |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1042 | payload_gen.AddOperationWithData( |
| 1043 | True, common.OpType.REPLACE, |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1044 | dst_extents=[(0, kernel_op_size / block_size)], |
| 1045 | data_blob=os.urandom(kernel_op_size)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1046 | |
| 1047 | # Generate payload (complete w/ signature) and create the test object. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1048 | if fail_invalid_block_size: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1049 | use_block_size = block_size + 5 # Not a power of two. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1050 | elif fail_mismatched_block_size: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1051 | use_block_size = block_size * 2 # Different that payload stated. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1052 | else: |
| 1053 | use_block_size = block_size |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1054 | |
Amin Hassani | a86b108 | 2018-03-08 15:48:59 -0800 | [diff] [blame] | 1055 | # For the unittests 246 is the value that generated for the payload. |
| 1056 | metadata_size = 246 |
| 1057 | if fail_mismatched_metadata_size: |
| 1058 | metadata_size += 1 |
| 1059 | |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1060 | kwargs = { |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 1061 | 'payload_gen_dargs': { |
Gilad Arnold | 18f4f9f | 2013-04-02 16:24:41 -0700 | [diff] [blame] | 1062 | 'privkey_file_name': test_utils._PRIVKEY_FILE_NAME, |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 1063 | 'do_add_pseudo_operation': True, |
| 1064 | 'is_pseudo_in_kernel': True, |
| 1065 | 'padding': os.urandom(1024) if fail_excess_data else None}, |
| 1066 | 'checker_init_dargs': { |
| 1067 | 'assert_type': 'delta' if fail_wrong_payload_type else 'full', |
| 1068 | 'block_size': use_block_size}} |
| 1069 | if fail_invalid_block_size: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 1070 | self.assertRaises(PayloadError, _GetPayloadChecker, |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1071 | payload_gen.WriteToFileWithData, **kwargs) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1072 | else: |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 1073 | payload_checker = _GetPayloadChecker(payload_gen.WriteToFileWithData, |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1074 | **kwargs) |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1075 | |
Tudor Brindus | 2d22c1a | 2018-06-15 13:07:13 -0700 | [diff] [blame] | 1076 | kwargs = { |
| 1077 | 'pubkey_file_name': test_utils._PUBKEY_FILE_NAME, |
| 1078 | 'metadata_size': metadata_size, |
| 1079 | 'part_sizes': { |
| 1080 | common.KERNEL: kernel_part_size, |
| 1081 | common.ROOTFS: rootfs_part_size}} |
| 1082 | |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 1083 | should_fail = (fail_wrong_payload_type or fail_mismatched_block_size or |
Amin Hassani | a86b108 | 2018-03-08 15:48:59 -0800 | [diff] [blame] | 1084 | fail_mismatched_metadata_size or fail_excess_data or |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1085 | fail_rootfs_part_size_exceeded or |
| 1086 | fail_kernel_part_size_exceeded) |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 1087 | if should_fail: |
Amin Hassani | b05a65a | 2017-12-18 15:15:32 -0800 | [diff] [blame] | 1088 | self.assertRaises(PayloadError, payload_checker.Run, **kwargs) |
Gilad Arnold | eaed0d1 | 2013-04-30 15:38:22 -0700 | [diff] [blame] | 1089 | else: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1090 | self.assertIsNone(payload_checker.Run(**kwargs)) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1091 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1092 | # This implements a generic API, hence the occasional unused args. |
| 1093 | # pylint: disable=W0613 |
| 1094 | def ValidateCheckOperationTest(op_type_name, is_last, allow_signature, |
| 1095 | allow_unhashed, fail_src_extents, |
| 1096 | fail_dst_extents, |
| 1097 | fail_mismatched_data_offset_length, |
| 1098 | fail_missing_dst_extents, fail_src_length, |
| 1099 | fail_dst_length, fail_data_hash, |
Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 1100 | fail_prev_data_offset, fail_bad_minor_version): |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1101 | """Returns True iff the combination of arguments represents a valid test.""" |
| 1102 | op_type = _OpTypeByName(op_type_name) |
| 1103 | |
Amin Hassani | 0de7f78 | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 1104 | # REPLACE/REPLACE_BZ/REPLACE_XZ operations don't read data from src |
| 1105 | # partition. They are compatible with all valid minor versions, so we don't |
| 1106 | # need to check that. |
| 1107 | if (op_type in (common.OpType.REPLACE, common.OpType.REPLACE_BZ, |
| 1108 | common.OpType.REPLACE_XZ) and (fail_src_extents or |
| 1109 | fail_src_length or |
| 1110 | fail_bad_minor_version)): |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1111 | return False |
| 1112 | |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 1113 | # SOURCE_COPY operation does not carry data. |
| 1114 | if (op_type == common.OpType.SOURCE_COPY and ( |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1115 | fail_mismatched_data_offset_length or fail_data_hash or |
| 1116 | fail_prev_data_offset)): |
| 1117 | return False |
| 1118 | |
| 1119 | return True |
| 1120 | |
| 1121 | |
| 1122 | def TestMethodBody(run_method_name, run_dargs): |
| 1123 | """Returns a function that invokes a named method with named arguments.""" |
| 1124 | return lambda self: getattr(self, run_method_name)(**run_dargs) |
| 1125 | |
| 1126 | |
| 1127 | def AddParametricTests(tested_method_name, arg_space, validate_func=None): |
| 1128 | """Enumerates and adds specific parametric tests to PayloadCheckerTest. |
| 1129 | |
| 1130 | This function enumerates a space of test parameters (defined by arg_space), |
| 1131 | then binds a new, unique method name in PayloadCheckerTest to a test function |
| 1132 | that gets handed the said parameters. This is a preferable approach to doing |
| 1133 | the enumeration and invocation during the tests because this way each test is |
| 1134 | treated as a complete run by the unittest framework, and so benefits from the |
| 1135 | usual setUp/tearDown mechanics. |
| 1136 | |
| 1137 | Args: |
Gilad Arnold | cb63891 | 2013-06-24 04:57:11 -0700 | [diff] [blame] | 1138 | tested_method_name: Name of the tested PayloadChecker method. |
| 1139 | arg_space: A dictionary containing variables (keys) and lists of values |
| 1140 | (values) associated with them. |
| 1141 | validate_func: A function used for validating test argument combinations. |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1142 | """ |
| 1143 | for value_tuple in itertools.product(*arg_space.itervalues()): |
| 1144 | run_dargs = dict(zip(arg_space.iterkeys(), value_tuple)) |
| 1145 | if validate_func and not validate_func(**run_dargs): |
| 1146 | continue |
| 1147 | run_method_name = 'Do%sTest' % tested_method_name |
| 1148 | test_method_name = 'test%s' % tested_method_name |
| 1149 | for arg_key, arg_val in run_dargs.iteritems(): |
| 1150 | if arg_val or type(arg_val) is int: |
| 1151 | test_method_name += '__%s=%s' % (arg_key, arg_val) |
| 1152 | setattr(PayloadCheckerTest, test_method_name, |
| 1153 | TestMethodBody(run_method_name, run_dargs)) |
| 1154 | |
| 1155 | |
| 1156 | def AddAllParametricTests(): |
| 1157 | """Enumerates and adds all parametric tests to PayloadCheckerTest.""" |
| 1158 | # Add all _CheckElem() test cases. |
| 1159 | AddParametricTests('AddElem', |
| 1160 | {'linebreak': (True, False), |
| 1161 | 'indent': (0, 1, 2), |
| 1162 | 'convert': (str, lambda s: s[::-1]), |
| 1163 | 'is_present': (True, False), |
| 1164 | 'is_mandatory': (True, False), |
| 1165 | 'is_submsg': (True, False)}) |
| 1166 | |
| 1167 | # Add all _Add{Mandatory,Optional}Field tests. |
| 1168 | AddParametricTests('AddField', |
| 1169 | {'is_mandatory': (True, False), |
| 1170 | 'linebreak': (True, False), |
| 1171 | 'indent': (0, 1, 2), |
| 1172 | 'convert': (str, lambda s: s[::-1]), |
| 1173 | 'is_present': (True, False)}) |
| 1174 | |
| 1175 | # Add all _Add{Mandatory,Optional}SubMsg tests. |
| 1176 | AddParametricTests('AddSubMsg', |
| 1177 | {'is_mandatory': (True, False), |
| 1178 | 'is_present': (True, False)}) |
| 1179 | |
| 1180 | # Add all _CheckManifest() test cases. |
| 1181 | AddParametricTests('CheckManifest', |
| 1182 | {'fail_mismatched_block_size': (True, False), |
| 1183 | 'fail_bad_sigs': (True, False), |
| 1184 | 'fail_mismatched_oki_ori': (True, False), |
| 1185 | 'fail_bad_oki': (True, False), |
| 1186 | 'fail_bad_ori': (True, False), |
| 1187 | 'fail_bad_nki': (True, False), |
| 1188 | 'fail_bad_nri': (True, False), |
Gilad Arnold | 382df5c | 2013-05-03 12:49:28 -0700 | [diff] [blame] | 1189 | 'fail_old_kernel_fs_size': (True, False), |
| 1190 | 'fail_old_rootfs_fs_size': (True, False), |
| 1191 | 'fail_new_kernel_fs_size': (True, False), |
| 1192 | 'fail_new_rootfs_fs_size': (True, False)}) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1193 | |
| 1194 | # Add all _CheckOperation() test cases. |
| 1195 | AddParametricTests('CheckOperation', |
Amin Hassani | 0de7f78 | 2017-12-07 12:13:03 -0800 | [diff] [blame] | 1196 | {'op_type_name': ('REPLACE', 'REPLACE_BZ', 'REPLACE_XZ', |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 1197 | 'SOURCE_COPY', 'SOURCE_BSDIFF', |
| 1198 | 'PUFFDIFF', 'BROTLI_BSDIFF'), |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1199 | 'is_last': (True, False), |
| 1200 | 'allow_signature': (True, False), |
| 1201 | 'allow_unhashed': (True, False), |
| 1202 | 'fail_src_extents': (True, False), |
| 1203 | 'fail_dst_extents': (True, False), |
| 1204 | 'fail_mismatched_data_offset_length': (True, False), |
| 1205 | 'fail_missing_dst_extents': (True, False), |
| 1206 | 'fail_src_length': (True, False), |
| 1207 | 'fail_dst_length': (True, False), |
| 1208 | 'fail_data_hash': (True, False), |
Allie Wood | 7cf9f13 | 2015-02-26 14:28:19 -0800 | [diff] [blame] | 1209 | 'fail_prev_data_offset': (True, False), |
| 1210 | 'fail_bad_minor_version': (True, False)}, |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1211 | validate_func=ValidateCheckOperationTest) |
| 1212 | |
| 1213 | # Add all _CheckOperations() test cases. |
| 1214 | AddParametricTests('CheckOperations', |
Allie Wood | fb04d30 | 2015-04-03 14:25:48 -0700 | [diff] [blame] | 1215 | {'fail_nonexhaustive_full_update': (True, False)}) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1216 | |
| 1217 | # Add all _CheckOperations() test cases. |
| 1218 | AddParametricTests('CheckSignatures', |
| 1219 | {'fail_empty_sigs_blob': (True, False), |
| 1220 | 'fail_missing_pseudo_op': (True, False), |
| 1221 | 'fail_mismatched_pseudo_op': (True, False), |
| 1222 | 'fail_sig_missing_fields': (True, False), |
| 1223 | 'fail_unknown_sig_version': (True, False), |
| 1224 | 'fail_incorrect_sig': (True, False)}) |
| 1225 | |
Gilad Arnold | 0d575cd | 2015-07-13 17:29:21 -0700 | [diff] [blame] | 1226 | # Add all _CheckManifestMinorVersion() test cases. |
| 1227 | AddParametricTests('CheckManifestMinorVersion', |
Amin Hassani | 0f59a9a | 2019-09-27 10:24:31 -0700 | [diff] [blame] | 1228 | {'minor_version': (None, 0, 2, 3, 4, 5, 555), |
Allie Wood | f5c4f3e | 2015-02-20 16:57:46 -0800 | [diff] [blame] | 1229 | 'payload_type': (checker._TYPE_FULL, |
| 1230 | checker._TYPE_DELTA)}) |
| 1231 | |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1232 | # Add all Run() test cases. |
| 1233 | AddParametricTests('Run', |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1234 | {'rootfs_part_size_provided': (True, False), |
| 1235 | 'kernel_part_size_provided': (True, False), |
| 1236 | 'fail_wrong_payload_type': (True, False), |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1237 | 'fail_invalid_block_size': (True, False), |
Amin Hassani | a86b108 | 2018-03-08 15:48:59 -0800 | [diff] [blame] | 1238 | 'fail_mismatched_metadata_size': (True, False), |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1239 | 'fail_mismatched_block_size': (True, False), |
Gilad Arnold | 06eea33 | 2015-07-13 18:06:33 -0700 | [diff] [blame] | 1240 | 'fail_excess_data': (True, False), |
| 1241 | 'fail_rootfs_part_size_exceeded': (True, False), |
| 1242 | 'fail_kernel_part_size_exceeded': (True, False)}) |
Gilad Arnold | 5502b56 | 2013-03-08 13:22:31 -0800 | [diff] [blame] | 1243 | |
| 1244 | |
| 1245 | if __name__ == '__main__': |
| 1246 | AddAllParametricTests() |
| 1247 | unittest.main() |