blob: 1babca07dfdffb1abea99c132c045717379bec9e [file] [log] [blame]
Patrick Williamsd8c66bc2016-06-20 12:57:21 -05001from oeqa.selftest.base import oeSelfTest
2from oeqa.utils.commands import runCmd, bitbake, get_bb_var
3import os
4import glob
5import re
6import shutil
7import tempfile
8from oeqa.utils.decorators import testcase
9from oeqa.utils.ftools import write_file
10
11
12class Signing(oeSelfTest):
13
14 gpg_dir = ""
15 pub_key_name = 'key.pub'
16 secret_key_name = 'key.secret'
17
18 @classmethod
19 def setUpClass(cls):
20 # Import the gpg keys
21
22 cls.gpg_dir = os.path.join(cls.testlayer_path, 'files/signing/')
23
24 # key.secret key.pub are located in gpg_dir
25 pub_key_location = cls.gpg_dir + cls.pub_key_name
26 secret_key_location = cls.gpg_dir + cls.secret_key_name
27 runCmd('gpg --homedir %s --import %s %s' % (cls.gpg_dir, pub_key_location, secret_key_location))
28
29 @classmethod
30 def tearDownClass(cls):
31 # Delete the files generated by 'gpg --import'
32
33 gpg_files = glob.glob(cls.gpg_dir + '*.gpg*')
34 random_seed_file = cls.gpg_dir + 'random_seed'
35 gpg_files.append(random_seed_file)
36
37 for gpg_file in gpg_files:
38 runCmd('rm -f ' + gpg_file)
39
40 @testcase(1362)
41 def test_signing_packages(self):
42 """
43 Summary: Test that packages can be signed in the package feed
44 Expected: Package should be signed with the correct key
45 Product: oe-core
46 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
47 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
48 """
49 import oe.packagedata
50
51 package_classes = get_bb_var('PACKAGE_CLASSES')
52 if 'package_rpm' not in package_classes:
53 self.skipTest('This test requires RPM Packaging.')
54
55 test_recipe = 'ed'
56
57 feature = 'INHERIT += "sign_rpm"\n'
58 feature += 'RPM_GPG_PASSPHRASE = "test123"\n'
59 feature += 'RPM_GPG_NAME = "testuser"\n'
60 feature += 'RPM_GPG_PUBKEY = "%s%s"\n' % (self.gpg_dir, self.pub_key_name)
61 feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
62
63 self.write_config(feature)
64
65 bitbake('-c cleansstate %s' % test_recipe)
66 bitbake(test_recipe)
67 self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
68
69 pkgdatadir = get_bb_var('PKGDATA_DIR', test_recipe)
70 pkgdata = oe.packagedata.read_pkgdatafile(pkgdatadir + "/runtime/ed")
71 if 'PKGE' in pkgdata:
72 pf = pkgdata['PN'] + "-" + pkgdata['PKGE'] + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
73 else:
74 pf = pkgdata['PN'] + "-" + pkgdata['PKGV'] + '-' + pkgdata['PKGR']
75 deploy_dir_rpm = get_bb_var('DEPLOY_DIR_RPM', test_recipe)
76 package_arch = get_bb_var('PACKAGE_ARCH', test_recipe).replace('-', '_')
77 staging_bindir_native = get_bb_var('STAGING_BINDIR_NATIVE')
78
79 pkg_deploy = os.path.join(deploy_dir_rpm, package_arch, '.'.join((pf, package_arch, 'rpm')))
80
81 # Use a temporary rpmdb
82 rpmdb = tempfile.mkdtemp(prefix='oeqa-rpmdb')
83
84 runCmd('%s/rpm --define "_dbpath %s" --import %s%s' %
85 (staging_bindir_native, rpmdb, self.gpg_dir, self.pub_key_name))
86
87 ret = runCmd('%s/rpm --define "_dbpath %s" --checksig %s' %
88 (staging_bindir_native, rpmdb, pkg_deploy))
89 # tmp/deploy/rpm/i586/ed-1.9-r0.i586.rpm: rsa sha1 md5 OK
90 self.assertIn('rsa sha1 md5 OK', ret.output, 'Package signed incorrectly.')
91 shutil.rmtree(rpmdb)
92
93 @testcase(1382)
94 def test_signing_sstate_archive(self):
95 """
96 Summary: Test that sstate archives can be signed
97 Expected: Package should be signed with the correct key
98 Product: oe-core
99 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
100 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
101 """
102
103 test_recipe = 'ed'
104
105 builddir = os.environ.get('BUILDDIR')
106 sstatedir = os.path.join(builddir, 'test-sstate')
107
108 self.add_command_to_tearDown('bitbake -c clean %s' % test_recipe)
109 self.add_command_to_tearDown('bitbake -c cleansstate %s' % test_recipe)
110 self.add_command_to_tearDown('rm -rf %s' % sstatedir)
111
112 # Determine the pub key signature
113 ret = runCmd('gpg --homedir %s --list-keys' % self.gpg_dir)
114 pub_key = re.search(r'^pub\s+\S+/(\S+)\s+', ret.output, re.M)
115 self.assertIsNotNone(pub_key, 'Failed to determine the public key signature.')
116 pub_key = pub_key.group(1)
117
118 feature = 'SSTATE_SIG_KEY ?= "%s"\n' % pub_key
119 feature += 'SSTATE_SIG_PASSPHRASE ?= "test123"\n'
120 feature += 'SSTATE_VERIFY_SIG ?= "1"\n'
121 feature += 'GPG_PATH = "%s"\n' % self.gpg_dir
122 feature += 'SSTATE_DIR = "%s"\n' % sstatedir
123
124 self.write_config(feature)
125
126 bitbake('-c cleansstate %s' % test_recipe)
127 bitbake(test_recipe)
128
129 recipe_sig = glob.glob(sstatedir + '/*/*:ed:*_package.tgz.sig')
130 recipe_tgz = glob.glob(sstatedir + '/*/*:ed:*_package.tgz')
131
132 self.assertEqual(len(recipe_sig), 1, 'Failed to find .sig file.')
133 self.assertEqual(len(recipe_tgz), 1, 'Failed to find .tgz file.')
134
135 ret = runCmd('gpg --homedir %s --verify %s %s' % (self.gpg_dir, recipe_sig[0], recipe_tgz[0]))
136 # gpg: Signature made Thu 22 Oct 2015 01:45:09 PM EEST using RSA key ID 61EEFB30
137 # gpg: Good signature from "testuser (nocomment) <testuser@email.com>"
138 self.assertIn('gpg: Good signature from', ret.output, 'Package signed incorrectly.')
139
140
141class LockedSignatures(oeSelfTest):
142
143 @testcase(1420)
144 def test_locked_signatures(self):
145 """
146 Summary: Test locked signature mechanism
147 Expected: Locked signatures will prevent task to run
148 Product: oe-core
149 Author: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
150 AutomatedBy: Daniel Istrate <daniel.alexandrux.istrate@intel.com>
151 """
152
153 test_recipe = 'ed'
154 locked_sigs_file = 'locked-sigs.inc'
155
156 self.add_command_to_tearDown('rm -f %s' % os.path.join(self.builddir, locked_sigs_file))
157
158 bitbake(test_recipe)
159 # Generate locked sigs include file
160 bitbake('-S none %s' % test_recipe)
161
162 feature = 'require %s\n' % locked_sigs_file
163 feature += 'SIGGEN_LOCKEDSIGS_TASKSIG_CHECK = "warn"\n'
164 self.write_config(feature)
165
166 # Build a locked recipe
167 bitbake(test_recipe)
168
169 # Make a change that should cause the locked task signature to change
170 recipe_append_file = test_recipe + '_' + get_bb_var('PV', test_recipe) + '.bbappend'
171 recipe_append_path = os.path.join(self.testlayer_path, 'recipes-test', test_recipe, recipe_append_file)
172 feature = 'SUMMARY += "test locked signature"\n'
173
174 os.mkdir(os.path.join(self.testlayer_path, 'recipes-test', test_recipe))
175 write_file(recipe_append_path, feature)
176
177 self.add_command_to_tearDown('rm -rf %s' % os.path.join(self.testlayer_path, 'recipes-test', test_recipe))
178
179 # Build the recipe again
180 ret = bitbake(test_recipe)
181
182 # Verify you get the warning and that the real task *isn't* run (i.e. the locked signature has worked)
183 patt = r'WARNING: The %s:do_package sig is computed to be \S+, but the sig is locked to \S+ in SIGGEN_LOCKEDSIGS\S+' % test_recipe
184 found_warn = re.search(patt, ret.output)
185
186 self.assertIsNotNone(found_warn, "Didn't find the expected warning message. Output: %s" % ret.output)