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

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: Fix indentation/style 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 | « no previous file | 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]"
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
27 # Usage: lsbequals path-to-lsb-file key expected-value
28 # Returns 0 if they match, 1 otherwise.
29 # Also outputs a warning message if they don't match.
30 lsbequals() {
31 local lsbfile="$1"
32 local key="$2"
33 local expectval="$3"
34 local realval=$(lsbval "$lsbfile" $key)
35 if [ "$realval" != "$expectval" ]; then
36 echo "$key mismatch. Expected '$expectval', image contains '$realval'"
37 return 1
38 fi
39 return 0
40 }
41
42 # Usage: check_keyval_in_list lsbfile lsbkey [list of values]
43 # Extracts the lsb-release value for the specified key, and confirms it
44 # matches one of the whitelisted values specified in value_array.
45 # Implementation note:
46 # You can't really pass bash arrays to functions. Best you can do is either
47 # serialize to string/pass/deserialize (e.g. using whitspace/IFS hacks), or,
48 # let the array contents be received as multiple arguments to the target
49 # function. We take the latter approach here, hence the shift's to get the
50 # first 2 arguments out, before we process the rest of the varargs.
gauravsh 2011/02/03 01:12:38 you could, I guess, also use an eval assuming the
51 check_keyval_in_list() {
52 local lsbfile="$1"
53 shift
54 local lsbkey="$1"
55 shift
56 local lsbval=$(lsbval "$lsbfile" "$lsbkey")
57 while [ $# -gt 0 ]; do
58 if [ "$lsbval" == "$1" ]; then
59 return 0
60 fi
61 shift
62 done
63 # If we get here, it wasn't found
64 echo "$lsbkey: Value '$lsbval' was not recognized"
65 return 1
66 }
67
68 # Usage: lsb_syntaxcheck path-to-lsb-file
69 # Enforces a number of basic sanity checks on the overall format and contents
70 # of the lsb-release file:
71 # - Every line is "key=value".
72 # - No space after key, no space before value.
73 # - key is all A-Z or _, but not starting with _.
74 # - value is made up of printable characters, or is empty.
75 # - Each line is a reasonable size (<255 bytes).
76 # - The whole file is a reasonable size (4kb).
77 lsb_syntaxcheck() {
78 local lsbfile="$1"
79 syntaxbad=0
80 # Checks for key being A-Z_, 1 or more characters, not starting with _.
81 # Also checks for = with no spaces on either side.
82 # Checks that the value contains printables (and not starting with space).
83 # Alternatively, the value is permitted to be empty (0 chars) too.
84 badlines=$(grep -Ev '^[A-Z][A-Z_]*=([[:graph:]][[:print:]]*)?$' "$lsbfile")
85 if [ -n "$badlines" ]; then
86 syntaxbad=1
87 echo "$lsbfile: Some lines seem non-well-formed:"
88 echo "$badlines"
89 fi
90
91 # Checks for a lines exceeding a reasonable overall length.
92 badlines=$(grep -E '^.{255}' "$lsbfile")
93 if [ -n "$badlines" ]; then
94 syntaxbad=1
95 echo "$lsbfile: Some lsb-release lines seem unreasonably long:"
96 echo "$badlines"
97 fi
98 # Overall file size check:
99 size=$(ls -sk "$lsbfile" | cut -d ' ' -f 1)
100 if [ $size -gt 4 ]; then
101 syntaxbad=1
102 echo "$lsbfile: This file exceeds 4kb"
103 fi
104 return $syntaxbad
105 }
106
107 main() {
108 # We want to catch all the discrepancies, not just the first one.
109 # So, any time we find one, we set testfail=1 and continue.
110 # When finished we will use testfail to determine our exit value.
111 local testfail=0
112
113 if [ $# -ne 1 ] && [ $# -ne 2 ]; then
114 usage
115 exit 1
116 fi
117
118 local image="$1"
119
120 # Default config location: same name/directory as this script,
121 # with a .config file extension, ie ensure_sane_lsb-release.config.
122 local configfile="$(dirname "$0")/${0/%.sh/.config}"
gauravsh 2011/02/03 01:12:38 A thought: Since we will only keep the a default c
123 # Or, maybe a config was provided on the command line.
124 if [ $# -eq 2 ]; then
125 configfile="$2"
126 fi
127 # Either way, load test-expectations data from config.
128 echo -n "Loading config from $configfile... "
129 . "$configfile"
130 echo "Done."
131
132 local rootfs=$(make_temp_dir)
133 mount_image_partition_ro "$image" 3 "$rootfs"
134 local lsb="$rootfs/$LSB_FILE"
135
136 # Basic syntax check first.
137 lsb_syntaxcheck "$lsb" || testfail=1
138
139 lsbequals $lsb CHROMEOS_AUSERVER "$expected_auserver" || testfail=1
140 lsbequals $lsb CHROMEOS_RELEASE_NAME "$expected_release_name" || testfail=1
141 check_keyval_in_list $lsb CHROMEOS_RELEASE_TRACK \
142 "${expected_release_tracks[@]}" || testfail=1
143
144 if check_keyval_in_list $lsb CHROMEOS_RELEASE_BOARD \
145 "${expected_boards[@]}"; then
146 # Pick the right set of test-expectation data to use. The cuts
147 # turn e.g. x86-foo-pvtkeys into x86-foo.
148 local board=$(lsbval $lsb CHROMEOS_RELEASE_BOARD |
149 cut -d = -f 2 |
150 cut -d - -f 1,2)
151 # a copy of the board string with '-' squished to variable-name-safe '_'.
152 local boardvar=${board//-/_}
153 eval "expected_appid=\"\$expected_appid_$boardvar\""
154 lsbequals $lsb CHROMEOS_RELEASE_APPID "$expected_appid" || testfail=1
155 else # unrecognized board
156 testfail=1
157 fi
158
159 exit $testfail
160 }
161
162 main "$@"
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698