1#!/usr/bin/env python 2# 3# Copyright (C) 2018 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 17import unittest 18 19import mkuserimg_mke2fs 20 21class MkuserimgMke2fsTest(unittest.TestCase): 22 def setUp(self): 23 self.optional_arguments = { 24 "-j": "10", "-T": "1230768000.0", "-C": "fs_config", 25 "-D": "product_out", "-B": "block_list_file", 26 "-d": "base_alloc_file_in", "-A": "base_alloc_file_out", 27 "-L": "label", "-i": "20", "-M": "30", "-e": "8192", 28 "-o": "16384", "-U": "mke2fs_uuid", "-S": "mke2fs_hash_seed", 29 } 30 31 def test_parse_arguments_smoke(self): 32 args_list = ["source_directory", "output_file", "ext4", "data", "8192"] 33 for key, value in self.optional_arguments.items(): 34 args_list += [key, value] 35 args_list.append("-c") 36 37 args = mkuserimg_mke2fs.ParseArguments(args_list) 38 39 self.assertEqual("source_directory", args.src_dir) 40 self.assertEqual("output_file", args.output_file) 41 self.assertEqual("ext4", args.ext_variant) 42 self.assertEqual("data", args.mount_point) 43 self.assertEqual("8192", args.fs_size) 44 45 self.assertFalse(args.android_sparse) 46 self.assertEqual("10", args.journal_size) 47 self.assertEqual("1230768000.0", args.timestamp) 48 self.assertEqual("fs_config", args.fs_config) 49 self.assertEqual("product_out", args.product_out) 50 self.assertEqual("block_list_file", args.block_list_file) 51 self.assertEqual("base_alloc_file_in", args.base_alloc_file_in) 52 self.assertEqual("base_alloc_file_out", args.base_alloc_file_out) 53 self.assertEqual("label", args.label) 54 self.assertEqual("20", args.inodes) 55 self.assertEqual("30", args.reserved_percent) 56 self.assertEqual("8192", args.flash_erase_block_size) 57 self.assertEqual("16384", args.flash_logical_block_size) 58 self.assertEqual("mke2fs_uuid", args.mke2fs_uuid) 59 self.assertEqual("mke2fs_hash_seed", args.mke2fs_hash_seed) 60 self.assertTrue(args.share_dup_blocks) 61 62 def test_parse_arguments_with_filecontext(self): 63 args_list = ["-s", "source_directory", "output_file", "ext4", "data", 64 "8192"] 65 for key, value in self.optional_arguments.items(): 66 args_list += [key, value] 67 args_list += ["-c", "file_contexts.bin"] 68 69 args = mkuserimg_mke2fs.ParseArguments(args_list) 70 71 self.assertEqual("file_contexts.bin", args.file_contexts) 72 73 self.assertEqual("source_directory", args.src_dir) 74 self.assertEqual("output_file", args.output_file) 75 self.assertEqual("ext4", args.ext_variant) 76 self.assertEqual("data", args.mount_point) 77 self.assertEqual("8192", args.fs_size) 78 79 self.assertTrue(args.android_sparse) 80 self.assertEqual("10", args.journal_size) 81 self.assertEqual("1230768000.0", args.timestamp) 82 self.assertEqual("fs_config", args.fs_config) 83 self.assertEqual("product_out", args.product_out) 84 self.assertEqual("block_list_file", args.block_list_file) 85 self.assertEqual("base_alloc_file_in", args.base_alloc_file_in) 86 self.assertEqual("base_alloc_file_out", args.base_alloc_file_out) 87 self.assertEqual("label", args.label) 88 self.assertEqual("20", args.inodes) 89 self.assertEqual("30", args.reserved_percent) 90 self.assertEqual("8192", args.flash_erase_block_size) 91 self.assertEqual("16384", args.flash_logical_block_size) 92 self.assertEqual("mke2fs_uuid", args.mke2fs_uuid) 93 self.assertEqual("mke2fs_hash_seed", args.mke2fs_hash_seed) 94 self.assertTrue(args.share_dup_blocks) 95 96 def test_parse_arguments_not_enough_arguments(self): 97 args_list = ["-s", "source_directory", "output_file", "ext4", "data",] 98 for key, value in self.optional_arguments.items(): 99 args_list += [key, value] 100 101 with self.assertRaises(SystemExit): 102 mkuserimg_mke2fs.ParseArguments(args_list) 103 104 def test_construct_e2fs_opts_smoke(self): 105 args_list = ["-s", "source_directory", "output_file", "ext4", "data", 106 "8192"] 107 for key, value in self.optional_arguments.items(): 108 args_list += [key, value] 109 args_list += ["-c", "file_contexts.bin"] 110 111 args = mkuserimg_mke2fs.ParseArguments(args_list) 112 113 mke2fs_cmd, e2fsdroid_cmd = mkuserimg_mke2fs.ConstructE2fsCommands( 114 args) 115 116 expected_mke2fs_extended_opts = ( 117 "android_sparse,stripe_width=2,stride=4,hash_seed={}".format( 118 args.mke2fs_hash_seed)) 119 expected_mke2fs_cmd = [ 120 "mke2fs", "-J", "size=10", "-L", args.label, "-N", args.inodes, "-m", 121 args.reserved_percent, "-U", args.mke2fs_uuid, "-E", 122 expected_mke2fs_extended_opts, "-t", args.ext_variant, "-b", "4096", 123 args.output_file, str(int(args.fs_size) / 4096)] 124 125 expected_e2fsdroid_cmd = [ 126 "e2fsdroid", "-T", args.timestamp, "-C", args.fs_config, "-p", 127 args.product_out, "-B", args.block_list_file, "-d", 128 args.base_alloc_file_in, "-D", args.base_alloc_file_out, "-s", "-S", 129 args.file_contexts, "-f", args.src_dir, "-a", args.mount_point, 130 args.output_file] 131 132 self.assertEqual(' '.join(mke2fs_cmd), ' '.join(expected_mke2fs_cmd)) 133 134 self.assertEqual(' '.join(e2fsdroid_cmd), 135 ' '.join(expected_e2fsdroid_cmd)) 136