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