Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(154)

Side by Side Diff: scripts/image_signing/ensure_sane_lsb-release.sh

Issue 6246037: Add sanity test for /etc/lsb-release file in CrOS images (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/vboot_reference.git@master
Patch Set: Created 9 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « scripts/image_signing/ensure_sane_lsb-release.config ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 #!/bin/bash
2
3 # Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 # Abort on error.
8 set -e
9
10 LSB_FILE=/etc/lsb-release
11
12 # Load common constants and variables.
13 . "$(dirname "$0")/common.sh"
14
15 usage() {
16 echo "Usage $PROG image [config]"
petkov 2011/02/02 15:57:12 why 4-space indent? i think our style is 2 space.
jimhebert 2011/02/02 23:51:36 Ack. I started out by copying the style from ensur
17 }
18
19 # Usage: lsbval path-to-lsb-file key
20 # Returns the value for the given lsb-release file variable.
21 lsbval() {
22 local lsbfile="$1"
23 local key="$2"
24 grep ^$key= "$lsbfile" | sed s/^$key=//
25 }
26
petkov 2011/02/02 15:57:12 remove extra blank line
jimhebert 2011/02/02 23:51:36 Done
27
28 # Usage: lsbequals path-to-lsb-file key expected-value
29 # Returns 0 if they match, 1 otherwise.
30 # Also outputs a warning message if they don't match.
31 lsbequals() {
32 local lsbfile="$1"
33 local key="$2"
34 local expectval="$3"
35 local realval=$(lsbval "$lsbfile" $key)
36 if [ "$realval" != "$expectval" ]; then
37 echo "$key mismatch. Expected '$expectval', image contains '$realval'"
38 return 1
39 fi
40 return 0
41 }
42
43 # Usage: lsb_syntaxcheck path-to-lsb-file
44 # Enforces a number of basic sanity checks on the overall format and contents
45 # of the lsb-release file:
46 # - Every line is "key=value".
47 # - No space after key, no space before value.
48 # - key is all A-Z or _, but not starting with _.
49 # - value is made up of printable characters, or is empty.
50 # - Each line is a reasonable size (256 bytes).
51 # - The whole file is a reasonable size (4kb).
52 lsb_syntaxcheck() {
53 local lsbfile="$1"
54 syntaxbad=0
55 # Checks for key being A-Z_, 1 or more characters, not starting with _.
56 # Also checks for = with no spaces on either side.
57 # Checks that the value contains printables (and not starting with space).
58 # Alternatively, the 2nd grep permits the value to be empty (0 chars) too.
59 badlines=$(grep -E -v '^[A-Z][A-Z_]*=[[:graph:]][[:print:]]*' "$lsbfile" |\
petkov 2011/02/02 15:57:12 no need for trailing \ you could do this in a sin
jimhebert 2011/02/02 23:51:36 Done
60 grep -E -v '^[A-Z][A-Z_]*=$')
61 if [ -n "$badlines" ]; then
62 syntaxbad=1
63 echo "$lsbfile: Some lines seem non-well-formed:"
64 echo "$badlines"
65 fi
66
67 # Checks for a lines exceeding a reasonable overall length.
68 # regex(7) says {number} cannot exceed RE_DUP_MAX (255!) so we use this
69 # workaround to get the equivalent of {257}.
70 badlines=$(grep -E '^.{255}.{2}' "$lsbfile")
petkov 2011/02/02 15:57:12 stick with 255 for simplicity? :)
jimhebert 2011/02/02 23:51:36 Done
71 if [ -n "$badlines" ]; then
72 syntaxbad=1
73 echo "$lsbfile: Some lsb-release lines seem unreasonably long:"
74 echo "$badlines"
75 fi
76 # Overall file size check:
77 size=$(ls -sk "$lsbfile" | cut -d ' ' -f 1)
78 if [ $size -gt 4 ]; then
79 syntaxbad=1
80 echo "$lsbfile: This file exceeds 4kb"
81 fi
82 return $syntaxbad
83 }
84
85
petkov 2011/02/02 15:57:12 remove extra blank line
jimhebert 2011/02/02 23:51:36 Done
86 main() {
87 # We want to catch all the discrepancies, not just the first one.
88 # So, any time we find one, we set testfail=1 and continue.
89 # When finished we will use testfail to determine our exit value.
90 local testfail=0
91
92 if [ $# -ne 1 ] && [ $# -ne 2 ]; then
93 usage
94 exit 1
95 fi
96
97 local image="$1"
98
99 # Default config location: same name/directory as this script,
100 # with a .config file extension, ie ensure_sane_lsb-release.config.
101 local configfile="$(dirname "$0")/${0/%.sh/.config}"
102 # Or, maybe a config was provided on the command line.
103 if [ $# -eq 2 ]; then
104 configfile="$2"
105 fi
106 # Either way, load test-expectations data from config.
107 . "$configfile"
petkov 2011/02/02 15:57:12 you may want to print an info message showing whic
jimhebert 2011/02/02 23:51:36 Done
108
109 local rootfs=$(make_temp_dir)
110 mount_image_partition_ro "$image" 3 "$rootfs"
petkov 2011/02/02 15:57:12 on error, nothing gets unmounted? and the temp dir
jimhebert 2011/02/02 23:51:36 From my reading of common_minimal.sh, this is all
111 local lsb="$rootfs/$LSB_FILE"
112
113 # Basic syntax check first.
114 lsb_syntaxcheck "$lsb" || testfail=1
115
116 # Pick the right set of test-expectation data to use. The cuts
117 # turn e.g. x86-foo-pvtkeys into x86-foo.
118 local board=$(lsbval $lsb CHROMEOS_RELEASE_BOARD | \
petkov 2011/02/02 15:57:12 no need for trailing \ after | I think. also, I th
jimhebert 2011/02/02 23:51:36 Done.
119 cut -d = -f 2 | cut -d - -f 1,2)
petkov 2011/02/02 15:57:12 this may be a bit flaky but i don't have much bett
gauravsh 2011/02/03 01:12:38 Yes, I agree with this concern. The proper solutio
120 # a copy of the board string with '-' squished to variable-name-safe '_'.
121 local boardvar=${board//-/_}
petkov 2011/02/02 15:57:12 i see why you're doing this but it's a bit flaky.
jimhebert 2011/02/02 23:51:36 I agree with the sentiments and had the same thoug
122 eval "expected_appid=\"\$expected_appid_$boardvar\""
123
gauravsh 2011/02/02 01:33:42 extra newline
jimhebert 2011/02/02 23:51:36 Done
124
125 lsbequals $lsb CHROMEOS_AUSERVER "$expected_auserver" || testfail=1
126 lsbequals $lsb CHROMEOS_RELEASE_NAME "$expected_release_name" || testfail=1
127 lsbequals $lsb CHROMEOS_RELEASE_APPID "$expected_appid" || testfail=1
128
129 # CHROMEOS_RELEASE_BOARD and CHROMEOS_RELEASE_TRACK must appear in their
130 # respective whitelists, expected_boards and expected_release_tracks.
131 # First, boards:
132 local board_recognized=0
133 for b in ${expected_boards[@]}; do
gauravsh 2011/02/02 01:33:42 shouldn't the board check happen before the eval o
jimhebert 2011/02/02 23:51:36 Done.
134 if [ $b == $board ]; then
135 board_recognized=1
136 fi
137 done
138 if [ $board_recognized -eq 0 ]; then
139 echo "Board '$board' unrecognized"
140 testfail=1
141 fi
142
143 # The same check as above, for tracks
144 local track_recognized=0
145 local track=$(lsbval $lsb CHROMEOS_RELEASE_TRACK)
146 for t in ${expected_release_tracks[@]}; do
147 if [ $t == $track ]; then
148 track_recognized=1
gauravsh 2011/02/02 01:33:42 these 2 checks for CHROMEOS_RELEASE_BOARD and CHRO
jimhebert 2011/02/02 23:51:36 Done.
149 fi
150 done
151 if [ $track_recognized -eq 0 ]; then
152 echo "Track '$track' unrecognized"
153 testfail=1
154 fi
155
156 exit $testfail
157 }
158 main $@
petkov 2011/02/02 15:57:12 add blank line before main
petkov 2011/02/02 15:57:12 "$@"
jimhebert 2011/02/02 23:51:36 Done
jimhebert 2011/02/02 23:51:36 Done
OLDNEW
« no previous file with comments | « scripts/image_signing/ensure_sane_lsb-release.config ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698